Extract SubdefGroup class

This commit is contained in:
Benoît Burnichon
2016-02-24 13:52:33 +01:00
parent 06f12cabce
commit 2b097e9be7
3 changed files with 199 additions and 66 deletions

View File

@@ -0,0 +1,115 @@
<?php
/**
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Databox;
use Alchemy\Phrasea\Media\Type\Type;
class SubdefGroup implements \IteratorAggregate, \Countable
{
private $name;
/**
* @var Type
*/
private $type;
private $isDocumentOrderable;
/**
* @var \databox_subdef[]
*/
private $subdefs = [];
/**
* @param string $name
* @param Type $type
* @param bool $isDocumentOrderable
*/
public function __construct($name, Type $type, $isDocumentOrderable)
{
$this->name = $name;
$this->type = $type;
$this->isDocumentOrderable = $isDocumentOrderable;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return Type
*/
public function getType()
{
return $this->type;
}
/**
* @return bool
*/
public function isDocumentOrderable()
{
return $this->isDocumentOrderable;
}
public function addSubdef(\databox_subdef $subdef)
{
$this->subdefs[$subdef->get_name()] = $subdef;
}
/**
* @param string|\databox_subdef $subdef
* @return bool
*/
public function hasSubdef($subdef)
{
$subdefName = $subdef instanceof \databox_subdef ? $subdef->get_name() : $subdef;
return isset($this->subdefs[$subdefName]);
}
/**
* @param string $subdefName
* @return \databox_subdef
*/
public function getSubdef($subdefName)
{
if (!isset($this->subdefs[$subdefName])) {
throw new \RuntimeException('Requested subdef was not found');
}
return $this->subdefs[$subdefName];
}
/**
* @param string|\databox_subdef $subdef
*/
public function removeSubdef($subdef)
{
$subdefName = $subdef instanceof \databox_subdef ? $subdef->get_name() : $subdef;
unset($this->subdefs[$subdefName]);
}
public function getIterator()
{
return new \ArrayIterator($this->subdefs);
}
public function count()
{
return count($this->subdefs);
}
}

View File

@@ -0,0 +1,37 @@
<?php
/**
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Media;
class MediaTypeFactory
{
/**
* @param string $mediaType
* @return Type\Type
*/
public function createMediaType($mediaType)
{
switch (strtolower($mediaType))
{
case Type\Type::TYPE_AUDIO:
return new Type\Audio();
case Type\Type::TYPE_IMAGE:
return new Type\Image();
case Type\Type::TYPE_VIDEO:
return new Type\Video();
case Type\Type::TYPE_DOCUMENT:
return new Type\Document();
case Type\Type::TYPE_FLASH:
return new Type\Flash();
}
throw new \RuntimeException('Could not create requested media type');
}
}

View File

