1 | <?php |
||
47 | abstract class AbstractRelation implements RelationInterface |
||
48 | { |
||
49 | /** |
||
50 | * Relation type |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | const TYPE = 'abstract'; |
||
55 | /** |
||
56 | * Relation URL |
||
57 | * |
||
58 | * @var Url |
||
59 | */ |
||
60 | protected $url = null; |
||
61 | /** |
||
62 | * Relation label |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $label = null; |
||
67 | /** |
||
68 | * Relation email |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $email = null; |
||
73 | /** |
||
74 | * Coupling |
||
75 | * |
||
76 | * @var int |
||
77 | */ |
||
78 | protected $coupling = self::LOOSE_COUPLING; |
||
79 | |||
80 | /** |
||
81 | * @param Url $url Relation URL |
||
82 | * @param string $label Relation label |
||
83 | * @param string $email Relation email |
||
84 | * @param int $coupling Coupling |
||
85 | * @throws OutOfBoundsException If the object coupling is invalid |
||
86 | */ |
||
87 | 39 | public function __construct( |
|
88 | Url $url = null, |
||
89 | $label = null, |
||
90 | $email = null, |
||
91 | $coupling = null |
||
92 | ) { |
||
93 | // If the coupling type is invalid |
||
94 | 39 | if (($coupling !== self::LOOSE_COUPLING) && ($coupling !== self::TIGHT_COUPLING)) { |
|
95 | 1 | throw new OutOfBoundsException( |
|
96 | 1 | sprintf('Invalid object coupling "%s"', $coupling), |
|
97 | OutOfBoundsException::INVALID_OBJECT_COUPLING |
||
98 | 1 | ); |
|
99 | } |
||
100 | |||
101 | 38 | $this->url = $url; |
|
102 | 38 | $this->label = $label; |
|
103 | 38 | $this->email = $email; |
|
104 | 38 | $this->coupling = $coupling; |
|
105 | 38 | } |
|
106 | |||
107 | /** |
||
108 | * Return the URL |
||
109 | * |
||
110 | * @return Url URL |
||
111 | */ |
||
112 | 2 | public function getUrl() |
|
113 | { |
||
114 | 2 | return $this->url; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Set the URL |
||
119 | * |
||
120 | * @param Url|null $url URL |
||
121 | * @return RelationInterface Self reference |
||
122 | */ |
||
123 | 1 | public function setUrl(Url $url = null) |
|
124 | { |
||
125 | 1 | $relation = clone $this; |
|
126 | 1 | $relation->url = $url; |
|
127 | 1 | return $relation; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * Return the label |
||
132 | * |
||
133 | * @return string|null Label |
||
134 | */ |
||
135 | 1 | public function getLabel() |
|
136 | { |
||
137 | 1 | return $this->label; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * Set the label |
||
142 | * |
||
143 | * @param string|null $label Label |
||
144 | * @return RelationInterface Self reference |
||
145 | */ |
||
146 | 1 | public function setLabel($label) |
|
147 | { |
||
148 | 1 | $relation = clone $this; |
|
149 | 1 | $relation->label = $label; |
|
150 | 1 | return $relation; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Return the email address |
||
155 | * |
||
156 | * @return string|null Email address |
||
157 | */ |
||
158 | 2 | public function getEmail() |
|
159 | { |
||
160 | 2 | return $this->email; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * Set the email address |
||
165 | * |
||
166 | * @param string|null $email Email address |
||
167 | * @return RelationInterface Self reference |
||
168 | */ |
||
169 | 1 | public function setEmail($email) |
|
170 | { |
||
171 | 1 | $relation = clone $this; |
|
172 | 1 | $relation->email = $email; |
|
173 | 1 | return $relation; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Return the coupling |
||
178 | * |
||
179 | * @return int Coupling |
||
180 | */ |
||
181 | 1 | public function getCoupling() |
|
182 | { |
||
183 | 1 | return $this->coupling; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * Set the coupling |
||
188 | * |
||
189 | * @param int $coupling Coupling |
||
190 | * @return RelationInterface Self reference |
||
191 | * @throws OutOfBoundsException If the object coupling is invalid |
||
192 | */ |
||
193 | 1 | public function setCoupling($coupling) |
|
194 | { |
||
195 | // If the coupling type is invalid |
||
196 | 1 | if (($coupling !== self::LOOSE_COUPLING) && ($coupling !== self::TIGHT_COUPLING)) { |
|
197 | 1 | throw new OutOfBoundsException( |
|
198 | 1 | sprintf('Invalid object coupling "%s"', $coupling), |
|
199 | OutOfBoundsException::INVALID_OBJECT_COUPLING |
||
200 | 1 | ); |
|
201 | } |
||
202 | |||
203 | 1 | $relation = clone $this; |
|
204 | 1 | $relation->coupling = $coupling; |
|
205 | 1 | return $relation; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Return the unique relation signature |
||
210 | * |
||
211 | * @return string Relation signature |
||
212 | */ |
||
213 | 37 | public function getSignature() |
|
214 | { |
||
215 | 37 | return md5( |
|
216 | 37 | $this->getType(). |
|
217 | (empty($this->url) |
||
218 | 37 | ? (empty($this->email) ? (empty($this->label) ? '-' : $this->label) : $this->email) |
|
219 | 37 | : $this->url) |
|
220 | 37 | ); |
|
221 | } |
||
222 | |||
223 | /** |
||
224 | * Return the relation type |
||
225 | * |
||
226 | * @return string Relation type |
||
227 | */ |
||
228 | 37 | public function getType() |
|
229 | { |
||
230 | 37 | return static::TYPE; |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * Serialize the property |
||
235 | * |
||
236 | * @return mixed Property serialization |
||
237 | */ |
||
238 | 4 | public function __toString() |
|
243 | } |
||
244 |