Completed
Push — master ( cbe892...9bd3b4 )
by Jean-Christophe
02:05
created

ManyToManyParser::generateConditionParser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0

1 Method

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