Files
abcParser/src/Domain/Core/Tune.php

371 lines
7.0 KiB
PHP

<?php
namespace XaiCorp\AbcParser\Domain\Core;
use Enzyme\Axiom\Atoms\IntegerAtom;
use Enzyme\Axiom\Atoms\StringAtom;
use Webpatser\Uuid\Uuid;
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
class Tune implements EntityInterface
{
use IdentityTrait;
/**
* @var Int
*/
protected $index = 1;
/**
* @var array
*/
protected $titles = [];
/**
* @var array
*/
protected $authors = [];
/**
* @var array
*/
protected $composers = [];
/**
* @var array
*/
protected $groups = [];
/**
* multiline history
* @var array
*/
protected $history = [];
/**
* @var array
*/
protected $origins = [];
/**
* @var array
*/
protected $rhythms = [];
/**
* @var Setting[]
*/
protected $tuneSettings;
/**
* @var string
*/
protected $music = '';
// public static $propertyNames = [
// 'X' => 'id',
// 'T' => 'Title',
// 'A' => 'Author',
// 'C' => 'Composer',
// 'G' => 'Group,',
// 'H' => 'History',
// 'O' => 'Origin',
// 'R' => 'Rhythm',
// ];
public function __construct(array $params, Uuid $identity = null)
{
$this->setIdentity($identity);
foreach ($params as $attribute => $value) {
$this->set($attribute, $value);
}
}
public function getETag()
{
return md5(implode(';', array_flatten([
$this->getTitles(),
$this->getAuthors(),
$this->getComposers(),
$this->getGroups(),
$this->getHistory(),
$this->getOrigins(),
$this->getRhythms(),
//TODO: add settings
])));
}
/**
* @param UnsignedIntegerAtom $index
* @return $this
*/
public function setIndex(UnsignedIntegerAtom $index) : Tune
{
$this->index = $index->getValue();
return $this;
}
/**
* @return Int
*/
public function getIndex() : int
{
return $this->index;
}
/**
* @return string[]
*/
public function getTitles() : array
{
return $this->titles;
}
/**
* @param string $title
* @return $this
* @deprecated
*/
public function addTitle(string $title) : Tune
{
$this->titles[] = $title;
return $this;
}
/**
* @param Author $author
* @return $this
*/
public function addAuthor(Author $author) : Tune
{
$this->authors[] = $author;
return $this;
}
/**
* @return Author[]
*/
public function getAuthors() : array //TODO: PersonCollection
{
return $this->authors;
}
/**
* @param Composer $composer
* @return $this
*/
public function addComposer(Composer $composer) : Tune
{
$this->composers[] = $composer;
return $this;
}
/**
* @return Composer[]
*/
public function getComposers()
{
return $this->composers;
}
/**
* @param string $group
* @return $this
*/
public function addGroup(string $group) : Tune
{
$this->groups[] = $group;
return $this;
}
/**
* @return array
*/
public function getGroups()
{
return $this->groups;
}
/**
* @param string $historyLine
* @return $this
*/
public function addHistoryLine(string $historyLine)
{
$this->history[] = $historyLine;
return $this;
}
/**
* @return array
*/
public function getHistory()
{
return $this->history;
}
/**
* @param string $origin
* @return $this
*/
public function addOrigin(string $origin)
{
$this->origins[] = $origin;
return $this;
}
/**
* @return array
*/
public function getOrigins()
{
return $this->origins;
}
/**
* @param StringAtom $rhythm
* @return $this
*/
public function addRhythm(string $rhythm)
{
$this->rhythms[] = $rhythm;
return $this;
}
/**
* @return array
*/
public function getRhythms()
{
return $this->rhythms;
}
/**
* @param Setting $tuneSetting
* @return $this
*/
public function addSetting(Setting $tuneSetting)
{
$this->tuneSettings[] = $tuneSetting;
return $this;
}
/**
* @return Setting[]
*/
public function getSettings()
{
return $this->tuneSettings;
}
/**
* @param $attribute
* @param $value
*/
protected function set($attribute, $value)
{
switch ($attribute) {
case 'index':
call_user_func([$this, 'set'.$attribute], $value);
break;
case 'Authors':
call_user_func([$this, 'addAuthor'], $value);
break;
case 'Composers':
$this->addComposer($value);
break;
case 'settings':
foreach ($value as $key => $settingParams) {
$this->addSetting(Setting::create($settingParams));
}
break;
default:
$this->append($attribute, $value);
}
}
protected function append($attribute, MultivalueAttribute $values)
{
$method = 'add' . substr($attribute, 0, -1);
foreach ($values->toArray() as $value) {
call_user_func([$this, $method], $value);
}
}
/**
* Checks whether this model has the given attribute set.
*
* @param string $attribute
*
* @return boolean
*/
public function has($attribute)
{
return isset($this->{$attribute});
}
/**
* Get the value associated with the given attribute.
*
* @param string $attribute
*
* @return mixed
*/
public function get($attribute)
{
// TODO: Implement get() method.
}
/**
* Get all the attributes associated with this model.
*
* @return array
*/
public function getAll()
{
// TODO: Implement getAll() method.
}
/**
* @param StringAtom $musicLine
* @return $this
* @deprecated this should be on the setting
*/
public function addMusicLine(string $musicLine)
{
$this->music .= $musicLine . PHP_EOL;
return $this;
}
public function getMusic()
{
return $this->music;
}
public function merge(Tune $source)
{
//TODO
$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->history = array_merge($this->getHistory(), $source->getHistory());
$this->tuneSettings = array_merge($this->getSettings(), $source->getSettings());
return $this;
}
}