1818use Sunrise \Http \Router \Exception \InvalidDescriptorArgumentException ;
1919use Sunrise \Http \Router \RouteDescriptorInterface ;
2020
21- /**
22- * Import functions
23- */
24- use function is_array ;
25- use function is_int ;
26- use function is_string ;
27- use function is_subclass_of ;
28-
2921/**
3022 * Annotation for a route description
3123 *
@@ -218,9 +210,10 @@ private function extractNameFromParams(array $params) : string
218210 {
219211 $ name = $ params ['name ' ] ?? '' ;
220212
221- if ('' === $ name || !is_string ($ name )) {
222- throw new InvalidDescriptorArgumentException ('@Route.name must contain a non-empty string. ' );
223- }
213+ InvalidDescriptorArgumentException::assertIsNotEmptyString (
214+ $ name ,
215+ '@Route.name must contain a non-empty string. '
216+ );
224217
225218 return $ name ;
226219 }
@@ -236,10 +229,16 @@ private function extractHostFromParams(array $params) : ?string
236229 {
237230 $ host = $ params ['host ' ] ?? null ;
238231
239- if (isset ($ host ) && ('' === $ host || !is_string ($ host ))) {
240- throw new InvalidDescriptorArgumentException ('@Route.host must contain a non-empty string. ' );
232+ // isn't required parameter...
233+ if (null === $ host ) {
234+ return null ;
241235 }
242236
237+ InvalidDescriptorArgumentException::assertIsNotEmptyString (
238+ $ host ,
239+ '@Route.host must contain a non-empty string. '
240+ );
241+
243242 return $ host ;
244243 }
245244
@@ -254,9 +253,10 @@ private function extractPathFromParams(array $params) : string
254253 {
255254 $ path = $ params ['path ' ] ?? '' ;
256255
257- if ('' === $ path || !is_string ($ path )) {
258- throw new InvalidDescriptorArgumentException ('@Route.path must contain a non-empty string. ' );
259- }
256+ InvalidDescriptorArgumentException::assertIsNotEmptyString (
257+ $ path ,
258+ '@Route.path must contain a non-empty string. '
259+ );
260260
261261 return $ path ;
262262 }
@@ -272,14 +272,16 @@ private function extractMethodsFromParams(array $params) : array
272272 {
273273 $ methods = $ params ['methods ' ] ?? [];
274274
275- if ([] === $ methods || !is_array ($ methods )) {
276- throw new InvalidDescriptorArgumentException ('@Route.methods must contain a non-empty array. ' );
277- }
275+ InvalidDescriptorArgumentException::assertIsNotEmptyArray (
276+ $ methods ,
277+ '@Route.methods must contain a non-empty array. '
278+ );
278279
279280 foreach ($ methods as $ value ) {
280- if (!is_string ($ value )) {
281- throw new InvalidDescriptorArgumentException ('@Route.methods must contain strings. ' );
282- }
281+ InvalidDescriptorArgumentException::assertIsNotEmptyString (
282+ $ value ,
283+ '@Route.methods must contain non-empty strings. '
284+ );
283285 }
284286
285287 return $ methods ;
@@ -294,18 +296,24 @@ private function extractMethodsFromParams(array $params) : array
294296 */
295297 private function extractMiddlewaresFromParams (array $ params ) : array
296298 {
297- $ middlewares = $ params ['middlewares ' ] ?? [] ;
299+ $ middlewares = $ params ['middlewares ' ] ?? null ;
298300
299- if (!is_array ($ middlewares )) {
300- throw new InvalidDescriptorArgumentException ('@Route.middlewares must contain an array. ' );
301+ // isn't required parameter...
302+ if (null === $ middlewares ) {
303+ return [];
301304 }
302305
306+ InvalidDescriptorArgumentException::assertIsArray (
307+ $ middlewares ,
308+ '@Route.middlewares must contain an array. '
309+ );
310+
303311 foreach ($ middlewares as $ value ) {
304- if (! is_string ( $ value ) || ! is_subclass_of ( $ value , MiddlewareInterface::class)) {
305- throw new InvalidDescriptorArgumentException (
306- ' @Route.middlewares must contain the class names of existing middlewares. '
307- );
308- }
312+ InvalidDescriptorArgumentException:: assertIsSubclassOf (
313+ $ value ,
314+ MiddlewareInterface:: class,
315+ ' @Route.middlewares must contain the class names of existing middlewares. '
316+ );
309317 }
310318
311319 return $ middlewares ;
@@ -320,12 +328,18 @@ private function extractMiddlewaresFromParams(array $params) : array
320328 */
321329 private function extractAttributesFromParams (array $ params ) : array
322330 {
323- $ attributes = $ params ['attributes ' ] ?? [] ;
331+ $ attributes = $ params ['attributes ' ] ?? null ;
324332
325- if (!is_array ($ attributes )) {
326- throw new InvalidDescriptorArgumentException ('@Route.attributes must contain an array. ' );
333+ // isn't required parameter...
334+ if (null === $ attributes ) {
335+ return [];
327336 }
328337
338+ InvalidDescriptorArgumentException::assertIsArray (
339+ $ attributes ,
340+ '@Route.attributes must contain an array. '
341+ );
342+
329343 return $ attributes ;
330344 }
331345
@@ -338,12 +352,18 @@ private function extractAttributesFromParams(array $params) : array
338352 */
339353 private function extractSummaryFromParams (array $ params ) : string
340354 {
341- $ summary = $ params ['summary ' ] ?? '' ;
355+ $ summary = $ params ['summary ' ] ?? null ;
342356
343- if (!is_string ($ summary )) {
344- throw new InvalidDescriptorArgumentException ('@Route.summary must contain a string. ' );
357+ // isn't required parameter...
358+ if (null === $ summary ) {
359+ return '' ;
345360 }
346361
362+ InvalidDescriptorArgumentException::assertIsString (
363+ $ summary ,
364+ '@Route.summary must contain a string. '
365+ );
366+
347367 return $ summary ;
348368 }
349369
@@ -356,12 +376,18 @@ private function extractSummaryFromParams(array $params) : string
356376 */
357377 private function extractDescriptionFromParams (array $ params ) : string
358378 {
359- $ description = $ params ['description ' ] ?? '' ;
379+ $ description = $ params ['description ' ] ?? null ;
360380
361- if (!is_string ($ description )) {
362- throw new InvalidDescriptorArgumentException ('@Route.description must contain a string. ' );
381+ // isn't required parameter...
382+ if (null === $ description ) {
383+ return '' ;
363384 }
364385
386+ InvalidDescriptorArgumentException::assertIsString (
387+ $ description ,
388+ '@Route.description must contain a string. '
389+ );
390+
365391 return $ description ;
366392 }
367393
@@ -374,16 +400,23 @@ private function extractDescriptionFromParams(array $params) : string
374400 */
375401 private function extractTagsFromParams (array $ params ) : array
376402 {
377- $ tags = $ params ['tags ' ] ?? [] ;
403+ $ tags = $ params ['tags ' ] ?? null ;
378404
379- if (!is_array ($ tags )) {
380- throw new InvalidDescriptorArgumentException ('@Route.tags must contain an array. ' );
405+ // isn't required parameter...
406+ if (null === $ tags ) {
407+ return [];
381408 }
382409
410+ InvalidDescriptorArgumentException::assertIsArray (
411+ $ tags ,
412+ '@Route.tags must contain an array. '
413+ );
414+
383415 foreach ($ tags as $ value ) {
384- if (!is_string ($ value )) {
385- throw new InvalidDescriptorArgumentException ('@Route.tags must contain strings. ' );
386- }
416+ InvalidDescriptorArgumentException::assertIsNotEmptyString (
417+ $ value ,
418+ '@Route.tags must contain non-empty strings. '
419+ );
387420 }
388421
389422 return $ tags ;
@@ -398,12 +431,18 @@ private function extractTagsFromParams(array $params) : array
398431 */
399432 private function extractPriorityFromParams (array $ params ) : int
400433 {
401- $ priority = $ params ['priority ' ] ?? 0 ;
434+ $ priority = $ params ['priority ' ] ?? null ;
402435
403- if (!is_int ($ priority )) {
404- throw new InvalidDescriptorArgumentException ('@Route.priority must contain an integer. ' );
436+ // isn't required parameter...
437+ if (null === $ priority ) {
438+ return 0 ;
405439 }
406440
441+ InvalidDescriptorArgumentException::assertIsInteger (
442+ $ priority ,
443+ '@Route.priority must contain an integer. '
444+ );
445+
407446 return $ priority ;
408447 }
409448}
0 commit comments