mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-17 15:03:25 +00:00
Put flags in ES structure
This commit is contained in:
54
lib/Alchemy/Phrasea/SearchEngine/Elastic/Structure/Flag.php
Normal file
54
lib/Alchemy/Phrasea/SearchEngine/Elastic/Structure/Flag.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Alchemy\Phrasea\SearchEngine\Elastic\Structure;
|
||||
|
||||
use Alchemy\Phrasea\SearchEngine\Elastic\StringUtils;
|
||||
use Assert\Assertion;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class Flag
|
||||
{
|
||||
private $name;
|
||||
|
||||
public static function createFromLegacyStatus(array $status)
|
||||
{
|
||||
if (!isset($status['labelon'])) {
|
||||
throw new InvalidArgumentException('Status array must contain the "labelon" key.');
|
||||
}
|
||||
return new self(self::normalizeName($status['labelon']));
|
||||
}
|
||||
|
||||
public function __construct($name)
|
||||
{
|
||||
Assertion::string($name);
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getIndexField()
|
||||
{
|
||||
return sprintf('flags.%s', $this->name);
|
||||
}
|
||||
|
||||
public static function normalizeName($key)
|
||||
{
|
||||
return StringUtils::slugify($key, '_');
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO: Rewrite to have all data injected at construct time in createFromLegacyStatus()
|
||||
*/
|
||||
public function getBitPositionInDatabox(\databox $databox)
|
||||
{
|
||||
foreach ($databox->getStatusStructure() as $bit => $status) {
|
||||
$candidate_name = self::normalizeName($status['labelon']);
|
||||
if ($candidate_name === $this->name) {
|
||||
return (int) $status['bit'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -3,6 +3,7 @@
|
||||
namespace Alchemy\Phrasea\SearchEngine\Elastic\Structure;
|
||||
|
||||
use Alchemy\Phrasea\SearchEngine\Elastic\Mapping;
|
||||
use Assert\Assertion;
|
||||
use DomainException;
|
||||
|
||||
final class GlobalStructure implements Structure
|
||||
@@ -17,6 +18,7 @@ final class GlobalStructure implements Structure
|
||||
private $private = array();
|
||||
/** @var Field[] */
|
||||
private $facets = array();
|
||||
private $flags = array();
|
||||
|
||||
/**
|
||||
* @param \databox[] $databoxes
|
||||
@@ -24,17 +26,32 @@ final class GlobalStructure implements Structure
|
||||
*/
|
||||
public static function createFromDataboxes(array $databoxes)
|
||||
{
|
||||
$structure = new self();
|
||||
$fields = [];
|
||||
$flags = [];
|
||||
foreach ($databoxes as $databox) {
|
||||
foreach ($databox->get_meta_structure() as $fieldStructure) {
|
||||
$field = Field::createFromLegacyField($fieldStructure);
|
||||
$structure->add($field);
|
||||
$fields[] = Field::createFromLegacyField($fieldStructure);
|
||||
}
|
||||
foreach ($databox->getStatusStructure() as $status) {
|
||||
$flags[] = Flag::createFromLegacyStatus($status);
|
||||
}
|
||||
}
|
||||
return $structure;
|
||||
return new self($fields, $flags);
|
||||
}
|
||||
|
||||
public function add(Field $field)
|
||||
public function __construct(array $fields, array $flags)
|
||||
{
|
||||
Assertion::allIsInstanceOf($fields, Field::class);
|
||||
Assertion::allIsInstanceOf($flags, Flag::class);
|
||||
foreach ($fields as $field) {
|
||||
$this->add($field);
|
||||
}
|
||||
foreach ($flags as $flag) {
|
||||
$this->flags[$flag->getName()] = $flag;
|
||||
}
|
||||
}
|
||||
|
||||
private function add(Field $field)
|
||||
{
|
||||
$name = $field->getName();
|
||||
if (isset($this->fields[$name])) {
|
||||
@@ -120,6 +137,17 @@ final class GlobalStructure implements Structure
|
||||
throw new DomainException(sprintf('Unknown field "%s".', $name));
|
||||
}
|
||||
|
||||
public function getAllFlags()
|
||||
{
|
||||
return $this->flags;
|
||||
}
|
||||
|
||||
public function getFlagByName($name)
|
||||
{
|
||||
return isset($this->flags[$name]) ?
|
||||
$this->flags[$name] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of collections indexed by field name.
|
||||
*
|
||||
|
@@ -75,6 +75,16 @@ final class LimitedStructure implements Structure
|
||||
return $this->structure->isPrivate($name);
|
||||
}
|
||||
|
||||
public function getAllFlags()
|
||||
{
|
||||
return $this->structure->getAllFlags();
|
||||
}
|
||||
|
||||
public function getFlagByName($name)
|
||||
{
|
||||
return $this->structure->getFlagByName($name);
|
||||
}
|
||||
|
||||
private function limit(array $fields)
|
||||
{
|
||||
$allowed_collections = $this->allowedCollections();
|
||||
|
@@ -24,4 +24,7 @@ interface Structure
|
||||
* @throws \DomainException
|
||||
*/
|
||||
public function isPrivate($name);
|
||||
|
||||
public function getAllFlags();
|
||||
public function getFlagByName($name);
|
||||
}
|
||||
|
Reference in New Issue
Block a user