Passed
Push — ft/fragments ( 6fc2e4...3b32df )
by Ben
11:43
created

Fragment::fromNew()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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