|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the SeamsCMSDeliveryBundle package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Seams-CMS.com |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace SeamsCMS\Delivery\Model; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Content |
|
18
|
|
|
* @package SeamsCMS\Delivery\Model |
|
19
|
|
|
*/ |
|
20
|
|
|
class Content |
|
21
|
|
|
{ |
|
22
|
|
|
use HydratorTrait { |
|
23
|
|
|
fromArray as fromArrayTrait; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** @var array */ |
|
27
|
|
|
private $content; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $field |
|
31
|
|
|
* @param string|null $locale |
|
32
|
|
|
* @param string|null $fallbackLocale |
|
33
|
|
|
* |
|
34
|
|
|
* @return scalar|Content|Content[]|null |
|
35
|
|
|
*/ |
|
36
|
|
|
public function get($field, string $locale = null, string $fallbackLocale = null) |
|
37
|
|
|
{ |
|
38
|
|
|
if ($locale && $this->has($field, $locale)) { |
|
39
|
|
|
return $this->content[$field]['locales'][$locale]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ($fallbackLocale && $this->has($field, $fallbackLocale)) { |
|
43
|
|
|
return $this->content[$field]['locales'][$fallbackLocale]; |
|
44
|
|
|
} elseif ($this->has($field)) { |
|
45
|
|
|
return $this->content[$field]['value']; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $field |
|
53
|
|
|
* @param string|null $locale |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public function has(string $field, string $locale = null): bool |
|
58
|
|
|
{ |
|
59
|
|
|
if ($locale) { |
|
60
|
|
|
return isset($this->content[$field]['locales'][$locale]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return isset($this->content[$field]['value']); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $field |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool True if the field exists and is localized, false otherwise |
|
70
|
|
|
*/ |
|
71
|
|
|
public function isLocalized(string $field): bool |
|
72
|
|
|
{ |
|
73
|
|
|
return !empty($this->content[$field]['locales']); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param array $data |
|
78
|
|
|
* @return Content |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function fromArray($data) |
|
81
|
|
|
{ |
|
82
|
|
|
$data['meta'] = ContentMeta::fromArray($data['meta']); |
|
83
|
|
|
|
|
84
|
|
|
foreach ($data['content'] as $key => $item) { |
|
85
|
|
|
$item['value'] = static::convert($item['value']); |
|
86
|
|
|
|
|
87
|
|
|
foreach ($item['locales'] as $localizedKey => $localizedItem) { |
|
88
|
|
|
$item['locales'][$localizedKey] = static::convert($localizedItem); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return self::fromArrayTrait($data); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param $item |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function convert($item) |
|
99
|
|
|
{ |
|
100
|
|
|
// Scalar string doesn't need to be converted |
|
101
|
|
|
if (!is_array($item)) { |
|
102
|
|
|
return $item; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// If we found a meta-key, we are an entry |
|
106
|
|
|
if (isset($item['value'])) { |
|
107
|
|
|
$item['value'] = static::convert($item['value']); |
|
108
|
|
|
|
|
109
|
|
|
return $item; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// Are we an (indexed) list? |
|
113
|
|
|
foreach ($item as $listIndex => $listItem) { |
|
114
|
|
|
$item[$listIndex] = static::fromArray($listItem); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $item; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|