diff --git a/lib/Alchemy/Phrasea/Databox/SubdefGroup.php b/lib/Alchemy/Phrasea/Databox/SubdefGroup.php new file mode 100644 index 0000000000..0cc82d7f51 --- /dev/null +++ b/lib/Alchemy/Phrasea/Databox/SubdefGroup.php @@ -0,0 +1,115 @@ +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); + } +} diff --git a/lib/Alchemy/Phrasea/Media/MediaTypeFactory.php b/lib/Alchemy/Phrasea/Media/MediaTypeFactory.php new file mode 100644 index 0000000000..33a351ce5f --- /dev/null +++ b/lib/Alchemy/Phrasea/Media/MediaTypeFactory.php @@ -0,0 +1,37 @@ +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)