Completed
Push — master ( 13576f...97d117 )
by Joshua
09:01 queued 07:27
created

Content::convert()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 20
ccs 0
cts 13
cp 0
crap 20
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 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