Passed
Push — master ( 054586...a82a12 )
by Anton
03:12
created

Dataset::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package Cadmium\System\Modules\Entitizer
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules\Entitizer\Utils {
11
12
	use Modules\Entitizer;
13
14
	abstract class Dataset {
15
16
		protected $params = null, $virtuals = [], $data = [];
17
18
		/**
19
		 * Add a virtual param
20
		 */
21
22
		protected function addVirtual(string $name, callable $virtual) {
23
24
			if (isset($this->params[$name]) || isset($this->virtuals[$name])) return;
25
26
			$this->virtuals[$name] = $virtual;
27
		}
28
29
		/**
30
		 * Constructor
31
		 */
32
33
		public function __construct() {
34
35
			$this->params = Entitizer::getDefinition(static::$table)->getParams();
36
37
			if (static::$nesting) $this->params['parent_id'] = $this->params['id'];
38
39
			$this->init(); $this->reset();
40
		}
41
42
		/**
43
		 * Reset all the params to their default values
44
		 *
45
		 * @return Modules\Entitizer\Utils\Dataset : the current dataset object
46
		 */
47
48
		public function reset() : Dataset {
49
50
			# Reset params
51
52
			foreach ($this->params as $name => $param) $this->data[$name] = $param->cast(null);
53
54
			# Reset virtuals
55
56
			foreach ($this->virtuals as $name => $virtual) $this->data[$name] = $virtual($this->data);
57
58
			# ------------------------
59
60
			return $this;
61
		}
62
63
		/**
64
		 * Update the params with the values given in the data array
65
		 *
66
		 * @return Modules\Entitizer\Utils\Dataset : the current dataset object
67
		 */
68
69
		public function update(array $data) : Dataset {
70
71
			# Update params
72
73 View Code Duplication
			foreach ($data as $name => $value) {
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...
74
75
				if (isset($this->params[$name])) $this->data[$name] = $this->params[$name]->cast($value);
76
			}
77
78
			# Update extras
79
80
			foreach ($this->virtuals as $name => $virtual) $this->data[$name] = $virtual($this->data);
81
82
			# ------------------------
83
84
			return $this;
85
		}
86
87
		/**
88
		 * Validate and return the data array without affecting the dataset
89
		 */
90
91
		public function cast(array $data) : array {
92
93
			$cast = [];
94
95 View Code Duplication
			foreach ($data as $name => $value) {
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...
96
97
				if (isset($this->params[$name])) $cast[$name] = $this->params[$name]->cast($value);
98
			}
99
100
			# ------------------------
101
102
			return $cast;
103
		}
104
105
		/**
106
		 * Get a param value
107
		 *
108
		 * @return mixed|null : the value or null if the param does not exist
109
		 */
110
111
		public function get(string $name) {
112
113
			return ($this->data[$name] ?? null);
114
		}
115
116
		/**
117
		 * Get the array of params and their values
118
		 */
119
120
		public function getData() : array {
121
122
			return $this->data;
123
		}
124
125
		/**
126
		 * An alias for the get method
127
		 */
128
129
		public function __get(string $name) {
130
131
			return ($this->data[$name] ?? null);
132
		}
133
134
		/**
135
		 * Check if a param exists
136
		 */
137
138
		 public function __isset(string $name) : bool {
139
140
 			return isset($this->data[$name]);
141
 		}
142
	}
143
}
144