Skip to content

Commit

Permalink
Add __invoke for use in callback
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Sep 14, 2023
1 parent 07bae0a commit dbd6a49
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/Date/Formats.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@
/**
* @extends Utils\Formats<Formatter>
*/
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');
Expand Down
8 changes: 8 additions & 0 deletions src/Date/FormatsAccessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace h4kuna\Format\Date;

interface FormatsAccessor
{
function get(string|int $key): Formatter;
}
3 changes: 2 additions & 1 deletion src/Date/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace h4kuna\Format\Date;

use DateTimeInterface;
use h4kuna\Format\Utils\Service;

interface Formatter
interface Formatter extends Service
{
function format(?DateTimeInterface $dateTime): string;
}
5 changes: 5 additions & 0 deletions src/Date/Formatters/DateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public function format(?DateTimeInterface $dateTime): string
return $dateTime === null ? $this->emptyValueSpace : $dateTime->format($this->formatSpace);
}

public function __invoke(?DateTimeInterface $dateTime): string
{
return $this->format($dateTime);
}

}
6 changes: 6 additions & 0 deletions src/Date/Formatters/IntlDateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
9 changes: 8 additions & 1 deletion src/Number/Formats.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
/**
* @extends Utils\Formats<NumberFormatter>
*/
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) {
Expand Down
8 changes: 8 additions & 0 deletions src/Number/FormatsAccessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace h4kuna\Format\Number;

interface FormatsAccessor
{
function get(string|int $key): Formatter;
}
4 changes: 3 additions & 1 deletion src/Number/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace h4kuna\Format\Number;

interface Formatter
use h4kuna\Format\Utils\Service;

interface Formatter extends Service
{
function format(string|int|float|null $number): string;
}
7 changes: 7 additions & 0 deletions src/Number/Formatters/IntlNumberFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ public function format(string|int|float|null $number): string

return $result;
}


public function __invoke(float|int|string|null $number): string
{
return $this->format($number);
}

}
6 changes: 6 additions & 0 deletions src/Number/Formatters/NumberFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
15 changes: 12 additions & 3 deletions src/Utils/Formats.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down Expand Up @@ -90,14 +90,23 @@ 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);
}

$this->default = $default;
}


/**
* @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
Expand Down
8 changes: 8 additions & 0 deletions src/Utils/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace h4kuna\Format\Utils;

interface Service
{

}

0 comments on commit dbd6a49

Please sign in to comment.