Completed
Push — master ( 37d0d3...61d24d )
by Jean-Christophe
01:28
created

Member::addOneToMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace micro\orm\creator;
4
5
use micro\annotations\IdAnnotation;
6
use micro\annotations\ManyToOneAnnotation;
7
use micro\annotations\OneToManyAnnotation;
8
use micro\annotations\ManyToManyAnnotation;
9
use micro\annotations\JoinTableAnnotation;
10
use micro\annotations\JoinColumnAnnotation;
11
12
class Member {
13
	private $name;
14
	private $primary;
15
	private $manyToOne;
16
	private $annotations;
17
18
	public function __construct($name) {
19
		$this->name=$name;
20
		$this->annotations=array ();
21
		$this->primary=false;
22
		$this->manyToOne=false;
23
	}
24
25
	public function __toString() {
26
		$annotationsStr="";
27
		if (sizeof($this->annotations) > 0) {
28
			$annotationsStr="\n\t/**";
29
			$annotations=$this->annotations;
30
			\array_walk($annotations, function ($item) {
31
				return $item . "";
32
			});
33
			if (\sizeof($annotations) > 1) {
34
				$annotationsStr.="\n\t * " . implode("\n\t * ", $annotations);
35
			} else {
36
				$annotationsStr.="\n\t * " . $annotations[0];
37
			}
38
			$annotationsStr.="\n\t*/";
39
		}
40
		return $annotationsStr . "\n\tprivate $" . $this->name . ";\n";
41
	}
42
43
	public function setPrimary() {
44
		if ($this->primary === false) {
45
			$this->annotations[]=new IdAnnotation();
46
			$this->primary=true;
47
		}
48
	}
49
50
	public function addManyToOne($name, $className, $nullable=false) {
51
		$this->annotations[]=new ManyToOneAnnotation();
52
		$joinColumn=new JoinColumnAnnotation();
53
		$joinColumn->name=$name;
54
		$joinColumn->className=$className;
55
		$joinColumn->nullable=$nullable;
56
		$this->annotations[]=$joinColumn;
57
		$this->manyToOne=true;
58
	}
59
60
	public function addOneToMany($mappedBy, $className) {
61
		$oneToMany=new OneToManyAnnotation();
62
		$oneToMany->mappedBy=$mappedBy;
63
		$oneToMany->className=$className;
64
		$this->annotations[]=$oneToMany;
65
	}
66
67
	public function addManyToMany($targetEntity, $inversedBy, $joinTable, $joinColumns=[], $inverseJoinColumns=[]) {
68
		$manyToMany=new ManyToManyAnnotation();
69
		$manyToMany->targetEntity=$targetEntity;
70
		$manyToMany->inversedBy=$inversedBy;
71
		$jt=new JoinTableAnnotation();
72
		$jt->name=$joinTable;
73
		if (\sizeof($joinColumns) == 2) {
74
			$jt->joinColumns=$joinColumns;
75
		}
76
		if (\sizeof($inverseJoinColumns) == 2) {
77
			$jt->inverseJoinColumns=$inverseJoinColumns;
78
		}
79
		$this->annotations[]=$manyToMany;
80
		$this->annotations[]=$jt;
81
	}
82
83
	public function getName() {
84
		return $this->name;
85
	}
86
87
	public function isManyToOne() {
88
		return $this->manyToOne;
89
	}
90
91
	public function getManyToOne() {
92
		foreach ( $this->annotations as $annotation ) {
93
			if ($annotation instanceof JoinColumnAnnotation) {
1 ignored issue
show
Bug introduced by
The class micro\annotations\JoinColumnAnnotation does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
94
				return $annotation;
95
			}
96
		}
97
		return null;
98
	}
99
100
	public function isPrimary() {
101
		return $this->primary;
102
	}
103
104
	public function getGetter() {
105
		$result="\n\t public function get" . \ucfirst($this->name) . "(){\n";
106
		$result.="\t\t" . 'return $this->' . $this->name . ";\n";
107
		$result.="\t}\n";
108
		return $result;
109
	}
110
111
	public function getSetter() {
112
		$result="\n\t public function set" . \ucfirst($this->name) . '($' . $this->name . "){\n";
113
		$result.="\t\t" . '$this->' . $this->name . '=$' . $this->name . ";\n";
114
		$result.="\t}\n";
115
		return $result;
116
	}
117
118
	public function hasAnnotations(){
119
		return \count($this->annotations)>0;
120
	}
121
}
122