add merge tune to extract tunes by title
update Tune::merge() and add Tune::equals()
This commit is contained in:
@@ -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);
|
||||
})
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,8 @@ class ExtractTuneFromCollectionTest extends \Codeception\Test\Unit
|
||||
|
||||
$result = $extractor->extractTuneByTitle($title);
|
||||
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertNotEquals($tunes, $result);
|
||||
foreach ($tunes as $tune) {
|
||||
$this->assertNotEquals($tune, $result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,7 +212,6 @@ class TuneTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$result = $targetTune->merge($sourceTune);
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$this->assertTrue($expected->equals($result));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user