Skip to content

Commit

Permalink
Sync with main repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Gymnasiast committed Oct 26, 2023
1 parent e9e1599 commit aeed162
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 90 deletions.
16 changes: 13 additions & 3 deletions src/DependencyInjectionContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@

final class DependencyInjectionContainer
{
/** @var array<class-string, object> */
private array $objects = [];

public function add(object $object): void
/**
* @param object $object
* @param class-string|null $className
* @return void
*/
public function add(object $object, string|null $className = null): void
{
$className = get_class($object);
if ($className === null)
{
$className = get_class($object);
}

$this->objects[$className] = $object;
}

public function get(string $className): ?object
public function get(string $className):object|null
{
return $this->objects[$className] ?? null;
}
Expand Down
59 changes: 59 additions & 0 deletions src/EllipsizedString.php
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;
}
}
14 changes: 13 additions & 1 deletion src/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Cyndaron\Util;

use DateTime;
use DateTimeImmutable;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
Expand All @@ -14,10 +16,20 @@ final class FileCache
public const CACHE_DIR = ROOT_DIR . '/cache/cyndaron';

public readonly string $filename;
/** @var class-string[] */
public readonly array $allowedClasses;

/**
* @param string $cacheKey
* @param class-string[] $allowedClasses
*/
public function __construct(string $cacheKey, array $allowedClasses)
{
$allowedClasses[] = DateTime::class;
$allowedClasses[] = DateTimeImmutable::class;
$allowedClasses[] = \Safe\DateTime::class;
$allowedClasses[] = \Safe\DateTimeImmutable::class;

$this->filename = self::CACHE_DIR . "/$cacheKey.phps";
$this->allowedClasses = $allowedClasses;
}
Expand All @@ -29,7 +41,7 @@ public function load(mixed &$target): bool
$serialized = file_get_contents($this->filename);
if ($serialized)
{
$unserialized = unserialize($serialized, $this->allowedClasses);
$unserialized = unserialize($serialized, ['allowed_classes' => $this->allowedClasses]);
if ($unserialized)
{
$target = $unserialized;
Expand Down
22 changes: 22 additions & 0 deletions src/Link.php
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']);
}
}
26 changes: 26 additions & 0 deletions src/LinkWithIcon.php
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

Check failure on line 22 in src/LinkWithIcon.php

View workflow job for this annotation

GitHub Actions / ci-tests

No error to ignore is reported on line 22.
{
return new self($input['link'], $input['name'], $input['icon']);
}
}
32 changes: 32 additions & 0 deletions src/Mail.php
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 {

Check failure on line 28 in src/Mail.php

View workflow job for this annotation

GitHub Actions / ci-tests

Method Cyndaron\Util\Mail::createMailWithDefaults() has invalid return type Cyndaron\Mail\Mail.
$from = self::getNoreplyAddress();
return new \Cyndaron\Mail\Mail($from, $to, $subject, $plainTextMessage, $htmlMessage);

Check failure on line 30 in src/Mail.php

View workflow job for this annotation

GitHub Actions / ci-tests

Instantiated class Cyndaron\Mail\Mail not found.
}
}
80 changes: 0 additions & 80 deletions src/Mail/Mail.php

This file was deleted.

3 changes: 3 additions & 0 deletions src/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
use PDO;
use function file_exists;
use function file_put_contents;
use function is_array;
use function var_export;
use const ROOT_DIR;
use function dirname;
use function assert;

final class Setting
{
Expand Down Expand Up @@ -73,6 +75,7 @@ public static function buildCache(): void
$settings->execute([]);
while ($row = $settings->fetch())
{
assert(is_array($row));
$data[$row['name']] = $row['value'];
}

Expand Down
12 changes: 12 additions & 0 deletions src/UserSafeError.php
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
{
}
8 changes: 2 additions & 6 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use RuntimeException;
use Safe\DateTimeImmutable;
use Safe\Exceptions\FilesystemException;
use Symfony\Component\Mime\Address;
use function html_entity_decode;
use function Safe\preg_replace;
use function Safe\date;
use function Safe\mkdir;
Expand Down Expand Up @@ -64,12 +66,6 @@ public static function getDomain(): string
return str_replace(['www.', 'http://', 'https://', '/'], '', $_SERVER['HTTP_HOST']);
}

public static function getNoreplyAddress(): string
{
$domain = static::getDomain();
return "noreply@$domain";
}

public static function slug(string $string): string
{
return strtr(strtolower($string), [
Expand Down

0 comments on commit aeed162

Please sign in to comment.