initial commit
[namibia] / module / Company / src / Company / Entity / CompanyRetail.php
1 <?php
2 namespace Company\Entity;
3
4 use Doctrine\ORM\Mapping as ORM;
5
6
7
8 /**
9  * @ORM\Entity
10  * @ORM\HasLifecycleCallbacks
11  * @ORM\Table(name="company_retail")
12  */
13 class CompanyRetail
14 {
15
16         /**
17          * Can archive records.
18          */
19         const ARCHIVE = true;
20         /**
21          * Pull Synchronization Strategy for this table.
22          */
23         const PULL_SYNCH_STRATEGY = false;
24         /**
25          * Push Synchronization Strategy for this table.
26          */
27         const PUSH_SYNCH_STRATEGY = false;
28
29
30         /**
31          * @ORM\Id
32          * @ORM\Column(type="integer");
33          * @ORM\GeneratedValue(strategy="AUTO")
34          */
35         protected $id;
36
37         /**
38          * @ORM\ManyToOne(targetEntity="Company", inversedBy="retailers")
39          * @ORM\JoinColumn(name="company_id", referencedColumnName="id")
40          **/
41         protected $company;
42
43         /**
44          * @ORM\ManyToOne(targetEntity="\Retail\Entity\Retail")
45          * @ORM\JoinColumn(name="retail_id", referencedColumnName="id")
46          **/
47         protected $retail;
48
49         /**
50          * @ORM\Column(type="string", length=150)
51          */
52         protected $name;
53
54         /**
55          * @ORM\Column(type="string", length=150)
56          */
57         protected $username;
58
59         /**
60          * @ORM\Column(type="string", length=64)
61          */
62         protected $password;
63
64
65         /**
66          * @ORM\Column(type="datetime");
67          */
68         protected $created;
69
70         /**
71          * @ORM\Column(type="datetime", nullable=true);
72          */
73         protected $updated;
74
75         /**
76          * @ORM\Column(type="boolean");
77          */
78         protected $archived = 0;
79
80
81
82         /**
83          * Magic getter to expose protected properties.
84          * @param string $property
85          * @return mixed
86          */
87         public function __get($property)
88         {
89                 return $this->$property;
90         }
91
92         /**
93          * Magic setter to save protected properties.
94          * @param string $property
95          * @param mixed $value
96          */
97         public function __set($property, $value)
98         {
99                 $this->$property = $value;
100         }
101
102
103         /**
104          * @ORM\PrePersist
105          */
106         public function setCreateTime()
107         {
108                 $this->created = new \DateTime("now");
109                 $this->company = \Utility\Registry::resolveCompanyContext($this->company);
110                 if (empty($this->name) || is_null($this->name))
111                 {
112                         $this->name = $this->retail->name;
113                 }
114         }
115
116         /**
117          * @ORM\PreUpdate
118          */
119         public function setUpdateTime()
120         {
121                 $this->updated = new \DateTime("now");
122         }
123
124         public function fromArray($data)
125         {
126                 isset($data['company'])
127                         && $this->company = $data['company'];
128                 isset($data['retail'])
129                         && $this->retail = $data['retail'];
130                 isset($data['name'])
131                         && $this->name = $data['name'];
132                 isset($data['username'])
133                         && $this->username = $data['username'];
134                 isset($data['password'])
135                         && $this->password = $data['password'];
136                 isset($data['archived'])
137                         && $this->archived = $data['archived'];
138         }
139
140         public function toArray(
141                         array $expand = array(), array $intersect = array(),
142                         $showIdentifiers = false, $expandAll = 0
143                         )
144         {
145                 $intersect = array_flip($intersect);
146                 $includeAll = empty($intersect);
147                 $dateTimeFormat = \Utility\Definitions\Locale::getDateTimeFormat();
148                 $data = array();
149                 $data['id']                     = $this->id;
150                 ($includeAll || isset($intersect['company']))
151                         && $data['company'] = (in_array('company', $expand) || $expandAll || $showIdentifiers)
152                                                                         && !is_null($this->company)
153                                 ? (!$showIdentifiers || in_array('company', $expand) ? $this->company->toArray(
154                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
155                                                 ) : $this->company->id)
156                                 : null;
157                 ($includeAll || isset($intersect['retail']))
158                         && $data['retail'] = (in_array('retail', $expand) || $expandAll || $showIdentifiers)
159                                                                         && !is_null($this->retail)
160                                 ? (!$showIdentifiers || in_array('retail', $expand) ? $this->retail->toArray(
161                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
162                                                 ) : $this->retail->id)
163                                 : null;
164                 $data['name']       = $this->name;
165                 $data['username']       = $this->username;
166                 $data['password']       = $this->password;
167                 $data['created']        = $this->created->format($dateTimeFormat);
168                 $data['updated']        = !is_null($this->updated)
169                         ? $this->updated->format($dateTimeFormat)
170                         : null;
171                 $data['archived']       = $this->archived;
172
173                 return $data;
174         }
175 }