Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
66.67% |
18 / 27 |
|
44.44% |
4 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Entry | |
66.67% |
18 / 27 |
|
44.44% |
4 / 9 |
21.26 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| json | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| uid | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| unwrap | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| isset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| render | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| __get | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| initFields | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Value; |
| 6 | |
| 7 | use Cosray\Field; |
| 8 | |
| 9 | /** |
| 10 | * @property-read Field\Entries $field |
| 11 | */ |
| 12 | class Entry extends Value |
| 13 | { |
| 14 | /** @var array<string, Field\Field> */ |
| 15 | protected array $fields = []; |
| 16 | |
| 17 | public function __construct( |
| 18 | Field\Owner $owner, |
| 19 | Field\Entries $field, |
| 20 | ValueContext $context, |
| 21 | public readonly string $type, |
| 22 | ) { |
| 23 | parent::__construct($owner, $field, $context); |
| 24 | |
| 25 | $this->initFields(); |
| 26 | } |
| 27 | |
| 28 | public function __toString(): string |
| 29 | { |
| 30 | return $this->render(); |
| 31 | } |
| 32 | |
| 33 | public function json(): array |
| 34 | { |
| 35 | return $this->unwrap(); |
| 36 | } |
| 37 | |
| 38 | public function uid(): ?string |
| 39 | { |
| 40 | $uid = $this->data['uid'] ?? null; |
| 41 | |
| 42 | return is_string($uid) ? $uid : null; |
| 43 | } |
| 44 | |
| 45 | public function unwrap(): array |
| 46 | { |
| 47 | $result = []; |
| 48 | |
| 49 | foreach ($this->fields as $name => $field) { |
| 50 | $result[$name] = $field->structure(); |
| 51 | } |
| 52 | |
| 53 | return [ |
| 54 | 'uid' => $this->uid(), |
| 55 | 'type' => $this->type, |
| 56 | 'fields' => $result, |
| 57 | ]; |
| 58 | } |
| 59 | |
| 60 | public function isset(): bool |
| 61 | { |
| 62 | return count($this->fields) > 0; |
| 63 | } |
| 64 | |
| 65 | public function render(mixed ...$args): string |
| 66 | { |
| 67 | $out = '<div class="entry">'; |
| 68 | |
| 69 | foreach ($this->fields as $field) { |
| 70 | $out .= $field->value()->render(...$args); |
| 71 | } |
| 72 | |
| 73 | $out .= '</div>'; |
| 74 | |
| 75 | return $out; |
| 76 | } |
| 77 | |
| 78 | public function __get(string $name): mixed |
| 79 | { |
| 80 | if (isset($this->fields[$name])) { |
| 81 | return $this->fields[$name]->value(); |
| 82 | } |
| 83 | |
| 84 | throw new \Cosray\Exception\NoSuchProperty("Entry doesn't have field '{$name}'"); |
| 85 | } |
| 86 | |
| 87 | protected function initFields(): void |
| 88 | { |
| 89 | $data = $this->data['fields'] ?? []; |
| 90 | |
| 91 | if (!is_array($data)) { |
| 92 | $data = []; |
| 93 | } |
| 94 | |
| 95 | $this->fields = $this->field->entryFieldsFor($this->type, $data); |
| 96 | } |
| 97 | } |