initial commit
[namibia] / module / Location / src / Location / Entity / Town.php
1 <?php
2 namespace Location\Entity;
3
4 use Doctrine\ORM\Mapping as ORM;
5
6
7
8 /**
9  * A town in a region.
10  * @ORM\Entity
11  * @ORM\HasLifecycleCallbacks
12  * @ORM\Table(name="lib_city")
13  */
14 class Town
15 {
16
17         /**
18          * Can archive records.
19          */
20         const ARCHIVE = true;
21         /**
22          * Pull Synchronization Strategy for this table.
23          */
24         const PULL_SYNCH_STRATEGY = 'Update';
25         /**
26          * Push Synchronization Strategy for this table.
27          */
28         const PUSH_SYNCH_STRATEGY = 'Update';
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="Region", inversedBy="towns")
39          * @ORM\JoinColumn(name="lib_region_id", referencedColumnName="id")
40          **/
41         protected $region;
42
43         /**
44          * @ORM\Column(type="string", length=150)
45          */
46         protected $name;
47
48         /**
49          * @ORM\Column(type="datetime");
50          */
51         protected $created;
52
53         /**
54          * @ORM\Column(type="datetime", nullable=true);
55          */
56         protected $updated;
57
58         /**
59          * @ORM\Column(type="boolean");
60          */
61         protected $archived = false;
62
63
64         /**
65          * Magic getter to expose protected properties.
66          *
67          * @param string $property
68          * @return mixed
69          */
70         public function __get($property)
71         {
72                 return $this->$property;
73         }
74
75         /**
76          * Magic setter to save protected properties.
77          *
78          * @param string $property
79          * @param mixed $value
80          */
81         public function __set($property, $value)
82         {
83                 $this->$property = $value;
84         }
85
86         /**
87          * @ORM\PrePersist
88          */
89         public function setCreateTime()
90         {
91                 $this->created = new \DateTime("now");
92         }
93
94         /**
95          * @ORM\PreUpdate
96          */
97         public function setUpdateTime()
98         {
99                 $this->updated = new \DateTime("now");
100         }
101
102         /**
103          * Convert the object to an array.
104          * @param array $expand
105          * @param array $intersect
106          * @return array
107          */
108         public function toArray(array $expand = array(), array $intersect = array())
109         {
110                 $includeAll = empty($intersect);
111                 $data = array();
112                 ($includeAll || isset($intersect['id']))
113                         && $data['id'] = $this->id;
114                 ($includeAll || isset($intersect['region']))
115                         && in_array('region', $expand)
116                         && $data['region'] = $this->region->toArray($expand, $intersect);
117                 ($includeAll || isset($intersect['name']))
118                         && $data['name'] = $this->name;
119                 ($includeAll || isset($intersect['created']))
120                         && $data['created'] = !is_null($this->created)
121                                 ? $this->created->format(\Utility\Registry::getConfigParam('DateTimeFormat'))
122                                 : null;
123                 ($includeAll || isset($intersect['updated']))
124                         && $data['updated'] = !is_null($this->updated)
125                                 ? $this->updated->format(\Utility\Registry::getConfigParam('DateTimeFormat'))
126                                 : null;
127                 return $data;
128         }
129
130         /**
131          * Convert the object to an array for synchronization.
132          * @return array
133          */
134         public function toSynchArray()
135         {
136                 return array(
137                                 'id'     => $this->id,
138                                 'region' => $this->region->id,
139                                 'name'   => $this->name
140                 );
141         }
142
143         /**
144          * Populate from an array.
145          * @param array $data
146          */
147         public function fromArray($data = array())
148         {
149                 isset($data['id'])
150                         && $this->id = $data['id'];
151                 isset($data['region'])
152                         && $this->region  = $data['region'];
153                 isset($data['name'])
154                         && $this->name  = $data['name'];
155         }
156
157 }