Completed
Push — master ( ab54c8...77228d )
by Jean-Christophe
01:47
created

ManyToManyParser::setInversedBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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.0.0.4
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
25
	public function __construct($instance, $member) {
26
		$this->instance=$instance;
27
		$this->member=$member;
28
	}
29
30
	public function init() {
31
		$member=$this->member;
32
		$class=$this->instance;
33
		if(\is_string($class)===false)
34
			$class=get_class($class);
35
		$annot=OrmUtils::getAnnotationInfoMember($class, "#manyToMany", $member);
36
		if ($annot !== false) {
37
			$this->targetEntity=$annot["targetEntity"];
38
			$this->inversedBy=strtolower($this->targetEntity) . "s";
39
			if (!is_null($annot["inversedBy"]))
40
				$this->inversedBy=$annot["inversedBy"];
41
			$this->targetEntityClass=get_class(new $this->targetEntity());
42
43
			$annotJoinTable=OrmUtils::getAnnotationInfoMember($class, "#joinTable", $member);
44
			$this->joinTable=$annotJoinTable["name"];
45
			
46
			$this->myFkField=OrmUtils::getDefaultFk($class);
47
			$this->myPk=OrmUtils::getFirstKey($class);
48
			if(isset($annotJoinTable["joinColumns"])){
49
				$joinColumnsAnnot=$annotJoinTable["joinColumns"];
50
				if (!is_null($joinColumnsAnnot)) {
51
					$this->myFkField=$joinColumnsAnnot["name"];
52
					$this->myPk=$joinColumnsAnnot["referencedColumnName"];
53
				}
54
			}
55
			$this->targetEntityTable=OrmUtils::getTableName($this->targetEntity);
56
			$this->fkField=OrmUtils::getDefaultFk($this->targetEntityClass);
57
			$this->pk=OrmUtils::getFirstKey($this->targetEntityClass);
58
			if(isset($annotJoinTable["inverseJoinColumns"])){
59
				$inverseJoinColumnsAnnot=$annotJoinTable["inverseJoinColumns"];
60
				if (!is_null($inverseJoinColumnsAnnot)) {
61
					$this->fkField=$inverseJoinColumnsAnnot["name"];
62
					$this->pk=$inverseJoinColumnsAnnot["referencedColumnName"];
63
				}
64
			}
65
			return true;
66
		}
67
		return false;
68
	}
69
70
	public function getMember() {
71
		return $this->member;
72
	}
73
74
	public function setMember($member) {
75
		$this->member=$member;
76
		return $this;
77
	}
78
79
	public function getJoinTable() {
80
		return $this->joinTable;
81
	}
82
83
	public function setJoinTable($joinTable) {
84
		$this->joinTable=$joinTable;
85
		return $this;
86
	}
87
88
	public function getMyFkField() {
89
		return $this->myFkField;
90
	}
91
92
	public function setMyFkField($myFkField) {
93
		$this->myFkField=$myFkField;
94
		return $this;
95
	}
96
97
	public function getFkField() {
98
		return $this->fkField;
99
	}
100
101
	public function setFkField($fkField) {
102
		$this->fkField=$fkField;
103
		return $this;
104
	}
105
106
	public function getTargetEntity() {
107
		return $this->targetEntity;
108
	}
109
110
	public function setTargetEntity($targetEntity) {
111
		$this->targetEntity=$targetEntity;
112
		return $this;
113
	}
114
115
	public function getTargetEntityClass() {
116
		return $this->targetEntityClass;
117
	}
118
119
	public function setTargetEntityClass($targetEntityClass) {
120
		$this->targetEntityClass=$targetEntityClass;
121
		return $this;
122
	}
123
124
	public function getTargetEntityTable() {
125
		return $this->targetEntityTable;
126
	}
127
128
	public function setTargetEntityTable($targetEntityTable) {
129
		$this->targetEntityTable=$targetEntityTable;
130
		return $this;
131
	}
132
133
	public function getMyPk() {
134
		return $this->myPk;
135
	}
136
137
	public function setMyPk($myPk) {
138
		$this->myPk=$myPk;
139
		return $this;
140
	}
141
142
	public function getPk() {
143
		return $this->pk;
144
	}
145
146
	public function setPk($pk) {
147
		$this->pk=$pk;
148
		return $this;
149
	}
150
151
	public function getInversedBy() {
152
		return $this->inversedBy;
153
	}
154
155
	public function setInversedBy($inversedBy) {
156
		$this->inversedBy=$inversedBy;
157
		return $this;
158
	}
159
160
	public function getInstance() {
161
		return $this->instance;
162
	}
163
164
	public function setInstance($instance) {
165
		$this->instance=$instance;
166
		return $this;
167
	}
168
}
169