Lines 90.00% 9 / 10
Methods 80.00% 4 / 5
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 fromCollection 100.00% 1 / 1 100.00% 1 / 1 1
 fromEncodedChanges 100.00% 4 / 4 100.00% 1 / 1 1
 fromDecodedValue 100.00% 1 / 1 100.00% 1 / 1 1
 encodeToJson 66.66% 2 / 3 0.00% 0 / 1 2.15
13final readonly class StateChangesFragment implements JsonFragment
14{
15    private function __construct(
16        public StateChangeCollection $collection,
17        private ?EncodedStateChangeCollection $encodedChanges,
18    ) {}
19
20    public static function fromCollection(StateChangeCollection $changes): self
21    {
22        return self::fromEncodedChanges(EncodedStateChangeCollection::fromList($changes->mapToList(static fn(StateChange $change): EncodedStateChange => new EncodedStateChange($change))));
23    }
24
25    public static function fromEncodedChanges(EncodedStateChangeCollection $encodedChanges): self
26    {
27        return new self(
28            StateChangeCollection::fromList($encodedChanges->mapToList(static fn(EncodedStateChange $encoded): StateChange => $encoded->change)),
29            $encodedChanges,
30        );
31    }
32
33    public static function fromDecodedValue(mixed $value, string $path, WireMapper $mapper): static
34    {
35        return new self(StateChangeCollection::fromList($mapper->decodeObjectList(StateChange::class, $value, $path)), null);
36    }
37
38    public function encodeToJson(WireMapper $mapper): string
39    {
40        if ($this->encodedChanges === null) {
41            return $mapper->encodeObjectList($this->collection->listValues());
42        }
43
44        return '[' . implode(',', $this->encodedChanges->mapToList(static fn(EncodedStateChange $encoded): string => $encoded->encodeToJson($mapper))) . ']';
45    }
46}