start to add ability to merge tunes
started refactoring tune builders to use string type hint instead of StringAtom
This commit is contained in:
80
src/Domain/Core/MultivalueAttribute.php
Normal file
80
src/Domain/Core/MultivalueAttribute.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace XaiCorp\AbcParser\Domain\Core;
|
||||
|
||||
class MultivalueAttribute
|
||||
{
|
||||
/**
|
||||
* @var \XaiCorp\AbcParser\Domain\Core\Collection
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
$this->data = new Collection();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $item
|
||||
* @return \XaiCorp\AbcParser\Domain\Core\MultivalueAttribute
|
||||
*/
|
||||
public static function create(string $item)
|
||||
{
|
||||
return (new self())->add($item);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $items
|
||||
* @return \XaiCorp\AbcParser\Domain\Core\MultivalueAttribute
|
||||
*/
|
||||
public static function createFromArray(array $items)
|
||||
{
|
||||
$attribute = new self();
|
||||
|
||||
foreach ($items as $item) {
|
||||
$attribute->add($item);
|
||||
}
|
||||
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $item
|
||||
* @return static
|
||||
*/
|
||||
public function add(string $item)
|
||||
{
|
||||
$this->data = $this->data->push($item)->unique();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MultivalueAttribute $attribute
|
||||
* @return static
|
||||
*/
|
||||
public function merge(MultivalueAttribute $attribute)
|
||||
{
|
||||
$this->data = $this->data
|
||||
->pushArray($attribute->data->toArray())
|
||||
->unique();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function first()
|
||||
{
|
||||
return $this->data->firstOrDefault('');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return $this->data->toArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user