/**
  * Gets the resources for the provided query
  * @param \ride\library\http\jsonapi\JsonApiQuery $query
  * @param integer $total Total number of entries before pagination
  * @return mixed Array with resource data or false when an error occured
  */
 protected function getResources(JsonApiQuery $query, &$total)
 {
     $cacheControls = $this->dependencyInjector->getAll('ride\\library\\cache\\control\\CacheControl');
     $nameQuery = null;
     $lockedQuery = null;
     $enabledQuery = null;
     $filters = $query->getFilters();
     foreach ($filters as $filterName => $filterValue) {
         switch ($filterName) {
             case 'name':
                 $nameQuery = $filterValue;
                 break;
             case 'isLocked':
                 $lockedQuery = $filterValue;
                 break;
             case 'isEnabled':
                 $enabledQuery = $filterValue;
                 break;
             default:
                 $this->addFilterNotFoundError($this->type, $filterName);
                 break;
         }
     }
     $sorter = $this->createSorter($this->type, array('name', 'isLocked', 'isEnabled'));
     if ($this->document->getErrors()) {
         return false;
     }
     // perform filter
     if ($nameQuery || $lockedQuery || $enabledQuery) {
         foreach ($cacheControls as $key => $cacheControl) {
             if ($nameQuery && $this->filterStringValue($nameQuery, $cacheControl->getName()) === false) {
                 unset($cacheControls[$key]);
             } elseif ($lockedQuery !== null && ($lockedQuery && !$cacheControl->isLocked() || !$lockedQuery && $cacheControl->isLocked())) {
                 unset($cacheControls[$key]);
             } elseif ($enabledQuery !== null && ($enabledQuery && !$cacheControl->isEnabled() || !$enabledQuery && $cacheControl->isEnabled())) {
                 unset($cacheControls[$key]);
             }
         }
     }
     // create resource data from the parameters
     foreach ($cacheControls as $key => $cacheControl) {
         $cacheControls[$key] = array('id' => $cacheControl->getName(), 'name' => $cacheControl->getName(), 'isLocked' => !$cacheControl->canToggle(), 'isEnabled' => $cacheControl->isEnabled(), 'control' => $cacheControl);
     }
     // perform sort
     $cacheControls = $sorter->sort($cacheControls);
     // perform pagination
     $total = count($cacheControls);
     $cacheControls = array_slice($cacheControls, $query->getOffset(), $query->getLimit(100));
     // return
     return $cacheControls;
 }
 /**
  * Applies the API query on the model query through this strategy
  * @param \ride\library\http\jsonapi\JsonApiQuery $jsonApiQuery
  * @param \ride\library\orm\query\ModelQuery $modelQuery
  * @return null
  */
 public function applyFilter(JsonApiQuery $jsonApiQuery, ModelQuery $modelQuery)
 {
     $query = $jsonApiQuery->getFilter('elastic', null);
     if (!$query) {
         return;
     }
     $model = $modelQuery->getModel();
     if ($model->getMeta()->isLocalized()) {
         $query = str_replace(array('%locale%', '%25locale%25'), $modelQuery->getLocale(), $query);
     }
     $parameters = array('query' => $query, 'limit' => $jsonApiQuery->getLimit(50), 'offset' => $jsonApiQuery->getOffset());
     $result = $this->search->searchByQueryString($model, $parameters);
     $this->search->applyResultToModelQuery($result, $modelQuery);
 }
Пример #3
0
 /**
  * Gets the resources for the provided query
  * @param \ride\library\http\jsonapi\JsonApiQuery $query
  * @param integer $total Total number of entries before pagination
  * @return mixed Array with resource data or false when an error occured
  */
 protected function getResources(JsonApiQuery $query, &$total)
 {
     $config = $this->getConfig();
     $parameters = $config->getAll();
     $parameters = $config->getConfigHelper()->flattenConfig($parameters);
     $keyQuery = null;
     $valueQuery = null;
     $filters = $query->getFilters();
     foreach ($filters as $filterName => $filterValue) {
         switch ($filterName) {
             case 'key':
                 $keyQuery = $filterValue;
                 break;
             case 'value':
                 $valueQuery = $filterValue;
                 break;
             default:
                 $this->addFilterNotFoundError($this->type, $filterName);
                 break;
         }
     }
     $sorter = $this->createSorter($this->type, array('key', 'value'));
     if ($this->document->getErrors()) {
         return false;
     }
     // perform filter
     if ($keyQuery || $valueQuery) {
         foreach ($parameters as $key => $value) {
             if ($keyQuery && $this->filterStringValue($keyQuery, $key) === false) {
                 unset($parameters[$key]);
             } elseif ($valueQuery && $this->filterStringValue($valueQuery, $value) === false) {
                 unset($parameters[$key]);
             }
         }
     }
     // create resource data from the parameters
     foreach ($parameters as $key => $value) {
         $parameters[$key] = array('id' => $key, 'key' => $key, 'value' => $value);
     }
     // perform sort
     $parameters = $sorter->sort($parameters);
     // perform pagination
     $total = count($parameters);
     $parameters = array_slice($parameters, $query->getOffset(), $query->getLimit(100));
     // return
     return $parameters;
 }
