Passed
Push — 0.3.0 ( 0d73f1...9a02d7 )
by Anton
04:10
created

Entity::init()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 22
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 6.6037
cc 8
eloc 7
nc 11
nop 2
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\Collection, Entity\Modify;
10
11
		protected $definition = null, $dataset = null;
12
13
		# Select default entity from DB
14
15
		private function selectDefault(string $name, string $value) {
16
17
			$selection = array_keys($this->definition->params());
18
19
			return DB::select(static::$table, $selection, [$name => $value], null, 1);
20
		}
21
22
		# Select nesting entity from DB
23
24
		private function selectNesting(string $name, string $value) {
25
26
			# Process selection
27
28
			$selection = array_keys($this->definition->params());
29
30
			foreach ($selection as $key => $field) $selection[$key] = ('ent.' . $field);
31
32
			# Process query
33
34
			$query = ("SELECT " . implode(', ', $selection) .", rel.ancestor as parent_id ") .
35
36
			         ("FROM " . static::$table . " ent ") .
37
38
			         ("LEFT JOIN " . static::$table_relations . " rel ON rel.descendant = ent.id AND rel.depth = 1 ") .
39
40
			         ("WHERE ent." . $name . " = '" . addslashes($value) . "' LIMIT 1");
41
42
			# ------------------------
43
44
			return DB::send($query);
45
		}
46
47
		# Set selected data
48
49
		protected function setData(array $data) {
50
51
			$id = $this->definition->param('id')->cast($data['id']);
52
53
			# Import data
54
55
			if (isset(self::$cache[static::$table][$id])) {
56
57
				$this->dataset = self::$cache[static::$table][$id]->dataset;
58
			}
59
60
			# Update data
61
62
			$this->dataset->update($data);
63
64
			# Cache entity
65
66
			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...
67
68
			# ------------------------
69
70
			return true;
71
		}
72
73
		# Constructor
74
75
		public function __construct() {
76
77
			# Get definition
78
79
			$this->definition = Entitizer::definition(static::$table);
80
81
			# Preset data
82
83
			$this->dataset = Entitizer::dataset(static::$table);
84
		}
85
86
		# Init entity
87
88
		public function init($value, string $name = 'id') {
89
90
			if (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->selectDefault($name, $value); else $this->selectNesting($name, $value);
105
106
			# ------------------------
107
108
			return ((DB::last() && (DB::last()->rows === 1)) ? $this->setData(DB::last()->row()) : false);
109
		}
110
111
		# Check if unique param value exists
112
113
		public function check($value, string $name) {
114
115
			# Get initiation index
116
117
			if (false === ($index = $this->definition->index($name))) return false;
118
119
			if ($index->type !== 'UNIQUE') return false;
120
121
			# Process name & value
122
123
			$name = $index->name; $value = $this->definition->param($name)->cast($value);
124
125
			# Select entity from DB
126
127
			$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...
128
129
			DB::select(static::$table, 'id', $condition, null, 1);
130
131
			# ------------------------
132
133
			return ((DB::last() && DB::last()->status) ? DB::last()->rows : false);
134
		}
135
136
		# Return definition
137
138
		public function definition() {
139
140
			return $this->definition;
141
		}
142
143
		# Return data
144
145
		public function data() {
146
147
			return $this->dataset->data();
148
		}
149
150
		# Return param value
151
152
		public function get(string $name) {
153
154
			return $this->dataset->get($name);
155
		}
156
157
		# Getter
158
159
		public function __get(string $name) {
160
161
			return $this->dataset->get($name);
162
		}
163
	}
164
}
165