diff --git a/src/Action/SearchAction.php b/src/Action/SearchAction.php index 393084f..56a3330 100644 --- a/src/Action/SearchAction.php +++ b/src/Action/SearchAction.php @@ -3,6 +3,7 @@ namespace Bolt\Extension\Bolt\JsonApi\Action; use Bolt\Extension\Bolt\JsonApi\Converter\Parameter\ParameterCollection; +use Bolt\Extension\Bolt\JsonApi\Converter\Parameter\Type\Page; use Bolt\Extension\Bolt\JsonApi\Exception\ApiInvalidRequestException; use Bolt\Extension\Bolt\JsonApi\Response\ApiResponse; use Bolt\Extension\Bolt\JsonApi\Storage\Query\PagingResultSet; @@ -52,22 +53,24 @@ public function handle($contentType = null, Request $request, ParameterCollectio $results = $set->get($contentType); - $page = $parameters->getParametersByType('page'); + /** @var Page $page */ + $page = $parameters->get('page'); $this->throwErrorOnNoResults($results, "No search results found for query [$search]"); foreach ($results as $key => $item) { + $contentType = (string) $item->getContenttype(); // optimize this part... - $fields = $parameters->get('fields')->getFields(); + $fields = $parameters->get('fields')->getFields($contentType); $items[$key] = $this->parser->parseItem($item, $fields); } return new ApiResponse([ 'links' => $this->dataLinks->makeLinks( $searchContentType, - $page['number'], + $page->getNumber(), $set->getTotalPages(), - $page['limit'], + $page->getSize(), $request ), 'meta' => [ diff --git a/src/Converter/JSONAPIConverter.php b/src/Converter/JSONAPIConverter.php index 19c9878..e679043 100644 --- a/src/Converter/JSONAPIConverter.php +++ b/src/Converter/JSONAPIConverter.php @@ -47,6 +47,7 @@ public function grabParameters($converter, Request $request) $parameters = []; $parameters['search'] = $request->query->get('q', false); + $contentType = $request->get('contentType', false); if (! $this->isSearch($parameters)) { //Get content type if it isn't a search... @@ -55,6 +56,8 @@ public function grabParameters($converter, Request $request) if (! $parameters['contentType']) { $parameters['contentType'] = $request->attributes->get('contentType'); } + } else if ($contentType) { + $parameters['contentType'] = $contentType; } $parameters['page']['size'] = $request->query->get('page[size]', false, true); diff --git a/src/Converter/Parameter/ParameterFactory.php b/src/Converter/Parameter/ParameterFactory.php index b0745bf..6d2b48c 100644 --- a/src/Converter/Parameter/ParameterFactory.php +++ b/src/Converter/Parameter/ParameterFactory.php @@ -26,7 +26,7 @@ class ParameterFactory */ public static function build($parameters, Config $config, MetadataDriver $metadata) { - $contentType = $parameters['contentType']; + $contentType = (empty($parameters['contentType']) ? null : $parameters['contentType']); foreach ($parameters as $key => $value) { //Get FQDN diff --git a/src/Converter/Parameter/Type/AbstractParameter.php b/src/Converter/Parameter/Type/AbstractParameter.php index 18161e9..72bb79e 100644 --- a/src/Converter/Parameter/Type/AbstractParameter.php +++ b/src/Converter/Parameter/Type/AbstractParameter.php @@ -65,9 +65,13 @@ abstract public function getParameter(); /** * Returns all field names for the given contenttype. */ - protected function getAllFieldNames() + protected function getAllFieldNames($contentType = null) { - return $this->metadata->getClassMetadata($this->contentType)['fields']; + if (!$contentType) { + return $this->metadata->getClassMetadata($this->contentType)['fields']; + } + + return $this->metadata->getClassMetadata($contentType)['fields']; } /** diff --git a/src/Converter/Parameter/Type/Fields.php b/src/Converter/Parameter/Type/Fields.php index 31d23ae..7d8ad5f 100644 --- a/src/Converter/Parameter/Type/Fields.php +++ b/src/Converter/Parameter/Type/Fields.php @@ -21,33 +21,46 @@ public function convertRequest() { $this->fields = []; - if ($this->config->getAllowedFields($this->contentType)) { - $allowedFields = $this->config->getAllowedFields($this->contentType); + if (! isset($this->contentType)) { + $allContentTypes = array_keys($this->config->getContentTypes()); + foreach ($allContentTypes as $contentType) { + $this->getFieldsForContentType($contentType); + } } else { - $allowedFields = array_keys($this->getAllFieldNames()); + $this->getFieldsForContentType($this->contentType); } - if (isset($this->values[$this->contentType])) { - $values = explode(',', $this->values[$this->contentType]); + return $this; + } + + protected function getFieldsForContentType($contentType) + { + if ($this->config->getAllowedFields($contentType)) { + $allowedFields = $this->config->getAllowedFields($contentType); + } else { + $allowedFields = array_keys($this->getAllFieldNames($contentType)); + } + + if (isset($this->values[$contentType])) { + $values = explode(',', $this->values[$contentType]); foreach ($values as $v) { if (in_array($v, $allowedFields)) { - $this->fields[] = $v; + $this->fields[$contentType] = $v; } } } // Default on the default/fallback fields defined in the config. - if (empty($this->fields)) { - $this->fields = $allowedFields; - if ($this->config->getListFields($this->contentType)) { - $this->fields = $this->config->getListFields(($this->contentType)); + if (empty($this->fields[$contentType])) { + $this->fields[$contentType] = $allowedFields; + if ($this->config->getListFields($contentType)) { + $this->fields[$contentType] = $this->config->getListFields($contentType); // todo: do we need to filter these through 'allowed-fields'? } } - - return $this; } + public function findConfigValues() { } @@ -63,9 +76,13 @@ public function getParameter() /** * @return array */ - public function getFields() + public function getFields($contentType = null) { - return $this->fields; + if (! $contentType) { + return $this->fields[$this->contentType]; + } + + return $this->fields[$contentType]; } /** diff --git a/src/Converter/Parameter/Type/Page.php b/src/Converter/Parameter/Type/Page.php index 13ac27d..0490d0f 100644 --- a/src/Converter/Parameter/Type/Page.php +++ b/src/Converter/Parameter/Type/Page.php @@ -26,8 +26,8 @@ class Page extends AbstractParameter public function convertRequest() { //False will return 0, which will be less than 1, so it should default correctly. - $size = intval($this->values['size']); - $number = intval($this->values['number']); + $size = (int) $this->values['size']; + $number = (int) $this->values['number']; // Get limit for query $this->size = $size >= 1 ? $size : self::DEFAULT_PAGE_SIZE;