Ejemplo n.º 1
0
 /**
  * Formats $data to an array.
  * $data could be:
  *  - JSON string
  *  - object
  *  - array
  *  - null
  *  - IArrayCastable
  *
  * @param mixed $data
  * @return array
  * @throws \InvalidArgumentException
  */
 public function cast($data)
 {
     switch (true) {
         case is_null($data):
             return array();
         case is_array($data):
             return $data;
         case $data instanceof IArrayCastable:
             return $data->toArray();
         case is_object($data):
             return (array) $data;
         case is_string($data):
             return Json::createFromString($data)->toArray();
         default:
             throw new \InvalidArgumentException(sprintf('$data must be a JSON, an array or an array castable object: %s given.', gettype($data)));
     }
 }
Ejemplo n.º 2
0
 /**
  * {@inheritDoc}
  *
  * @return Json
  */
 public function toJson($options = JSON_PRETTY_PRINT)
 {
     $result = new Json($this);
     $result->setOptions($options);
     return $result;
 }