Completed
Branch feature/pre-split (afd44c)
by Anton
07:02
created

DynamicEntity   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 112
c 0
b 0
f 0
rs 10
wmc 6
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isPublic() 0 4 1
A isFillable() 0 12 3
A getMutator() 0 12 2
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Models;
10
11
use Spiral\Models\Exceptions\EntityException;
12
use Spiral\Models\Prototypes\AbstractEntity;
13
14
/**
15
 * DataEntity in spiral used to represent basic data set with validation rules, filters and
16
 * accessors. Most of spiral models (ORM and ODM, HttpFilters) will extend data entity. In addition
17
 * it creates magic set of getters and setters for every field name (see validator trait) in model.
18
 *
19
 * DataEntity provides ability to configure it's state using internal properties.
20
 */
21
class DynamicEntity extends AbstractEntity
22
{
23
    /**
24
     * List of fields must be hidden from publicFields() method.
25
     *
26
     * @see publicFields()
27
     *
28
     * @var array
29
     */
30
    protected $hidden = [];
31
32
    /**
33
     * Set of fields allowed to be filled using setFields() method.
34
     *
35
     * @see setFields()
36
     *
37
     * @var array
38
     */
39
    protected $fillable = [];
40
41
    /**
42
     * List of fields not allowed to be filled by setFields() method. Replace with and empty array
43
     * to allow all fields.
44
     *
45
     * By default all entity fields are settable! Opposite behaviour has to be described in entity
46
     * child implementations.
47
     *
48
     * @see setFields()
49
     *
50
     * @var array|string
51
     */
52
    protected $secured = [];
53
54
    /**
55
     * @see setField()
56
     *
57
     * @var array
58
     */
59
    protected $setters = [];
60
61
    /**
62
     * @see getField()
63
     *
64
     * @var array
65
     */
66
    protected $getters = [];
67
68
    /**
69
     * Accessor used to mock field data and filter every request thought itself.
70
     *
71
     * @see getField()
72
     * @see setField()
73
     *
74
     * @var array
75
     */
76
    protected $accessors = [];
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function isPublic(string $field): bool
82
    {
83
        return !in_array($field, $this->hidden);
84
    }
85
86
    /**
87
     * Check if field can be set using setFields() method.
88
     *
89
     * @see   setField()
90
     * @see   $fillable
91
     * @see   $secured
92
     *
93
     * @param string $field
94
     *
95
     * @return bool
96
     */
97
    protected function isFillable(string $field): bool
98
    {
99
        if (!empty($this->fillable)) {
100
            return in_array($field, $this->fillable);
101
        }
102
103
        if ($this->secured === '*') {
104
            return false;
105
        }
106
107
        return !in_array($field, $this->secured);
108
    }
109
110
    /**
111
     * Check and return name of mutator (getter, setter, accessor) associated with specific field.
112
     *
113
     * @param string $field
114
     * @param string $mutator Mutator type (setter, getter, accessor).
115
     *
116
     * @return mixed|null
117
     *
118
     * @throws EntityException
119
     */
120
    protected function getMutator(string $field, string $mutator)
121
    {
122
        //We do support 3 mutators: getter, setter and accessor, all of them can be
123
        //referenced to valid field name by adding "s" at the end
124
        $mutator = $mutator . 's';
125
126
        if (isset($this->{$mutator}[$field])) {
127
            return $this->{$mutator}[$field];
128
        }
129
130
        return null;
131
    }
132
}