/** * 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('query', null); if ($query) { $modelQuery->getModel()->applySearch($modelQuery, array('query' => $query)); } }
/** * 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) { $expression = $jsonApiQuery->getFilter('expression', null); if ($expression) { $modelQuery->addCondition($expression); } }
private function search(OrmManager $orm, JsonApiQuery $documentQuery) { $term = $documentQuery->getFilter('term'); if ($term) { $term = '%' . $term . '%'; } $geoLocationModel = $orm->getGeoLocationModel(); $geoLocationLocalizedModel = $orm->getGeoLocationLocalizedModel(); // query localized table if ($term) { $query = $geoLocationLocalizedModel->createQuery(); $query->setFields('{entry}, {name}'); $query->setLimit(100); $query->addCondition('{locale} = %1%', $orm->getLocale()); $query->addCondition('{name} LIKE %1%', $term); $geoLocationLocalizedResult = $query->query('entry'); } // query main table $query = $geoLocationModel->createQuery(); $query->setFields('{id}, {path}, {code}'); $query->setLimit(100); if ($term) { if (!$geoLocationLocalizedResult) { $query->addCondition('{code} LIKE %1%', $term); } else { $query->addCondition('{id} IN %1%', array_keys($geoLocationLocalizedResult)); } } $type = $documentQuery->getFilter('type'); if ($type) { if (is_array($type)) { $query->addCondition('{type} IN %1%', $type); } elseif (strpos($type, ',')) { $query->addCondition('{type} IN %1%', explode(',', $type)); } else { $query->addCondition('{type} = %1%', $type); } } $path = $documentQuery->getFilter('path'); if ($path) { if (strpos($path, '~') == false) { $path = '~' . $path . '~'; } $query->addCondition('{path} LIKE %1%', '%' . $path . '%'); } $geoLocationResult = $query->query(); // foreach ($geoLocationResult as $index => $geoLocation) { // $result[$index] = $index; // } // merge result return $geoLocationResult; }
/** * 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); }
/** * Adds a included resource for a compound document * @param JsonApiResource $resource * @return boolean True when added, false if not */ public function addIncluded(JsonApiResource $resource) { $type = $resource->getType(); $id = $resource->getId(); $relationships = $resource->getRelationships(); if (isset($this->index[$type][$id]) || !$resource->getAttributes() && !$relationships) { return false; } $this->indexResource($resource); if (!isset($this->included[$type])) { $this->included[$type] = array(); } $this->included[$type][$id] = $resource; $relationshipPath = $resource->getRelationshipPath(); foreach ($relationships as $name => $relationship) { $fieldRelationshipPath = ($relationshipPath ? $relationshipPath . '.' : '') . $name; if (!$this->query->isIncluded($fieldRelationshipPath)) { continue; } $data = $relationship->getData(); if (is_array($data)) { foreach ($data as $d) { $this->addIncluded($d); } } elseif ($data) { $this->addIncluded($data); } } return true; }
/** * 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; }
/** * 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; }
public function testGetParameter() { $parameters = array('parameter1' => 'value1', 'parameter2' => 'value2'); $query = new JsonApiQuery($parameters); $this->assertEquals($query->getParameter('parameter1'), 'value1'); $this->assertEquals($query->getParameter('parameter2'), 'value2'); $this->assertEquals($query->getParameter('parameter3'), null); $this->assertEquals($query->getParameter('parameter3', 'default'), 'default'); }
/** * 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; }