@@ -8,14 +8,16 @@
* file that was distributed with this source code.
*/
use Alchemy\Phrasea\Databox\SubdefGroup;
use Alchemy\Phrasea\Media\MediaTypeFactory;
use Symfony\Component\Translation\TranslatorInterface;
class databox_subdefsStructure implements IteratorAggregate, Countable
{
/**
* @var array|databox_subdef[][]
* @var SubdefGroup[]
*/
protected $AvSubdefs = [];
protected $subdefGroups = [];
/**
* @var TranslatorInterface
@@ -33,22 +35,20 @@ class databox_subdefsStructure implements IteratorAggregate, Countable
*/
public function getIterator()
{
return new ArrayIterator($this->AvSubdefs);
return new ArrayIterator($this->subdefGroups);
}
public function count()
{
$n = 0;
foreach ($this->AvSubdefs as $subdefs) {
foreach ($this->subdefGroups as $subdefs) {
$n += count($subdefs);
}
return $n;
}
/**
* @param databox $databox
*/
public function __construct(databox $databox, TranslatorInterface $translator)
{
$this->databox = $databox;
@@ -59,16 +59,14 @@ class databox_subdefsStructure implements IteratorAggregate, Countable
/**
* @param string $searchGroup
* @return databox_subdef[]
* @return SubdefGroup|databox_subdef[]
*/
public function getSubdefGroup($searchGroup)
{
$searchGroup = strtolower($searchGroup);
foreach ($this->AvSubdefs as $groupname => $subdefgroup) {
if ($searchGroup == $groupname) {
return $subdefgroup;
}
if (isset($this->subdefGroups[$searchGroup])) {
return $this->subdefGroups[$searchGroup];
}
return null;
@@ -78,55 +76,36 @@ class databox_subdefsStructure implements IteratorAggregate, Countable
{
$sx_struct = $this->databox->get_sxml_structure();
$avSubdefs = [
'image' => [],
'video' => [],
'audio' => [],
'document' => [],
'flash' => []
];
if (! $sx_struct) {
return;
}
$subdefgroup = $sx_struct->subdefs[0];
$mediaTypeFactory = new MediaTypeFactory();
foreach ($subdefgroup as $k => $subdefs) {
$subdefgroup_name = strtolower($subdefs->attributes()->name);
$isDocumentOrderable = isset($subdefs->attributes()->document_orderable)
? p4field::isyes($subdefs->attributes()->document_orderable) : true;
if ( ! isset($avSubdefs[$subdefgroup_name])) {
$avSubdefs[$subdefgroup_name] = [];
}
foreach ($subdefs as $sd) {
$subdef_name = strtolower($sd->attributes()->name);
$type = null;
switch ($subdefgroup_name) {
case 'audio':
$type = new \Alchemy\Phrasea\Media\Type\Audio();
break;
case 'image':
$type = new \Alchemy\Phrasea\Media\Type\Image();
break;
case 'video':
$type = new \Alchemy\Phrasea\Media\Type\Video();
break;
case 'document':
$type = new \Alchemy\Phrasea\Media\Type\Document();
break;
case 'flash':
$type = new \Alchemy\Phrasea\Media\Type\Flash();
break;
default:
continue;
if (! isset($this->subdefGroups[$subdefgroup_name])) {
try {
$type = $mediaTypeFactory->createMediaType($subdefgroup_name);
} catch (RuntimeException $exception) {
// Skip undefined media type group
continue;
}
$avSubdefs[$subdefgroup_name][$subdef_name] = new databox_subdef($type, $sd, $this->translator);
$this->subdefGroups[$subdefgroup_name] = new SubdefGroup($subdefgroup_name, $type, $isDocumentOrderable);
}
$group = $this->getSubdefGroup($subdefgroup_name);
foreach ($subdefs as $sd) {
$group->addSubdef(new databox_subdef($group->getType(), $sd, $this->translator));
}
}
$this->AvSubdefs = $avSubdefs;
}
/**
@@ -137,11 +116,10 @@ class databox_subdefsStructure implements IteratorAggregate, Countable
*/
public function hasSubdef($subdef_type, $subdef_name)
{
$type = strtolower($subdef_type);
$name = strtolower($subdef_name);
$group = $this->getSubdefGroup(strtolower($subdef_type));
if (isset($this->AvSubdefs[$type][$name])) {
return true;
if ($group) {
return $group->hasSubdef(strtolower($subdef_name));
}
return false;
@@ -159,22 +137,26 @@ class databox_subdefsStructure implements IteratorAggregate, Countable
$type = strtolower($subdef_type);
$name = strtolower($subdef_name);
if (isset($this->AvSubdefs[$type]) && isset($this->AvSubdefs[$type][$name])) {
return $this->AvSubdefs[$type][$name];
$group = $this->getSubdefGroup(strtolower($subdef_type));
if (!$group) {
throw new Exception_Databox_SubdefNotFound(sprintf('Databox subdef name `%s` of type `%s` not found', $name, $type));
}
throw new Exception_Databox_SubdefNotFound(sprintf('Databox subdef name `%s` of type `%s` not found', $name, $type));
try {
return $group->getSubdef($name);
} catch (RuntimeException $exception) {
throw new Exception_Databox_SubdefNotFound(sprintf('Databox subdef name `%s` of type `%s` not found', $name, $type), $exception);
}
}
/**
*
* @param string $group
* @param string $name
* @return databox_subdefsStructure
* @param string $group
* @param string $name
* @return self
*/
public function delete_subdef($group, $name)
{
$dom_struct = $this->databox->get_dom_structure();
$dom_xp = $this->databox->get_xpath_structure();
$nodes = $dom_xp->query(
@@ -189,8 +171,8 @@ class databox_subdefsStructure implements IteratorAggregate, Countable
$parent->removeChild($node);
}
if (isset($AvSubdefs[$group]) && isset($AvSubdefs[$group][$name])) {
unset($AvSubdefs[$group][$name]);
if ($this->hasSubdef($group, $name)) {
$this->getSubdefGroup($group)->removeSubdef($name);
}
$this->databox->saveStructure($dom_struct);
@@ -199,10 +181,9 @@ class databox_subdefsStructure implements IteratorAggregate, Countable
}
/**
*
* @param string $groupname
* @param string $name
* @param string $class
* @param string $groupname
* @param string $name
* @param string $class
* @return databox_subdefsStructure
*/
public function add_subdef($groupname, $name, $class)