@@ -45,9 +45,23 @@ abstract class AbstractRestfulController extends AbstractController
45
45
protected $ identifierName = 'id ' ;
46
46
47
47
/**
48
- * @var int From Zend\Json\Json
48
+ * Flag to pass to json_decode and/or Zend\Json\Json::decode.
49
+ *
50
+ * The flags in Zend\Json\Json::decode are integers, but when evaluated
51
+ * in a boolean context map to the flag passed as the second parameter
52
+ * to json_decode(). As such, you can specify either the Zend\Json\Json
53
+ * constant or the boolean value. By default, starting in v3, we use
54
+ * the boolean value, and cast to integer if using Zend\Json\Json::decode.
55
+ *
56
+ * Default value is boolean true, meaning JSON should be cast to
57
+ * associative arrays (vs objects).
58
+ *
59
+ * Override the value in an extending class to set the default behavior
60
+ * for your class.
61
+ *
62
+ * @var int|bool
49
63
*/
50
- protected $ jsonDecodeType = Json:: TYPE_ARRAY ;
64
+ protected $ jsonDecodeType = true ;
51
65
52
66
/**
53
67
* Map of custom HTTP methods and their handlers
@@ -444,16 +458,16 @@ public function onDispatch(MvcEvent $e)
444
458
*
445
459
* @param Request $request
446
460
* @return mixed
461
+ * @throws Exception\DomainException If a JSON request was made, but no
462
+ * method for parsing JSON is available.
447
463
*/
448
464
public function processPostData (Request $ request )
449
465
{
450
466
if ($ this ->requestHasContentType ($ request , self ::CONTENT_TYPE_JSON )) {
451
- $ data = Json::decode ($ request ->getContent (), $ this ->jsonDecodeType );
452
- } else {
453
- $ data = $ request ->getPost ()->toArray ();
467
+ return $ this ->create ($ this ->jsonDecode ($ request ->getContent ()));
454
468
}
455
469
456
- return $ this ->create ($ data );
470
+ return $ this ->create ($ request -> getPost ()-> toArray () );
457
471
}
458
472
459
473
/**
@@ -564,14 +578,16 @@ protected function getIdentifier($routeMatch, $request)
564
578
*
565
579
* @param mixed $request
566
580
* @return object|string|array
581
+ * @throws Exception\DomainException If a JSON request was made, but no
582
+ * method for parsing JSON is available.
567
583
*/
568
584
protected function processBodyContent ($ request )
569
585
{
570
586
$ content = $ request ->getContent ();
571
587
572
588
// JSON content? decode and return it.
573
589
if ($ this ->requestHasContentType ($ request , self ::CONTENT_TYPE_JSON )) {
574
- return Json:: decode ( $ content , $ this ->jsonDecodeType );
590
+ return $ this ->jsonDecode ( $ request -> getContent () );
575
591
}
576
592
577
593
parse_str ($ content , $ parsedParams );
@@ -585,4 +601,35 @@ protected function processBodyContent($request)
585
601
586
602
return $ parsedParams ;
587
603
}
604
+
605
+ /**
606
+ * Decode a JSON string.
607
+ *
608
+ * Uses json_decode by default. If that is not available, checks for
609
+ * availability of Zend\Json\Json, and uses that if present.
610
+ *
611
+ * Otherwise, raises an exception.
612
+ *
613
+ * Marked protected to allow usage from extending classes.
614
+ *
615
+ * @param string
616
+ * @return mixed
617
+ * @throws Exception\DomainException if no JSON decoding functionality is
618
+ * available.
619
+ */
620
+ protected function jsonDecode ($ string )
621
+ {
622
+ if (function_exists ('json_decode ' )) {
623
+ return json_decode ($ string , (bool ) $ this ->jsonDecodeType );
624
+ }
625
+
626
+ if (class_exists (Json::class)) {
627
+ return Json::decode ($ string , (int ) $ this ->jsonDecodeType );
628
+ }
629
+
630
+ throw new Exception \DomainException (sprintf (
631
+ 'Unable to parse JSON request, due to missing ext/json and/or %s ' ,
632
+ Json::class
633
+ ));
634
+ }
588
635
}
0 commit comments