Ubiquity  2.0.2
php rapid development framework
Member.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\orm\creator;
4 
12 
13 class Member {
14  private $name;
15  private $primary;
16  private $manyToOne;
17  private $annotations;
18 
19  public function __construct($name) {
20  $this->name=$name;
21  $this->annotations=array ();
22  $this->primary=false;
23  $this->manyToOne=false;
24  }
25 
26  public function __toString() {
27  $annotationsStr="";
28  if (sizeof($this->annotations) > 0) {
29  $annotationsStr="\n\t/**";
31  \array_walk($annotations, function ($item) {
32  return $item . "";
33  });
34  if (\sizeof($annotations) > 1) {
35  $annotationsStr.="\n\t * " . implode("\n\t * ", $annotations);
36  } else {
37  $annotationsStr.="\n\t * " . \end($annotations);
38  }
39  $annotationsStr.="\n\t*/";
40  }
41  return $annotationsStr . "\n\tprivate $" . $this->name . ";\n";
42  }
43 
44  public function setPrimary() {
45  if ($this->primary === false) {
46  $this->annotations[]=new IdAnnotation();
47  $this->primary=true;
48  }
49  }
50 
51  public function setDbType($infos){
52  $annot=new ColumnAnnotation();
53  $annot->name=$this->name;
54  $annot->dbType=$infos["Type"];
55  $annot->nullable=(\strtolower($infos["Nullable"])==="yes");
56  $this->annotations["column"]=$annot;
57  }
58 
59  public function addManyToOne($name, $className, $nullable=false) {
60  $this->annotations[]=new ManyToOneAnnotation();
61  $joinColumn=new JoinColumnAnnotation();
62  $joinColumn->name=$name;
63  $joinColumn->className=$className;
64  $joinColumn->nullable=$nullable;
65  $this->annotations[]=$joinColumn;
66  $this->manyToOne=true;
67  }
68 
69  public function addOneToMany($mappedBy, $className) {
70  $oneToMany=new OneToManyAnnotation();
71  $oneToMany->mappedBy=$mappedBy;
72  $oneToMany->className=$className;
73  $this->annotations[]=$oneToMany;
74  }
75 
76  public function addManyToMany($targetEntity, $inversedBy, $joinTable, $joinColumns=[], $inverseJoinColumns=[]) {
77  $manyToMany=new ManyToManyAnnotation();
78  $manyToMany->targetEntity=$targetEntity;
79  $manyToMany->inversedBy=$inversedBy;
80  $jt=new JoinTableAnnotation();
81  $jt->name=$joinTable;
82  if (\sizeof($joinColumns) == 2) {
83  $jt->joinColumns=$joinColumns;
84  }
85  if (\sizeof($inverseJoinColumns) == 2) {
86  $jt->inverseJoinColumns=$inverseJoinColumns;
87  }
88  $this->annotations[]=$manyToMany;
89  $this->annotations[]=$jt;
90  }
91 
92  public function getName() {
93  return $this->name;
94  }
95 
96  public function isManyToOne() {
97  return $this->manyToOne;
98  }
99 
100  public function getManyToOne() {
101  foreach ( $this->annotations as $annotation ) {
102  if ($annotation instanceof JoinColumnAnnotation) {
103  return $annotation;
104  }
105  }
106  return null;
107  }
108 
109  public function isPrimary() {
110  return $this->primary;
111  }
112 
113  public function getGetter() {
114  $result="\n\t public function get" . \ucfirst($this->name) . "(){\n";
115  $result.="\t\t" . 'return $this->' . $this->name . ";\n";
116  $result.="\t}\n";
117  return $result;
118  }
119 
120  public function getSetter() {
121  $result="\n\t public function set" . \ucfirst($this->name) . '($' . $this->name . "){\n";
122  $result.="\t\t" . '$this->' . $this->name . '=$' . $this->name . ";\n";
123  $result.="\t}\n";
124  return $result;
125  }
126 
127  public function hasAnnotations(){
128  return \count($this->annotations)>1;
129  }
130 
131  public function isNullable(){
132  if(isset($this->annotations["column"]))
133  return $this->annotations["column"]->nullable;
134  return false;
135  }
136 
137  public function getDbType(){
138  if(isset($this->annotations["column"]))
139  return $this->annotations["column"]->dbType;
140  return "mixed";
141  }
142 }
addManyToMany($targetEntity, $inversedBy, $joinTable, $joinColumns=[], $inverseJoinColumns=[])
Definition: Member.php:76
addOneToMany($mappedBy, $className)
Definition: Member.php:69
Annotation Column.
addManyToOne($name, $className, $nullable=false)
Definition: Member.php:59