Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.78% covered (warning)
77.78%
7 / 9
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Embedded
77.78% covered (warning)
77.78%
7 / 9
85.71% covered (warning)
85.71%
6 / 7
8.70
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
 of
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 types
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 add
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setIf
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 properties
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace LambdaTwelve\OneRecord\Model\Builder;
6
7use LambdaTwelve\OneRecord\Model\LocalRef;
8use LambdaTwelve\OneRecord\Rdf\Iri;
9use LambdaTwelve\OneRecord\Rdf\Literal;
10
11/**
12 * An object that lives inside a logistics object (cargo:Value, Dimensions,
13 * Party, Address, CodeListElement, ...): a type and properties, written as a
14 * blank node when the parent is built. Validated against the vocabulary with
15 * the parent.
16 */
17final class Embedded
18{
19    /** @var array<string, list<Literal|Iri|LocalRef|Embedded>> property IRI => values */
20    private array $properties = [];
21
22    /**
23     * @param list<string> $types
24     */
25    private function __construct(private readonly array $types) {}
26
27    public static function of(string $type, string ...$moreTypes): self
28    {
29        return new self(array_values([$type, ...$moreTypes]));
30    }
31
32    /**
33     * @return list<string>
34     */
35    public function types(): array
36    {
37        return $this->types;
38    }
39
40    /**
41     * Replace the property's values with one value.
42     *
43     * @return $this
44     */
45    public function set(string $property, mixed $value): self
46    {
47        $this->properties[$property] = [Values::term($value)];
48
49        return $this;
50    }
51
52    /**
53     * Add one more value to a property.
54     *
55     * @return $this
56     */
57    public function add(string $property, mixed $value): self
58    {
59        $this->properties[$property][] = Values::term($value);
60
61        return $this;
62    }
63
64    /**
65     * Set a property only when the value is not null; keeps mappers free of ifs.
66     *
67     * @return $this
68     */
69    public function setIf(string $property, mixed $value): self
70    {
71        return $value === null ? $this : $this->set($property, $value);
72    }
73
74    /**
75     * @return array<string, list<Literal|Iri|LocalRef|Embedded>>
76     */
77    public function properties(): array
78    {
79        return $this->properties;
80    }
81}