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