handle types and categories in a single select input

This commit is contained in:
2021-10-12 15:52:06 +02:00
parent 9eec15873e
commit 88d073b9a9
10 changed files with 323 additions and 78 deletions

View File

@@ -368,7 +368,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
public function setTypes(array $type = null)
{
// remove all keys from the input data
$this->type = \array_values($type);
$this->types = \array_values($type);
foreach ($this->children as $child) {
$child->setTypes($type);
@@ -387,6 +387,40 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
return $this->types;
}
public function addType(?string $type): self
{
if (NULL === $type) {
return $this;
}
if (!\in_array($type, $this->types ?? [])) {
$this->types[] = $type;
}
foreach ($this->children as $child) {
$child->addType($type);
}
return $this;
}
public function removeType(?string $type): self
{
if (NULL === $type) {
return $this;
}
if (\in_array($type, $this->types ?? [])) {
$this->types = \array_filter($this->types, fn($e) => !\in_array($e, $this->types));
}
foreach ($this->children as $child) {
$child->removeType($type);
}
return $this;
}
/**
* @return bool
*/
@@ -541,7 +575,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
}
foreach ($this->children as $child) {
$child->addCategory($child);
$child->addCategory($category);
}
return $this;
@@ -556,7 +590,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
$this->categories->removeElement($category);
foreach ($this->children as $child) {
$child->removeCategory($child);
$child->removeCategory($category);
}
return $this;
@@ -631,6 +665,72 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
return $this;
}
public function addTypesAndCategories($typeAndCategory): self
{
if ($typeAndCategory instanceof ThirdPartyCategory) {
$this->addCategory($typeAndCategory);
return $this;
}
if (is_string($typeAndCategory)) {
$this->addType($typeAndCategory);
return $this;
}
throw new \UnexpectedValueException(sprintf(
"typeAndCategory should be a string or a %s", ThirdPartyCategory::class));
}
public function removeTypesAndCategories($typeAndCategory): self
{
if ($typeAndCategory instanceof ThirdPartyCategory) {
$this->removeCategory($typeAndCategory);
return $this;
}
if (is_string($typeAndCategory)) {
$this->removeType($typeAndCategory);
return $this;
}
throw new \UnexpectedValueException(sprintf(
"typeAndCategory should be a string or a %s", ThirdPartyCategory::class));
}
public function getTypesAndCategories(): array
{
return \array_merge(
$this->getCategories()->toArray(),
$this->getTypes() ?? []
);
}
public function setTypesAndCategories(array $typesAndCategories): self
{
$types = \array_filter($typesAndCategories, fn($item) => !$item instanceof ThirdPartyCategory);
$this->setTypes($types);
// handle categories
foreach ($typesAndCategories as $t) {
$this->addTypesAndCategories($t);
}
$categories = \array_filter($typesAndCategories, fn($item) => $item instanceof ThirdPartyCategory);
$categoriesHashes = \array_map(fn(ThirdPartyCategory $c) => \spl_object_hash($c), $categories);
foreach ($categories as $c) {
$this->addCategory($c);
}
foreach ($this->getCategories() as $t) {
if (!\in_array(\spl_object_hash($t), $categoriesHashes)) {
$this->removeCategory($t);
}
}
return $this;
}
/**
* @param ThirdParty $child