Completed
Push — master ( cd00c2...c10be1 )
by Joshua
13s queued 11s
created

Content::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the -SeamsCMSDeliverySdk 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
    /**
31
     * Content constructor.
32
     *
33
     */
34
    protected function __construct()
35
    {
36
    }
37
38
    /**
39
     * @param string $field
40
     * @param string|null $locale
41
     * @param string|null $fallbackLocale
42
     *
43
     * @return scalar|Content|Content[]|null
44
     */
45
    public function get($field, string $locale = null, string $fallbackLocale = null)
46
    {
47
        if ($locale && $this->has($field, $locale)) {
48
            return $this->content[$field]['locales'][$locale];
49
        }
50
51
        if ($fallbackLocale && $this->has($field, $fallbackLocale)) {
52
            return $this->content[$field]['locales'][$fallbackLocale];
53
        } elseif ($this->has($field)) {
54
            return $this->content[$field]['value'];
55
        }
56
57
        return null;
58
    }
59
60
    /**
61
     * @param string $field
62
     * @param string|null $locale
63
     *
64
     * @return bool
65
     */
66
    public function has(string $field, string $locale = null): bool
67
    {
68
        if ($locale) {
69
            return isset($this->content[$field]['locales'][$locale]);
70
        }
71
72
        return isset($this->content[$field]['value']);
73
    }
74
75
    /**
76
     * @param string $field
77
     *
78
     * @return bool True if the field exists and is localized, false otherwise
79
     */
80
    public function isLocalized(string $field): bool
81
    {
82
        return !empty($this->content[$field]['locales']);
83
    }
84
85
    /**
86
     * @param array $data
87
     * @return Content
88
     */
89
    public static function fromArray($data)
90
    {
91
        $data['meta'] = ContentMeta::fromArray($data['meta']);
92
93
        foreach ($data['content'] as $key => $item) {
94
            $item['value'] = static::convert($item['value']);
95
96
            foreach ($item['locales'] as $localizedKey => $localizedItem) {
97
                $item['locales'][$localizedKey] = static::convert($localizedItem);
98
            }
99
100
            $data['content'][$key] = $item;
101
        }
102
103
        return self::fromArrayTrait($data);
104
    }
105
106
    /**
107
     * @param mixed $item
108
     * @return array|mixed
109
     */
110
    public static function convert($item)
111
    {
112
        // Scalar string doesn't need to be converted
113
        if (!is_array($item)) {
114
            return $item;
115
        }
116
117
        // Are we an (indexed) list?
118
        foreach ($item as $listIndex => $listItem) {
119
            // Do we have meta and content? Then we assume we are (recursive) content
120
            if (isset($listItem['meta']) && isset($listItem['content'])) {
121
                $item[$listIndex] = static::fromArray($listItem);
122
            }
123
        }
124
125
        return $item;
126
    }
127
}
128