Completed
Branch feature/pre-split (e801ec)
by Anton
03:11
created

SchematicEntity::publicFields()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 0
dl 19
loc 19
rs 9.2
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A SchematicEntity::isFillable() 0 12 3
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Models;
9
10
use Spiral\Models\Events\DescribeEvent;
11
use Spiral\Models\Prototypes\AbstractEntity;
12
use Spiral\Models\Reflections\ReflectionEntity;
13
14
/**
15
 * Entity which code follows external behaviour schema.
16
 */
17
class SchematicEntity extends AbstractEntity
18
{
19
    /**
20
     * Schema constants. Starts with 4, but why not?
21
     */
22
    const SH_HIDDEN   = 4;
23
    const SH_SECURED  = 5;
24
    const SH_FILLABLE = 6;
25
    const SH_MUTATORS = 7;
26
27
    /**
28
     * Behaviour schema.
29
     *
30
     * @var array
31
     */
32
    private $schema = [];
33
34
    /**
35
     * @param array $fields
36
     * @param array $schema
37
     */
38
    public function __construct(array $fields, array $schema)
39
    {
40
        $this->schema = $schema;
41
        parent::__construct($fields);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function isPublic(string $field): bool
48
    {
49
        return !in_array($field, $this->schema[self::SH_HIDDEN]);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function isFillable(string $field): bool
56
    {
57
        if (!empty($this->schema[self::SH_FILLABLE])) {
58
            return in_array($field, $this->schema[self::SH_FILLABLE]);
59
        }
60
61
        if ($this->schema[self::SH_SECURED] === '*') {
62
            return false;
63
        }
64
65
        return !in_array($field, $this->schema[self::SH_SECURED]);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function getMutator(string $field, string $mutator)
72
    {
73
        if (isset($this->schema[self::SH_MUTATORS][$mutator][$field])) {
74
            return $this->schema[self::SH_MUTATORS][$mutator][$field];
75
        }
76
77
        return null;
78
    }
79
80
    /**
81
     * Method used while entity static analysis to describe model related property using even
82
     * dispatcher and associated model traits.
83
     *
84
     * @param ReflectionEntity $reflection
85
     * @param string           $property
86
     * @param mixed            $value
87
     *
88
     * @return mixed Returns filtered value.
89
     * @event describe(DescribeEvent)
90
     */
91
    public static function describeProperty(ReflectionEntity $reflection, string $property, $value)
92
    {
93
        static::initialize(true);
94
95
        /**
96
         * Clarifying property value using traits or other listeners.
97
         *
98
         * @var DescribeEvent $event
99
         */
100
        $event = static::events()->dispatch(
101
            'describe',
102
            new DescribeEvent($reflection, $property, $value)
103
        );
104
105
        return $event->getValue();
106
    }
107
}