Пример #4
0
 /**
  * Gets the resources for the provided query
  * @param \ride\library\http\jsonapi\JsonApiQuery $query
  * @param integer $total Total number of entries before pagination
  * @return mixed Array with resource data or false when an error occured
  */
 protected function getResources(JsonApiQuery $query, &$total)
 {
     $locales = $this->i18n->getLocales();
     $codeQuery = null;
     $nameQuery = null;
     $filters = $query->getFilters();
     foreach ($filters as $filterName => $filterValue) {
         switch ($filterName) {
             case 'code':
                 $codeQuery = $filterValue;
                 break;
             case 'name':
                 $nameQuery = $filterValue;
                 break;
             default:
                 $this->addFilterNotFoundError($this->type, $filterName);
                 break;
         }
     }
     // perform filter
     if ($codeQuery || $nameQuery) {
         foreach ($locales as $index => $locale) {
             if ($codeQuery && $this->filterStringValue($codeQuery, $locale->getCode()) === false) {
                 unset($locales[$index]);
             } elseif ($nameQuery && $this->filterStringValue($nameQuery, $locale->getName()) === false) {
                 unset($locales[$index]);
             }
         }
     }
     $sorter = $this->createSorter($this->type, array('code', 'name'));
     if ($this->document->getErrors()) {
         return false;
     }
     // perform sort
     $locales = $sorter->sort($locales);
     // perform pagination
     $total = count($locales);
     $locales = array_slice($locales, $query->getOffset(), $query->getLimit(100));
     // return
     return $locales;
 }
 /**
  * @dataProvider providerGetLimitThrowsExceptionWhenInvalidLimitParameterProvided
  * @expectedException ride\library\http\jsonapi\exception\BadRequestJsonApiException
  */
 public function testGetLimitThrowsExceptionWhenInvalidLimitParameterProvided($limit, $default = null, $maximum = null)
 {
     $query = new JsonApiQuery(array(JsonApiQuery::PARAMETER_PAGE => array(JsonApiQuery::PARAMETER_LIMIT => $limit)));
     $query->getLimit($default, $maximum);
 }
 /**
  * Gets the resources for the provided query
  * @param \ride\library\http\jsonapi\JsonApiQuery $query
  * @param integer $total Total number of entries before pagination
  * @return mixed Array with resource data or false when an error occured
  */
 protected function getResources(JsonApiQuery $query, &$total)
 {
     $locales = null;
     $translations = array();
     $keyQuery = null;
     $valueQuery = null;
     $filters = $query->getFilters();
     foreach ($filters as $filterName => $filterValue) {
         switch ($filterName) {
             case 'locale':
                 $locales = $filterValue;
                 if ($locales && !is_array($locales)) {
                     $locales = explode(',', $locales);
                 }
                 break;
             case 'key':
                 $keyQuery = $filterValue;
                 break;
             case 'value':
                 $valueQuery = $filterValue;
                 break;
             default:
                 $this->addFilterNotFoundError($this->type, $filterName);
                 break;
         }
     }
     $sorter = $this->createSorter($this->type, array('locale', 'key', 'value'));
     if ($this->document->getErrors()) {
         return false;
     }
     // lookup the requested locales
     if ($locales) {
         foreach ($locales as $index => $locale) {
             try {
                 $locale = $this->i18n->getLocale($locale);
                 $locales[$index] = $locale;
             } catch (LocaleNotFoundException $exception) {
                 $this->getLog()->logException($exception);
                 unset($locales[$index]);
             }
         }
     } else {
         $locales = $this->i18n->getLocales();
     }
     // lookup the translations for the locales
     foreach ($locales as $locale) {
         $translator = $this->i18n->getTranslator($locale);
         $localeTranslations = $translator->getTranslations();
         foreach ($localeTranslations as $key => $value) {
             if ($keyQuery && $this->filterStringValue($keyQuery, $key) === false) {
                 continue;
             } elseif ($valueQuery && $this->filterStringValue($valueQuery, $value) === false) {
                 continue;
             }
             $id = $locale->getCode() . '-' . $key;
             $translations[$id] = array('id' => $id, 'locale' => $locale, 'key' => $key, 'value' => $value);
         }
     }
     // perform sort
     $translations = $sorter->sort($translations);
     // perform pagination
     $total = count($translations);
     $translations = array_slice($translations, $query->getOffset(), $query->getLimit(100));
     // return
     return $translations;
 }