finished default parser
This commit is contained in:
38
src/Domain/Atoms/EmailAtom.php
Normal file
38
src/Domain/Atoms/EmailAtom.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Atoms;
|
||||
|
||||
use Enzyme\Axiom\Atoms\AtomInterface;
|
||||
|
||||
class EmailAtom implements AtomInterface
|
||||
{
|
||||
protected $value;
|
||||
|
||||
public function __construct($email)
|
||||
{
|
||||
$this->validateEmail($email);
|
||||
$this->value = $email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the underlying value associated with this atom.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
private function validateEmail($value)
|
||||
{
|
||||
if (!$this->isValid($value)) {
|
||||
throw new \Exception('Not a valid email');
|
||||
}
|
||||
}
|
||||
|
||||
private function isValid($value)
|
||||
{
|
||||
//TODO:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
12
src/Domain/Atoms/UnsignedIntegerAtom.php
Normal file
12
src/Domain/Atoms/UnsignedIntegerAtom.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Atoms;
|
||||
|
||||
use Enzyme\Axiom\Atoms\IntegerAtom;
|
||||
|
||||
class UnsignedIntegerAtom extends IntegerAtom
|
||||
{
|
||||
protected function isInvalidOrFloat($value)
|
||||
{
|
||||
return parent::isInvalidOrFloat($value) || $value <= 0;
|
||||
}
|
||||
}
|
||||
7
src/Domain/Boundary/PersonFactory.php
Normal file
7
src/Domain/Boundary/PersonFactory.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Boundary;
|
||||
|
||||
interface PersonFactory
|
||||
{
|
||||
|
||||
}
|
||||
7
src/Domain/Boundary/PersonRepository.php
Normal file
7
src/Domain/Boundary/PersonRepository.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Boundary;
|
||||
|
||||
interface PersonRepository
|
||||
{
|
||||
|
||||
}
|
||||
7
src/Domain/Core/AttributeTrait.php
Normal file
7
src/Domain/Core/AttributeTrait.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Core;
|
||||
|
||||
trait AttributeTrait
|
||||
{
|
||||
|
||||
}
|
||||
7
src/Domain/Core/Author.php
Normal file
7
src/Domain/Core/Author.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Core;
|
||||
|
||||
class Author extends Person
|
||||
{
|
||||
|
||||
}
|
||||
7
src/Domain/Core/Composer.php
Normal file
7
src/Domain/Core/Composer.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Core;
|
||||
|
||||
class Composer extends Person
|
||||
{
|
||||
|
||||
}
|
||||
15
src/Domain/Core/EntityInterface.php
Normal file
15
src/Domain/Core/EntityInterface.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Core;
|
||||
|
||||
use Enzyme\Axiom\Models\ModelInterface;
|
||||
use Webpatser\Uuid\Uuid;
|
||||
|
||||
interface EntityInterface extends ModelInterface
|
||||
{
|
||||
/**
|
||||
* Get the unique identity for this mode.
|
||||
*
|
||||
* @return Uuid
|
||||
*/
|
||||
public function identity();
|
||||
}
|
||||
32
src/Domain/Core/IdentityTrait.php
Normal file
32
src/Domain/Core/IdentityTrait.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Core;
|
||||
|
||||
use Webpatser\Uuid\Uuid;
|
||||
|
||||
trait IdentityTrait
|
||||
{
|
||||
/**
|
||||
* @var Uuid
|
||||
*/
|
||||
protected $identity;
|
||||
|
||||
|
||||
/**
|
||||
* Get the unique identity for this model.
|
||||
*
|
||||
* @return Uuid
|
||||
*/
|
||||
public function identity()
|
||||
{
|
||||
return $this->identity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Uuid|null $identity
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function setIdentity(Uuid $identity = null)
|
||||
{
|
||||
$this->identity = is_null($identity) ? Uuid::generate(4) : $identity;
|
||||
}
|
||||
}
|
||||
73
src/Domain/Core/Person.php
Normal file
73
src/Domain/Core/Person.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Core;
|
||||
|
||||
use Enzyme\Axiom\Atoms\StringAtom;
|
||||
use Webpatser\Uuid\Uuid;
|
||||
use XaiCorp\AbcParser\Domain\Atoms\EmailAtom;
|
||||
|
||||
class Person implements EntityInterface
|
||||
{
|
||||
use IdentityTrait;
|
||||
|
||||
const TYPE_COMPOSER = 'composer';
|
||||
const TYPE_AUTHOR = 'author';
|
||||
|
||||
/**
|
||||
* @var StringAtom
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* @var EmailAtom
|
||||
*/
|
||||
protected $email;
|
||||
|
||||
public function __construct(StringAtom $name, EmailAtom $email, Uuid $identity = null)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->email = $email;
|
||||
|
||||
$this->setIdentity($identity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether this model has the given attribute set.
|
||||
*
|
||||
* @param string $attribute
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function has($attribute)
|
||||
{
|
||||
return in_array($attribute, [
|
||||
'name',
|
||||
'email',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value associated with the given attribute.
|
||||
*
|
||||
* @param string $attribute
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($attribute)
|
||||
{
|
||||
if (!$this->has($attribute)) {
|
||||
throw new \Exception('Cannot access attribute on person');
|
||||
}
|
||||
|
||||
return $this->{$attribute};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the attributes associated with this model.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAll()
|
||||
{
|
||||
// TODO: Implement getAll() method.
|
||||
}
|
||||
}
|
||||
313
src/Domain/Core/Setting.php
Normal file
313
src/Domain/Core/Setting.php
Normal file
@@ -0,0 +1,313 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Core;
|
||||
|
||||
|
||||
use Enzyme\Axiom\Atoms\StringAtom;
|
||||
|
||||
class Setting
|
||||
{
|
||||
use IdentityTrait;
|
||||
|
||||
//fields that support multiple values
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $transcribers = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $notes = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $discography = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $sources = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $words = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $books = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $filenames = [];
|
||||
|
||||
//fields that only ever have one value
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $part = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $tempo = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $noteLength = '1/8';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $meter = '4/4';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $key = 'C';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $music = [];
|
||||
|
||||
/**
|
||||
* @return \XaiCorp\AbcParser\Domain\Core\Transcriber[]
|
||||
*/
|
||||
public function getTranscribers(): array
|
||||
{
|
||||
return $this->transcribers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transcriber $transcriber
|
||||
* @return Setting
|
||||
*/
|
||||
public function addTranscriber(Transcriber $transcriber): Setting
|
||||
{
|
||||
$this->transcribers[] = $transcriber;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getNotes(): array
|
||||
{
|
||||
return $this->notes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $note
|
||||
* @return Setting
|
||||
*/
|
||||
public function addNote(string $note): Setting
|
||||
{
|
||||
$this->notes[] = $note;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getDiscography(): array
|
||||
{
|
||||
return $this->discography;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $recording
|
||||
* @return Setting
|
||||
*/
|
||||
public function addRecording(string $recording): Setting
|
||||
{
|
||||
$this->discography[] = $recording;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSources(): array
|
||||
{
|
||||
return $this->sources;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $source
|
||||
* @return Setting
|
||||
*/
|
||||
public function addSource(string $source): Setting
|
||||
{
|
||||
$this->sources[] = $source;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getWords(): array
|
||||
{
|
||||
return $this->words;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $words
|
||||
* @return Setting
|
||||
*/
|
||||
public function addWords(string $words): Setting
|
||||
{
|
||||
$this->words[] = $words;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getBooks(): array
|
||||
{
|
||||
return $this->books;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $book
|
||||
* @return Setting
|
||||
*/
|
||||
public function addBook(string $book): Setting
|
||||
{
|
||||
$this->books[] = $book;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFilenames(): array
|
||||
{
|
||||
return $this->filenames;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @return Setting
|
||||
*/
|
||||
public function addFilename(string $filename): Setting
|
||||
{
|
||||
$this->filenames[] = $filename;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPart(): string
|
||||
{
|
||||
return $this->part;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $part
|
||||
* @return Setting
|
||||
*/
|
||||
public function setPart(string $part): Setting
|
||||
{
|
||||
$this->part = $part;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTempo(): string
|
||||
{
|
||||
return $this->tempo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $tempo
|
||||
* @return Setting
|
||||
*/
|
||||
public function setTempo(string $tempo): Setting
|
||||
{
|
||||
$this->tempo = $tempo;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getNoteLength(): string
|
||||
{
|
||||
return $this->noteLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $noteLength
|
||||
* @return Setting
|
||||
*/
|
||||
public function setNoteLength(string $noteLength): Setting
|
||||
{
|
||||
$this->noteLength = $noteLength;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMeter(): string
|
||||
{
|
||||
return $this->meter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $meter
|
||||
* @return Setting
|
||||
*/
|
||||
public function setMeter(string $meter): Setting
|
||||
{
|
||||
$this->meter = $meter;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getKey(): string
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return Setting
|
||||
*/
|
||||
public function setKey(string $key): Setting
|
||||
{
|
||||
$this->key = $key;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMusic(): string
|
||||
{
|
||||
return implode('\n', $this->music);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $music
|
||||
* @return Setting
|
||||
*/
|
||||
public function addMusicLine(string $music): Setting
|
||||
{
|
||||
$this->music[] = $music;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
7
src/Domain/Core/Transcriber.php
Normal file
7
src/Domain/Core/Transcriber.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Core;
|
||||
|
||||
class Transcriber extends Person
|
||||
{
|
||||
|
||||
}
|
||||
309
src/Domain/Core/Tune.php
Normal file
309
src/Domain/Core/Tune.php
Normal file
@@ -0,0 +1,309 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
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)
|
||||
{
|
||||
call_user_func([$this, 'set'.$attribute], [$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
|
||||
*/
|
||||
public function appendMusicLine(string $musicLine)
|
||||
{
|
||||
$this->music .= $musicLine->getValue() . PHP_EOL;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
19
src/Domain/Core/TuneCollection.php
Normal file
19
src/Domain/Core/TuneCollection.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Core;
|
||||
|
||||
use Enzyme\Collection\Collection;
|
||||
|
||||
class TuneCollection extends Collection
|
||||
{
|
||||
/**
|
||||
* Get the value of the first element in this collection.
|
||||
*
|
||||
* @throws \Enzyme\Collection\CollectionException If the collection is empty.
|
||||
*
|
||||
* @return \XaiCorp\AbcParser\Domain\Core\Tune
|
||||
*/
|
||||
public function first()
|
||||
{
|
||||
return parent::first();
|
||||
}
|
||||
}
|
||||
204
src/Domain/Modules/Interpreter/Builder.php
Normal file
204
src/Domain/Modules/Interpreter/Builder.php
Normal file
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter;
|
||||
|
||||
use Enzyme\Axiom\Atoms\StringAtom;
|
||||
use XaiCorp\AbcParser\Domain\Atoms\EmailAtom;
|
||||
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
|
||||
use XaiCorp\AbcParser\Domain\Core\Author;
|
||||
use XaiCorp\AbcParser\Domain\Core\Composer;
|
||||
use XaiCorp\AbcParser\Domain\Core\Setting;
|
||||
use XaiCorp\AbcParser\Domain\Core\Transcriber;
|
||||
use XaiCorp\AbcParser\Domain\Core\Tune;
|
||||
use XaiCorp\AbcParser\Domain\Core\TuneCollection;
|
||||
use XaiCorp\AbcParser\Models\Laravel5\TuneSetting;
|
||||
|
||||
class Builder
|
||||
{
|
||||
/**
|
||||
* @var Tune
|
||||
*/
|
||||
protected $tune;
|
||||
|
||||
/**
|
||||
* @var TuneCollection
|
||||
*/
|
||||
protected $collection;
|
||||
|
||||
/**
|
||||
* @var Setting
|
||||
*/
|
||||
protected $setting;
|
||||
|
||||
/**
|
||||
* Interpreter constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->collection = new TuneCollection();
|
||||
$this->tune = new Tune([]);
|
||||
$this->setting = new Setting();
|
||||
|
||||
$this->tune->addSetting($this->setting);
|
||||
}
|
||||
|
||||
public function getTune()
|
||||
{
|
||||
return $this->tune;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \XaiCorp\AbcParser\Domain\Core\TuneCollection
|
||||
*/
|
||||
public function getCollection()
|
||||
{
|
||||
return $this->collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function handleEndOfMusic()
|
||||
{
|
||||
if ($this->tune && empty($this->tune->getTitles())) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->tune->addSetting($this->setting);
|
||||
$this->collection[] = $this->tune;
|
||||
|
||||
$this->tune = new Tune([]);
|
||||
$this->setting = new Setting();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UnsignedIntegerAtom $index
|
||||
* @return $this
|
||||
*/
|
||||
public function setIndex(UnsignedIntegerAtom $index)
|
||||
{
|
||||
$this->tune->setIndex($index);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function appendMusic(StringAtom $musicLine)
|
||||
{
|
||||
$this->setting->addMusicLine($musicLine->getValue());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addTitle(StringAtom $title)
|
||||
{
|
||||
$this->tune->addTitle($title->getValue());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setKey(StringAtom $key)
|
||||
{
|
||||
$this->setting->setKey($key->getValue());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setMeter(StringAtom $key)
|
||||
{
|
||||
$this->setting->setMeter($key->getValue());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addAuthor(StringAtom $name)
|
||||
{
|
||||
$author = new Author($name, new EmailAtom(''));
|
||||
$this->tune->addAuthor($author);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addComposer(StringAtom $composerName)
|
||||
{
|
||||
$composer = new Composer($composerName, new EmailAtom(''));
|
||||
$this->tune->addComposer($composer);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addTranscriber(StringAtom $transcriberName)
|
||||
{
|
||||
$transcriber = new Transcriber($transcriberName, new EmailAtom(''));
|
||||
$this->setting->addTranscriber($transcriber);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addDiscography(StringAtom $recording)
|
||||
{
|
||||
$this->setting->addRecording($recording->getValue());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addGroup(StringAtom $groupName)
|
||||
{
|
||||
$this->tune->addGroup($groupName->getValue());
|
||||
}
|
||||
|
||||
public function setParts(StringAtom $part)
|
||||
{
|
||||
$this->setting->setPart($part->getValue());
|
||||
}
|
||||
|
||||
public function addSource(StringAtom $source)
|
||||
{
|
||||
$this->setting->addSource($source->getValue());
|
||||
}
|
||||
|
||||
public function addFilename(StringAtom $filename)
|
||||
{
|
||||
$this->setting->addFilename($filename->getValue());
|
||||
}
|
||||
|
||||
public function addHistory(StringAtom $history)
|
||||
{
|
||||
$this->tune->addHistoryLine($history->getValue());
|
||||
}
|
||||
|
||||
public function addRhythm(StringAtom $rhythm)
|
||||
{
|
||||
$this->tune->addRhythm($rhythm->getValue());
|
||||
}
|
||||
|
||||
public function addBook(StringAtom $param)
|
||||
{
|
||||
$this->setting->addBook($param->getValue());
|
||||
}
|
||||
|
||||
public function addWords(StringAtom $param)
|
||||
{
|
||||
$this->setting->addWords($param->getValue());
|
||||
}
|
||||
|
||||
public function addTempo(StringAtom $param)
|
||||
{
|
||||
$this->setting->setTempo($param->getValue());
|
||||
}
|
||||
|
||||
public function addNoteLength(StringAtom $param)
|
||||
{
|
||||
$this->setting->setNoteLength($param->getValue());
|
||||
}
|
||||
|
||||
public function addNote(StringAtom $param)
|
||||
{
|
||||
$this->setting->addNote($param->getValue());
|
||||
}
|
||||
|
||||
public function addOrigin(StringAtom $param)
|
||||
{
|
||||
$this->tune->addOrigin($param->getValue());
|
||||
}
|
||||
}
|
||||
116
src/Domain/Modules/Interpreter/Context.php
Normal file
116
src/Domain/Modules/Interpreter/Context.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter;
|
||||
|
||||
use Enzyme\Axiom\Atoms\StringAtom;
|
||||
use Enzyme\Collection\Collection;
|
||||
|
||||
class Context extends Collection
|
||||
{
|
||||
const MODE_NO_DATA = 0;
|
||||
const MODE_FILE_HEADER = 1;
|
||||
const MODE_TUNE_HEADER = 2;
|
||||
const MODE_TUNE_BODY = 3;
|
||||
const MODE_HISTORY = 4;
|
||||
|
||||
const ABC_VERSION_20 = '2.0';
|
||||
const ABC_VERSION_21 = '2.1';
|
||||
const ABC_VERSION_DEFAULT = self::ABC_VERSION_21;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $abcText;
|
||||
|
||||
/**
|
||||
* @var Collection
|
||||
*/
|
||||
protected $abcLines;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $state = self::MODE_NO_DATA;
|
||||
|
||||
public function __construct(StringAtom $abc)
|
||||
{
|
||||
$this->abcText = $abc->getValue();
|
||||
|
||||
parent::__construct(preg_split("/(\r?\n)/", $abc->getValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getState()
|
||||
{
|
||||
return $this->state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setStateInFileHeader()
|
||||
{
|
||||
$this->state = self::MODE_FILE_HEADER;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setStateInTuneHeader()
|
||||
{
|
||||
$this->state = self::MODE_TUNE_HEADER;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setStateInTuneBody()
|
||||
{
|
||||
$this->state = self::MODE_TUNE_BODY;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setStateInHistory()
|
||||
{
|
||||
$this->state = self::MODE_HISTORY;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setStateInBetween()
|
||||
{
|
||||
$this->state = self::MODE_NO_DATA;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $state
|
||||
* @return bool
|
||||
*/
|
||||
public function isState(int $state) : bool
|
||||
{
|
||||
return $this->state === $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* get abc version of the file
|
||||
* @return string
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
return '2.1';
|
||||
}
|
||||
}
|
||||
121
src/Domain/Modules/Interpreter/DefaultInterpreter.php
Normal file
121
src/Domain/Modules/Interpreter/DefaultInterpreter.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter;
|
||||
|
||||
use Enzyme\Axiom\Atoms\StringAtom;
|
||||
use XaiCorp\AbcParser\Domain\Core\Tune;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Author;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Book;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Composer;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Discography;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\EndOfTune;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Filename;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Group;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\History;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Index;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\KeySignature;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Meter;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\MusicLine;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\NoteLength;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Notes;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Origin;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Parts;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Rhythm;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Source;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Tempo;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Title;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Transcriber;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon\Words;
|
||||
|
||||
class DefaultInterpreter extends Interpreter
|
||||
{
|
||||
protected $keyMap = [
|
||||
'K' => 'key',
|
||||
'L' => 'length',
|
||||
'M' => 'meter',
|
||||
'P' => 'part',
|
||||
'Q' => 'tempo',
|
||||
'B' => 'book',
|
||||
'N' => 'note',
|
||||
'D' => 'disc',
|
||||
'S' => 'source',
|
||||
'W' => 'words',
|
||||
'Z' => 'transcriber',
|
||||
'F' => 'filename',
|
||||
'H' => 'history',
|
||||
'A' => 'author',
|
||||
'C' => 'composer',
|
||||
'G' => 'group',
|
||||
'R' => 'rhythm',
|
||||
'T' => 'title',
|
||||
'O' => 'origin'
|
||||
];
|
||||
|
||||
protected $lexers = [
|
||||
Index::class,
|
||||
Title::class,
|
||||
Author::class,
|
||||
Book::class,
|
||||
Composer::class,
|
||||
Transcriber::class,
|
||||
Discography::class,
|
||||
Group::class,
|
||||
Source::class,
|
||||
Origin::class,
|
||||
KeySignature::class,
|
||||
Meter::class,
|
||||
Parts::class,
|
||||
Tempo::class,
|
||||
NoteLength::class,
|
||||
Notes::class,
|
||||
History::class,
|
||||
Filename::class,
|
||||
Words::class,
|
||||
Rhythm::class,
|
||||
MusicLine::class,
|
||||
EndOfTune::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* @param \XaiCorp\AbcParser\Domain\Modules\Interpreter\Context $context
|
||||
* @return $this
|
||||
*/
|
||||
public function execute(Context $context)
|
||||
{
|
||||
$lexicon = $this->pipeline();
|
||||
|
||||
do {
|
||||
$result = (new InterpreterPipeline())
|
||||
->send($context)
|
||||
->through($lexicon)
|
||||
->via('handle')
|
||||
->then(function ($context) {
|
||||
return $context;
|
||||
});
|
||||
$context->next();
|
||||
} while ($context->count() > $context->key());
|
||||
|
||||
// return $this->builder->getTune();
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLastTune()
|
||||
{
|
||||
return $this->builder->getTune();
|
||||
}
|
||||
|
||||
public function getTunes()
|
||||
{
|
||||
return $this->builder->getCollection();
|
||||
}
|
||||
|
||||
protected function pipeline()
|
||||
{
|
||||
$pipeline = [];
|
||||
|
||||
foreach ($this->lexers as $lexerClass) {
|
||||
$pipeline[] = new $lexerClass($this->builder);
|
||||
}
|
||||
|
||||
return $pipeline;
|
||||
}
|
||||
}
|
||||
36
src/Domain/Modules/Interpreter/Interpreter.php
Normal file
36
src/Domain/Modules/Interpreter/Interpreter.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter;
|
||||
|
||||
use XaiCorp\AbcParser\Domain\Core\Tune;
|
||||
use XaiCorp\AbcParser\Domain\Core\TuneCollection;
|
||||
|
||||
abstract class Interpreter
|
||||
{
|
||||
const LN_VERSION = '/^%abc(-\d\.\d)?$/';
|
||||
const LN_BLANK = '/^\s*$/';
|
||||
const LN_FIELD = '/^\w:(.+)$/';
|
||||
|
||||
/**
|
||||
* @var Builder
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
/**
|
||||
* Interpreter constructor.
|
||||
*/
|
||||
public function __construct(Builder $builder)
|
||||
{
|
||||
$this->builder = $builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Context $context
|
||||
* @return Interpreter
|
||||
*/
|
||||
abstract public function execute(Context $context);
|
||||
|
||||
/**
|
||||
* @return TuneCollection
|
||||
*/
|
||||
abstract public function getTunes();
|
||||
}
|
||||
9
src/Domain/Modules/Interpreter/InterpreterPipeline.php
Normal file
9
src/Domain/Modules/Interpreter/InterpreterPipeline.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter;
|
||||
|
||||
use Illuminate\Pipeline\Pipeline;
|
||||
|
||||
class InterpreterPipeline extends Pipeline
|
||||
{
|
||||
|
||||
}
|
||||
40
src/Domain/Modules/Interpreter/Lexicon/Composer.php
Normal file
40
src/Domain/Modules/Interpreter/Lexicon/Composer.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
|
||||
|
||||
use Enzyme\Axiom\Atoms\StringAtom;
|
||||
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
|
||||
|
||||
class Composer implements Lex
|
||||
{
|
||||
use LineKeyDataTrait;
|
||||
|
||||
/**
|
||||
* @var Builder
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
public function __construct(Builder $builder)
|
||||
{
|
||||
$this->builder = $builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Context $context
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
* @throws \Enzyme\Axiom\Exceptions\AtomException
|
||||
*/
|
||||
public function handle(Context $context, \Closure $next)
|
||||
{
|
||||
$line = $context->current();
|
||||
list($key, $data) = $this->getKeyDataFromLine($line);
|
||||
|
||||
if ($key === 'C') {
|
||||
$this->builder->addComposer(new StringAtom($data));
|
||||
}
|
||||
|
||||
return $next($context);
|
||||
}
|
||||
}
|
||||
36
src/Domain/Modules/Interpreter/Lexicon/Index.php
Normal file
36
src/Domain/Modules/Interpreter/Lexicon/Index.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
|
||||
|
||||
use Enzyme\Axiom\Atoms\StringAtom;
|
||||
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
|
||||
|
||||
class Index implements Lex
|
||||
{
|
||||
use LineKeyDataTrait;
|
||||
|
||||
/**
|
||||
* @var Builder
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
public function __construct(Builder $builder)
|
||||
{
|
||||
$this->builder = $builder;
|
||||
}
|
||||
|
||||
public function handle(Context $context, \Closure $next)
|
||||
{
|
||||
$line = $context->current();
|
||||
list($key, $data) = $this->getKeyDataFromLine($line);
|
||||
|
||||
if ($key === 'X') {
|
||||
$context->setStateInTuneHeader();
|
||||
|
||||
$this->builder->setIndex(new UnsignedIntegerAtom($data));
|
||||
}
|
||||
|
||||
return $next($context);
|
||||
}
|
||||
}
|
||||
7
src/Domain/Modules/Interpreter/Lexicon/Lex.php
Normal file
7
src/Domain/Modules/Interpreter/Lexicon/Lex.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
|
||||
|
||||
interface Lex
|
||||
{
|
||||
|
||||
}
|
||||
23
src/Domain/Modules/Interpreter/Lexicon/LineKeyDataTrait.php
Normal file
23
src/Domain/Modules/Interpreter/Lexicon/LineKeyDataTrait.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
|
||||
|
||||
trait LineKeyDataTrait
|
||||
{
|
||||
/**
|
||||
* @param string $line
|
||||
* @return array [$key, $data]
|
||||
*/
|
||||
protected function getKeyDataFromLine(string $line)
|
||||
{
|
||||
$key = '';
|
||||
$data = '';
|
||||
$matches = [];
|
||||
preg_match('/^(.):(.*)/', $line, $matches);
|
||||
|
||||
if (count($matches) === 3) {
|
||||
$key = trim($matches[1]);
|
||||
$data = trim($matches[2]);
|
||||
}
|
||||
return [$key, $data];
|
||||
}
|
||||
}
|
||||
40
src/Domain/Modules/Interpreter/Lexicon/Title.php
Normal file
40
src/Domain/Modules/Interpreter/Lexicon/Title.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Modules\Interpreter\Lexicon;
|
||||
|
||||
use Enzyme\Axiom\Atoms\StringAtom;
|
||||
use XaiCorp\AbcParser\Domain\Atoms\UnsignedIntegerAtom;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
|
||||
|
||||
class Title implements Lex
|
||||
{
|
||||
use LineKeyDataTrait;
|
||||
|
||||
/**
|
||||
* @var Builder
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
public function __construct(Builder $builder)
|
||||
{
|
||||
$this->builder = $builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Context $context
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
* @throws \Enzyme\Axiom\Exceptions\AtomException
|
||||
*/
|
||||
public function handle(Context $context, \Closure $next)
|
||||
{
|
||||
$line = $context->current();
|
||||
list($key, $data) = $this->getKeyDataFromLine($line);
|
||||
|
||||
if ($key === 'T') {
|
||||
$this->builder->addTitle(new StringAtom($data));
|
||||
}
|
||||
|
||||
return $next($context);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user