Passed
Push — master ( 4162a6...5bc725 )
by Jean-Christophe
05:05
created

ManyToManyParser::setPk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\orm\parser;
4
5
use Ubiquity\orm\OrmUtils;
6
7
/**
8
 * ManyToManyParser
9
 *
10
 * @author jc
11
 * @version 1.1.0.0
12
 */
13
class ManyToManyParser {
14
	private $table;
15
	private $member;
16
	private $joinTable;
17
	private $myFkField;
18
	private $fkField;
19
	private $targetEntity;
20
	private $targetEntityClass;
21
	private $targetEntityTable;
22
	private $myPk;
23
	private $inversedBy;
24
	private $pk;
25
	private $instance;
26
	private $whereValues;
27
28 7
	public function __construct($instance, $member = null) {
29 7
		$this->instance = $instance;
30 7
		$this->member = $member;
31 7
		$this->whereValues = [ ];
32 7
	}
33
34 7
	public function init($annot = false) {
35 7
		$member = $this->member;
36 7
		$class = $this->instance;
37 7
		if (\is_object ( $class )) {
38 7
			$class = get_class ( $class );
39
		}
40 7
		if ($annot === false) {
41 1
			$annot = OrmUtils::getAnnotationInfoMember ( $class, "#manyToMany", $member );
42
		}
43 7
		if ($annot !== false) {
44 7
			$this->_init ( $class, $annot );
45 7
			return true;
46
		}
47
		return false;
48
	}
49
50 7
	private function _init($class, $annot) {
51 7
		$this->table = OrmUtils::getTableName ( $class );
52 7
		$this->targetEntity = $annot ["targetEntity"];
53 7
		$this->inversedBy = lcfirst ( $this->targetEntity ) . "s";
54 7
		if (! is_null ( $annot ["inversedBy"] ))
55 7
			$this->inversedBy = $annot ["inversedBy"];
56 7
		$this->targetEntityClass = get_class ( new $this->targetEntity () );
57
58 7
		$annotJoinTable = OrmUtils::getAnnotationInfoMember ( $class, "#joinTable", $this->member );
59 7
		$this->joinTable = $annotJoinTable ["name"];
60
61 7
		$this->myFkField = OrmUtils::getDefaultFk ( $class );
62 7
		$this->myPk = OrmUtils::getFirstKey ( $class );
63 7
		if (isset ( $annotJoinTable ["joinColumns"] )) {
64
			$joinColumnsAnnot = $annotJoinTable ["joinColumns"];
65
			if (! is_null ( $joinColumnsAnnot )) {
66
				$this->myFkField = $joinColumnsAnnot ["name"];
67
				$this->myPk = $joinColumnsAnnot ["referencedColumnName"];
68
			}
69
		}
70 7
		$this->targetEntityTable = OrmUtils::getTableName ( $this->targetEntity );
71 7
		$this->fkField = OrmUtils::getDefaultFk ( $this->targetEntityClass );
72 7
		$this->pk = OrmUtils::getFirstKey ( $this->targetEntityClass );
73 7
		if (isset ( $annotJoinTable ["inverseJoinColumns"] )) {
74
			$inverseJoinColumnsAnnot = $annotJoinTable ["inverseJoinColumns"];
75
			if (! is_null ( $inverseJoinColumnsAnnot )) {
76
				$this->fkField = $inverseJoinColumnsAnnot ["name"];
77
				$this->pk = $inverseJoinColumnsAnnot ["referencedColumnName"];
78
			}
79
		}
80 7
	}
81
82
	public function getMember() {
83
		return $this->member;
84
	}
85
86
	public function setMember($member) {
87
		$this->member = $member;
88
		return $this;
89
	}
90
91 1
	public function getJoinTable() {
92 1
		return $this->joinTable;
93
	}
94
95
	public function setJoinTable($joinTable) {
96
		$this->joinTable = $joinTable;
97
		return $this;
98
	}
99
100 1
	public function getMyFkField() {
101 1
		return $this->myFkField;
102
	}
103
104
	public function setMyFkField($myFkField) {
105
		$this->myFkField = $myFkField;
106
		return $this;
107
	}
108
109 1
	public function getFkField() {
110 1
		return $this->fkField;
111
	}
112
113
	public function setFkField($fkField) {
114
		$this->fkField = $fkField;
115
		return $this;
116
	}
117
118
	public function getTargetEntity() {
119
		return $this->targetEntity;
120
	}
121
122
	public function setTargetEntity($targetEntity) {
123
		$this->targetEntity = $targetEntity;
124
		return $this;
125
	}
126
127 2
	public function getTargetEntityClass() {
128 2
		return $this->targetEntityClass;
129
	}
130
131
	public function setTargetEntityClass($targetEntityClass) {
132
		$this->targetEntityClass = $targetEntityClass;
133
		return $this;
134
	}
135
136 7
	public function getTargetEntityTable() {
137 7
		return $this->targetEntityTable;
138
	}
139
140
	public function setTargetEntityTable($targetEntityTable) {
141
		$this->targetEntityTable = $targetEntityTable;
142
		return $this;
143
	}
144
145 7
	public function getMyPk() {
146 7
		return $this->myPk;
147
	}
148
149
	public function setMyPk($myPk) {
150
		$this->myPk = $myPk;
151
		return $this;
152
	}
153
154 7
	public function getPk() {
155 7
		return $this->pk;
156
	}
157
158
	public function setPk($pk) {
159
		$this->pk = $pk;
160
		return $this;
161
	}
162
163
	public function getInversedBy() {
164
		return $this->inversedBy;
165
	}
166
167
	public function setInversedBy($inversedBy) {
168
		$this->inversedBy = $inversedBy;
169
		return $this;
170
	}
171
172
	public function getInstance() {
173
		return $this->instance;
174
	}
175
176
	public function setInstance($instance) {
177
		$this->instance = $instance;
178
		return $this;
179
	}
180
181 1
	public function getSQL($alias = "", $aliases = null) {
182 1
		if ($alias !== "") {
183 1
			$targetEntityTable = $alias;
184 1
			$alias = "`" . $alias . "`";
185
		} else {
186
			$targetEntityTable = $this->targetEntityTable;
187
		}
188 1
		$jtAlias = uniqid ( $this->joinTable );
189 1
		$table = $this->table;
190 1
		if (is_array ( $aliases )) {
191 1
			if (isset ( $aliases [$this->table] ))
192
				$table = $aliases [$this->table];
193
		}
194 1
		return " INNER JOIN `" . $this->joinTable . "` `{$jtAlias}` on `" . $jtAlias . "`.`" . $this->myFkField . "`=`" . $table . "`.`" . $this->myPk . "`" . " INNER JOIN `" . $this->targetEntityTable . "` {$alias} on `" . $jtAlias . "`.`" . $this->fkField . "`=`" . $targetEntityTable . "`.`" . $this->pk . "`";
195
	}
196
197 6
	public function getConcatSQL() {
198 6
		return "SELECT `" . $this->myFkField . "` as '_field' ,GROUP_CONCAT(`" . $this->fkField . "` SEPARATOR ',') as '_concat' FROM `" . $this->joinTable . "` {condition} GROUP BY 1";
199
	}
200
201 6
	public function getParserWhereMask($mask = "'{value}'") {
202 6
		return "`" . $this->getTargetEntityTable () . "`.`" . $this->getPk () . "`=" . $mask;
203
	}
204
205 3
	private function getParserConcatWhereMask($mask = "'{value}'") {
206 3
		return "`" . $this->myFkField . "`=" . $mask;
207
	}
208
209 5
	private function getParserConcatWhereInMask($mask = "'{values}'") {
210 5
		return " INNER JOIN (" . $mask . ") as _tmp ON `" . $this->myFkField . "`=_tmp._id";
211
	}
212
213 6
	public function generateConcatSQL() {
214 6
		$sql = $this->getConcatSQL ();
215 6
		$where = "";
216 6
		if (($size = sizeof ( $this->whereValues )) > 0) {
217 6
			if ($size > 3) {
218 5
				$res = array_fill ( 0, $size, "?" );
219 5
				$res [0] = "SELECT ? as _id";
220 5
				$where = $this->getParserConcatWhereInMask ( implode ( " UNION ALL SELECT ", $res ) );
221
			} else {
222 3
				$mask = $this->getParserConcatWhereMask ( " ?" );
223 3
				$res = array_fill ( 0, $size, $mask );
224 3
				$where = "WHERE " . implode ( " OR ", $res );
225
			}
226
		}
227 6
		return str_replace ( "{condition}", $where, $sql );
228
	}
229
230 6
	public function addValue($value) {
231 6
		$this->whereValues [$value] = true;
232 6
	}
233
234
	/**
235
	 *
236
	 * @return array
237
	 */
238 6
	public function getWhereValues() {
239 6
		return array_keys ( $this->whereValues );
240
	}
241
}
242