Completed
Push — 0.2.3 ( 4e241d...b4719c )
by Anton
06:36 queued 02:56
created

Entity::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 22
rs 9.2
c 1
b 0
f 1
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 Entitizer {
8
9
		use Entity\Action\Init, Entity\Action\Create, Entity\Action\Fill, Entity\Action\Edit, Entity\Action\Remove;
10
11
		protected $definition = null, $error = false, $id = 0, $data = [];
12
13
		# Init parent entities
14
15
		private function getPath() {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
16
17
			$entity = $this; $path = [array_merge(['id' => $this->id], $this->data)];
18
19
			while (0 !== $entity->data['parent_id']) {
20
21
				$entity = self::get(static::$type, $entity->data['parent_id']);
22
23
				if (0 === $entity->id) return [];
24
25
				$path[] = array_merge(['id' => $entity->id], $entity->data);
26
			}
27
28
			# ------------------------
29
30
			return array_reverse($path);
31
		}
32
33
		# Constructor
34
35
		public function __construct(int $id = 0) {
36
37
			# Create definition object
38
39
			$this->definition = Entitizer\Definition::get(static::$type);
40
41
			# Preset data array
42
43
			$this->data = $this->definition->implement();
44
45
			# Preset path
46
47
			if (static::$nesting) $this->data['path'] = [];
48
49
			# Implement entity
50
51
			$this->implement();
52
53
			# Init entity
54
55
			if ($id > 0) $this->init($id);
56
		}
57
58
		# Check if unique param value exists
59
60
		public function check(string $name, $value) {
61
62 View Code Duplication
			if ((false === ($param = $this->definition->get($name))) || !$param->unique()) 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...
63
64
			# Select entity from DB
65
66
			$name = $param->name(); $value = $param->cast($value);
67
68
			$condition = ($name . " = '" . addslashes($value) . "' AND id != " . $this->id);
69
70
			DB::select(static::$table, 'id', $condition, null, 1);
71
72
			# ------------------------
73
74
			return ((DB::last() && DB::last()->status) ? DB::last()->rows : false);
75
		}
76
77
		# Count children
78
79
		public function children() {
80
81
			DB::select(static::$table, 'COUNT(id) as count', ['parent_id' => $this->id]);
82
83
			if (!(DB::last() && DB::last()->status)) return false;
84
85
			# ------------------------
86
87
			return intval(DB::last()->row()['count']);
88
		}
89
90
		# Check for error
91
92
		public function error() {
93
94
			return $this->error;
95
		}
96
97
		# Return id
98
99
		public function id() {
100
101
			return $this->id;
102
		}
103
104
		# Return data
105
106
		public function data() {
107
108
			return $this->data;
109
		}
110
111
		# Getter
112
113
		public function __get(string $name) {
114
115
			if ($name === 'id') return $this->id;
116
117
			return (isset($this->data[$name]) ? $this->data[$name] : null);
118
		}
119
	}
120
}
121