add merge tune to extract tunes by title

update Tune::merge() and add Tune::equals()
This commit is contained in:
2018-05-11 21:44:46 -04:00
parent d4d3654653
commit 65c56586b9
5 changed files with 29 additions and 9 deletions

View File

@@ -16,13 +16,20 @@ class ExtractTuneFromCollection
return new self($tunes);
}
public function extractTuneByTitle(string $title)
/**
* @param string $title
* @return Tune
*/
public function extractTuneByTitle(string $title) : Tune
{
return $this->tunes
->filter(function (Tune $tune) use ($title) {
return in_array($title, $tune->getTitles());
})
->unique()
->reduce(new Tune([]), function (Tune $carry, Tune $tune) {
return $carry->merge($tune);
})
;
}
}

View File

@@ -9,7 +9,7 @@ class Collection extends BaseCollection
{
/**
* @param $initial
* @param callable $callback
* @param callable($carry, $item) $callback
* @return mixed
*/
public function reduce($initial, callable $callback)

View File

@@ -263,11 +263,9 @@ class Tune implements EntityInterface
*/
public function getSettings()
{
return $this->tuneSettings;
return $this->tuneSettings ?: [];
}
/**
* @param $attribute
* @param $value
@@ -363,8 +361,23 @@ class Tune implements EntityInterface
$this->titles = array_merge($this->getTitles(), $source->getTitles());
$this->authors = array_merge($this->getAuthors(), $source->getAuthors());
$this->composers = array_merge($this->getComposers(), $source->getComposers());
$this->groups = array_merge($this->getGroups(), $source->getGroups());
$this->history = array_merge($this->getHistory(), $source->getHistory());
$this->origins = array_merge($this->getOrigins(), $source->getOrigins());
$this->rhythms = array_merge($this->getRhythms(), $source->getRhythms());
$this->tuneSettings = array_merge($this->getSettings(), $source->getSettings());
return $this;
}
/**
* @param \XaiCorp\AbcParser\Domain\Core\Tune $otherTune
* @return bool
*/
public function equals(Tune $otherTune)
{
//TODO:
return $this->getTitles() === $otherTune->getTitles()
&& $this->getRhythms() === $otherTune->getRhythms()
;
}
}