Completed
Pull Request — 5.1 (#94)
by Kirill
02:58
created

Entity::mock()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
rs 9.4285
cc 3
eloc 12
nc 4
nop 1
1
<?php
2
3
namespace Analogue\ORM;
4
5
use Analogue\ORM\System\Proxies\EntityProxy;
6
7
class Entity extends ValueObject
8
{
9
    /**
10
     * Entities Hidden Attributes, that will be discarded when converting
11
     * the entity to Array/Json
12
     * (can include any embedded object's attribute)
13
     *
14
     * @var array
15
     */
16
    protected $hidden = [];
17
18
    /**
19
     * Return the entity's attribute
20
     * @param  string $key
21
     * @return mixed
22
     */
23
    public function __get($key)
24
    {
25
        if ($this->hasGetMutator($key)) {
26
            $method = 'get' . $this->getMutatorMethod($key);
27
28
            $attribute = null;
29
30
            if (isset($this->attributes[$key])) {
31
                $attribute = $this->attributes[$key];
32
            }
33
34
            return $this->$method($attribute);
35
        }
36
        if (!array_key_exists($key, $this->attributes)) {
37
            return null;
38
        }
39
        if ($this->attributes[$key] instanceof EntityProxy) {
40
            $this->attributes[$key] = $this->attributes[$key]->load();
41
        }
42
        return $this->attributes[$key];
43
    }
44
45
    /**
46
     * Dynamically set attributes on the entity.
47
     *
48
     * @param  string $key
49
     * @param  mixed  $value
50
     * @return void
51
     */
52
    public function __set($key, $value)
53
    {
54
        if ($this->hasSetMutator($key)) {
55
            $method = 'set' . $this->getMutatorMethod($key);
56
57
            $this->$method($value);
58
        } else {
59
            $this->attributes[$key] = $value;
60
        }
61
    }
62
63
    /**
64
     * Is a getter method defined ?
65
     *
66
     * @param  string $key
67
     * @return boolean
68
     */
69
    protected function hasGetMutator($key)
70
    {
71
        return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false;
72
    }
73
74
    /**
75
     * Is a setter method defined ?
76
     *
77
     * @param  string $key
78
     * @return boolean
79
     */
80
    protected function hasSetMutator($key)
81
    {
82
        return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false;
83
    }
84
85
    /**
86
     * @param $key
87
     * @return string
88
     */
89
    protected function getMutatorMethod($key)
90
    {
91
        return ucfirst($key) . 'Attribute';
92
    }
93
94
    /**
95
     * Convert every attributes to value / arrays
96
     *
97
     * @return array
98
     */
99
    public function toArray()
100
    {
101
        // First, call the trait method before filtering
102
        // with Entity specific methods
103
        $attributes = $this->attributesToArray($this->attributes);
104
        
105
        foreach ($this->attributes as $key => $attribute) {
106
            if (in_array($key, $this->hidden)) {
107
                unset($attributes[$key]);
108
                continue;
109
            }
110
            if ($this->hasGetMutator($key)) {
111
                $method = 'get' . $this->getMutatorMethod($key);
112
                $attributes[$key] = $this->$method($attribute);
113
            }
114
        }
115
        return $attributes;
116
    }
117
118
    /**
119
     * Create entity with arbitrary arguments
120
     *
121
     * @param array|string $argumentsOrPrimaryKey
122
     * @return Entity
123
     * @throws MappingException
124
     * @throws \InvalidArgumentException
125
     */
126
    public static function mock($argumentsOrPrimaryKey)
127
    {
128
        if (!is_array($argumentsOrPrimaryKey)) {
129
            $primaryKey = Manager::getInstance()
130
                ->mapper(static::class)
131
                ->getEntityMap()
132
                ->getKeyName();
133
134
            $argumentsOrPrimaryKey = [$primaryKey => $argumentsOrPrimaryKey];
135
        }
136
137
        $instance = (new \ReflectionClass(static::class))
138
            ->newInstanceWithoutConstructor();
139
140
        foreach ($argumentsOrPrimaryKey as $key => $value) {
141
            $instance->$key = $value;
142
        }
143
144
        return $instance;
145
    }
146
}
147