Clean up complex field hierarchy to improve readability

This commit is contained in:
Thibaud Fabre
2016-10-27 15:22:39 +02:00
parent e1fc53dc64
commit 59befbfecc
3 changed files with 97 additions and 80 deletions

View File

@@ -13,72 +13,12 @@ namespace Alchemy\Phrasea\SearchEngine\Elastic\Mapping;
use Alchemy\Phrasea\SearchEngine\Elastic\FieldMapping; use Alchemy\Phrasea\SearchEngine\Elastic\FieldMapping;
class ComplexFieldMapping extends FieldMapping class ComplexFieldMapping extends ComplexMapping
{ {
/**
* @var FieldMapping[]
*/
private $children = [];
/** public function __construct($name, $type = null)
* @var null|string
*/
private $childKey = 'fields';
public function useAsPropertyContainer()
{ {
$this->childKey = 'properties'; parent::__construct($name, $type ?: FieldMapping::TYPE_OBJECT);
}
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;
} }
/** /**
@@ -86,20 +26,6 @@ class ComplexFieldMapping extends FieldMapping
*/ */
protected function getProperties() protected function getProperties()
{ {
if (! $this->hasChildren()) { return [ 'fields' => parent::getProperties() ];
return [];
}
$properties = [ ];
foreach ($this->children as $name => $child) {
$properties[$name] = $child->toArray();
}
if ($this->childKey) {
return [$this->childKey => $properties];
}
return $properties;
} }
} }

View File

@@ -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;
}
}

View File

@@ -13,13 +13,19 @@ namespace Alchemy\Phrasea\SearchEngine\Elastic\Mapping;
use Alchemy\Phrasea\SearchEngine\Elastic\FieldMapping; use Alchemy\Phrasea\SearchEngine\Elastic\FieldMapping;
class ComplexPropertiesMapping extends ComplexFieldMapping class ComplexPropertiesMapping extends ComplexMapping
{ {
public function __construct($name) public function __construct($name)
{ {
parent::__construct($name, FieldMapping::TYPE_OBJECT); parent::__construct($name, FieldMapping::TYPE_OBJECT);
}
$this->useAsPropertyContainer(); /**
* @return array
*/
public function toArray()
{
return [ 'properties' => parent::getProperties() ];
} }
} }