structure = $structure; $this->locales = $locales; $this->queryLocale = $queryLocale; $this->fields = $fields; } public function narrowToFields(array $fields) { if (is_array($this->fields)) { // Ensure we are not escaping from original fields restrictions $fields = array_intersect($this->fields, $fields); if (!$fields) { throw new QueryException('Query narrowed to non available fields'); } } return new static($this->structure, $this->locales, $this->queryLocale, $fields); } /** * @return Field[] */ public function getUnrestrictedFields() { // TODO Restore search optimization by using "caption_all" field // (only when $this->fields is null) return $this->filterFields($this->structure->getUnrestrictedFields()); } public function getPrivateFields() { return $this->filterFields($this->structure->getPrivateFields()); } public function getHighlightedFields() { return $this->filterFields($this->structure->getAllFields()); } /** * @param Field[] $fields * @return Field[] */ private function filterFields(array $fields) { if ($this->fields !== null) { $fields = array_intersect_key($fields, array_flip($this->fields)); } return array_values($fields); } public function get($name) { if ($name instanceof ASTField) { $name = $name->getValue(); } return $this->structure->get($name); } public function getFlag($name) { if ($name instanceof Flag) { $name = $name->getName(); } return $this->structure->getFlagByName($name); } public function getMetadataTag($name) { return $this->structure->getMetadataTagByName($name); } /** * @todo Maybe we should put this logic in Field class? */ public function localizeField(Field $field) { $index_field = $field->getIndexField(); if ($field->getType() === FieldMapping::TYPE_STRING) { return $this->localizeFieldName($index_field); } else { return [$index_field]; } } private function localizeFieldName($field) { $fields = array(); foreach ($this->locales as $locale) { $boost = ($locale === $this->queryLocale) ? '^5' : ''; $fields[] = sprintf('%s.%s%s', $field, $locale, $boost); } // TODO Put generic analyzers on main field instead of "light" sub-field $fields[] = sprintf('%s.light^10', $field); return $fields; } public function getFields() { return $this->fields; } }