Completed
Push — master ( 416cb1...222de5 )
by Anton
03:44
created

Init::implement()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 1
nc 1
1
<?php
2
3
namespace Modules\Entitizer\Utils\Entity\Action {
4
5
	use DB;
6
7
	trait Init {
8
9
		protected $definition = null, $error = false, $id = 0, $data = [];
10
11
		# Init entity
12
13
		public function init($value, string $name = 'id') {
14
15
			if (0 !== $this->id) return false;
16
17
			# Check param name
18
19
			if ($name === 'id') $param = $this->definition->id();
20
21 View Code Duplication
			else 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...
22
23
			# Select entity from DB
24
25
			$selection = array_merge(['id'], array_keys($this->definition->params()));
26
27
			$name = $param->name(); $value = $param->cast($value);
28
29
			DB::select(static::$table, $selection, [$name => $value], null, 1);
30
31
			if (($this->error = !(DB::last() && DB::last()->status)) || (DB::last()->rows !== 1)) return false;
32
33
			$data = DB::last()->row();
34
35
			# Cast data
36
37
			$this->id = $this->definition->id()->cast($data['id']);
38
39
			foreach ($this->definition->params() as $name => $param) $this->data[$name] = $param->cast($data[$name]);
40
41
			# Init path
42
43
			if (static::$nesting) $this->data['path'] = $this->getPath();
44
45
			# Implement entity
46
47
			$this->implement();
48
49
			# Cache entity
50
51
			self::$cache[static::$type][$this->id] = $this;
52
53
			# ------------------------
54
55
			return true;
56
		}
57
58
		# Get path interface
59
60
		abstract protected function getPath();
61
62
		# Implementor interface
63
64
		abstract protected function implement();
65
	}
66
}
67