Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.57% covered (warning)
78.57%
33 / 42
72.73% covered (warning)
72.73%
8 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Value
78.57% covered (warning)
78.57%
33 / 42
72.73% covered (warning)
72.73%
8 / 11
40.46
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 __get
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 __toString
n/a
0 / 0
n/a
0 / 0
0
 isset
n/a
0 / 0
n/a
0 / 0
0
 json
n/a
0 / 0
n/a
0 / 0
0
 unwrap
n/a
0 / 0
n/a
0 / 0
0
 styleClass
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 elementId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 value
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 zxx
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 meta
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 effective
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 filled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
 assetsPath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAssets
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Value;
6
7use Cosray\Assets\Assets;
8use Cosray\Exception\NoSuchProperty;
9use Cosray\Field\Field;
10use Cosray\Field\Field as FieldBase;
11use Cosray\Field\Owner;
12use Cosray\Locale;
13
14abstract class Value
15{
16    public readonly string $fieldType;
17    protected readonly Locale $locale;
18    protected readonly Locale $defaultLocale;
19    protected readonly string $fieldName;
20    protected readonly array $data;
21
22    public function __construct(
23        protected readonly Owner $owner,
24        protected readonly Field $field,
25        protected readonly ValueContext $context,
26    ) {
27        $this->locale = $owner->locale();
28        $this->defaultLocale = $owner->defaultLocale();
29        $this->data = $context->data;
30        $this->fieldName = $context->fieldName;
31        $this->fieldType = $field->type;
32    }
33
34    public function __get(string $name): mixed
35    {
36        if (array_key_exists($name, $this->data)) {
37            return $this->data[$name];
38        }
39
40        if (array_key_exists($name, $this->data['meta'] ?? [])) {
41            return $this->meta($name);
42        }
43
44        throw new NoSuchProperty("The field '{$this->fieldName}' doesn't have the property '{$name}'");
45    }
46
47    abstract public function __toString(): string;
48
49    abstract public function isset(): bool;
50
51    abstract public function json(): mixed;
52
53    abstract public function unwrap(): mixed;
54
55    public function styleClass(): ?string
56    {
57        $value = $this->meta('class');
58
59        return is_string($value) && $value !== '' ? $value : null;
60    }
61
62    public function elementId(): ?string
63    {
64        $value = $this->meta('id');
65
66        return is_string($value) && $value !== '' ? $value : null;
67    }
68
69    protected function value(): mixed
70    {
71        $value = $this->data['value'] ?? [];
72
73        if (!is_array($value) && !isset($this->data['type'])) {
74            return $value;
75        }
76
77        return is_array($value) ? $this->effective($value) : null;
78    }
79
80    protected function zxx(): mixed
81    {
82        $value = $this->data['value'] ?? [];
83
84        if (!is_array($value)) {
85            return null;
86        }
87
88        return $value[FieldBase::NEUTRAL_LOCALE] ?? null;
89    }
90
91    protected function meta(string $key, mixed $default = null): mixed
92    {
93        $meta = $this->data['meta'][$key] ?? null;
94
95        if (!is_array($meta) && !isset($this->data['type']) && array_key_exists($key, $this->data)) {
96            return $this->data[$key];
97        }
98
99        if (!is_array($meta)) {
100            return $default;
101        }
102
103        return $this->effective($meta) ?? $default;
104    }
105
106    protected function effective(array $map): mixed
107    {
108        $locale = $this->locale;
109
110        while ($locale) {
111            if ($this->filled($map[$locale->id] ?? null)) {
112                return $map[$locale->id];
113            }
114
115            $locale = $locale->fallback();
116        }
117
118        if ($this->filled($map[FieldBase::NEUTRAL_LOCALE] ?? null)) {
119            return $map[FieldBase::NEUTRAL_LOCALE];
120        }
121
122        return null;
123    }
124
125    protected function filled(mixed $value): bool
126    {
127        return $value !== null && $value !== '' && $value !== [];
128    }
129
130    protected function assetsPath(): string
131    {
132        return 'node/' . $this->owner->uid() . '/';
133    }
134
135    protected function getAssets(): Assets
136    {
137        static $assets = null;
138
139        if (!$assets) {
140            $assets = new Assets($this->owner->request(), $this->owner->config());
141        }
142
143        return $assets;
144    }
145}