Passed
Push — master ( db5c4e...adaaa2 )
by Kirill
03:03
created

DataEntity::getMutator()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 20
rs 9.4888
cc 5
nc 8
nop 2
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Models;
13
14
use Spiral\Models\Exception\EntityException;
15
16
/**
17
 * DataEntity in spiral used to represent basic data set with filters and accessors. Most of spiral
18
 * models (ORM and ODM, HttpFilters) will extend data entity.
19
 */
20
class DataEntity extends AbstractEntity
21
{
22
    /**
23
     * Set of fields allowed to be filled using setFields() method.
24
     *
25
     * @see setFields()
26
     * @var array
27
     */
28
    protected const FILLABLE = [];
29
30
    /**
31
     * List of fields not allowed to be filled by setFields() method. Replace with and empty array
32
     * to allow all fields.
33
     *
34
     * By default all entity fields are settable! Opposite behaviour has to be described in entity
35
     * child implementations.
36
     *
37
     * @see setFields()
38
     * @var array|string
39
     */
40
    protected const SECURED = '*';
41
42
    /**
43
     * @see setField()
44
     * @var array
45
     */
46
    protected const SETTERS = [];
47
48
    /**
49
     * @see getField()
50
     * @var array
51
     */
52
    protected const GETTERS = [];
53
54
    /**
55
     * Accessor used to mock field data and filter every request thought itself.
56
     *
57
     * @see getField()
58
     * @see setField()
59
     * @var array
60
     */
61
    protected const ACCESSORS = [];
62
63
    /**
64
     * Check if field can be set using setFields() method.
65
     *
66
     * @param string $field
67
     * @return bool
68
     *
69
     * @see  $secured
70
     * @see  setField()
71
     * @see  $fillable
72
     */
73
    protected function isFillable(string $field): bool
74
    {
75
        if (static::FILLABLE === '*') {
0 ignored issues
show
introduced by
The condition static::FILLABLE === '*' is always false.
Loading history...
76
            return true;
77
        }
78
79
        if (!empty(static::FILLABLE)) {
0 ignored issues
show
introduced by
The condition empty(static::FILLABLE) is always true.
Loading history...
80
            return in_array($field, static::FILLABLE);
81
        }
82
83
        if (static::SECURED === '*') {
0 ignored issues
show
introduced by
The condition static::SECURED === '*' is always true.
Loading history...
84
            return false;
85
        }
86
87
        return !in_array($field, static::SECURED);
88
    }
89
90
    /**
91
     * Check and return name of mutator (getter, setter, accessor) associated with specific field.
92
     *
93
     * @param string $field
94
     * @param string $mutator Mutator type (setter, getter, accessor).
95
     * @return mixed|null
96
     *
97
     * @throws EntityException
98
     */
99
    protected function getMutator(string $field, string $mutator)
100
    {
101
        $target = [];
102
        switch ($mutator) {
103
            case ModelSchema::MUTATOR_ACCESSOR:
104
                $target = static::ACCESSORS;
105
                break;
106
            case ModelSchema::MUTATOR_GETTER:
107
                $target = static::GETTERS;
108
                break;
109
            case ModelSchema::MUTATOR_SETTER:
110
                $target = static::SETTERS;
111
                break;
112
        }
113
114
        if (isset($target[$field])) {
115
            return $target[$field];
116
        }
117
118
        return null;
119
    }
120
}
121