From dbd6a49af6e881038b8e25c72ccbffb29ce2cee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Thu, 14 Sep 2023 15:51:37 +0200 Subject: [PATCH] Add __invoke for use in callback --- src/Date/Formats.php | 8 +++++++- src/Date/FormatsAccessor.php | 8 ++++++++ src/Date/Formatter.php | 3 ++- src/Date/Formatters/DateTimeFormatter.php | 5 +++++ src/Date/Formatters/IntlDateFormatter.php | 6 ++++++ src/Number/Formats.php | 9 ++++++++- src/Number/FormatsAccessor.php | 8 ++++++++ src/Number/Formatter.php | 4 +++- src/Number/Formatters/IntlNumberFormatter.php | 7 +++++++ src/Number/Formatters/NumberFormatter.php | 6 ++++++ src/Utils/Formats.php | 15 ++++++++++++--- src/Utils/Service.php | 8 ++++++++ 12 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 src/Date/FormatsAccessor.php create mode 100644 src/Number/FormatsAccessor.php create mode 100644 src/Utils/Service.php diff --git a/src/Date/Formats.php b/src/Date/Formats.php index 96510a1..ce1a410 100644 --- a/src/Date/Formats.php +++ b/src/Date/Formats.php @@ -8,9 +8,15 @@ /** * @extends Utils\Formats */ -class Formats extends Utils\Formats +class Formats extends Utils\Formats implements FormatsAccessor { + public function get(int|string $key): Formatter + { + return parent::get($key); + } + + protected function createDefaultCallback($object = null): callable { return static fn (): DateTimeFormatter => new DateTimeFormatter('Y-m-d H:i:s'); diff --git a/src/Date/FormatsAccessor.php b/src/Date/FormatsAccessor.php new file mode 100644 index 0000000..352bfa9 --- /dev/null +++ b/src/Date/FormatsAccessor.php @@ -0,0 +1,8 @@ +emptyValueSpace : $dateTime->format($this->formatSpace); } + public function __invoke(?DateTimeInterface $dateTime): string + { + return $this->format($dateTime); + } + } diff --git a/src/Date/Formatters/IntlDateFormatter.php b/src/Date/Formatters/IntlDateFormatter.php index 85bd585..68ee620 100644 --- a/src/Date/Formatters/IntlDateFormatter.php +++ b/src/Date/Formatters/IntlDateFormatter.php @@ -53,4 +53,10 @@ public function format(?DateTimeInterface $dateTime): string return $this->nbsp ? Space::nbsp($result) : $result; } + + public function __invoke(?DateTimeInterface $dateTime): string + { + return $this->format($dateTime); + } + } diff --git a/src/Number/Formats.php b/src/Number/Formats.php index af0fa74..5ef445e 100644 --- a/src/Number/Formats.php +++ b/src/Number/Formats.php @@ -8,8 +8,15 @@ /** * @extends Utils\Formats */ -class Formats extends Utils\Formats +class Formats extends Utils\Formats implements FormatsAccessor { + + public function get(int|string $key): Formatter + { + return parent::get($key); + } + + protected function createDefaultCallback($object = null): callable { if ($object === null) { diff --git a/src/Number/FormatsAccessor.php b/src/Number/FormatsAccessor.php new file mode 100644 index 0000000..4663b58 --- /dev/null +++ b/src/Number/FormatsAccessor.php @@ -0,0 +1,8 @@ +format($number); + } + } diff --git a/src/Number/Formatters/NumberFormatter.php b/src/Number/Formatters/NumberFormatter.php index dd77635..b790466 100644 --- a/src/Number/Formatters/NumberFormatter.php +++ b/src/Number/Formatters/NumberFormatter.php @@ -134,4 +134,10 @@ public function format(string|int|float|null $number): string ); } + + public function __invoke(float|int|string|null $number): string + { + return $this->format($number); + } + } diff --git a/src/Utils/Formats.php b/src/Utils/Formats.php index c808f88..6e0e9d7 100644 --- a/src/Utils/Formats.php +++ b/src/Utils/Formats.php @@ -35,7 +35,7 @@ public function __construct( */ public function add(string|int $key, $setup): void { - if (is_callable($setup)) { + if (self::isCallback($setup)) { $this->factories[$key] = $setup; unset($this->formats[$key]); } else { @@ -58,7 +58,7 @@ public function get(string|int $key) if (isset($this->formats[$key]) === false) { if (isset($this->factories[$key])) { $service = $this->factories[$key]; - $format = is_callable($service) ? $service($this) : $service; + $format = self::isCallback($service) ? $service($this) : $service; } else { $format = $this->getDefault()($key, $this, null); } @@ -90,7 +90,7 @@ public function setDefault($default): void { if ($this->default !== null) { throw new InvalidStateException('Default format could be setup only onetime.'); - } elseif (is_callable($default) === false) { + } elseif (self::isCallback($default) === false) { $default = $this->createDefaultCallback($default); } @@ -98,6 +98,15 @@ public function setDefault($default): void } + /** + * @return ($service is callable ? true : false) + */ + private static function isCallback(mixed $service): bool + { + return is_callable($service) && ($service instanceof Service === false); + } + + /** * @param T|null $object * @return defaultCallback diff --git a/src/Utils/Service.php b/src/Utils/Service.php new file mode 100644 index 0000000..0b6b7e4 --- /dev/null +++ b/src/Utils/Service.php @@ -0,0 +1,8 @@ +