Exemplo n.º 1
0
 /**
  * Translate from json to xml
  *
  * @param  PayloadInterface $payload
  */
 public static function translate(PayloadInterface $payload)
 {
     switch ($payload->getContentType()) {
         case 'text/xml':
         case 'application/xml':
             return clone $payload;
         case 'application/json':
             $obj = new Payload('text/xml');
             $data = json_decode($payload->getContent(), TRUE);
             static::insureValidKeys($data);
             $xml = ArrayToXml::convert($data);
             $obj->setContent($xml);
             return $obj;
         default:
             return static::failedTranslation($payload);
     }
 }
    public function testRealWorld1()
    {
        $subject = <<<EOD
{
    "collection": {
        "error": {
            "code": "404",
            "message": "Cycle 9710018 does not exist.",
            "title": "Not found"
        },
        "href": "http://dev.ovagraph.local/api/3.0/cycles/9710018",
        "version": "1.0"
    }
}    
EOD;
        $payload = new Payload('application/vnd.collection+json');
        $payload->setContent($subject);
        $json = CollectionJsonToJson::translate($payload)->getContent();
        $control = '{"collection":{"error":{"code":"404","message":"Cycle 9710018 does not exist.","title":"Not found"}}}';
        $this->assertSame($control, $json);
    }
 /**
  * Method to produce and return a consistent Payload object.
  *
  * Use this when a translation fails.
  *
  * @param  \AKlump\Http\Transfer\Payload $payload [description]
  *
  * @return \AKlump\Http\Transfer\Payload A new payload containing a failure message.
  */
 public static function failedTranslation($payload)
 {
     $obj = new Payload('text/error');
     $obj->setContent('Unable to recognize/translate mime type: ' . $payload->getContentType());
     return $obj;
 }
 public static function translate(PayloadInterface $payload)
 {
     if ($payload->getContentType() === 'application/json') {
         return clone $payload;
     }
     $obj = new Payload('application/json');
     if (!in_array($payload->getContentType(), array('application/vnd.collection+json'))) {
         return static::failedTranslation($payload);
     }
     $source = json_decode($payload->getContent());
     $items = array();
     $output = new \stdClass();
     $root = 'collection';
     if (isset($source->collection->items)) {
         $items = $source->collection->items;
     } elseif (isset($source->template->data)) {
         $items = array((object) array('data' => $source->template->data));
         $root = 'template';
     }
     foreach ($items as $item) {
         $output_item = (object) array('data' => new \stdClass());
         if (isset($item->data)) {
             foreach ($item->data as $data) {
                 if (!empty($data->name)) {
                     $output_item->data->{$data->name} = $data->value;
                 }
             }
         }
         if (isset($item->links)) {
             foreach ($item->links as $link) {
                 if (!empty($link->name)) {
                     $name = $link->name;
                 } else {
                     $name = $link->href;
                     $parsed = parse_url($name);
                     unset($parsed['scheme']);
                     $name = preg_replace('/[.\\/_]+/', '_', implode('_', $parsed));
                 }
                 $render = isset($link->render) ? $link->render : 'link';
                 $key = $render . 's';
                 if (!isset($output_item->{$key})) {
                     $output_item->{$key} = new \stdClass();
                 }
                 $output_item->{$key}->{$name} = $link->href;
             }
         }
         $output->items[] = $output_item;
     }
     if (isset($source->collection->error)) {
         $output->error = $source->collection->error;
     }
     switch ($root) {
         case 'collection':
             $output = (object) array('collection' => $output);
             break;
         case 'template':
             $output = (object) array('template' => reset($output->items));
             break;
     }
     $obj->setContent(json_encode($output));
     return $obj;
 }