Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
18 / 21 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Verification | |
85.71% |
18 / 21 |
|
60.00% |
3 / 5 |
11.35 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromJsonLd | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
4.59 | |||
| readNode | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| toJsonLd | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| node | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace LambdaTwelve\OneRecord\Api; |
| 6 | |
| 7 | use LambdaTwelve\OneRecord\JsonLd\ExpandedDocument; |
| 8 | use LambdaTwelve\OneRecord\JsonLd\JsonLd; |
| 9 | use LambdaTwelve\OneRecord\JsonLd\JsonLdException; |
| 10 | use LambdaTwelve\OneRecord\JsonLd\Nodes; |
| 11 | use LambdaTwelve\OneRecord\Rdf\BlankNode; |
| 12 | use LambdaTwelve\OneRecord\Rdf\Graph; |
| 13 | use LambdaTwelve\OneRecord\Rdf\Iri; |
| 14 | use LambdaTwelve\OneRecord\Spec\ApiVersion; |
| 15 | use LambdaTwelve\OneRecord\Vocabulary\Generated\Api; |
| 16 | |
| 17 | /** |
| 18 | * api:Verification: a third party flags problems in a logistics object |
| 19 | * without proposing new values. Posted to POST /logistics-objects/{id}. |
| 20 | */ |
| 21 | final readonly class Verification |
| 22 | { |
| 23 | /** |
| 24 | * @param non-empty-list<Error> $errors |
| 25 | */ |
| 26 | public function __construct( |
| 27 | public Iri $logisticsObject, |
| 28 | public array $errors, |
| 29 | public ?int $revision = null, |
| 30 | public bool $notifyRequestStatusChange = false, |
| 31 | ) {} |
| 32 | |
| 33 | /** |
| 34 | * @param string|array<string, mixed>|ExpandedDocument $document |
| 35 | */ |
| 36 | public static function fromJsonLd(string|array|ExpandedDocument $document): self |
| 37 | { |
| 38 | try { |
| 39 | $expanded = $document instanceof ExpandedDocument ? $document : JsonLd::expand($document); |
| 40 | } catch (JsonLdException $e) { |
| 41 | throw InvalidDocument::because('Invalid body request', $e->getMessage()); |
| 42 | } |
| 43 | if (!\in_array(Api::Verification, $expanded->rootTypes(), true)) { |
| 44 | throw InvalidDocument::because('Invalid resource', 'The body is not an api:Verification.'); |
| 45 | } |
| 46 | |
| 47 | return self::readNode($expanded->graph, $expanded->root); |
| 48 | } |
| 49 | |
| 50 | public static function readNode(Graph $graph, Iri|BlankNode $node): self |
| 51 | { |
| 52 | $target = Nodes::iri($graph, $node, Api::hasLogisticsObject) |
| 53 | ?? throw InvalidDocument::because('Invalid resource', 'api:hasLogisticsObject must reference the logistics object.', Api::hasLogisticsObject); |
| 54 | $errors = ErrorDocument::readFrom($graph, $node, Api::hasError); |
| 55 | if ($errors === []) { |
| 56 | throw InvalidDocument::because('Invalid resource', 'A verification must report at least one error.', Api::hasError); |
| 57 | } |
| 58 | |
| 59 | return new self($target, $errors, Nodes::int($graph, $node, Api::hasRevision), Nodes::bool($graph, $node, Api::notifyRequestStatusChange) ?? false); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @return array<string, mixed> |
| 64 | */ |
| 65 | public function toJsonLd(?ApiVersion $version = null): array |
| 66 | { |
| 67 | $version ??= ApiVersion::latest(); |
| 68 | |
| 69 | return ['@context' => [...Nodes::context(), 'api:hasProperty' => ['@type' => 'xsd:anyURI'], 'api:hasResource' => ['@type' => 'xsd:anyURI']], ...$this->node($version)]; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return array<string, mixed> |
| 74 | */ |
| 75 | public function node(ApiVersion $version): array |
| 76 | { |
| 77 | $node = ['@type' => 'api:Verification', 'api:hasLogisticsObject' => Nodes::ref($this->logisticsObject), 'api:hasError' => ErrorDocument::nodes($this->errors, $version)]; |
| 78 | if ($this->revision !== null) { |
| 79 | $node['api:hasRevision'] = Nodes::positiveInteger($this->revision); |
| 80 | } |
| 81 | if ($this->notifyRequestStatusChange) { |
| 82 | $node['api:notifyRequestStatusChange'] = true; |
| 83 | } |
| 84 | |
| 85 | return $node; |
| 86 | } |
| 87 | } |