Entity   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 53
dl 0
loc 176
rs 10
c 4
b 0
f 0
wmc 22

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A __isset() 0 4 2
A jsonSerialize() 0 20 5
A __get() 0 13 3
A __toString() 0 9 2
A mapper() 0 10 2
A __set() 0 12 4
A __unset() 0 3 1
A __clone() 0 4 2
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   https://www.platine-php.com
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
use Stringable;
56
57
/**
58
 * @class Entity
59
 * @package Platine\Orm
60
 * @template TEntity as Entity
61
 */
62
abstract class Entity implements JsonSerializable, Stringable
63
{
64
    /**
65
     * The instance of data mapper
66
     * @var DataMapper<TEntity>|null
67
     */
68
    private ?DataMapper $dataMapper = null;
69
70
    /**
71
     * The data mapper constructor arguments
72
     * @var array<int, mixed>
73
     */
74
    private array $dataMapperArgs = [];
75
76
    /**
77
     *
78
     * @param EntityManager<TEntity> $manager
79
     * @param EntityMapper<TEntity>  $mapper
80
     * @param array<string, mixed> $columns
81
     * @param array<string, \Platine\Orm\Relation\RelationLoader<TEntity>> $loaders
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, \Platine\O...elationLoader<TEntity>> at position 4 could not be parsed: Expected '>' at position 4, but found '\Platine\Orm\Relation\RelationLoader'.
Loading history...
82
     * @param bool $isReadOnly
83
     * @param bool $isNew
84
     */
85
    final public function __construct(
86
        EntityManager $manager,
87
        EntityMapper $mapper,
88
        array $columns = [],
89
        array $loaders = [],
90
        bool $isReadOnly = false,
91
        bool $isNew = false
92
    ) {
93
        $this->dataMapperArgs = [
94
            $manager,
95
            $mapper,
96
            $columns,
97
            $loaders,
98
            $isReadOnly,
99
            $isNew
100
        ];
101
    }
102
103
    /**
104
     * Clone the object
105
     * @return void
106
     */
107
    public function __clone(): void
108
    {
109
        if ($this->dataMapper !== null) {
110
            $this->dataMapper = clone $this->dataMapper;
111
        }
112
    }
113
114
    /**
115
     * Convert entity to JSON array
116
     * @return array<string, mixed>
117
     */
118
    public function jsonSerialize(): mixed
119
    {
120
        $rawColumns = $this->mapper()->getRawColumns();
121
        $data = [];
122
        foreach ($rawColumns as $name => $value) {
123
            if ($this->mapper()->hasRelation($name)) {
124
                $relation = $this->mapper()->getRelated($name);
125
                if ($relation instanceof self) {
126
                    $data[$name] = $relation->jsonSerialize();
127
                } else {
128
                    $data[$name] = $relation;
129
                }
130
            } elseif ($this->mapper()->hasColumn($name)) {
131
                $data[$name] = $this->mapper()->getColumn($name);
132
            } else {
133
                $data[$name] = $value;
134
            }
135
        }
136
137
        return $data;
138
    }
139
140
    /**
141
     * Shortcut to DataMapper getColumn and getRelated
142
     * @param string $name
143
     * @return mixed
144
     */
145
    public function __get(string $name): mixed
146
    {
147
        if ($this->mapper()->hasRelation($name)) {
148
            return $this->mapper()->getRelated($name);
149
        }
150
151
        if ($this->mapper()->hasColumn($name)) {
152
            return $this->mapper()->getColumn($name);
153
        }
154
155
        throw new PropertyNotFoundException(sprintf(
156
            'Unknown column or relation [%s]',
157
            $name
158
        ));
159
    }
160
161
    /**
162
     * Shortcut to DataMapper setColumn and setRelated
163
     * @param string $name
164
     * @param mixed $value
165
     * @return void
166
     */
167
    public function __set(string $name, mixed $value): void
168
    {
169
        if ($this->mapper()->hasRelation($name)) {
170
            if (is_array($value)) {
171
                foreach ($value as $entity) {
172
                    $this->mapper()->link($name, $entity);
173
                }
174
            } else {
175
                $this->mapper()->setRelated($name, $value);
176
            }
177
        } else {
178
            $this->mapper()->setColumn($name, $value);
179
        }
180
    }
181
182
    /**
183
     * Shortcut to DataMapper hasColumn and hasRelated
184
     * @param string $name
185
     * @return bool
186
     */
187
    public function __isset(string $name): bool
188
    {
189
        return $this->mapper()->hasRelation($name)
190
                || $this->mapper()->hasColumn($name);
191
    }
192
193
    /**
194
     * Shortcut to DataMapper clearColumn
195
     * @param string $name
196
     * @return void
197
     */
198
    public function __unset(string $name): void
199
    {
200
        $this->mapper()->clearColumn($name, true);
201
    }
202
203
    /**
204
     * Return the string representation of this entity
205
     * @return string
206
     */
207
    public function __toString(): string
208
    {
209
        $columns = $this->mapper()->getRawColumns();
210
        $columnsStr = '';
211
        foreach ($columns as $name => $value) {
212
            $columnsStr .= sprintf('%s=%s, ', $name, (string) $value);
213
        }
214
215
        return sprintf('[%s(%s)]', __CLASS__, rtrim($columnsStr, ', '));
216
    }
217
218
        /**
219
     * Map the entity information
220
     * @param EntityMapperInterface<TEntity> $mapper
221
     */
222
    abstract public static function mapEntity(EntityMapperInterface $mapper): void;
223
224
    /**
225
     * Return the instance of data mapper
226
     * @return DataMapperInterface<TEntity>
227
     */
228
    final protected function mapper(): DataMapperInterface
229
    {
230
        if ($this->dataMapper === null) {
231
            /** @var DataMapper<TEntity> $dataMapper */
232
            $dataMapper = new DataMapper(...$this->dataMapperArgs);
233
234
            $this->dataMapper = $dataMapper;
235
        }
236
237
        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...
238
    }
239
}
240