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