Lines
90.00%
9 / 10
Functions and Methods
80.00%
4 / 5
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| StateChangesFragment | 90.00% | 9 / 10 | 80.00% | 4 / 5 | 6.04 | 0.00% | 0 / 1 | |||
| __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 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Ipc\Wire; | |
| 6 | ||
| 7 | use Stewart\Contracts\State\StateChange; | |
| 8 | use Stewart\Runtime\Ipc\Wire\Collection\EncodedStateChangeCollection; | |
| 9 | use Stewart\Runtime\Json\JsonFragment; | |
| 10 | use Stewart\Runtime\Json\WireMapper; | |
| 11 | use Stewart\Support\State\Collection\StateChangeCollection; | |
| 12 | ||
| 13 | final 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 | } |