Completed
Push — master ( 494025...63e9a9 )
by Joshua
14s queued 11s
created

Content::getMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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