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); return new self($tunes);
} }
public function extractTuneByTitle(string $title) /**
* @param string $title
* @return Tune
*/
public function extractTuneByTitle(string $title) : Tune
{ {
return $this->tunes return $this->tunes
->filter(function (Tune $tune) use ($title) { ->filter(function (Tune $tune) use ($title) {
return in_array($title, $tune->getTitles()); return in_array($title, $tune->getTitles());
}) })
->unique() ->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 $initial
* @param callable $callback * @param callable($carry, $item) $callback
* @return mixed * @return mixed
*/ */
public function reduce($initial, callable $callback) public function reduce($initial, callable $callback)

View File

@@ -263,11 +263,9 @@ class Tune implements EntityInterface
*/ */
public function getSettings() public function getSettings()
{ {
return $this->tuneSettings; return $this->tuneSettings ?: [];
} }
/** /**
* @param $attribute * @param $attribute
* @param $value * @param $value
@@ -363,8 +361,23 @@ class Tune implements EntityInterface
$this->titles = array_merge($this->getTitles(), $source->getTitles()); $this->titles = array_merge($this->getTitles(), $source->getTitles());
$this->authors = array_merge($this->getAuthors(), $source->getAuthors()); $this->authors = array_merge($this->getAuthors(), $source->getAuthors());
$this->composers = array_merge($this->getComposers(), $source->getComposers()); $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->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()); $this->tuneSettings = array_merge($this->getSettings(), $source->getSettings());
return $this; 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()
;
}
} }

View File

@@ -35,7 +35,8 @@ class ExtractTuneFromCollectionTest extends \Codeception\Test\Unit
$result = $extractor->extractTuneByTitle($title); $result = $extractor->extractTuneByTitle($title);
$this->assertCount(1, $result); foreach ($tunes as $tune) {
$this->assertNotEquals($tunes, $result); $this->assertNotEquals($tune, $result);
}
} }
} }

View File

@@ -212,7 +212,6 @@ class TuneTest extends \PHPUnit_Framework_TestCase
$result = $targetTune->merge($sourceTune); $result = $targetTune->merge($sourceTune);
$this->assertEquals($expected, $result); $this->assertTrue($expected->equals($result));
} }
} }