-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9e1599
commit aeed162
Showing
10 changed files
with
182 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Cyndaron\Util; | ||
|
||
use Stringable; | ||
|
||
use function strrpos; | ||
use function strpos; | ||
use function strlen; | ||
use function substr; | ||
|
||
final class EllipsizedString implements Stringable | ||
{ | ||
private readonly string $string; | ||
|
||
public function __construct(string $string, int $desiredLength) | ||
{ | ||
if (strlen($string) > $desiredLength) | ||
{ | ||
$string = $this->process($string, $desiredLength); | ||
} | ||
|
||
$this->string = $string; | ||
} | ||
|
||
private function process(string $string, int $desiredLength): string | ||
{ | ||
$space1Pos = strrpos(substr($string, 0, $desiredLength), ' '); | ||
$space2Pos = strpos($string, ' ', $desiredLength) ?: strlen($string); | ||
|
||
$noSpaceNearby = ($space1Pos === false && ($space2Pos + 10) > strlen($string)) || | ||
($space2Pos > $desiredLength + 10); | ||
|
||
if ($noSpaceNearby) | ||
{ | ||
return substr($string, 0, $desiredLength) . '…'; | ||
} | ||
|
||
$space1PosIsCloserToDesiredLength = $space1Pos !== false && | ||
($desiredLength - $space1Pos < $space2Pos - $desiredLength); | ||
if ($space1PosIsCloserToDesiredLength) | ||
{ | ||
return substr($string, 0, $space1Pos) . '…'; | ||
} | ||
|
||
if ($space2Pos !== strlen($string)) | ||
{ | ||
return substr($string, 0, $space2Pos) . '…'; | ||
} | ||
|
||
return substr($string, 0, $space2Pos); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->string; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Cyndaron\Util; | ||
|
||
class Link | ||
{ | ||
public function __construct( | ||
public readonly string $link, | ||
public readonly string $name, | ||
) { | ||
} | ||
|
||
/** | ||
* @param array{link: string, name: string} $input | ||
* @return self | ||
*/ | ||
public static function fromArray(array $input): self | ||
{ | ||
return new self($input['link'], $input['name']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Cyndaron\Util; | ||
|
||
final class LinkWithIcon extends Link | ||
{ | ||
public function __construct( | ||
string $link, | ||
string $name, | ||
public readonly string $icon, | ||
) { | ||
parent::__construct($link, $name); | ||
} | ||
|
||
/** | ||
* | ||
* @param array{link: string, name: string, icon: string} $input | ||
* @return self | ||
* @phpstan-ignore-next-line | ||
*/ | ||
public static function fromArray(array $input): self | ||
{ | ||
return new self($input['link'], $input['name'], $input['icon']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Cyndaron\Util; | ||
|
||
use Symfony\Component\Mime\Address; | ||
use function html_entity_decode; | ||
|
||
final class Mail | ||
{ | ||
public static function getNoreplyAddressRaw(): string | ||
{ | ||
$domain = Util::getDomain(); | ||
return "noreply@$domain"; | ||
} | ||
|
||
public static function getNoreplyAddress(): Address | ||
{ | ||
$fromName = html_entity_decode(Setting::get(Setting::ORGANISATION) ?: Setting::get('siteName')); | ||
return new Address(self::getNoreplyAddressRaw(), $fromName); | ||
} | ||
|
||
public static function createMailWithDefaults( | ||
Address $to, | ||
string $subject, | ||
string|null $plainTextMessage = null, | ||
string|null $htmlMessage = null | ||
): \Cyndaron\Mail\Mail { | ||
$from = self::getNoreplyAddress(); | ||
return new \Cyndaron\Mail\Mail($from, $to, $subject, $plainTextMessage, $htmlMessage); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Cyndaron\Util; | ||
|
||
/** | ||
* Can be ‘implemented’ by any Throwable that does not contain any sensitive date | ||
* and may safely be presented to the user. | ||
*/ | ||
interface UserSafeError | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters