mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-18 07:23:13 +00:00
Clean up complex field hierarchy to improve readability
This commit is contained in:
@@ -13,72 +13,12 @@ namespace Alchemy\Phrasea\SearchEngine\Elastic\Mapping;
|
||||
|
||||
use Alchemy\Phrasea\SearchEngine\Elastic\FieldMapping;
|
||||
|
||||
class ComplexFieldMapping extends FieldMapping
|
||||
class ComplexFieldMapping extends ComplexMapping
|
||||
{
|
||||
/**
|
||||
* @var FieldMapping[]
|
||||
*/
|
||||
private $children = [];
|
||||
|
||||
/**
|
||||
* @var null|string
|
||||
*/
|
||||
private $childKey = 'fields';
|
||||
|
||||
public function useAsPropertyContainer()
|
||||
public function __construct($name, $type = null)
|
||||
{
|
||||
$this->childKey = 'properties';
|
||||
}
|
||||
|
||||
public function useAsFieldContainer()
|
||||
{
|
||||
$this->childKey = 'fields';
|
||||
}
|
||||
|
||||
public function useAsBareContainer()
|
||||
{
|
||||
$this->childKey = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FieldMapping $child
|
||||
* @return FieldMapping
|
||||
*/
|
||||
public function addChild(FieldMapping $child)
|
||||
{
|
||||
if (isset($this->children[$child->getName()])) {
|
||||
throw new \LogicException(sprintf('There is already a "%s" multi field.', $child->getName()));
|
||||
}
|
||||
|
||||
if ($child->getType() !== $this->getType() && $this->getType() !== self::TYPE_OBJECT) {
|
||||
throw new \LogicException('Child field type must match parent type.');
|
||||
}
|
||||
|
||||
return $this->children[$child->getName()] = $child;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RawFieldMapping
|
||||
*/
|
||||
public function addRawChild()
|
||||
{
|
||||
return $this->addChild(new RawFieldMapping($this->getType()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasChildren()
|
||||
{
|
||||
return ! empty($this->children);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return FieldMapping[]
|
||||
*/
|
||||
public function getChildren()
|
||||
{
|
||||
return $this->children;
|
||||
parent::__construct($name, $type ?: FieldMapping::TYPE_OBJECT);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,20 +26,6 @@ class ComplexFieldMapping extends FieldMapping
|
||||
*/
|
||||
protected function getProperties()
|
||||
{
|
||||
if (! $this->hasChildren()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$properties = [ ];
|
||||
|
||||
foreach ($this->children as $name => $child) {
|
||||
$properties[$name] = $child->toArray();
|
||||
}
|
||||
|
||||
if ($this->childKey) {
|
||||
return [$this->childKey => $properties];
|
||||
}
|
||||
|
||||
return $properties;
|
||||
return [ 'fields' => parent::getProperties() ];
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of phrasea-4.0.
|
||||
*
|
||||
* (c) Alchemy <info@alchemy.fr>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Alchemy\Phrasea\SearchEngine\Elastic\Mapping;
|
||||
|
||||
use Alchemy\Phrasea\SearchEngine\Elastic\FieldMapping;
|
||||
|
||||
class ComplexMapping extends FieldMapping
|
||||
{
|
||||
/**
|
||||
* @var FieldMapping[]
|
||||
*/
|
||||
private $children = [];
|
||||
|
||||
/**
|
||||
* @param FieldMapping $child
|
||||
* @return FieldMapping
|
||||
*/
|
||||
public function addChild(FieldMapping $child)
|
||||
{
|
||||
if (isset($this->children[$child->getName()])) {
|
||||
throw new \LogicException(sprintf('There is already a "%s" multi field.', $child->getName()));
|
||||
}
|
||||
|
||||
if ($child->getType() !== $this->getType() && $this->getType() !== self::TYPE_OBJECT) {
|
||||
throw new \LogicException('Child field type must match parent type.');
|
||||
}
|
||||
|
||||
return $this->children[$child->getName()] = $child;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RawFieldMapping
|
||||
*/
|
||||
public function addRawChild()
|
||||
{
|
||||
return $this->addChild(new RawFieldMapping($this->getType()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasChildren()
|
||||
{
|
||||
return ! empty($this->children);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return FieldMapping[]
|
||||
*/
|
||||
public function getChildren()
|
||||
{
|
||||
return $this->children;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getProperties()
|
||||
{
|
||||
if (! $this->hasChildren()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$properties = [ ];
|
||||
|
||||
foreach ($this->children as $name => $child) {
|
||||
$properties[$name] = $child->toArray();
|
||||
}
|
||||
|
||||
if ($this->childKey) {
|
||||
return [$this->childKey => $properties];
|
||||
}
|
||||
|
||||
return $properties;
|
||||
}
|
||||
}
|
@@ -13,13 +13,19 @@ namespace Alchemy\Phrasea\SearchEngine\Elastic\Mapping;
|
||||
|
||||
use Alchemy\Phrasea\SearchEngine\Elastic\FieldMapping;
|
||||
|
||||
class ComplexPropertiesMapping extends ComplexFieldMapping
|
||||
class ComplexPropertiesMapping extends ComplexMapping
|
||||
{
|
||||
|
||||
public function __construct($name)
|
||||
{
|
||||
parent::__construct($name, FieldMapping::TYPE_OBJECT);
|
||||
}
|
||||
|
||||
$this->useAsPropertyContainer();
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return [ 'properties' => parent::getProperties() ];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user