|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace miBadger\ActiveRecord\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use miBadger\ActiveRecord\ColumnProperty; |
|
6
|
|
|
|
|
7
|
|
|
const TRAIT_ADDRESS_FIELD_ADDRESS = "address_address"; |
|
8
|
|
|
const TRAIT_ADDRESS_FIELD_ZIPCODE = "address_zipcode"; |
|
9
|
|
|
const TRAIT_ADDRESS_FIELD_CITY = "address_city"; |
|
10
|
|
|
const TRAIT_ADDRESS_FIELD_COUNTRY = "address_country"; |
|
11
|
|
|
|
|
12
|
|
|
trait Address |
|
13
|
|
|
{ |
|
14
|
|
|
protected $address; |
|
15
|
|
|
|
|
16
|
|
|
protected $zipcode; |
|
17
|
|
|
|
|
18
|
|
|
protected $city; |
|
19
|
|
|
|
|
20
|
|
|
protected $country; |
|
21
|
|
|
|
|
22
|
|
|
protected function initAddress() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ADDRESS, [ |
|
|
|
|
|
|
25
|
|
|
'value' => &$this->address, |
|
26
|
|
|
'validate' => null, |
|
27
|
|
|
'type' => 'VARCHAR', |
|
28
|
|
|
'length' => 1024, |
|
29
|
|
|
'properties' => null |
|
30
|
|
|
]); |
|
31
|
|
|
|
|
32
|
|
|
$this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ZIPCODE, [ |
|
|
|
|
|
|
33
|
|
|
'value' => &$this->zipcode, |
|
34
|
|
|
'validate' => null, |
|
35
|
|
|
'type' => 'VARCHAR', |
|
36
|
|
|
'length' => 1024, |
|
37
|
|
|
'properties' => null |
|
38
|
|
|
]); |
|
39
|
|
|
|
|
40
|
|
|
$this->extendTableDefinition(TRAIT_ADDRESS_FIELD_CITY, [ |
|
|
|
|
|
|
41
|
|
|
'value' => &$this->city, |
|
42
|
|
|
'validate' => null, |
|
43
|
|
|
'type' => 'VARCHAR', |
|
44
|
|
|
'length' => 1024, |
|
45
|
|
|
'properties' => null |
|
46
|
|
|
]); |
|
47
|
|
|
|
|
48
|
|
|
$this->extendTableDefinition(TRAIT_ADDRESS_FIELD_COUNTRY, [ |
|
|
|
|
|
|
49
|
|
|
'value' => &$this->country, |
|
50
|
|
|
'validate' => null, |
|
51
|
|
|
'type' => 'VARCHAR', |
|
52
|
|
|
'length' => 1024, |
|
53
|
|
|
'properties' => null |
|
54
|
|
|
]); |
|
55
|
|
|
|
|
56
|
|
|
$this->address = null; |
|
57
|
|
|
$this->zipcode = null; |
|
58
|
|
|
$this->city = null; |
|
59
|
|
|
$this->country = null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getAddress() |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
return $this->address; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function setAddress($address) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->address = $address; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getZipcode() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
return $this->zipcode; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function setZipcode($zipcode) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->zipcode = $zipcode; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getCity() |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
return $this->city; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function setCity($city) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->city = $city; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getCountry() |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
return $this->country; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function setCountry($country) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->country = $country; |
|
100
|
|
|
} |
|
101
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.