Passed
Push — develop ( 3ce561...bf3f40 )
by nguereza
02:28
created

Entity::__unset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Platine ORM
5
 *
6
 * Platine ORM provides a flexible and powerful ORM implementing a data-mapper pattern.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine ORM
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file Entity.php
33
 *
34
 *  The Entity class
35
 *
36
 *  @package    Platine\Orm
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   http://www.iacademy.cf
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Orm;
48
49
use JsonSerializable;
50
use Platine\Orm\Exception\PropertyNotFoundException;
51
use Platine\Orm\Mapper\DataMapper;
52
use Platine\Orm\Mapper\DataMapperInterface;
53
use Platine\Orm\Mapper\EntityMapper;
54
use Platine\Orm\Mapper\EntityMapperInterface;
55
56
/**
57
 * Class Entity
58
 * @package Platine\Orm
59
 */
60
abstract class Entity implements JsonSerializable
61
{
62
    /**
63
     * The instance of data mapper
64
     * @var DataMapper|null
65
     */
66
    private ?DataMapper $dataMapper = null;
67
68
    /**
69
     * The data mapper constructor arguments
70
     * @var array<int, mixed>
71
     */
72
    private array $dataMapperArgs = [];
73
74
    /**
75
     *
76
     * @param EntityManager $manager
77
     * @param EntityMapper $mapper
78
     * @param array<string, mixed> $columns
79
     * @param array<string, \Platine\Orm\Relation\RelationLoader> $loaders
80
     * @param bool $isReadOnly
81
     * @param bool $isNew
82
     */
83
    final public function __construct(
84
        EntityManager $manager,
85
        EntityMapper $mapper,
86
        array $columns = [],
87
        array $loaders = [],
88
        bool $isReadOnly = false,
89
        bool $isNew = false
90
    ) {
91
        $this->dataMapperArgs = [
92
            $manager,
93
            $mapper,
94
            $columns,
95
            $loaders,
96
            $isReadOnly,
97
            $isNew
98
        ];
99
    }
100
101
    /**
102
     * Convert entity to JSON array
103
     * @return array<string, mixed>
104
     */
105
    public function jsonSerialize()
106
    {
107
        $rawColumns = $this->mapper()->getRawColumns();
108
        $data = [];
109
        foreach ($rawColumns as $name => $value) {
110
            if ($this->mapper()->hasRelation($name)) {
111
                $data[$name] = $this->mapper()->getRelated($name);
112
            } elseif ($this->mapper()->hasColumn($name)) {
113
                $data[$name] = $this->mapper()->getColumn($name);
114
            } else {
115
                $data[$name] = $value;
116
            }
117
        }
118
119
        return $data;
120
    }
121
122
    /**
123
     * Shortcut to DataMapper getColumn and getRelated
124
     * @param string $name
125
     * @return mixed
126
     */
127
    public function __get(string $name)
128
    {
129
        if ($this->mapper()->hasRelation($name)) {
130
            return $this->mapper()->getRelated($name);
131
        }
132
133
        if ($this->mapper()->hasColumn($name)) {
134
            return $this->mapper()->getColumn($name);
135
        }
136
137
        throw new PropertyNotFoundException(sprintf(
138
            'Unknown column or relation [%s]',
139
            $name
140
        ));
141
    }
142
143
    /**
144
     * Shortcut to DataMapper setColumn and setRelated
145
     * @param string $name
146
     * @param mixed $value
147
     * @return void
148
     */
149
    public function __set(string $name, $value)
150
    {
151
        if ($this->mapper()->hasRelation($name)) {
152
            if (is_array($value)) {
153
                foreach ($value as $entity) {
154
                    $this->mapper()->link($name, $entity);
155
                }
156
            } else {
157
                $this->mapper()->setRelated($name, $value);
158
            }
159
        } else {
160
            $this->mapper()->setColumn($name, $value);
161
        }
162
    }
163
164
    /**
165
     * Shortcut to DataMapper hasColumn and hasRelated
166
     * @param string $name
167
     * @return bool
168
     */
169
    public function __isset(string $name)
170
    {
171
        return $this->mapper()->hasRelation($name)
172
                || $this->mapper()->hasColumn($name);
173
    }
174
    
175
    /**
176
     * Shortcut to DataMapper clearColumn
177
     * @param string $name
178
     * @return bool
179
     */
180
    public function __unset(string $name)
181
    {
182
        $this->mapper()->clearColumn($name, true);
183
    }
184
185
    /**
186
     * Return the string representation of this entity
187
     * @return string
188
     */
189
    public function __toString(): string
190
    {
191
        $columns = $this->mapper()->getRawColumns();
192
        $columnsStr = '';
193
        foreach ($columns as $name => $value) {
194
            $columnsStr .= sprintf('%s=%s, ', $name, (string) $value);
195
        }
196
197
        return sprintf('[%s(%s)]', __CLASS__, rtrim($columnsStr, ', '));
198
    }
199
200
        /**
201
     * Map the entity information
202
     * @param EntityMapperInterface $mapper
203
     */
204
    abstract public static function mapEntity(EntityMapperInterface $mapper): void;
205
206
    /**
207
     * Return the instance of data mapper
208
     * @return DataMapperInterface
209
     */
210
    final protected function mapper(): DataMapperInterface
211
    {
212
        if ($this->dataMapper === null) {
213
            $this->dataMapper = new DataMapper(...$this->dataMapperArgs);
214
        }
215
216
        return $this->dataMapper;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dataMapper could return the type null which is incompatible with the type-hinted return Platine\Orm\Mapper\DataMapperInterface. Consider adding an additional type-check to rule them out.
Loading history...
217
    }
218
}
219