1 | <?php |
||
60 | abstract class AbstractObjectMutator implements ObjectMutatorInterface |
||
61 | { |
||
62 | /** |
||
63 | * Fake data generator |
||
64 | * |
||
65 | * @var Generator |
||
66 | */ |
||
67 | protected $generator; |
||
68 | /** |
||
69 | * Twitter status URL |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | const TWITTER_STATUS = 'https://twitter.com/%s/status/%s'; |
||
74 | /** |
||
75 | * Facebook post URL |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | const FACEBOOK_POST = 'https://www.facebook.com/%s/posts/%s'; |
||
80 | /** |
||
81 | * Google+ post URL |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | const GOOGLEPLUS_POST = 'https://plus.google.com/+%s/posts/%s'; |
||
86 | /** |
||
87 | * Instagram post URL |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | const INSTAGRAM_POST = 'https://www.instagram.com/p/%s/'; |
||
92 | |||
93 | /** |
||
94 | * Abstract object mutator constructor |
||
95 | * |
||
96 | * @param Generator $generator |
||
97 | */ |
||
98 | 2 | public function __construct(Generator $generator) |
|
102 | |||
103 | /** |
||
104 | * Magic setter for randomization of setter calls |
||
105 | * |
||
106 | * @param string $method Method name |
||
107 | * @param array $arguments Arguments |
||
108 | * @return ObjectInterface Object |
||
109 | * @throws RuntimeException If the randomized setter is invalid |
||
110 | */ |
||
111 | 4 | public function __call($method, array $arguments) |
|
112 | { |
||
113 | // If the randomized setter is invalid |
||
114 | 4 | $setterMethod = 'set'.substr($method, 9); |
|
115 | 4 | if (strncmp('setRandom', $method, 9) || !is_callable([$this, $setterMethod])) { |
|
116 | 1 | throw new RuntimeException( |
|
117 | 1 | sprintf('Invalid randomized setter "%s"', $setterMethod), |
|
118 | 1 | RuntimeException::INVALID_RANDOMIZED_SETTER |
|
119 | ); |
||
120 | } |
||
121 | |||
122 | // If no object is given |
||
123 | 3 | if (!count($arguments) || !($arguments[0] instanceof ObjectInterface)) { |
|
124 | 1 | throw new RuntimeException('Invalid mutator subject', RuntimeException::INVALID_MUTATOR_SUBJECT); |
|
125 | } |
||
126 | |||
127 | // If the setter call should be randomized |
||
128 | 2 | if ((count($arguments) > 1) && !$this->happens($arguments[1])) { |
|
129 | 2 | return $arguments[0]; |
|
130 | } |
||
131 | |||
132 | 2 | return $this->$setterMethod(...$arguments); |
|
133 | } |
||
134 | |||
135 | |||
136 | /** |
||
137 | * Set the object title |
||
138 | * |
||
139 | * @param ObjectInterface $object Object |
||
140 | * @return ObjectInterface $object Object |
||
141 | */ |
||
142 | 2 | protected function setTitle(ObjectInterface $object) |
|
146 | |||
147 | /** |
||
148 | * Set the object title |
||
149 | * |
||
150 | * @param ObjectInterface $object Object |
||
151 | * @return ObjectInterface $object Object |
||
152 | */ |
||
153 | 2 | protected function setLocation(ObjectInterface $object) |
|
162 | |||
163 | /** |
||
164 | * Set the object description |
||
165 | * |
||
166 | * @param ObjectInterface $object Object |
||
167 | * @return ObjectInterface $object Object |
||
168 | */ |
||
169 | 2 | protected function setDescription(ObjectInterface $object) |
|
173 | |||
174 | /** |
||
175 | * Set the object abstract |
||
176 | * |
||
177 | * @param ObjectInterface $object Object |
||
178 | * @return ObjectInterface $object Object |
||
179 | */ |
||
180 | 2 | protected function setAbstract(ObjectInterface $object) |
|
184 | |||
185 | /** |
||
186 | * Set the object keywords |
||
187 | * |
||
188 | * @param ObjectInterface $object Object |
||
189 | * @return ObjectInterface $object Object |
||
190 | */ |
||
191 | 2 | protected function setKeywords(ObjectInterface $object) |
|
195 | |||
196 | /** |
||
197 | * Set the object categories |
||
198 | * |
||
199 | * @param ObjectInterface $object Object |
||
200 | * @return ObjectInterface $object Object |
||
201 | */ |
||
202 | 2 | protected function setCategories(ObjectInterface $object) |
|
206 | |||
207 | /** |
||
208 | * Set the object authors |
||
209 | * |
||
210 | * @param ObjectInterface $object Object |
||
211 | * @return ObjectInterface $object Object |
||
212 | */ |
||
213 | 2 | protected function setAuthors(ObjectInterface $object) |
|
222 | |||
223 | /** |
||
224 | * Add an additional author to the object |
||
225 | * |
||
226 | * @param ObjectInterface $object Object |
||
227 | * @return ObjectInterface |
||
228 | */ |
||
229 | 2 | protected function setAdditionalAuthor(ObjectInterface $object) |
|
241 | |||
242 | /** |
||
243 | * Set the object syndication URLs |
||
244 | * |
||
245 | * @param ObjectInterface $object Object |
||
246 | * @return ObjectInterface $object Object |
||
247 | */ |
||
248 | 1 | protected function setSyndication(ObjectInterface $object) |
|
257 | |||
258 | /** |
||
259 | * Add an additional syndication URL to the object |
||
260 | * |
||
261 | * @param ObjectInterface $object Object |
||
262 | * @return ObjectInterface $object Object |
||
263 | */ |
||
264 | 1 | protected function setAdditionalSyndication(ObjectInterface $object) |
|
276 | |||
277 | /** |
||
278 | * Set an object feature image |
||
279 | * |
||
280 | * @param ObjectInterface $object Object |
||
281 | * @return ObjectInterface $object Object |
||
282 | */ |
||
283 | 1 | protected function setFeatured(ObjectInterface $object) |
|
287 | |||
288 | /** |
||
289 | * Create a random signal with a particular probability |
||
290 | * |
||
291 | * @param float $probability Probability |
||
292 | * @return bool Signal |
||
293 | */ |
||
294 | 2 | protected function happens($probability = 0.0) |
|
298 | |||
299 | /** |
||
300 | * Format a relation string |
||
301 | * |
||
302 | * @param string $url URL |
||
303 | * @param string $email Email address |
||
304 | * @param string $label Label |
||
305 | * @return string Relation string |
||
306 | */ |
||
307 | 2 | protected function formatRelation($url = '', $email = '', $label = '') |
|
311 | } |
||
312 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.