Passed
Push — ft/pagefield ( db2ad6...f2722b )
by Ben
10:27
created

Fragment::getValue()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 14
rs 9.6111
cc 5
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\Fragments;
6
7
use Thinktomorrow\Chief\Fields\Fields;
8
9
class Fragment
10
{
11
    /** @var string */
12
    private $key;
13
14
    /** @var array */
15
    private $values;
16
17
    /** @var Fields */
18
    private $fields;
19
20
    /** @var null|int */
21
    private $modelId;
22
23
    /** @var null|string */
24
    private $modelIdInputName;
25
26
    private function __construct(string $key, array $values, Fields $fields, int $modelId = null, string $modelIdInputName = null)
27
    {
28
        $this->key = $key;
29
        $this->values = $values;
30
        $this->modelId = $modelId;
31
        $this->fields = $fields;
32
        $this->modelIdInputName = $modelIdInputName;
33
    }
34
35
    public static function fromModel(FragmentModel $fragmentModel): self
36
    {
37
        return new static($fragmentModel->key, $fragmentModel->values->all(), new Fields(), (int) $fragmentModel->id);
38
    }
39
40
    public static function fromNew(string $key, array $values): self
41
    {
42
        return new static($key, $values, new Fields());
43
    }
44
45
    public static function empty(string $key): self
46
    {
47
        return static::fromNew($key, []);
48
    }
49
50
    public static function fromRequestPayload(string $key, array $payload): self
51
    {
52
        if (!isset($payload['modelId'])) {
53
            return static::fromNew($key, $payload);
54
        }
55
56
        $modelId = $payload['modelId'];
57
        unset($payload['modelId']);
58
59
        return new static($key, $payload, new Fields(), (int) $modelId);
60
    }
61
62
    public function setFields(Fields $fields): self
63
    {
64
        return new static($this->key, $this->values, $fields, $this->modelId, $this->modelIdInputName);
65
    }
66
67
68
    /** @return string */
69
    public function getKey(): string
70
    {
71
        return $this->key;
72
    }
73
74
    public function getValue(string $key, ?string $locale = null)
75
    {
76
        if (!isset($this->values[$key])) {
77
            return null;
78
        }
79
80
        // When the value is an array, it is assumed that this is an array of locales, each representing the value for that locale
81
        if (is_array($this->values[$key])) {
82
            if ($locale && array_key_exists($locale, $this->values[$key])) {
83
                return $this->values[$key][$locale];
84
            }
85
        }
86
87
        return $this->values[$key];
88
    }
89
90
    /** @return array */
91
    public function getValues(): array
92
    {
93
        return $this->values;
94
    }
95
96
    public function getModelId(): int
97
    {
98
        return $this->modelId;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->modelId could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
99
    }
100
101
    public function hasModelId(): bool
102
    {
103
        return !is_null($this->modelId);
104
    }
105
106
    public function getFields(): Fields
107
    {
108
        return $this->fields;
109
    }
110
111
    public function setModelIdInputName(string $modelIdInputName): self
112
    {
113
        return new static($this->key, $this->values, $this->fields, $this->modelId, $modelIdInputName);
114
    }
115
116
    public function getModelIdInputName(): ?string
117
    {
118
        return $this->modelIdInputName;
119
    }
120
}
121