mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
handle types and categories in a single select input
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user