diff --git a/composer.json b/composer.json index 87eaa4dde5..9f781341c5 100644 --- a/composer.json +++ b/composer.json @@ -65,7 +65,8 @@ "twig/twig" : "~1.14, >=1.14.2", "twig/extensions" : "~1.0", "vierbergenlars/php-semver" : "~2.1", - "zend/gdata" : "~1.12.1" + "zend/gdata" : "~1.12.1", + "willdurand/negotiation" : "@stable" }, "require-dev": { "doctrine/data-fixtures" : "1.0.x@dev", diff --git a/composer.lock b/composer.lock index 13b332a281..f3aebcc6ed 100644 --- a/composer.lock +++ b/composer.lock @@ -1494,11 +1494,13 @@ }, { "name": "media-alchemyst/media-alchemyst", - "version": "dev-master", + "version": "0.4.4", "source": { "type": "git", "url": "https://github.com/nlegoff/Media-Alchemyst.git", "reference": "be5c7cd4e207c469fb88d88397bd220c4db323d8" + "url": "https://github.com/alchemy-fr/Media-Alchemyst.git", + "reference": "067599fd669b86ff3a1cfb344730014b4a339b7e" }, "dist": { "type": "zip", @@ -1509,11 +1511,11 @@ "require": { "alchemy/ghostscript": "~0.4.0", "imagine/imagine": "0.6.x@dev", - "mediavorus/mediavorus": "dev-fix-video-dim as 0.4.3", + "mediavorus/mediavorus": "~0.4.2", "monolog/monolog": "~1.0", "neutron/temporary-filesystem": "~2.1", "php": ">=5.3.3", - "php-ffmpeg/php-ffmpeg": "dev-fix-aspect-ratio as 0.4.4", + "php-ffmpeg/php-ffmpeg": "~0.4.2", "php-mp4box/php-mp4box": "~0.3.0", "php-unoconv/php-unoconv": "~0.3.0", "pimple/pimple": "~1.0", @@ -1538,6 +1540,7 @@ "MediaAlchemyst": "src" } }, + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -1569,16 +1572,16 @@ }, { "name": "mediavorus/mediavorus", - "version": "dev-fix-video-dim", + "version": "0.4.3", "source": { "type": "git", - "url": "https://github.com/nlegoff/Mediavorus.git", - "reference": "f11cd2453d5acbc6ac52421dc4edd760433b1515" + "url": "https://github.com/romainneutron/MediaVorus.git", + "reference": "7cc8c0b8d3440eaff9aa68fff2185216fd72bd75" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nlegoff/Mediavorus/zipball/f11cd2453d5acbc6ac52421dc4edd760433b1515", - "reference": "f11cd2453d5acbc6ac52421dc4edd760433b1515", + "url": "https://api.github.com/repos/romainneutron/MediaVorus/zipball/7cc8c0b8d3440eaff9aa68fff2185216fd72bd75", + "reference": "7cc8c0b8d3440eaff9aa68fff2185216fd72bd75", "shasum": "" }, "require": { @@ -1611,6 +1614,7 @@ "MediaVorus": "src" } }, + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -1625,10 +1629,7 @@ "keywords": [ "metadata" ], - "support": { - "source": "https://github.com/nlegoff/Mediavorus/tree/fix-video-dim" - }, - "time": "2014-06-10 14:03:47" + "time": "2014-01-06 15:45:32" }, { "name": "monolog/monolog", @@ -1956,16 +1957,16 @@ }, { "name": "php-ffmpeg/php-ffmpeg", - "version": "dev-fix-aspect-ratio", + "version": "0.4.4", "source": { "type": "git", - "url": "https://github.com/nlegoff/PHP-FFmpeg.git", - "reference": "c2ea1ca7ab0a4da0e876e36c8cddbfa4d92dbba6" + "url": "https://github.com/alchemy-fr/PHP-FFmpeg.git", + "reference": "8dfaf1815802614352bbd10eac7299a100bf9757" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nlegoff/PHP-FFmpeg/zipball/c2ea1ca7ab0a4da0e876e36c8cddbfa4d92dbba6", - "reference": "c2ea1ca7ab0a4da0e876e36c8cddbfa4d92dbba6", + "url": "https://api.github.com/repos/alchemy-fr/PHP-FFmpeg/zipball/8dfaf1815802614352bbd10eac7299a100bf9757", + "reference": "8dfaf1815802614352bbd10eac7299a100bf9757", "shasum": "" }, "require": { @@ -1986,7 +1987,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "0.5-dev" + "dev-master": "0.4-dev" } }, "autoload": { @@ -1994,6 +1995,7 @@ "FFMpeg": "src" } }, + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], @@ -2020,10 +2022,7 @@ "video", "video processing" ], - "support": { - "source": "https://github.com/nlegoff/PHP-FFmpeg/tree/fix-aspect-ratio" - }, - "time": "2014-06-10 10:27:04" + "time": "2013-12-17 16:54:46" }, { "name": "php-mp4box/php-mp4box", @@ -4035,12 +4034,9 @@ "package": "php-ffmpeg/php-ffmpeg" } ], - "minimum-stability": "dev", + "minimum-stability": "stable", "stability-flags": { "imagine/imagine": 20, - "mediavorus/mediavorus": 20, - "media-alchemyst/media-alchemyst": 20, - "php-ffmpeg/php-ffmpeg": 20, "doctrine/data-fixtures": 20 }, "platform": { diff --git a/lib/Alchemy/Phrasea/Application.php b/lib/Alchemy/Phrasea/Application.php index 8c15144e23..df5ad83678 100644 --- a/lib/Alchemy/Phrasea/Application.php +++ b/lib/Alchemy/Phrasea/Application.php @@ -76,6 +76,7 @@ use Alchemy\Phrasea\Core\Provider\BorderManagerServiceProvider; use Alchemy\Phrasea\Core\Provider\CacheServiceProvider; use Alchemy\Phrasea\Core\Provider\ConfigurationServiceProvider; use Alchemy\Phrasea\Core\Provider\ConfigurationTesterServiceProvider; +use Alchemy\Phrasea\Core\Provider\ContentNegociationServiceProvider; use Alchemy\Phrasea\Core\Provider\CSVServiceProvider; use Alchemy\Phrasea\Core\Provider\FileServeServiceProvider; use Alchemy\Phrasea\Core\Provider\FtpServiceProvider; @@ -301,6 +302,7 @@ class Application extends SilexApplication $this->register(new FileServeServiceProvider()); $this->register(new PluginServiceProvider()); $this->register(new PhraseaEventServiceProvider()); + $this->register(new ContentNegociationServiceProvider()); $this['phraseanet.exception_handler'] = $this->share(function ($app) { return PhraseaExceptionHandler::register($app['debug']); diff --git a/lib/Alchemy/Phrasea/Application/Api.php b/lib/Alchemy/Phrasea/Application/Api.php index d51c6cd070..50f90e612d 100644 --- a/lib/Alchemy/Phrasea/Application/Api.php +++ b/lib/Alchemy/Phrasea/Application/Api.php @@ -24,6 +24,7 @@ use Monolog\Logger; use Monolog\Processor\WebProcessor; use Silex\Application as SilexApplication; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; return call_user_func(function ($environment = PhraseaApplication::ENV_PROD) { @@ -39,6 +40,47 @@ return call_user_func(function ($environment = PhraseaApplication::ENV_PROD) { return $monolog; })); + $app->before(function(Request $request) use ($app) { + // register custom API format + $request->setFormat(\API_V1_result::FORMAT_JSON_EXTENDED, \API_V1_adapter::$extendedContentTypes['json']); + $request->setFormat(\API_V1_result::FORMAT_YAML_EXTENDED, \API_V1_adapter::$extendedContentTypes['yaml']); + $request->setFormat(\API_V1_result::FORMAT_JSONP_EXTENDED, \API_V1_adapter::$extendedContentTypes['jsonp']); + $request->setFormat(\API_V1_result::FORMAT_JSONP, array('text/javascript', 'application/javascript')); + + // handle content negociation + $priorities = array('application/json', 'application/yaml', 'text/yaml', 'text/javascript', 'application/javascript'); + foreach (\API_V1_adapter::$extendedContentTypes['json'] as $priorities[]); + foreach (\API_V1_adapter::$extendedContentTypes['yaml'] as $priorities[]); + $format = $app['format.negociator']->getBest($request->headers->get('accept') ,$priorities); + + // throw unacceptable http error if API can not handle asked format + if (null === $format) { + $app->abort(406); + } + + // set request format according to negociated content or override format with jsonp is callback parameter is defined + if (trim($request->query->get('callback')) !== '') { + $request->setRequestFormat('jsonp'); + } else { + $request->setRequestFormat($request->getFormat($format->getValue())); + } + + // tells whether asked format is extended or not + $request->attributes->set('_extended', in_array( + $request->getRequestFormat('json'), + array( + \API_V1_result::FORMAT_JSON_EXTENDED, + \API_V1_result::FORMAT_YAML_EXTENDED, + \API_V1_result::FORMAT_JSONP_EXTENDED + ) + )); + }); + + $app->after(function(Request $request, Response $response) use ($app) { + // set response content type + $response->headers->set('Content-Type', $request->getMimeType($request->getRequestFormat())); + }); + $app->register(new \API_V1_Timer()); $app['dispatcher']->dispatch(PhraseaEvents::API_LOAD_START, new ApiLoadStartEvent()); diff --git a/lib/Alchemy/Phrasea/Core/Event/Subscriber/ApiExceptionHandlerSubscriber.php b/lib/Alchemy/Phrasea/Core/Event/Subscriber/ApiExceptionHandlerSubscriber.php index d9ebf47189..03f62462cb 100644 --- a/lib/Alchemy/Phrasea/Core/Event/Subscriber/ApiExceptionHandlerSubscriber.php +++ b/lib/Alchemy/Phrasea/Core/Event/Subscriber/ApiExceptionHandlerSubscriber.php @@ -66,6 +66,8 @@ class ApiExceptionHandlerSubscriber implements EventSubscriberInterface } elseif ($e instanceof HttpExceptionInterface) { if (503 === $e->getStatusCode()) { $code = \API_V1_result::ERROR_MAINTENANCE; + } else if (406 === $e->getStatusCode()) { + $code = \API_V1_result::ERROR_UNACCEPTABLE; } else { $code = \API_V1_result::ERROR_INTERNALSERVERERROR; } diff --git a/lib/classes/API/V1/adapter.php b/lib/classes/API/V1/adapter.php index eff8dcb7fa..b3e1c34e21 100644 --- a/lib/classes/API/V1/adapter.php +++ b/lib/classes/API/V1/adapter.php @@ -43,6 +43,12 @@ class API_V1_adapter extends API_V1_Abstract const OBJECT_TYPE_STORY = 'http://api.phraseanet.com/api/objects/story'; const OBJECT_TYPE_STORY_METADATA_BAG = 'http://api.phraseanet.com/api/objects/story-metadata-bag'; + public static $extendedContentTypes = array( + 'json' => array('application/vnd.phraseanet.record-extended+json'), + 'yaml' => array('application/vnd.phraseanet.record-extended+yaml'), + 'jsonp' => array('application/vnd.phraseanet.record-extended+jsonp'), + ); + /** * API constructor * @@ -848,9 +854,9 @@ class API_V1_adapter extends API_V1_Abstract foreach ($search_result->getResults() as $record) { if ($record->is_grouping()) { - $ret['results']['stories'][] = $this->list_story($record); + $ret['results']['stories'][] = $this->list_story($record, $request->get('_extended')); } else { - $ret['results']['records'][] = $this->list_record($record); + $ret['results']['records'][] = $this->list_record($record, $request->get('_extended')); } } @@ -878,7 +884,7 @@ class API_V1_adapter extends API_V1_Abstract list($ret, $search_result) = $this->prepare_search_request($request); foreach ($search_result->getResults() as $record) { - $ret['results'][] = $this->list_record($record); + $ret['results'][] = $this->list_record($record, $request->get('_extended')); } /** @@ -958,8 +964,8 @@ class API_V1_adapter extends API_V1_Abstract $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); - $stories = array_map(function ($story) use ($that) { - return $that->list_story($story); + $stories = array_map(function ($story) use ($that, $request) { + return $that->list_story($story, $request->get('_extended')); }, array_values($record->get_grouping_parents()->get_elements())); $result->set_datas(array( @@ -1177,7 +1183,7 @@ class API_V1_adapter extends API_V1_Abstract $collection = collection::get_from_base_id($this->app, $request->get('base_id')); $record->move_to_collection($collection, $this->app['phraseanet.appbox']); - $result->set_datas(array("record" => $this->list_record($record))); + $result->set_datas(array("record" => $this->list_record($record, $request->get('_extended')))); } catch (\Exception $e) { $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, $e->getMessage()); } @@ -1199,7 +1205,7 @@ class API_V1_adapter extends API_V1_Abstract $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); try { $record = $databox->get_record($record_id); - $result->set_datas(array('record' => $this->list_record($record))); + $result->set_datas(array('record' => $this->list_record($record, $request->get('_extended')))); } catch (NotFoundHttpException $e) { $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('Record Not Found')); } catch (\Exception $e) { @@ -1223,7 +1229,7 @@ class API_V1_adapter extends API_V1_Abstract $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); try { $story = $databox->get_record($story_id); - $result->set_datas(array('story' => $this->list_story($story))); + $result->set_datas(array('story' => $this->list_story($story, $request->get('_extended')))); } catch (NotFoundHttpException $e) { $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('Story Not Found')); } catch (\Exception $e) { @@ -1339,7 +1345,7 @@ class API_V1_adapter extends API_V1_Abstract $result->set_datas( array( "basket" => $this->list_basket($Basket), - "basket_elements" => $this->list_basket_content($Basket) + "basket_elements" => $this->list_basket_content($Basket, $request->get('_extended')) ) ); @@ -1352,12 +1358,12 @@ class API_V1_adapter extends API_V1_Abstract * @param \Entities\Basket $Basket * @return type */ - protected function list_basket_content(\Entities\Basket $Basket) + protected function list_basket_content(\Entities\Basket $Basket, $extended = false) { $ret = array(); foreach ($Basket->getElements() as $basket_element) { - $ret[] = $this->list_basket_element($basket_element); + $ret[] = $this->list_basket_element($basket_element, $extended); } return $ret; @@ -1369,12 +1375,12 @@ class API_V1_adapter extends API_V1_Abstract * @param \Entities\BasketElement $basket_element * @return type */ - protected function list_basket_element(\Entities\BasketElement $basket_element) + protected function list_basket_element(\Entities\BasketElement $basket_element, $extended = false) { $ret = array( 'basket_element_id' => $basket_element->getId(), 'order' => $basket_element->getOrd(), - 'record' => $this->list_record($basket_element->getRecord($this->app)), + 'record' => $this->list_record($basket_element->getRecord($this->app), $extended), 'validation_item' => null != $basket_element->getBasket()->getValidation(), ); @@ -1528,7 +1534,7 @@ class API_V1_adapter extends API_V1_Abstract 'feed' => $this->list_publication($feed, $user), 'offset_start' => $offset_start, 'per_page' => $per_page, - 'entries' => $this->list_publications_entries($feed, $offset_start, $per_page), + 'entries' => $this->list_publications_entries($feed, $request->get('_extended'), $offset_start, $per_page), ); $result->set_datas($datas); @@ -1553,7 +1559,7 @@ class API_V1_adapter extends API_V1_Abstract 'total_entries' => $feed->get_count_total_entries(), 'offset_start' => $offset_start, 'per_page' => $per_page, - 'entries' => $this->list_publications_entries($feed, $offset_start, $per_page), + 'entries' => $this->list_publications_entries($feed, $request->get('_extended'), $offset_start, $per_page), )); return $result; @@ -1611,14 +1617,13 @@ class API_V1_adapter extends API_V1_Abstract * @param int $how_many * @return array */ - protected function list_publications_entries(Feed_Abstract $feed, $offset_start = 0, $how_many = 5) + protected function list_publications_entries(Feed_Abstract $feed, $extended = false, $offset_start = 0, $how_many = 5) { - $entries = $feed->get_entries($offset_start, $how_many)->get_entries(); $out = array(); foreach ($entries as $entry) { - $out[] = $this->list_publication_entry($entry); + $out[] = $this->list_publication_entry($entry, $extended); } return $out; @@ -1630,11 +1635,11 @@ class API_V1_adapter extends API_V1_Abstract * @param Feed_Entry_Adapter $entry * @return array */ - protected function list_publication_entry(Feed_Entry_Adapter $entry) + protected function list_publication_entry(Feed_Entry_Adapter $entry, $extended = false) { $items = array(); foreach ($entry->get_content() as $item) { - $items[] = $this->list_publication_entry_item($item); + $items[] = $this->list_publication_entry_item($item, $extended); } return array( @@ -1659,11 +1664,11 @@ class API_V1_adapter extends API_V1_Abstract * @param Feed_Entry_Item $item * @return array */ - protected function list_publication_entry_item(Feed_Entry_Item $item) + protected function list_publication_entry_item(Feed_Entry_Item $item, $extended = false) { $datas = array( - 'item_id' => $item->get_id() - , 'record' => $this->list_record($item->get_record()) + 'item_id' => $item->get_id(), + 'record' => $this->list_record($item->get_record(), $extended) ); return $datas; @@ -1874,7 +1879,7 @@ class API_V1_adapter extends API_V1_Abstract * @param record_adapter $record * @return array */ - public function list_record(record_adapter $record) + public function list_record(record_adapter $record, $extended = false) { $technicalInformation = array(); foreach ($record->get_technical_infos() as $name => $value) { @@ -1884,7 +1889,7 @@ class API_V1_adapter extends API_V1_Abstract ); } - return array( + $data = array( 'databox_id' => $record->get_sbas_id(), 'record_id' => $record->get_record_id(), 'mime_type' => $record->get_mime(), @@ -1899,6 +1904,45 @@ class API_V1_adapter extends API_V1_Abstract 'phrasea_type' => $record->get_type(), 'uuid' => $record->get_uuid(), ); + + if ($extended) { + $subdefs = $caption = array(); + foreach ($record->get_embedable_medias(array(), array()) as $name => $media) { + $subdefs[] = $this->list_embedable_media($media, $this->app['phraseanet.registry']); + } + + $fields = $record->get_caption()->get_fields(); + foreach ($fields as $field) { + $caption[] = array( + 'meta_structure_id' => $field->get_meta_struct_id(), + 'name' => $field->get_name(), + 'value' => $field->get_serialized_values(';'), + ); + } + + $that = $this; + $baskets = array_map(function ($basket) use ($that, $record) { + return $that->list_basket($basket); + }, (array) $record->get_container_baskets($this->app['EM'], $this->app['authentication']->getUser()) + ); + + $stories = array_map(function ($story) use ($that, $extended) { + return $that->list_story($story, $extended); + }, array_values($record->get_grouping_parents()->get_elements())); + + $extendedData = array( + 'subdefs' => $subdefs, + 'metadata' => $this->list_record_caption($record->get_caption()), + 'status' => $this->list_record_status($record->get_databox(), $record->get_status()), +// 'baskets' => $baskets, +// 'stories' => $stories, + 'caption' => $caption + ); + + $data = array_merge($data, $extendedData); + } + + return $data; } /** @@ -1908,15 +1952,15 @@ class API_V1_adapter extends API_V1_Abstract * * @return array */ - public function list_story(record_adapter $story) + public function list_story(record_adapter $story, $extended = false) { if (!$story->is_grouping()) { throw new \API_V1_exception_notfound('Story not found'); } $that = $this; - $records = array_map(function (\record_adapter $record) use ($that) { - return $that->list_record($record); + $records = array_map(function (\record_adapter $record) use ($that, $extended) { + return $that->list_record($record, $extended); }, array_values($story->get_children()->get_elements())); $caption = $story->get_caption(); diff --git a/lib/classes/API/V1/result.php b/lib/classes/API/V1/result.php index 04c69ed86c..ea285a6b4c 100644 --- a/lib/classes/API/V1/result.php +++ b/lib/classes/API/V1/result.php @@ -14,77 +14,52 @@ use Alchemy\Phrasea\Core\PhraseaEvents; use Silex\Application; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Yaml\Dumper; class API_V1_result { protected $app; - /** - * - * @var string - */ + /** @var string */ protected $api_version; - /** - * - * @var string - */ + /** @var string */ protected $response_time; - /** - * - * @var int - */ + /** @var int */ protected $http_code = 200; - /** - * - * @var string - */ + /** @var string */ protected $error_type; - /** - * - * @var string - */ + /** @var string */ protected $error_message; - /** - * - * @var string - */ + /** @var string */ protected $error_details; - /** - * - * @var string - */ + /** @var string */ protected $request; - /** - * - * @var mixed - */ + /** @var mixed */ protected $response; - /** - * - * @var string - */ + /** @var string */ protected $response_type; - /** - * Constant for responsetype json - */ + /** Constant for response type json */ const FORMAT_JSON = 'json'; - /** - * Constant for responsetype yaml - */ + /** Constant for response type yaml */ const FORMAT_YAML = 'yaml'; - /** - * Constant for responsetype jsonp - */ + /** Constant for response type jsonp */ const FORMAT_JSONP = 'jsonp'; + /** Constant for response type json+extended */ + const FORMAT_JSON_EXTENDED = 'json+extended'; + /** Constant for response type yaml+extended */ + const FORMAT_YAML_EXTENDED = 'yaml+extended'; + /** Constant for response type jsonp+extended */ + const FORMAT_JSONP_EXTENDED = 'jsonp+extended'; + const ERROR_BAD_REQUEST = 'Bad Request'; const ERROR_UNAUTHORIZED = 'Unauthorized'; const ERROR_FORBIDDEN = 'Forbidden'; @@ -92,6 +67,7 @@ class API_V1_result const ERROR_MAINTENANCE = 'Service Temporarily Unavailable'; const ERROR_METHODNOTALLOWED = 'Method Not Allowed'; const ERROR_INTERNALSERVERERROR = 'Internal Server Error'; + const ERROR_UNACCEPTABLE = 'Unacceptable'; /** * API v1 Result constructor @@ -112,37 +88,9 @@ class API_V1_result $this->response_time = $date->format(DATE_ATOM); $this->response = new stdClass(); - $this->parse_response_type(); - return $this; } - protected function parse_response_type() - { - if (trim($this->request->get('callback')) !== '') { - return $this->response_type = self::FORMAT_JSONP; - } - - $accept = $this->request->getAcceptableContentTypes(); - $response_types = array(); - - foreach ($accept as $key => $app_type) { - $response_types[strtolower($app_type)] = true; - } - - if (array_key_exists('application/json', $response_types)) { - return $this->response_type = self::FORMAT_JSON; - } - if (array_key_exists('application/yaml', $response_types)) { - return $this->response_type = self::FORMAT_YAML; - } - if (array_key_exists('text/yaml', $response_types)) { - return $this->response_type = self::FORMAT_YAML; - } - - return $this->response_type = self::FORMAT_JSON; - } - /** * Set datas to the response * If no datas provided (aka empty array), a stdClass if set, @@ -153,8 +101,9 @@ class API_V1_result */ public function set_datas(array $datas) { - if (count($datas) === 0) + if (count($datas) === 0) { $datas = new stdClass (); + } $this->response = $datas; return $this; @@ -177,16 +126,13 @@ class API_V1_result */ public function format() { - $request_uri = sprintf('%s %s' - , $this->request->getMethod() - , $this->request->getBasePath() - . $this->request->getPathInfo() - ); - $ret = array( 'meta' => array( 'api_version' => $this->api_version - , 'request' => $request_uri + , 'request' => sprintf('%s %s', + $this->request->getMethod(), + $this->request->getBasePath() . $this->request->getPathInfo() + ) , 'response_time' => $this->response_time , 'http_code' => $this->http_code , 'error_type' => $this->error_type @@ -207,45 +153,25 @@ class API_V1_result $return_value = false; - switch ($this->response_type) { + switch ($this->request->getRequestFormat('json')) { case self::FORMAT_JSON: + case self::FORMAT_JSON_EXTENDED: default: - $return_value = p4string::jsonencode($ret); + $return_value = \p4string::jsonencode($ret); break; case self::FORMAT_YAML: - if ($ret['response'] instanceof stdClass) + case self::FORMAT_YAML_EXTENDED: + if ($ret['response'] instanceof \stdClass) { $ret['response'] = array(); + } - $dumper = new Symfony\Component\Yaml\Dumper(); + $dumper = new Dumper(); $return_value = $dumper->dump($ret, 8); break; case self::FORMAT_JSONP: + case self::FORMAT_JSONP_EXTENDED: $callback = trim($this->request->get('callback')); - $return_value = $callback . '(' . p4string::jsonencode($ret) . ')'; - break; - } - - return $return_value; - } - - /** - * Return serailized datas content type - * - * @return string - */ - public function get_content_type() - { - switch ($this->response_type) { - - case self::FORMAT_JSON: - default: - $return_value = 'application/json'; - break; - case self::FORMAT_YAML: - $return_value = 'application/yaml'; - break; - case self::FORMAT_JSONP: - $return_value = 'text/javascript'; + $return_value = $callback . '(' . \p4string::jsonencode($ret) . ')'; break; } @@ -301,6 +227,10 @@ class API_V1_result $this->error_type = $const; $this->error_message = API_V1_exception_maintenance::get_details(); break; + case self::ERROR_UNACCEPTABLE: + $this->http_code = 406; + $this->error_type = $const; + break; case OAUTH2_ERROR_INVALID_REQUEST: $this->error_type = $const; break; @@ -345,6 +275,10 @@ class API_V1_result $this->error_type = self::ERROR_METHODNOTALLOWED; $this->error_message = API_V1_exception_methodnotallowed::get_details(); break; + case 406: + $this->http_code = $code; + $this->error_type = self::ERROR_UNACCEPTABLE; + break; case 500: $this->http_code = $code; $this->error_type = self::ERROR_INTERNALSERVERERROR; @@ -362,7 +296,7 @@ class API_V1_result */ public function get_http_code() { - if ($this->response_type == self::FORMAT_JSONP && $this->http_code != 500) { + if ($this->response_type == self::FORMAT_JSONP && $this->http_code !== 500) { return 200; } else { return $this->http_code; @@ -385,11 +319,7 @@ class API_V1_result */ public function get_response() { - $response = new Response( - $this->format(), - $this->get_http_code(), - array('Content-Type' => $this->get_content_type()) - ); + $response = new Response($this->format(), $this->get_http_code()); $response->setCharset('UTF-8'); return $response;