Completed
Branch 0.3.0 (b16461)
by Anton
04:03
created

Entity::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 9.2
cc 3
eloc 6
nc 4
nop 1
1
<?php
2
3
namespace Modules\Entitizer\Utils {
4
5
	use Modules\Entitizer, DB;
6
7
	abstract class Entity extends Cache {
8
9
		use Entity\Modify, Entity\Remove, Entity\View;
10
11
		protected $definition = null, $error = false, $modifiable = false, $data = [];
12
13
		# Select nesting entity from DB
14
15
		private function selectNesting(string $name, string $value) {
16
17
			# Process selection
18
19
			$selection = array_keys($this->definition->params());
20
21
			foreach ($selection as $key => $field) $selection[$key] = ('ent.' . $field);
22
23
			# Process query
24
25
			$query = ("SELECT " . implode(', ', $selection) .", rel.ancestor as parent_id ") .
26
27
			         ("FROM " . static::$table . " ent ") .
28
29
			         ("LEFT JOIN " . static::$table_relations . " rel ON rel.descendant = ent.id AND rel.depth = 1 ") .
30
31
			         ("WHERE ent." . $name . " = '" . addslashes($value) . "' LIMIT 1");
32
33
			# ------------------------
34
35
			return DB::send($query);
36
		}
37
38
		# Set selected data
39
40
		protected function setData(array $data) {
41
42
			# Set data
43
44
			$this->data = $this->definition->cast($data, true);
45
46
			if (static::$nesting) $this->data['parent_id'] = intval($data['parent_id'] ?? 0);
47
48
			# Implement entity
49
50
			$this->implement();
51
52
			# Cache entity
53
54
			self::$cache[static::$table][$this->id] = $this;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Entitizer\Utils\Entity>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
56
			# ------------------------
57
58
			return true;
59
		}
60
61
		# Constructor
62
63
		public function __construct(array $data = []) {
64
65
			# Get definition
66
67
			$this->definition = Entitizer::definition(static::$table);
68
69
			# Check data
70
71
			if ([] === $data) $this->modifiable = true;
72
73
			# Preset data
74
75
			$this->data = $this->definition->cast($data, true);
76
77
			# Preset parent id
78
79
			if (static::$nesting) $this->data['parent_id'] = 0;
80
81
			# Implement entity
82
83
			$this->implement();
84
		}
85
86
		# Init entity
87
88
		public function init($value, string $name = 'id') {
89
90
			if (!$this->modifiable || (0 !== $this->id)) return false;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Entitizer\Utils\Entity>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
91
92
			# Get initiation index
93
94
			if (false === ($index = $this->definition->index($name))) return false;
95
96
			if (($index->type !== 'PRIMARY') && ($index->type !== 'UNIQUE')) return false;
97
98
			# Process name & value
99
100
			$name = $index->name; $value = $this->definition->param($name)->cast($value);
101
102
			# Select entity from DB
103
104
			if (static::$nesting) $this->selectNesting($name, $value); else {
105
106
				$selection = array_keys($this->definition->params());
107
108
				DB::select(static::$table, $selection, [$name => $value], null, 1);
109
			}
110
111 View Code Duplication
			if (($this->error = !(DB::last() && DB::last()->status)) || (DB::last()->rows !== 1)) return false;
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
113
			# ------------------------
114
115
			return $this->setData(DB::last()->row());
116
		}
117
118
		# Check if unique param value exists
119
120
		public function check($value, string $name) {
121
122
			if (!$this->modifiable) return false;
123
124
			# Get initiation index
125
126
			if (false === ($index = $this->definition->index($name))) return false;
127
128
			if ($index->type !== 'UNIQUE') return false;
129
130
			# Process name & value
131
132
			$name = $index->name; $value = $this->definition->param($name)->cast($value);
133
134
			# Select entity from DB
135
136
			$condition = ($name . " = '" . addslashes($value) . "' AND id != " . $this->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Modules\Entitizer\Utils\Entity>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
137
138
			DB::select(static::$table, 'id', $condition, null, 1);
139
140
			# ------------------------
141
142
			return ((DB::last() && DB::last()->status) ? DB::last()->rows : false);
143
		}
144
145
		# Check for init errors
146
147
		public function error() {
148
149
			return $this->error;
150
		}
151
152
		# Check if modifiable
153
154
		public function modifiable() {
155
156
			return $this->modifiable;
157
		}
158
159
		# Return data
160
161
		public function data() {
162
163
			return $this->data;
164
		}
165
166
		# Return param value
167
168
		public function get(string $name) {
169
170
			return ($this->data[$name] ?? null);
171
		}
172
173
		# Getter
174
175
		public function __get(string $name) {
176
177
			return $this->get($name);
178
		}
179
	}
180
}
181