Exemple #1
0
 /**
  * Serialize the given serializable collection using the better serialization
  * for the current 'Content-Type' header.
  * 
  * The serialization result is written to the reponse body.
  * 
  * If none or an invalid 'Content-Type' header is provided the default one
  * will be used (which is 'application/json').
  * 
  * @param SerializableCollection $data the serializable collection
  *
  * @return Response the current response (after update)
  *
  * @throws \RuntimeException an error occurred during the serialization process
  */
 public function setSerializedBody(SerializableCollection $data)
 {
     $format = SerializableCollection::JSON;
     //read the content-type and, if no content-type is given use the default one
     $mediaTypes = $this->getHeader('Content-Type');
     $mediaType = count($mediaTypes) > 0 ? $mediaTypes[0] : 'application/json';
     //make sure to be using the correct content-type
     $this->headers->set('Content-Type', $mediaType . ';charset=utf8');
     switch ($mediaType) {
         case 'application/json':
             $format = SerializableCollection::JSON;
             break;
         case 'text/yaml':
         case 'text/x-yaml':
         case 'application/yaml':
         case 'application/x-yaml':
             $format = SerializableCollection::YAML;
             break;
         case 'application/xml':
         case 'text/xml':
             $format = SerializableCollection::XML;
             break;
         default:
             break;
     }
     //serialize given data and write it on the response body
     try {
         $this->body->rewind();
         $this->body->write($data->serialize($format));
     } catch (Gishiki\Algorithms\Collections\SerializationException $ex) {
         throw new \RuntimeException('The given content cannot be serialized');
     }
     return $this;
 }
Exemple #2
0
 /**
  * Deserialize the content passed as the request.
  * 
  * Supported media types for the deserialization are:
  *   - 'application/json'
  *   - 'application/xml'
  *   - 'text/xml'
  *   - 'application/x-www-form-urlencoded'
  *   - 'multipart/form-data'
  *   - 'text/yaml'
  *   - 'text/x-yaml'
  *   - 'application/yaml'
  *   - 'application/x-yaml'
  * 
  * @return SerializableCollection the deserialized body
  * @throw  RuntimeException       the error preventing the deserialization
  */
 public function getDeserializedBody()
 {
     if ($this->cachedDeserializedBody) {
         return $this->cachedDeserializedBody;
     }
     //get the media type that gives the serializator to be used
     $mediaType = $this->getMediaType();
     //get the body or something invalid
     $body = $this->body ? (string) $this->getBody() : null;
     //get the serializer
     $serializer = null;
     //this is what will be deserialized
     $data = null;
     switch ($mediaType) {
         case 'application/json':
             $data = $body;
             $serializer = SerializableCollection::JSON;
             break;
         case 'application/xml':
         case 'text/xml':
             $serializer = SerializableCollection::XML;
             $data = $body;
             break;
         case 'text/yaml':
         case 'text/x-yaml':
         case 'application/yaml':
         case 'application/x-yaml':
             $serializer = SerializableCollection::YAML;
             $data = $body;
             break;
         case 'application/x-www-form-urlencoded':
         case 'multipart/form-data':
             $data = $this->getParsedBody();
             break;
         default:
             $data = [];
             $serializer = null;
     }
     //return the serialization result
     try {
         return $this->cachedDeserializedBody = SerializableCollection::deserialize($data, $serializer);
     } catch (Gishiki\Algorithms\Collections\DeserializationException $ex) {
         throw new RuntimeException('The HTTP request is malformed');
     }
 }
Exemple #3
0
 /**
  * Read the application configuration (settings.json) and return the 
  * parsing result.
  * 
  * @return array the application configuration
  */
 public static function getApplicationSettings()
 {
     //get the json encoded application settings
     $config = file_get_contents(APPLICATION_DIR . 'settings.json');
     //parse the settings file
     $appConfiguration = SerializableCollection::deserialize($config)->all();
     //complete settings
     $appCompletedConfiguration = self::getValueFromEnvironment($appConfiguration);
     //return the application configuration
     return $appCompletedConfiguration;
 }