Completed
Branch feature/pre-split (bbd802)
by Anton
02:54
created

StaticDateEntity::getMutator()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 8
nop 2
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\Models;
8
9
use Spiral\Models\Exceptions\EntityException;
10
use Spiral\Models\Prototypes\AbstractEntity;
11
12
/**
13
 * This is analog of DataEntity based on constant arrays to define mutator and accessors.
14
 *
15
 * @see DataEntity
16
 */
17
class StaticDateEntity extends AbstractEntity
18
{
19
    /**
20
     * List of fields must be hidden from publicFields() method.
21
     *
22
     * @see publicFields()
23
     *
24
     * @var array
25
     */
26
    const HIDDEN = [];
27
28
    /**
29
     * Set of fields allowed to be filled using setFields() method.
30
     *
31
     * @see setFields()
32
     *
33
     * @var array
34
     */
35
    const FILLABLE = [];
36
37
    /**
38
     * List of fields not allowed to be filled by setFields() method. Replace with and empty array
39
     * to allow all fields.
40
     *
41
     * By default all entity fields are settable! Opposite behaviour has to be described in entity
42
     * child implementations.
43
     *
44
     * @see setFields()
45
     *
46
     * @var array|string
47
     */
48
    const SECURED = [];
49
50
    /**
51
     * @see setField()
52
     *
53
     * @var array
54
     */
55
    const SETTERS = [];
56
57
    /**
58
     * @see getField()
59
     *
60
     * @var array
61
     */
62
    const GETTERS = [];
63
64
    /**
65
     * Accessor used to mock field data and filter every request thought itself.
66
     *
67
     * @see getField()
68
     * @see setField()
69
     *
70
     * @var array
71
     */
72
    const ACCESSORS = [];
73
74
    /**
75
     * {@inheritdoc}
76
     *
77
     * Include every composition public data into result.
78
     */
79 View Code Duplication
    public function publicFields(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        $result = [];
82
83
        foreach ($this->getKeys() as $field => $value) {
84
            if (in_array($field, static::HIDDEN)) {
85
                //We might need to use isset in future, for performance
86
                continue;
87
            }
88
89
            $value = $this->getField($field);
90
91
            if ($value instanceof PublishableInterface) {
92
                $result[$field] = $value->publicFields();
93
            } else {
94
                $result[$field] = $value;
95
            }
96
        }
97
98
        return $result;
99
    }
100
101
    /**
102
     * Check if field can be set using setFields() method.
103
     *
104
     * @see   setField()
105
     * @see   $fillable
106
     * @see   $secured
107
     *
108
     * @param string $field
109
     *
110
     * @return bool
111
     */
112
    protected function isFillable(string $field): bool
113
    {
114
        if (!empty(static::FILLABLE)) {
115
            return in_array($field, static::FILLABLE);
116
        }
117
118
        if (static::SECURED === '*') {
119
            return false;
120
        }
121
122
        return !in_array($field, static::SECURED);
123
    }
124
125
    /**
126
     * Check and return name of mutator (getter, setter, accessor) associated with specific field.
127
     *
128
     * @param string $field
129
     * @param string $mutator Mutator type (setter, getter, accessor).
130
     *
131
     * @return mixed|null
132
     *
133
     * @throws EntityException
134
     */
135
    protected function getMutator(string $field, string $mutator)
136
    {
137
        $target = [];
138
        switch ($mutator) {
139
            case self::MUTATOR_ACCESSOR:
140
                $target = static::ACCESSORS;
141
                break;
142
            case self::MUTATOR_GETTER:
143
                $target = static::GETTERS;
144
                break;
145
            case self::MUTATOR_SETTER:
146
                $target = static::SETTERS;
147
                break;
148
        }
149
150
        if (isset($target[$field])) {
151
            return $target[$field];
152
        }
153
154
        return null;
155
    }
156
}