Test Failed
Push — master ( 7c6e58...60f0f2 )
by Jean-Christophe
16:45
created

DAOPreparedQuery::getPropsKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
ccs 0
cts 1
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Ubiquity\orm\core\prepared;
4
5
use Ubiquity\db\SqlUtils;
6
use Ubiquity\orm\DAO;
7
use Ubiquity\orm\OrmUtils;
8
use Ubiquity\orm\parser\ConditionParser;
9
10
/**
11
 * Ubiquity\orm\core\prepared$DAOPreparedQuery
12
 * This class is part of Ubiquity
13
 *
14
 * @author jcheron <[email protected]>
15
 * @version 1.0.3
16
 *
17
 */
18
abstract class DAOPreparedQuery {
19
	protected $databaseOffset;
20
21
	/**
22
	 *
23
	 * @var ConditionParser
24
	 */
25
	protected $conditionParser;
26
	protected $included;
27
	protected $hasIncluded;
28
	protected $useCache;
29
	protected $className;
30
	protected $tableName;
31
	protected $invertedJoinColumns = null;
32
	protected $oneToManyFields = null;
33
	protected $manyToManyFields = null;
34
	protected $transformers;
35
	protected $propsKeys;
36
	protected $accessors;
37
	protected $fieldList;
38
	protected $memberList;
39
	protected $firstPropKey;
40
	protected $condition;
41
	protected $preparedCondition;
42
	protected $additionalMembers = [ ];
43
	protected $sqlAdditionalMembers = "";
44
	/**
45
	 *
46
	 * @var \Ubiquity\db\Database
47
	 */
48
	protected $db;
49 28
50 28
	public function __construct($className, $condition = null, $included = false) {
51 28
		$this->className = $className;
52 28
		$this->included = $included;
53 28
		$this->condition = $condition;
54 28
		$this->conditionParser = new ConditionParser ( $condition );
55 28
		$this->prepare ();
56 28
		$this->preparedCondition = SqlUtils::checkWhere ( $this->conditionParser->getCondition () );
57
	}
58
59
	public function getFirstPropKey() {
60
		return $this->firstPropKey;
61
	}
62
63
	/**
64
	 *
65
	 * @return \Ubiquity\db\Database
66
	 */
67
	public function getDb() {
68
		return $this->db;
69
	}
70
71
	/**
72
	 *
73
	 * @return mixed
74
	 */
75
	public function getDatabaseOffset() {
76
		return $this->databaseOffset;
77
	}
78
79
	/**
80
	 *
81
	 * @return \Ubiquity\orm\parser\ConditionParser
82
	 */
83
	public function getConditionParser() {
84
		return $this->conditionParser;
85
	}
86
87
	/**
88
	 *
89
	 * @return mixed
90
	 */
91
	public function getIncluded() {
92
		return $this->included;
93
	}
94
95
	/**
96
	 *
97
	 * @return boolean
98
	 */
99
	public function getHasIncluded() {
100
		return $this->hasIncluded;
101
	}
102
103
	/**
104
	 *
105
	 * @return mixed
106
	 */
107
	public function getUseCache() {
108
		return $this->useCache;
109
	}
110
111
	/**
112
	 *
113
	 * @return mixed
114
	 */
115
	public function getClassName() {
116
		return $this->className;
117
	}
118
119
	/**
120
	 *
121
	 * @return mixed
122
	 */
123
	public function getTableName() {
124
		return $this->tableName;
125
	}
126
127
	/**
128
	 *
129
	 * @return mixed
130
	 */
131
	public function getInvertedJoinColumns() {
132
		return $this->invertedJoinColumns;
133
	}
134
135
	/**
136
	 *
137
	 * @return mixed
138
	 */
139
	public function getOneToManyFields() {
140
		return $this->oneToManyFields;
141
	}
142
143
	/**
144
	 *
145
	 * @return mixed
146
	 */
147
	public function getManyToManyFields() {
148
		return $this->manyToManyFields;
149
	}
150
151
	public function getTransformers() {
152
		return $this->transformers;
153
	}
154
155
	public function getPropsKeys() {
156
		return $this->propsKeys;
157
	}
158
159
	/**
160
	 *
161
	 * @return mixed
162
	 */
163
	public function getAccessors() {
164
		return $this->accessors;
165
	}
166
167
	public function getFieldList() {
168
		return $this->fieldList;
169
	}
170 28
171 28
	/**
172 28
	 *
173
	 * @return mixed
174 28
	 */
175 28
	public function getMemberList() {
176 28
		return $this->memberList;
177 28
	}
178
179
	protected function prepare() {
180 28
		$this->db = DAO::getDb ( $this->className );
181 28
		$this->included = DAO::_getIncludedForStep ( $this->included );
182 28
183 28
		$metaDatas = OrmUtils::getModelMetadata ( $this->className );
184 28
		$this->tableName = $metaDatas ['#tableName'];
185 28
		$this->hasIncluded = $this->included || (\is_array ( $this->included ) && \sizeof ( $this->included ) > 0);
186
		if ($this->hasIncluded) {
187 1
			DAO::_initRelationFields ( $this->included, $metaDatas, $this->invertedJoinColumns, $this->oneToManyFields, $this->manyToManyFields );
188 1
		}
189 1
		$this->transformers = $metaDatas ['#transformers'] [DAO::$transformerOp] ?? [ ];
190
		$this->fieldList = DAO::_getFieldList ( $this->tableName, $metaDatas );
191 1
		$this->memberList = \array_flip ( \array_diff ( $metaDatas ['#fieldNames'], $metaDatas ['#notSerializable'] ) );
192
		$this->propsKeys = OrmUtils::getPropKeys ( $this->className );
193 1
		$this->accessors = $metaDatas ['#accessors'];
194 1
		$this->firstPropKey = OrmUtils::getFirstPropKey ( $this->className );
195
	}
196
197 1
	protected function updateSqlAdditionalMembers() {
198 1
		if (\count ( $this->additionalMembers ) > 0) {
199 1
			$this->sqlAdditionalMembers = ',' . $this->parseExpressions ();
200 1
		}
201
	}
202 1
203
	protected function parseExpressions() {
204
		return \implode ( ',', $this->additionalMembers );
205
	}
206
207
	protected function addAditionnalMembers($object, $row) {
208
		foreach ( $this->additionalMembers as $member => $_ ) {
209
			$object->{$member} = $row [$member] ?? null;
210
			$object->_rest [$member] = $row [$member] ?? null;
211
		}
212 1
	}
213 1
214 1
	abstract public function execute($params = [ ], $useCache = false);
215 1
216
	/**
217
	 * Adds a new expression and associates it with a new member of the class added at runtime.
218
	 *
219
	 * @param string $sqlExpression The SQL expression (part of the SQL SELECT)
220
	 * @param string $memberName The new associated member name
221
	 */
222
	public function addMember(string $sqlExpression, string $memberName): void {
223
		$this->additionalMembers [$memberName] = $sqlExpression . " AS '{$memberName}'";
224
		$this->updateSqlAdditionalMembers ();
225
	}
226
227
	/**
228
	 * Adds new expressions and their associated members at runtime.
229
	 *
230
	 * @param array $expressionsNames An associative array of [memberName=>sqlExpression,...]
231
	 */
232
	public function addMembers(array $expressionsNames): void {
233
		foreach ( $expressionsNames as $member => $expression ) {
234
			$this->additionalMembers [$member] = $expression . " AS '{$member}'";
235
		}
236
		$this->updateSqlAdditionalMembers ();
237
	}
238
}
239
240