Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
69.23% |
9 / 13 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| AutoIncrementInteger | |
69.23% |
9 / 13 |
|
83.33% |
5 / 6 |
9.86 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromNative | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| toNative | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSchema | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| createRandom | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Identifiers; |
| 3 | |
| 4 | use Apie\Core\Attributes\Description; |
| 5 | use Apie\Core\Attributes\FakeMethod; |
| 6 | use Apie\Core\Attributes\SchemaMethod; |
| 7 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 8 | use Apie\Core\ValueObjects\Utils; |
| 9 | use Faker\Generator; |
| 10 | use Stringable; |
| 11 | |
| 12 | /** |
| 13 | * Indicate an auto-increment integer. |
| 14 | */ |
| 15 | #[FakeMethod("createRandom")] |
| 16 | #[SchemaMethod("getSchema")] |
| 17 | #[Description('An auto-increment integer often found in a database. null means not stored (yet)')] |
| 18 | class AutoIncrementInteger implements ValueObjectInterface, Stringable |
| 19 | { |
| 20 | /** |
| 21 | * @var array<string,int> |
| 22 | */ |
| 23 | private static array $fakeCounter = []; |
| 24 | |
| 25 | /** |
| 26 | * @var array<string, string> |
| 27 | */ |
| 28 | private static array $hash = []; |
| 29 | |
| 30 | private ?int $internal; |
| 31 | |
| 32 | final public function __construct(?int $input) |
| 33 | { |
| 34 | $this->internal = $input; |
| 35 | } |
| 36 | |
| 37 | final public static function fromNative(mixed $input): self |
| 38 | { |
| 39 | return new static($input === null ? null : Utils::toInt($input)); |
| 40 | } |
| 41 | final public function toNative(): int|null |
| 42 | { |
| 43 | return $this->internal; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @return array<string, string|int> |
| 48 | */ |
| 49 | final public static function getSchema(): array |
| 50 | { |
| 51 | return [ |
| 52 | 'type' => 'integer', |
| 53 | 'min' => 1, |
| 54 | ]; |
| 55 | } |
| 56 | |
| 57 | final public static function createRandom(Generator $generator): static |
| 58 | { |
| 59 | $hash = spl_object_hash($generator); |
| 60 | if ((self::$hash[static::class] ?? '') !== $hash) { |
| 61 | self::$fakeCounter[static::class] = 1; |
| 62 | self::$hash[static::class] = $hash; |
| 63 | } |
| 64 | return new static(self::$fakeCounter[static::class]++); |
| 65 | } |
| 66 | |
| 67 | public function __toString(): string |
| 68 | { |
| 69 | return (string) $this->internal; |
| 70 | } |
| 71 | } |