Ejemplo n.º 1
0
Archivo: Json.php Proyecto: seytar/psx
 public function read(MessageInterface $message)
 {
     $body = (string) $message->getBody();
     if (!empty($body)) {
         return JsonParser::decode($body, false);
     } else {
         return null;
     }
 }
Ejemplo n.º 2
0
Archivo: Form.php Proyecto: seytar/psx
 public function read(MessageInterface $message)
 {
     $body = (string) $message->getBody();
     $form = array();
     if (!empty($body)) {
         parse_str($body, $form);
         return CurveArray::objectify($form);
     } else {
         return null;
     }
 }
Ejemplo n.º 3
0
Archivo: Xml.php Proyecto: seytar/psx
 public function read(MessageInterface $message)
 {
     $body = (string) $message->getBody();
     if (!empty($body)) {
         $dom = new DOMDocument();
         $dom->encoding = 'UTF-8';
         $dom->loadXML($body);
         return $dom;
     } else {
         return null;
     }
 }
Ejemplo n.º 4
0
 /**
  * Extracts the body from an http message and transforms the data into an
  * array structure
  *
  * @param \PSX\Http\MessageInterface $message
  * @param \PSX\Data\TransformerInterface $transformer
  * @param string $readerType
  * @return array
  */
 public function extract(MessageInterface $message, TransformerInterface $transformer = null, $readerType = null)
 {
     $contentType = $message->getHeader('Content-Type');
     $reader = $this->getRequestReader($contentType, $readerType);
     $data = $reader->read($message);
     // get transformer
     if ($transformer === null) {
         $transformer = $this->transformerManager->getTransformerByContentType($contentType);
     }
     if ($transformer instanceof TransformerInterface) {
         $data = $transformer->transform($data);
     }
     return $data;
 }
Ejemplo n.º 5
0
 public function extract(MessageInterface $message, RecordInterface $record)
 {
     $auth = (string) $message->getHeader('Authorization');
     if (!empty($auth)) {
         if (strpos($auth, 'OAuth') !== false) {
             // get oauth data
             $data = array();
             $items = explode(',', $auth);
             foreach ($items as $v) {
                 $v = trim($v);
                 if (substr($v, 0, 6) == 'oauth_') {
                     $pair = explode('=', $v);
                     if (isset($pair[0]) && isset($pair[1])) {
                         $key = substr(strtolower($pair[0]), 6);
                         $val = trim($pair[1], '"');
                         $data[$key] = Oauth::urlDecode($val);
                     }
                 }
             }
             // check whether all required values are available
             foreach ($this->map as $k => $v) {
                 if (isset($data[$v])) {
                     $method = 'set' . ucfirst($k);
                     if (method_exists($record, $method)) {
                         $record->{$method}($data[$v]);
                     } else {
                         throw new InvalidDataException('Unknown parameter');
                     }
                 } elseif (in_array($k, $this->requiredFields)) {
                     throw new InvalidDataException('Required parameter "' . $v . '" is missing');
                 }
             }
             return $record;
         } else {
             throw new InvalidDataException('Unknown OAuth authentication');
         }
     } else {
         throw new InvalidDataException('Missing Authorization header');
     }
 }
Ejemplo n.º 6
0
 /**
  * @param \PSX\Http\MessageInterface $message
  * @return array
  */
 public static function buildHeaderFromMessage(MessageInterface $message)
 {
     $headers = $message->getHeaders();
     $result = array();
     foreach ($headers as $key => $value) {
         if ($key == 'set-cookie') {
             foreach ($value as $cookie) {
                 $result[] = $key . ': ' . $cookie;
             }
         } else {
             $result[] = $key . ': ' . implode(', ', $value);
         }
     }
     return $result;
 }