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

SchematicEntity   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 14
c 1
b 0
f 0
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isFillable() 0 15 6
A getMutator() 0 7 2
A __construct() 0 4 1
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
/**
15
 * Entity which code follows external behaviour schema.
16
 */
17
class SchematicEntity extends AbstractEntity
18
{
19
    /** @var array */
20
    private $schema = [];
21
22
    /**
23
     * @param array $data
24
     * @param array $schema
25
     */
26
    public function __construct(array $data, array $schema)
27
    {
28
        $this->schema = $schema;
29
        parent::__construct($data);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function isFillable(string $field): bool
36
    {
37
        if (!empty($this->schema[ModelSchema::FILLABLE]) && $this->schema[ModelSchema::FILLABLE] === '*') {
38
            return true;
39
        }
40
41
        if (!empty($this->schema[ModelSchema::FILLABLE])) {
42
            return in_array($field, $this->schema[ModelSchema::FILLABLE]);
43
        }
44
45
        if (!empty($this->schema[ModelSchema::SECURED]) && $this->schema[ModelSchema::SECURED] === '*') {
46
            return false;
47
        }
48
49
        return !in_array($field, $this->schema[ModelSchema::SECURED]);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function getMutator(string $field, string $mutator)
56
    {
57
        if (isset($this->schema[ModelSchema::MUTATORS][$mutator][$field])) {
58
            return $this->schema[ModelSchema::MUTATORS][$mutator][$field];
59
        }
60
61
        return null;
62
    }
63
}
64