add ExtractTune use case
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser;
|
||||
|
||||
/**
|
||||
* outline the functions and parameters available in the concrete builders
|
||||
* Class AbstractBuilder
|
||||
*/
|
||||
abstract class AbstractBuilder
|
||||
{
|
||||
|
||||
protected $classMap;
|
||||
|
||||
protected $collection;
|
||||
protected $currentTune;
|
||||
protected $currentSetting;
|
||||
protected $currentPerson;
|
||||
|
||||
public abstract function newCollection();
|
||||
public abstract function appendToCollection($key, $data);
|
||||
|
||||
public abstract function newTune();
|
||||
public abstract function appendToTune($key, $data);
|
||||
public abstract function newSetting();
|
||||
|
||||
public abstract function appendToSetting($key, $data);
|
||||
public abstract function newPerson($data);
|
||||
|
||||
public function storeTune() {
|
||||
$this->appendToCollection('tune', $this->tune);
|
||||
}
|
||||
}
|
||||
28
src/Application/UseCases/ExtractTuneFromCollection.php
Normal file
28
src/Application/UseCases/ExtractTuneFromCollection.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Application\UseCases;
|
||||
|
||||
use XaiCorp\AbcParser\Domain\Core\Tune;
|
||||
use XaiCorp\AbcParser\Domain\Core\TuneCollection;
|
||||
|
||||
class ExtractTuneFromCollection
|
||||
{
|
||||
public function __construct(TuneCollection $tunes)
|
||||
{
|
||||
$this->tunes = $tunes;
|
||||
}
|
||||
|
||||
public static function create(TuneCollection $tunes)
|
||||
{
|
||||
return new self($tunes);
|
||||
}
|
||||
|
||||
public function extractTuneByTitle(string $title)
|
||||
{
|
||||
return $this->tunes
|
||||
->filter(function (Tune $tune) use ($title) {
|
||||
return in_array($title, $tune->getTitles());
|
||||
})
|
||||
->unique()
|
||||
;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
namespace XaiCorp\AbcParser\Application\UseCases;
|
||||
|
||||
use Enzyme\Axiom\Atoms\StringAtom;
|
||||
use XaiCorp\AbcParser\Domain\Core\Collection;
|
||||
use XaiCorp\AbcParser\Domain\Core\TuneCollection;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
|
||||
@@ -16,11 +17,18 @@ class ImportAbcFile
|
||||
protected $builderTemplate;
|
||||
|
||||
/**
|
||||
* @param Builder|null $builderTemplate
|
||||
* @param Builder $builderTemplate
|
||||
*/
|
||||
public function __construct(Builder $builderTemplate = null)
|
||||
public function __construct(Builder $builderTemplate)
|
||||
{
|
||||
$this->builderTemplate = $builderTemplate ?: new Builder();
|
||||
$this->builderTemplate = $builderTemplate;
|
||||
}
|
||||
|
||||
public static function create(Builder $builderTemplate = null)
|
||||
{
|
||||
$builder = $builderTemplate ?: new Builder();
|
||||
|
||||
return new self($builder);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,7 +40,25 @@ class ImportAbcFile
|
||||
{
|
||||
$context = new Context(new StringAtom($abc));
|
||||
$parser = $this->getBestParser($context);
|
||||
$result = $parser->execute($context);
|
||||
$result = $parser->execute($context)->getTunes();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $abcs
|
||||
* @return \XaiCorp\AbcParser\Domain\Core\TuneCollection
|
||||
*/
|
||||
public function importMultiple(array $abcs)
|
||||
{
|
||||
$result = Collection::make($abcs)
|
||||
->map(function (string $abc) {
|
||||
return $this->import($abc);
|
||||
})
|
||||
->reduce(new TuneCollection(), function (TuneCollection $carry, TuneCollection $tunes) {
|
||||
return $carry->merge($tunes);
|
||||
})
|
||||
;
|
||||
|
||||
return $result;
|
||||
}
|
||||
@@ -41,9 +67,9 @@ class ImportAbcFile
|
||||
* @param Context $context
|
||||
* @return Interpreter
|
||||
*/
|
||||
public function getBestParser(Context $context) : Interpreter
|
||||
protected function getBestParser(Context $context) : Interpreter
|
||||
{
|
||||
$builder = clone $this->builderTemplate;
|
||||
$builder = clone ($this->builderTemplate);
|
||||
|
||||
switch($context->getVersion()) {
|
||||
default:
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Application\UseCases;
|
||||
|
||||
use XaiCorp\AbcParser\Domain\Core\TuneCollection;
|
||||
|
||||
class MergeCollections
|
||||
{
|
||||
|
||||
public function merge(TuneCollection $masterCollection, TuneCollection $additions)
|
||||
{
|
||||
$collection = clone $masterCollection;
|
||||
|
||||
return $collection->merge($additions);
|
||||
}
|
||||
}
|
||||
492
src/Domain/Core/Collection.php
Normal file
492
src/Domain/Core/Collection.php
Normal file
@@ -0,0 +1,492 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Core;
|
||||
|
||||
use Closure;
|
||||
use Enzyme\Collection\BaseCollection;
|
||||
use Enzyme\Collection\CollectionException;
|
||||
|
||||
class Collection extends BaseCollection
|
||||
{
|
||||
/**
|
||||
* @param $initial
|
||||
* @param callable $callback
|
||||
* @return mixed
|
||||
*/
|
||||
public function reduce($initial, callable $callback)
|
||||
{
|
||||
return array_reduce($this->items, $callback, $initial);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \XaiCorp\AbcParser\Domain\Core\Collection $collection
|
||||
* @return static
|
||||
*/
|
||||
public function merge(Collection $collection)
|
||||
{
|
||||
$results = $this->items;
|
||||
foreach ($collection as $key => $value) {
|
||||
if (is_numeric($key)) {
|
||||
$results[] = $value;
|
||||
} else {
|
||||
$results[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return new static($results);
|
||||
}
|
||||
|
||||
/**
|
||||
* Static helper method to instantiate a new collection. Useful for when you
|
||||
* want to immediately chain a method. Eg: Collection::make([1, 2, 3])->map(...).
|
||||
*
|
||||
* @param array $initial
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public static function make(array $initial)
|
||||
{
|
||||
return new static($initial);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a PHP style array from the current collection.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the collection has the specified key, and/or value associated
|
||||
* with the specified key.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has($key, $value = null)
|
||||
{
|
||||
$key_exists = self::keyExists($key, $this->items);
|
||||
|
||||
return null !== $value
|
||||
? $value === $this->get($key)
|
||||
: $key_exists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value associated with the specified key.
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @throws CollectionException If the key does not exist.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
if (false === self::keyExists($key, $this->items)) {
|
||||
throw new CollectionException(
|
||||
"An element with the key [${key}] does not exist."
|
||||
);
|
||||
}
|
||||
|
||||
return $this->items[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value associated with the specified key or return a default value
|
||||
* instead if it does not exist.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOrDefault($key, $default = null)
|
||||
{
|
||||
try {
|
||||
return $this->get($key);
|
||||
} catch (CollectionException $e) {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the given callback function for each element in this collection.
|
||||
*
|
||||
* @param Closure $fn
|
||||
*/
|
||||
public function each(Closure $fn)
|
||||
{
|
||||
foreach ($this->items as $key => $value) {
|
||||
if (false === $fn($value, $key)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the given callback function for each element in this collection
|
||||
* and save the results to a new collection.
|
||||
*
|
||||
* @param Closure $fn
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function map(Closure $fn)
|
||||
{
|
||||
$results = [];
|
||||
foreach ($this->items as $key => $value) {
|
||||
$results[] = $fn($value, $key);
|
||||
}
|
||||
|
||||
return new static($results);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the given callback function for each element in this collection
|
||||
* and save the results to a new collection with the specified key. The
|
||||
* callback function should return a 1 element associative array, eg:
|
||||
* ['key' => 'value'] to be mapped.
|
||||
*
|
||||
* @param Closure $fn
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function mapWithKey(Closure $fn)
|
||||
{
|
||||
$results = [];
|
||||
foreach ($this->items as $key => $value) {
|
||||
$result = $fn($value, $key);
|
||||
$keys = array_keys($result);
|
||||
|
||||
if (count($keys) < 1) {
|
||||
throw new CollectionException(
|
||||
'Map with key expects a 1 element associative array.'
|
||||
);
|
||||
}
|
||||
|
||||
$results[$keys[0]] = $result[$keys[0]];
|
||||
}
|
||||
|
||||
return new static($results);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pluck out all values in this collection which have the specified key.
|
||||
*
|
||||
* @param string $pluck_key
|
||||
* @param bool $deep Whether to traverse into sub-arrays.
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function pluck($pluck_key, $deep = true)
|
||||
{
|
||||
return self::pluckKey($this->items, $pluck_key, $deep);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of elements in this collection.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this collection is empty.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmpty()
|
||||
{
|
||||
return $this->count() < 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the first element in this collection.
|
||||
*
|
||||
* @throws CollectionException If the collection is empty.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function first()
|
||||
{
|
||||
if (true === $this->isEmpty()) {
|
||||
throw new CollectionException(
|
||||
'Cannot get first item as the collection is empty.'
|
||||
);
|
||||
}
|
||||
|
||||
return reset($this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the first element in this collection or return the
|
||||
* default value specified if the collection is empty.
|
||||
*
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function firstOrDefault($default = null)
|
||||
{
|
||||
try {
|
||||
return $this->first();
|
||||
} catch (CollectionException $e) {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the last element in this collection.
|
||||
*
|
||||
* @throws CollectionException If the collection is empty.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function last()
|
||||
{
|
||||
if (true === $this->isEmpty()) {
|
||||
throw new CollectionException(
|
||||
'Cannot get last element as collection is empty.'
|
||||
);
|
||||
}
|
||||
|
||||
end($this->items);
|
||||
$key = key($this->items);
|
||||
reset($this->items);
|
||||
|
||||
return $this->items[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the last element in this collection or return the
|
||||
* default value specified if the collection is empty.
|
||||
*
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function lastOrDefault($default = null)
|
||||
{
|
||||
try {
|
||||
return $this->first();
|
||||
} catch (CollectionException $e) {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a new collection of only the elements in the current collection
|
||||
* that have the specified keys.
|
||||
*
|
||||
* @param array $keys
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function only(array $keys)
|
||||
{
|
||||
return $this->filter(function ($value, $key) use ($keys) {
|
||||
return true === self::keyExists($key, array_flip($keys));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a new collection of all the elements in the current collection
|
||||
* except those that have the specified keys.
|
||||
*
|
||||
* @param array $keys
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function except(array $keys)
|
||||
{
|
||||
return $this->filter(function ($value, $key) use ($keys) {
|
||||
return false === self::keyExists($key, array_flip($keys));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new collection with the current collection's elements plus the
|
||||
* given value pushed onto the end of the array.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function push($value)
|
||||
{
|
||||
$items = $this->items;
|
||||
$items[] = $value;
|
||||
|
||||
return new static($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new collection with the current collection's elements plus the
|
||||
* given key and value pushed onto the end of the array.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function pushWithKey($key, $value)
|
||||
{
|
||||
$items = $this->items;
|
||||
$items[$key] = $value;
|
||||
|
||||
return new static($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new collection with the current collection's elements plus the
|
||||
* given array pushed onto the end of the array.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function pushArray(array $data)
|
||||
{
|
||||
$items = $this->items;
|
||||
foreach ($data as $key => $value) {
|
||||
if (true === is_int($key)) {
|
||||
$items[] = $value;
|
||||
} else {
|
||||
$items[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return new static($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new collection with a subset of all the current collection's
|
||||
* elements that pass the given callback functions truth test.
|
||||
*
|
||||
* @param Closure $fn
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function filter(Closure $fn)
|
||||
{
|
||||
$results = [];
|
||||
foreach ($this->items as $key => $value) {
|
||||
if (true === $fn($value, $key)) {
|
||||
$results[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Pushing this new array will normalize numeric keys if they exist.
|
||||
// After filtering, they may not start at zero and sequentially
|
||||
// go upwards, which is generally not expected.
|
||||
return (new static())->pushArray($results);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the collection using the provided callback function. Same expected
|
||||
* parameters and the PHP usort function.
|
||||
*
|
||||
* @param Closure $fn
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function sort(Closure $fn)
|
||||
{
|
||||
$sorted = $this->items;
|
||||
$result = usort($sorted, $fn);
|
||||
|
||||
if (false === $result) {
|
||||
throw new CollectionException(
|
||||
'The collection could be not sorted.'
|
||||
);
|
||||
}
|
||||
|
||||
return new static($sorted);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this collection has the specified number of elements within the
|
||||
* given range or equal too or above the minimum value specified.
|
||||
*
|
||||
* @param int $min
|
||||
* @param int $max Default is null.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasCount($min, $max = null)
|
||||
{
|
||||
if (null === $max) {
|
||||
return $this->count() >= $min;
|
||||
}
|
||||
|
||||
return $this->count() >= $min
|
||||
&& $this->count() <= $max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of the keys used by this collection.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function keys()
|
||||
{
|
||||
return array_keys($this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the specified key exists in the given collection.
|
||||
*
|
||||
* @param string $key
|
||||
* @param array $collection
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected static function keyExists($key, array $collection)
|
||||
{
|
||||
return true === isset($collection[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pluck all the values that have the specified key from the given
|
||||
* collection.
|
||||
*
|
||||
* @param array $collection
|
||||
* @param string $pluck_key
|
||||
* @param bool $deep Whether to traverse into sub-arrays.
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
protected static function pluckKey(array $collection, $pluck_key, $deep)
|
||||
{
|
||||
$results = [];
|
||||
foreach ($collection as $key => $value) {
|
||||
if (true === $deep && true === is_array($value)) {
|
||||
$deeper_results = self::pluckKey(
|
||||
$value,
|
||||
$pluck_key,
|
||||
$deep
|
||||
)->toArray();
|
||||
|
||||
foreach ($deeper_results as $deep_value) {
|
||||
$results[] = $deep_value;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($key === $pluck_key) {
|
||||
$results[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return new static($results);
|
||||
}
|
||||
}
|
||||
@@ -80,6 +80,20 @@ class Tune implements EntityInterface
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Domain\Core;
|
||||
|
||||
use Enzyme\Collection\Collection;
|
||||
|
||||
class TuneCollection extends Collection
|
||||
{
|
||||
/**
|
||||
@@ -16,4 +14,19 @@ class TuneCollection extends Collection
|
||||
{
|
||||
return parent::first();
|
||||
}
|
||||
|
||||
public function unique()
|
||||
{
|
||||
$result = [];
|
||||
/**
|
||||
* @var int $key
|
||||
* @var Tune $tune
|
||||
*/
|
||||
foreach ($this->items as $key => $tune) {
|
||||
$tuneId = $tune->getETag();
|
||||
$result[$tuneId] = $tune;
|
||||
}
|
||||
|
||||
return new TuneCollection(array_values($result));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,15 @@ class Builder
|
||||
$this->tune->addSetting($this->setting);
|
||||
}
|
||||
|
||||
public function __clone()
|
||||
{
|
||||
$this->collection = new TuneCollection();
|
||||
$this->tune = new Tune([]);
|
||||
$this->setting = new Setting();
|
||||
|
||||
$this->tune->addSetting($this->setting);
|
||||
}
|
||||
|
||||
public function getTune()
|
||||
{
|
||||
return $this->tune;
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Framework\Laravel5\Models;
|
||||
|
||||
class Person extends ValidatingModel
|
||||
{
|
||||
use UuidTrait;
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'persons';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['name', 'email'];
|
||||
|
||||
protected $validationRules = [
|
||||
'id' => 'required',
|
||||
'name' => 'required|string|max:63',
|
||||
'email' => 'required|email|max:255'
|
||||
];
|
||||
|
||||
protected $hidden = ['email'];
|
||||
|
||||
//Relationships
|
||||
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Framework\Laravel5\Models;
|
||||
|
||||
use Webpatser\Uuid\Uuid;
|
||||
|
||||
trait UuidTrait
|
||||
{
|
||||
public function __construct(array $attributes = [])
|
||||
{
|
||||
parent::__construct($attributes);
|
||||
|
||||
$uuid = Uuid::generate(4);
|
||||
$this->attributes['id'] = $uuid;
|
||||
}
|
||||
|
||||
public function getIdAttribute()
|
||||
{
|
||||
if (empty($this->attributes['id'])) {
|
||||
$uuid = Uuid::generate(4);
|
||||
$this->attributes['id'] = $uuid;
|
||||
}
|
||||
return Uuid::import($this->attributes['id']);
|
||||
}
|
||||
|
||||
public function setIdAttribute($value)
|
||||
{
|
||||
if (empty($this->attributes['id'])) {
|
||||
$uuid = Uuid::generate(4);
|
||||
$this->attributes['id'] = $uuid;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Framework\Laravel5\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model as BaseModel;
|
||||
|
||||
class ValidatingModel extends BaseModel
|
||||
{
|
||||
use ValidationTrait;
|
||||
|
||||
/**
|
||||
* validation rules to apply to model attributes
|
||||
* @var array
|
||||
*/
|
||||
protected $validationRules = [];
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
* @return bool
|
||||
*/
|
||||
public function save(array $options = [])
|
||||
{
|
||||
if ($this->validate()) {
|
||||
return parent::save($options);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Framework\Laravel5\Models;
|
||||
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
Trait ValidationTrait
|
||||
{
|
||||
|
||||
/**
|
||||
* @var MessageBag
|
||||
*/
|
||||
protected $messages;
|
||||
|
||||
public function validate()
|
||||
{
|
||||
// validation
|
||||
$data = $this->getAttributes();
|
||||
|
||||
$validator = Validator::make($data, $this->validationRules);
|
||||
|
||||
if ($validator->fails()) {
|
||||
if (! $this->messages) {
|
||||
$this->messages = new MessageBag();
|
||||
}
|
||||
$this->messages = $validator->getMessageBag();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|MessageBag
|
||||
*/
|
||||
public function getMessages()
|
||||
{
|
||||
return $this->messages?$this->messages->toArray():[];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getValidationRules()
|
||||
{
|
||||
return $this->validationRules;
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch the validation rules for a model class
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRules()
|
||||
{
|
||||
$class = get_called_class();
|
||||
$instance = new $class();
|
||||
return $instance->getValidationRules();
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Framework;
|
||||
|
||||
use XaiCorp\AbcParser\Application\Boundary\PersonMapper;
|
||||
|
||||
class PersonDbMapper implements PersonMapper
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function insert()
|
||||
{
|
||||
// TODO: Implement insert() method.
|
||||
}
|
||||
|
||||
public function update()
|
||||
{
|
||||
// TODO: Implement update() method.
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
// TODO: Implement delete() method.
|
||||
}
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Arr;
|
||||
|
||||
use XaiCorp\AbcParser\Interfaces\Builder;
|
||||
use XaiCorp\AbcParser\Interfaces\Exporter;
|
||||
use XaiCorp\AbcParser\Interfaces\Manipulator;
|
||||
|
||||
class Abc implements Builder, Manipulator, Exporter
|
||||
{
|
||||
protected $abc = [];
|
||||
protected $tunedIdx = 0;
|
||||
|
||||
public function newCollection()
|
||||
{
|
||||
$this->abc = [];
|
||||
}
|
||||
|
||||
public function appendToCollection($key, $data)
|
||||
{
|
||||
$this->abc[$key][] = $data;
|
||||
}
|
||||
|
||||
public function setOnCollection($key, $data)
|
||||
{
|
||||
$this->abc[$key] = $data;
|
||||
}
|
||||
|
||||
public function getCollection()
|
||||
{
|
||||
return $this->abc;
|
||||
}
|
||||
|
||||
public function newTune()
|
||||
{
|
||||
$this->abc[++$this->tunedIdx] = [];
|
||||
}
|
||||
|
||||
public function appendToTune($key, $data)
|
||||
{
|
||||
$this->abc[$this->tunedIdx][$key][] = $data;
|
||||
}
|
||||
|
||||
public function setOnTune($key, $data)
|
||||
{
|
||||
$this->abc[$this->tunedIdx][$key] = $data;
|
||||
}
|
||||
|
||||
public function newSetting()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function appendToSetting($key, $data)
|
||||
{
|
||||
$this->appendToTune($key, $data);
|
||||
}
|
||||
|
||||
public function setOnSetting($key, $data)
|
||||
{
|
||||
$this->setOnTune($key, $data);
|
||||
}
|
||||
|
||||
public function newPerson(array $data)
|
||||
{
|
||||
return $data['name'];
|
||||
}
|
||||
|
||||
public function getTunes()
|
||||
{
|
||||
return $this->abc;
|
||||
}
|
||||
|
||||
public function removeTune($tuneIndex)
|
||||
{
|
||||
unset($this->abc[$tuneIndex]);
|
||||
}
|
||||
|
||||
public function toAbc()
|
||||
{
|
||||
//TODO: translate to abc not json;
|
||||
return json_encode($this->abc);
|
||||
}
|
||||
|
||||
public function toJson()
|
||||
{
|
||||
return json_encode($this->abc);
|
||||
}
|
||||
|
||||
public function storeTune($music)
|
||||
{
|
||||
$this->abc[$this->tunedIdx]['music'] = trim($music);
|
||||
}
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Laravel5;
|
||||
|
||||
use XaiCorp\AbcParser\Interfaces\Builder;
|
||||
use XaiCorp\AbcParser\Interfaces\Exporter;
|
||||
use XaiCorp\AbcParser\Interfaces\Manipulator;
|
||||
use XaiCorp\AbcParser\Models\Laravel5\Collection;
|
||||
|
||||
class Abc implements Builder, Manipulator, Exporter
|
||||
{
|
||||
/**
|
||||
* @var \XaiCorp\AbcParser\Models\Laravel5\Collection
|
||||
*/
|
||||
public $collection;
|
||||
|
||||
/**
|
||||
* @var TuneSetting
|
||||
*/
|
||||
public $setting;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->collection = new Collection();
|
||||
}
|
||||
|
||||
public function newCollection()
|
||||
{
|
||||
$this->collection = new Collection();
|
||||
}
|
||||
public function appendToCollection($key, $data)
|
||||
{
|
||||
$allowedKeys = ['A', 'C', 'D', 'H', 'R', 'F', 'B', 'M', 'L', 'N', 'O', 'S', 'Z'];
|
||||
|
||||
if (in_array($key, $allowedKeys)) {
|
||||
$array = $this->collection->$key;
|
||||
$array[] = $data;
|
||||
$this->setOnCollection($key, $array);
|
||||
}
|
||||
}
|
||||
|
||||
public function setOnCollection($key, $data)
|
||||
{
|
||||
$allowedKeys = ['A', 'C', 'D', 'H', 'R', 'F', 'B', 'M', 'L', 'N', 'O', 'S', 'Z'];
|
||||
|
||||
if (in_array($key, $allowedKeys)) {
|
||||
$this->collection->$key = $data;
|
||||
}
|
||||
}
|
||||
|
||||
public function getCollection()
|
||||
{
|
||||
return $this->collection;
|
||||
}
|
||||
|
||||
|
||||
public function newTune()
|
||||
{
|
||||
$this->tune = new Tune();
|
||||
}
|
||||
|
||||
public function appendToTune($key, $data)
|
||||
{
|
||||
$array = $this->setting->tune->$key;
|
||||
$array[] = $data;
|
||||
$this->setOnTune($key, $array);
|
||||
}
|
||||
|
||||
public function setOnTune($key, $data)
|
||||
{
|
||||
if ($key == 'X') {
|
||||
$key = 'x_id';
|
||||
}
|
||||
$this->setting->tune->$key = $data;
|
||||
}
|
||||
|
||||
|
||||
public function newSetting()
|
||||
{
|
||||
$this->setting = new TuneSetting();
|
||||
$this->setting->tune = $this->tune;
|
||||
}
|
||||
|
||||
public function appendToSetting($key, $data)
|
||||
{
|
||||
$array = $this->setting->$key;
|
||||
$array[] = $data;
|
||||
$this->setOnSetting($key, $array);
|
||||
}
|
||||
|
||||
public function setOnSetting($key, $data)
|
||||
{
|
||||
$this->setting->$key = $data;
|
||||
}
|
||||
|
||||
|
||||
public function newPerson(array $data)
|
||||
{
|
||||
$person = new Person();
|
||||
foreach ($data as $key => $value) {
|
||||
$person->{$key} = $value;
|
||||
}
|
||||
|
||||
return $person;
|
||||
}
|
||||
|
||||
public function storeTune($music)
|
||||
{
|
||||
$this->setting->music = $music;
|
||||
$this->setting->tune = $this->tune;
|
||||
$this->collection->appendSetting($this->setting);
|
||||
}
|
||||
|
||||
|
||||
public function getTunes()
|
||||
{
|
||||
$this->collection->getSettings();
|
||||
}
|
||||
|
||||
public function removeTune($tuneIndex)
|
||||
{}
|
||||
|
||||
public function toAbc()
|
||||
{
|
||||
$this->toJson();
|
||||
}
|
||||
|
||||
public function toJson()
|
||||
{
|
||||
$this->collection->toJson();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Laravel5;
|
||||
|
||||
trait AttributesTrait
|
||||
{
|
||||
|
||||
public function getAttr($type)
|
||||
{
|
||||
return $this->$type;
|
||||
}
|
||||
|
||||
public function loadAttr($type)
|
||||
{
|
||||
$result = [];
|
||||
call_user_func([$this->attributesClass, $type.'s'], [$this->id])
|
||||
->get()
|
||||
->each(function ($item, $key) use (&$result) {
|
||||
$result[] = $item->string;
|
||||
});
|
||||
if (! empty($result)) {
|
||||
$this->setAttr($type, $result);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAttr($type, array $values)
|
||||
{
|
||||
$this->$type = $values;
|
||||
}
|
||||
|
||||
public function saveAttr($type)
|
||||
{
|
||||
$values = $this->$type;
|
||||
if (! empty($values)) {
|
||||
$instance = new $this->attributesClass();
|
||||
foreach ($values as $key => $value) {
|
||||
$attr = $instance->attr($type, $value, $this)->first();
|
||||
if (!$attr) {
|
||||
$attr = new $this->attributesClass();
|
||||
}
|
||||
|
||||
$attr->fill([
|
||||
'tune_id' => $this->id,
|
||||
'setting_id' => $this->id,
|
||||
'collection_id' => $this->id,
|
||||
'type' => $type,
|
||||
'string' => $value,
|
||||
'ordering' => $key,
|
||||
]);
|
||||
$attr->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function newFromBuilder($attributes = [], $connection = null)
|
||||
{
|
||||
$class = new \ReflectionClass($this->attributesClass);
|
||||
$model = parent::newFromBuilder($attributes, $connection);
|
||||
foreach ($class->getStaticPropertyValue('types') as $attribute) {
|
||||
$model = $model->loadAttr($attribute);
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function saveAttributes(array $options = [])
|
||||
{
|
||||
$class = new \ReflectionClass($this->attributesClass);
|
||||
foreach ($class->getStaticPropertyValue('types') as $attribute) {
|
||||
$this->saveAttr($attribute);
|
||||
}
|
||||
$saved = parent::save($options);
|
||||
|
||||
return $saved;
|
||||
}
|
||||
}
|
||||
@@ -1,251 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Laravel5;
|
||||
|
||||
|
||||
class Collection extends ValidatingModel implements \Countable
|
||||
{
|
||||
use AttributesTrait, UuidTrait;
|
||||
|
||||
/**
|
||||
* for AttributesTrait
|
||||
* @var string
|
||||
*/
|
||||
protected $attributesClass = CollectionAttribute::class;
|
||||
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'collections';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['name'];
|
||||
|
||||
protected $hidden = [
|
||||
'id',
|
||||
'Author',
|
||||
'Composer',
|
||||
'Discography',
|
||||
'Rhythm',
|
||||
'History',
|
||||
'File',
|
||||
'Book',
|
||||
'meter',
|
||||
'Note',
|
||||
'Origin',
|
||||
'Source',
|
||||
'Transcriber',
|
||||
];
|
||||
|
||||
protected $appends = ['A','C','D','H','R','F','B','N', 'O','S','Z',];
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Support\Collection
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
public function appendSetting($setting)
|
||||
{
|
||||
if (! $this->settings) {
|
||||
$this->settings = new \Illuminate\Support\Collection();
|
||||
}
|
||||
|
||||
$this->settings->push($setting);
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
public function getSettings()
|
||||
{
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
public function save(array $options = [])
|
||||
{
|
||||
if (! empty($this->settings)) {
|
||||
foreach ($this->settings as $setting) {
|
||||
$saved = $setting->save();
|
||||
}
|
||||
}
|
||||
return $this->saveAttributes($options);
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************
|
||||
* mutators and accessors
|
||||
*/
|
||||
|
||||
public function getMAttribute()
|
||||
{
|
||||
return $this->attributes['meter'];
|
||||
}
|
||||
|
||||
public function setMAttribute($value)
|
||||
{
|
||||
return $this->attributes['meter'] = $value;
|
||||
}
|
||||
|
||||
protected $Author;
|
||||
public function getAAttribute()
|
||||
{
|
||||
return $this->getAttr('Author');
|
||||
}
|
||||
|
||||
public function setAAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Author', $values);
|
||||
}
|
||||
|
||||
protected $Composer;
|
||||
public function getCAttribute()
|
||||
{
|
||||
return $this->getAttr('Composer');
|
||||
}
|
||||
|
||||
public function setCAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Composer', $values);
|
||||
}
|
||||
|
||||
protected $Discography;
|
||||
public function getDAttribute()
|
||||
{
|
||||
return $this->getAttr('Discography');
|
||||
}
|
||||
|
||||
public function setDAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Discography', $values);
|
||||
}
|
||||
|
||||
protected $Rhythm;
|
||||
public function getRAttribute()
|
||||
{
|
||||
return $this->getAttr('Rhythm');
|
||||
}
|
||||
|
||||
public function setRAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Rhythm', $values);
|
||||
}
|
||||
|
||||
protected $History;
|
||||
public function getHAttribute()
|
||||
{
|
||||
return $this->getAttr('History');
|
||||
}
|
||||
|
||||
public function setHAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('History', $values);
|
||||
}
|
||||
|
||||
protected $File;
|
||||
public function getFAttribute()
|
||||
{
|
||||
return $this->getAttr('File');
|
||||
}
|
||||
|
||||
public function setFAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('File', $values);
|
||||
}
|
||||
|
||||
protected $Book;
|
||||
public function getBAttribute()
|
||||
{
|
||||
return $this->getAttr('Book');
|
||||
}
|
||||
|
||||
public function setBAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Book', $values);
|
||||
}
|
||||
|
||||
protected $Note;
|
||||
public function getNAttribute()
|
||||
{
|
||||
return $this->getAttr('Note');
|
||||
}
|
||||
|
||||
public function setNAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Note', $values);
|
||||
}
|
||||
|
||||
protected $Origin;
|
||||
public function getOAttribute()
|
||||
{
|
||||
return $this->getAttr('Origin');
|
||||
}
|
||||
|
||||
public function setOAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Origin', $values);
|
||||
}
|
||||
|
||||
protected $Source;
|
||||
public function getSAttribute()
|
||||
{
|
||||
return $this->getAttr('Source');
|
||||
}
|
||||
|
||||
public function setSAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Source', $values);
|
||||
}
|
||||
|
||||
protected $Transcriber;
|
||||
public function getZAttribute()
|
||||
{
|
||||
return $this->getAttr('Transcriber');
|
||||
}
|
||||
|
||||
public function setZAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Transcriber', $values);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* implementation of Countable::count()
|
||||
*
|
||||
* @return int the number of tunes in the collection
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->settings);
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
$arr = parent::toArray();
|
||||
|
||||
foreach ($this->settings->toArray() as $key => $setting) {
|
||||
$arr[$key+1] = $setting;
|
||||
}
|
||||
|
||||
foreach (['A', 'C', 'Z'] as $attr) {
|
||||
if (isset($arr[$attr])) {
|
||||
foreach ($arr[$attr] as $key => $person) {
|
||||
$arr[$attr][$key] = $person->name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$arr['M'] = $this->meter;
|
||||
foreach ($arr as $key => $val) {
|
||||
if (empty($val)) {
|
||||
unset($arr[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
return $arr;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,190 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Laravel5;
|
||||
|
||||
use XaiCorp\AbcParser\Traits\ValidationTrait;
|
||||
|
||||
class CollectionAttribute extends ValidatingModel
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'collection_attributes';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $validationRules = [
|
||||
'collection_id' => 'required|exists:collections,id',
|
||||
'type' => 'required|string|in:Author,Composer,Discography,Rhythm,History,File,Book,Note,Source,Transcriber',
|
||||
'string' => 'required|string|max:255',
|
||||
'ordering' => 'required|integer'
|
||||
];
|
||||
|
||||
/**
|
||||
* supported attribute types
|
||||
* @var array
|
||||
*/
|
||||
public static $types = [
|
||||
'Author',
|
||||
'Composer',
|
||||
'Discography',
|
||||
'Rhythm',
|
||||
'History',
|
||||
'File',
|
||||
'Book',
|
||||
'Note',
|
||||
'Origin',
|
||||
'Source',
|
||||
'Transcriber',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['collection_id', 'type', 'string', 'ordering'];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for arrays.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = ['collection_id', 'type', 'ordering'];
|
||||
|
||||
public static function getTypes()
|
||||
{
|
||||
return self::$types;
|
||||
}
|
||||
|
||||
/*****************************************************************
|
||||
* Scopes
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeAuthors($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Author')->where('collection_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeComposers($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Composer')->where('collection_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeDiscographys($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Discography')->where('collection_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeFiles($query, $id)
|
||||
{
|
||||
return $query->where('type', 'File')->where('collection_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeRhythms($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Rhythm')->where('collection_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeHistorys($query, $id)
|
||||
{
|
||||
return $query->where('type', 'History')->where('collection_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeBooks($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Book')->where('collection_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeNotes($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Note')->where('collection_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeOrigins($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Origin')->where('collection_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeSources($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Source')->where('collection_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeTranscribers($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Transcriber')->where('collection_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $attr
|
||||
* @param mixed $value
|
||||
* @param \XaiCorp\AbcParser\Models\Laravel5\Tune|null $collection
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeAttr($query, $attr, $value, Collection $collection = null)
|
||||
{
|
||||
if ($collection) {
|
||||
$query = $query->where('collection_id', $collection->id);
|
||||
}
|
||||
return $query->where('type', strtolower($attr))->where('string', $value);
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Laravel5;
|
||||
|
||||
class Person extends ValidatingModel
|
||||
{
|
||||
use UuidTrait;
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'persons';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['name', 'email'];
|
||||
|
||||
protected $validationRules = [
|
||||
'id' => 'required',
|
||||
'name' => 'required|string|max:63',
|
||||
'email' => 'required|email|max:255'
|
||||
];
|
||||
|
||||
protected $hidden = ['email'];
|
||||
|
||||
//Relationships
|
||||
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Laravel5;
|
||||
|
||||
class Tune extends ValidatingModel
|
||||
{
|
||||
use AttributesTrait, UuidTrait;
|
||||
|
||||
/**
|
||||
* for AttributesTrait
|
||||
* @var string
|
||||
*/
|
||||
protected $attributesClass = TuneAttribute::class;
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'tunes';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $validationRules = [
|
||||
'x_id' => 'integer|min:1',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['x_id', 'X', 'T'];
|
||||
|
||||
protected $hidden = ['id', 'x_id', 'Title', 'Group', 'Origin', 'Rhythm', 'History'];
|
||||
|
||||
protected $appends = ['T', 'G', 'O', 'R', 'H'];
|
||||
|
||||
public function save(array $options = [])
|
||||
{
|
||||
$savedTune = true;
|
||||
|
||||
app('db')->beginTransaction();
|
||||
$savedAttr = $this->saveAttributes($options);
|
||||
if (isset($this->tune)) {
|
||||
$savedTune = $this->tune->save();
|
||||
}
|
||||
|
||||
if ($savedAttr && $savedTune) {
|
||||
app('db')->commit();
|
||||
return true;
|
||||
} else {
|
||||
app('db')->rollBack();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************
|
||||
* mutators and accessors
|
||||
*/
|
||||
|
||||
protected $Title;
|
||||
public function getTAttribute()
|
||||
{
|
||||
return $this->getAttr('Title');
|
||||
}
|
||||
|
||||
public function setTAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Title', $values);
|
||||
}
|
||||
|
||||
protected $Group;
|
||||
public function getGAttribute()
|
||||
{
|
||||
return $this->getAttr('Group');
|
||||
}
|
||||
public function setGAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Group', $values);
|
||||
}
|
||||
|
||||
public function getOAttribute()
|
||||
{
|
||||
return $this->getAttr('Origin');
|
||||
}
|
||||
|
||||
protected $Origin;
|
||||
public function setOAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Origin', $values);
|
||||
}
|
||||
|
||||
protected $Rhythm;
|
||||
public function getRAttribute()
|
||||
{
|
||||
return $this->getAttr('Rhythm');
|
||||
}
|
||||
|
||||
public function setRAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Rhythm', $values);
|
||||
}
|
||||
|
||||
protected $History;
|
||||
public function getHAttribute()
|
||||
{
|
||||
return $this->getAttr('History');
|
||||
}
|
||||
|
||||
public function setHAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('History', $values);
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
$arr = parent::toArray();
|
||||
|
||||
foreach (['A','C'] as $attr) {
|
||||
if (isset($arr[$attr])) {
|
||||
foreach ($arr[$attr] as $key => $person) {
|
||||
$arr[$attr][$key] = $person->name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($arr as $key => $val) {
|
||||
if (empty($val)) {
|
||||
unset($arr[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
return $arr;
|
||||
}
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Laravel5;
|
||||
|
||||
use XaiCorp\AbcParser\Traits\ValidationTrait;
|
||||
|
||||
class TuneAttribute extends ValidatingModel
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'tune_attributes';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $validationRules = [
|
||||
'tune_id' => 'required|exists:tunes,id',
|
||||
'type' => 'required|string|in:Title,Group,Origin,Rhythm,History',
|
||||
'string' => 'required|string|max:255',
|
||||
'ordering' => 'required|integer'
|
||||
];
|
||||
|
||||
/**
|
||||
* supported attribute types
|
||||
* @var array
|
||||
*/
|
||||
public static $types = [
|
||||
'Title',
|
||||
'Group',
|
||||
'Origin',
|
||||
'Rhythm',
|
||||
'History'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['tune_id', 'type', 'string', 'ordering'];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for arrays.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = ['tune_id', 'type', 'ordering'];
|
||||
|
||||
public static function getTypes()
|
||||
{
|
||||
return self::$types;
|
||||
}
|
||||
|
||||
/*****************************************************************
|
||||
* Scopes
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeTitle($query, $value)
|
||||
{
|
||||
return $query->where('type', 'Title')->where('string', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeTitles($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Title')->where('tune_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeGroups($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Group')->where('tune_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeOrigins($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Origin')->where('tune_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeRhythms($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Rhythm')->where('tune_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeHistorys($query, $id)
|
||||
{
|
||||
return $query->where('type', 'History')->where('tune_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $attr
|
||||
* @param mixed $value
|
||||
* @param \XaiCorp\AbcParser\Models\Laravel5\Tune|null $tune
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeAttr($query, $attr, $value, Tune $tune = null)
|
||||
{
|
||||
if ($tune) {
|
||||
$query = $query->where('tune_id', $tune->id);
|
||||
}
|
||||
return $query->where('type', strtolower($attr))->where('string', $value);
|
||||
}
|
||||
}
|
||||
@@ -1,173 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Laravel5;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model as BaseModel;
|
||||
use XaiCorp\AbcParser\Traits\ValidationTrait;
|
||||
|
||||
class TuneSetting extends ValidatingModel
|
||||
{
|
||||
use AttributesTrait, UuidTrait;
|
||||
|
||||
/**
|
||||
* for AttributesTrait
|
||||
* @var string
|
||||
*/
|
||||
protected $attributesClass = TuneSettingAttribute::class;
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'tune_settings';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $validationRules = [
|
||||
'tune_id' => 'required|exists:tunes,id',
|
||||
'meter' => 'string|max:3',
|
||||
'keysig' => 'string|max:5',
|
||||
'filename' => 'string|max:255',
|
||||
'tempo' => 'string|max:10',
|
||||
'L' => 'string|max:5',
|
||||
'music' => 'string',
|
||||
'parts' => 'string|max:255'
|
||||
];
|
||||
|
||||
public static $regex_Q = '/\d\/\d\d?=\d{2,3}/i';
|
||||
public static $regex_L = '/\d\/\d{1,2}/i';
|
||||
public static $regex_M = '/((C|C\|)|\d{1,2}\/\d{1,3})/i';
|
||||
public static $regex_K = '/[a-zA-G]{1,5}/i';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['tune_id', 'meter', 'keysig', 'filename', 'tempo', 'L', 'music', 'parts'];
|
||||
|
||||
protected $hidden = ['id', 'tune_id', 'Transcriber', 'Note', 'Discography', 'Source', 'Word', 'Book'];
|
||||
|
||||
protected $appends = ['Z', 'N', 'D', 'S', 'W', 'B'];
|
||||
|
||||
public function save(array $options = [])
|
||||
{
|
||||
return $this->saveAttributes($options);
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************
|
||||
* mutators and accessors
|
||||
*/
|
||||
|
||||
public function setMusicAttribute($value)
|
||||
{
|
||||
$this->attributes['music'] = trim($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @var Tune
|
||||
*/
|
||||
protected $tune;
|
||||
|
||||
public function setTuneAttribute(Tune $tune)
|
||||
{
|
||||
$this->tune = $tune;
|
||||
}
|
||||
|
||||
public function getTuneAttribute()
|
||||
{
|
||||
return $this->tune;
|
||||
}
|
||||
|
||||
protected $Transcriber;
|
||||
public function getZAttribute()
|
||||
{
|
||||
return $this->getAttr('Transcriber');
|
||||
}
|
||||
|
||||
public function setZAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Transcriber', $values);
|
||||
}
|
||||
|
||||
protected $Note;
|
||||
public function getNAttribute()
|
||||
{
|
||||
return $this->getAttr('Note');
|
||||
}
|
||||
|
||||
public function setNAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Note', $values);
|
||||
}
|
||||
|
||||
protected $Discography;
|
||||
public function getDAttribute()
|
||||
{
|
||||
return $this->getAttr('Discography');
|
||||
}
|
||||
|
||||
public function setDAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Discography', $values);
|
||||
}
|
||||
|
||||
protected $Source;
|
||||
public function getSAttribute()
|
||||
{
|
||||
return $this->getAttr('Source');
|
||||
}
|
||||
|
||||
public function setSAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Source', $values);
|
||||
}
|
||||
|
||||
protected $Word;
|
||||
public function getWAttribute()
|
||||
{
|
||||
return $this->getAttr('Word');
|
||||
}
|
||||
|
||||
public function setWAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Word', $values);
|
||||
}
|
||||
|
||||
protected $Book;
|
||||
public function getBAttribute()
|
||||
{
|
||||
return $this->getAttr('Book');
|
||||
}
|
||||
|
||||
public function setBAttribute(array $values)
|
||||
{
|
||||
$this->setAttr('Book', $values);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
$arr = parent::toArray();
|
||||
|
||||
if (isset($arr['Z'])) {
|
||||
foreach ($arr['Z'] as $key => $person) {
|
||||
$arr['Z'][$key] = $person->name;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($arr as $key => $val) {
|
||||
if (empty($val)) {
|
||||
unset($arr[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$tune = $this->tune->toArray();
|
||||
return array_merge($this->tune->toArray(), $arr);
|
||||
}
|
||||
}
|
||||
@@ -1,145 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Laravel5;
|
||||
|
||||
use XaiCorp\AbcParser\Traits\ValidationTrait;
|
||||
|
||||
class TuneSettingAttribute extends ValidatingModel
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'tune_setting_attributes';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $validationRules = [
|
||||
'setting_id' => 'required|exists:tune_settings,id',
|
||||
'type' => 'required|string|in:Transcriber,Note,Discography,Source,Word,Book',
|
||||
'string' => 'required|string|max:255',
|
||||
'ordering' => 'required|integer'
|
||||
];
|
||||
|
||||
/**
|
||||
* supported attribute types
|
||||
* @var array
|
||||
*/
|
||||
public static $types = [
|
||||
'Transcriber',
|
||||
'Note',
|
||||
'Discography',
|
||||
'Source',
|
||||
'Word',
|
||||
'Book'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['setting_id', 'type', 'string', 'ordering'];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for arrays.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = ['setting_id', 'type', 'ordering'];
|
||||
|
||||
public static function getTypes()
|
||||
{
|
||||
return self::$types;
|
||||
}
|
||||
|
||||
/*****************************************************************
|
||||
* Scopes
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeTranscriber($query, $value)
|
||||
{
|
||||
return $query->where('type', 'Transcriber')->where('string', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeTranscribers($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Transcriber')->where('setting_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeNotes($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Note')->where('setting_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeDiscographys($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Discography')->where('setting_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeSources($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Source')->where('setting_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeWords($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Word')->where('setting_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param integer $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeBooks($query, $id)
|
||||
{
|
||||
return $query->where('type', 'Book')->where('setting_id', $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $attr
|
||||
* @param mixed $value
|
||||
* @param \XaiCorp\AbcParser\Models\Laravel5\Tune|null $setting
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeAttr($query, $attr, $value, TuneSetting $setting = null)
|
||||
{
|
||||
if ($setting) {
|
||||
$query = $query->where('setting_id', $setting->id);
|
||||
}
|
||||
return $query->where('type', strtolower($attr))->where('string', $value);
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Memory;
|
||||
|
||||
use XaiCorp\AbcParser\Interfaces\Builder;
|
||||
use XaiCorp\AbcParser\Interfaces\Exporter;
|
||||
use XaiCorp\AbcParser\Interfaces\Manipulator;
|
||||
use Enzyme\Axiom\Repositories\RepositoryInterface;
|
||||
|
||||
class Abc implements Builder
|
||||
{
|
||||
protected $collection;
|
||||
|
||||
protected $currentTune;
|
||||
|
||||
protected $currentSetting;
|
||||
|
||||
protected $tuneRepository;
|
||||
|
||||
protected $settingRepository;
|
||||
|
||||
protected $personRepository;
|
||||
|
||||
/**
|
||||
* @var \XaiCorp\AbcParser\Interfaces\Repository
|
||||
*/
|
||||
protected $collectionRepository;
|
||||
|
||||
public function __construct(RepositoryInterface $collectionRepository)
|
||||
{
|
||||
$this->collectionRepository = $collectionRepository;
|
||||
}
|
||||
|
||||
public function newCollection()
|
||||
{
|
||||
$this->collection = new TuneCollection();
|
||||
}
|
||||
|
||||
public function appendToCollection($key, $data)
|
||||
{
|
||||
$this->collection->append($key, $data);
|
||||
}
|
||||
|
||||
public function setOnCollection($key, $data)
|
||||
{
|
||||
$this->collection->set($key, $data);
|
||||
}
|
||||
|
||||
public function getCollection()
|
||||
{
|
||||
return count($this->collection) ? $this->collection : false;
|
||||
}
|
||||
|
||||
public function newTune()
|
||||
{
|
||||
$this->currentTune = new Tune();
|
||||
}
|
||||
|
||||
public function appendToTune($key, $data)
|
||||
{
|
||||
$this->currentTune->append($key, $data);
|
||||
}
|
||||
|
||||
public function setOnTune($key, $data)
|
||||
{
|
||||
$this->currentTune->set($key, $data);
|
||||
}
|
||||
|
||||
public function newSetting()
|
||||
{
|
||||
$this->currentSetting = new Setting();
|
||||
}
|
||||
|
||||
public function appendToSetting($key, $data)
|
||||
{
|
||||
$this->currentSetting->append($key, $data);
|
||||
}
|
||||
|
||||
public function setOnSetting($key, $data)
|
||||
{
|
||||
$this->currentSetting->set($key, $data);
|
||||
}
|
||||
|
||||
public function newPerson(array $data)
|
||||
{
|
||||
return new Person($data);
|
||||
}
|
||||
|
||||
public function storeTune($music)
|
||||
{
|
||||
$this->currentSetting->set('music', trim($music));
|
||||
$this->currentTune->append('collection', $this->currentSetting);
|
||||
$this->collection->append('collection', $this->currentTune);
|
||||
|
||||
// save to repositories
|
||||
$this->collectionRepository->add($this->collection);
|
||||
}
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Memory;
|
||||
|
||||
abstract class BaseIterator extends BaseObject implements \Iterator, \Countable, \ArrayAccess
|
||||
{
|
||||
|
||||
private $position = 0;
|
||||
protected $collection = array();
|
||||
|
||||
/********************************************************
|
||||
*
|
||||
* Implementation of Countable
|
||||
*
|
||||
********************************************************/
|
||||
|
||||
/**
|
||||
* implementation of Countable::count()
|
||||
*
|
||||
* @return int the number of tunes in the collection
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->collection);
|
||||
}
|
||||
|
||||
/********************************************************
|
||||
*
|
||||
* Implementation of ArrayAccess
|
||||
*
|
||||
********************************************************/
|
||||
|
||||
public function offsetExists ( $offset )
|
||||
{
|
||||
return isset($this->collection[$offset]);
|
||||
}
|
||||
|
||||
public function offsetGet ( $offset )
|
||||
{
|
||||
return isset($this->collection[$offset])? $this->collection[$offset] : null;
|
||||
}
|
||||
|
||||
public function offsetSet ( $offset, $value )
|
||||
{
|
||||
$offset = ($offset === NULL) ? count($this->collection) : $offset;
|
||||
if(is_a($value, BaseObject::class)) { $this->collection[$offset] = $value; }
|
||||
}
|
||||
|
||||
public function offsetUnset ( $offset )
|
||||
{
|
||||
unset($this->collection[$offset]);
|
||||
}
|
||||
|
||||
/********************************************************
|
||||
*
|
||||
* Implementation of Iterator
|
||||
*
|
||||
********************************************************/
|
||||
|
||||
/**
|
||||
* implementation of Iterator::current()
|
||||
*
|
||||
* @return Tune
|
||||
*/
|
||||
public function current ()
|
||||
{
|
||||
return $this->collection[$this->position];
|
||||
}
|
||||
|
||||
/**
|
||||
* implementation of Iterator::key()
|
||||
*
|
||||
* @return Scalar the current index
|
||||
*/
|
||||
public function key ()
|
||||
{
|
||||
return $this->position;
|
||||
}
|
||||
|
||||
/**
|
||||
* implementation of Iterator::next()
|
||||
*
|
||||
* sets the position to the next index
|
||||
*/
|
||||
public function next ()
|
||||
{
|
||||
++$this->position;
|
||||
}
|
||||
|
||||
/**
|
||||
* implementation of Iterator::rewind()
|
||||
*
|
||||
* sets the position to the beginning of the collection
|
||||
*/
|
||||
public function rewind ()
|
||||
{
|
||||
$this->position = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* implementation of Iterator::valid()
|
||||
*
|
||||
* tests that there is a tune at the current position
|
||||
* @return boolean
|
||||
*/
|
||||
public function valid ()
|
||||
{
|
||||
$is_valid = isset($this->collection[$this->position]);
|
||||
if(! $is_valid) { $this->rewind(); }
|
||||
return $is_valid;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Memory;
|
||||
|
||||
abstract class BaseObject
|
||||
{
|
||||
public static $propertyNames = [];
|
||||
protected $change_hash;
|
||||
|
||||
/**
|
||||
* return true if the public property values are equal
|
||||
*
|
||||
* @param $ob1 first tune to check
|
||||
* @param $ob2 2nd tune
|
||||
* @return (Boolean) TRUE if the 2 tunes are identical, FALSE otherwise
|
||||
*/
|
||||
public function equals ($ob1, $ob2)
|
||||
{
|
||||
if( serialize($ob1) != serialize($ob2) ) { return FALSE; }
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->change_hash = $this->generate_hash();
|
||||
}
|
||||
|
||||
public static function propertyNames()
|
||||
{
|
||||
return static::propertyNames;
|
||||
}
|
||||
|
||||
protected abstract function generate_hash();
|
||||
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Memory;
|
||||
|
||||
class Person extends BaseObject
|
||||
{
|
||||
private $person_id = 0;
|
||||
|
||||
private $name = '';
|
||||
private $email = '';
|
||||
|
||||
// private $change_hash;
|
||||
|
||||
/**
|
||||
* Person constructor
|
||||
*
|
||||
* @param $params (array) list of parameters to set at initialization
|
||||
*/
|
||||
function __construct($params = array()) {
|
||||
//Init parameters
|
||||
foreach($params as $key=>$val)
|
||||
{
|
||||
$this->set($key, $val);
|
||||
}
|
||||
|
||||
$this->change_hash = $this->generate_hash();
|
||||
}
|
||||
|
||||
/**
|
||||
* magic setter
|
||||
* just passes to regular set method which contains validation
|
||||
*/
|
||||
public function __set($key, $value)
|
||||
{
|
||||
$this->set($key, $value);
|
||||
}
|
||||
|
||||
public function set($key, $value)
|
||||
{
|
||||
$retVar = FALSE;
|
||||
switch($key)
|
||||
{
|
||||
case 'person_id' :
|
||||
if( $this->person_id === 0 && is_numeric($value) && intval($value) > 0 )
|
||||
{
|
||||
$this->{$key} = intval($value);
|
||||
$retVar = TRUE;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'name':
|
||||
if(is_string($value) && ! is_null($value))
|
||||
{
|
||||
$this->{$key} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
break;
|
||||
case 'email':
|
||||
if(filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
||||
$this->{$key} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
return $retVar;
|
||||
}
|
||||
|
||||
public function get($key)
|
||||
{
|
||||
if ( is_string($key) && isset($this->$key) )
|
||||
{
|
||||
return $this->$key;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* set default values for the person
|
||||
* used when creating a new person from scratch
|
||||
*/
|
||||
// public function init()
|
||||
// {
|
||||
// $this->change_hash = $this->generate_hash();
|
||||
// }
|
||||
|
||||
/**
|
||||
* Test if the data has changed
|
||||
*
|
||||
* @return (bool) True if the data has changed since last load/save
|
||||
*/
|
||||
public function is_changed()
|
||||
{
|
||||
return $this->change_hash !== $this->generate_hash();
|
||||
}
|
||||
|
||||
/********************************************************
|
||||
*
|
||||
* Private functions
|
||||
*
|
||||
********************************************************/
|
||||
|
||||
/**
|
||||
* generate hash for the whole person object
|
||||
*
|
||||
* @returns (string) md5 hash of all attributes
|
||||
*/
|
||||
protected function generate_hash()
|
||||
{
|
||||
$data = array(
|
||||
$this->person_id,
|
||||
$this->name,
|
||||
$this->email
|
||||
);
|
||||
|
||||
return md5(serialize($data));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/********************************************************
|
||||
*
|
||||
* Static functions
|
||||
*
|
||||
********************************************************/
|
||||
|
||||
|
||||
}
|
||||
@@ -1,281 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Memory;
|
||||
|
||||
class Setting extends BaseObject {
|
||||
|
||||
private $settingID = 0;
|
||||
private $tuneID;
|
||||
|
||||
//fields that support multiple values
|
||||
private $Z = array(); //transcriber
|
||||
private $N = array(); //notes
|
||||
private $D = array(); //discography
|
||||
private $S = array(); //source
|
||||
private $W = array();
|
||||
private $B = array(); //Book
|
||||
private $F = array(); //file name
|
||||
|
||||
//fields that only ever have one value
|
||||
private $P = ''; //Parts;
|
||||
private $Q = ''; //tempo
|
||||
private $L = '1/8'; //default note length
|
||||
private $M = '4/4'; //meter
|
||||
|
||||
private $K = 'C'; //key
|
||||
private $music = ''; //multiline string
|
||||
|
||||
// private $music_hash;
|
||||
// private $change_hash;
|
||||
|
||||
public static $regex_Q = '/\d\/\d\d?=\d{2,3}/i';
|
||||
public static $regex_L = '/\d\/\d{1,2}/i';
|
||||
public static $regex_M = '/((C|C\|)|\d{1,2}\/\d{1,3})/i';
|
||||
public static $regex_K = '/[a-zA-G]{1,5}/i';
|
||||
|
||||
public static $propertyNames = array(
|
||||
'Z' => 'Transcriber',
|
||||
'N' => 'Notes',
|
||||
'D' => 'Discography',
|
||||
'S' => 'Source',
|
||||
'W' => 'Words,',
|
||||
'B' => 'Book',
|
||||
'F' => 'Filename',
|
||||
'P' => 'Parts',
|
||||
'Q' => 'Tempo,',
|
||||
'L' => 'default note length',
|
||||
'M' => 'Meter',
|
||||
'K' => 'Key',
|
||||
);
|
||||
|
||||
/**
|
||||
* Tune constructor
|
||||
*
|
||||
* @param $params (array) list of parameters to set at initialization
|
||||
*/
|
||||
function __construct(array $params = array()) {
|
||||
//Init parameters
|
||||
foreach($params as $key=>$val)
|
||||
{
|
||||
$this->set($key, $val);
|
||||
}
|
||||
|
||||
$this->change_hash = $this->generate_hash();
|
||||
}
|
||||
|
||||
|
||||
/********************************************************
|
||||
*
|
||||
* public functions
|
||||
*
|
||||
********************************************************/
|
||||
|
||||
/**
|
||||
* get the value of a private property
|
||||
*
|
||||
* @return (mixed) value of the property or FALSE if property does not exist
|
||||
*/
|
||||
public function get($property)
|
||||
{
|
||||
if(isset($this->{$property})) { return $this->{$property}; }
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* magic setter
|
||||
* just passes to regular set method which contains validation
|
||||
*/
|
||||
public function __set($key, $value)
|
||||
{
|
||||
$this->set($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* set new value for a property
|
||||
* overwrites the existing value
|
||||
*
|
||||
* @return (boolean) TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function set($property, $value)
|
||||
{
|
||||
$retVar = FALSE;
|
||||
|
||||
switch($property)
|
||||
{
|
||||
case 'settingID':
|
||||
if($this->settingID !== 0) { // if settingID is not 0 don't allow setting it
|
||||
$retVar = FALSE;
|
||||
break;
|
||||
}
|
||||
//follow through since this is also a numeric value
|
||||
case 'tuneID':
|
||||
if(is_numeric($value) && $value > 0)
|
||||
{
|
||||
$this->{$property} = (int)$value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'Q':
|
||||
case 'L':
|
||||
case 'M':
|
||||
if ( ! Setting::validate_line($property, $value) ) { return FALSE; }
|
||||
case 'K':
|
||||
case 'P':
|
||||
case 'music':
|
||||
if( ! is_array($value) && ! is_int($value) )
|
||||
{
|
||||
$this->{$property} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
if( is_array($value) && count($value) > 0)
|
||||
{
|
||||
$contains_only = TRUE;
|
||||
foreach($value as $person)
|
||||
{
|
||||
if(! is_a($person, __NAMESPACE__."\Person"))
|
||||
{
|
||||
$contains_only = FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( $contains_only )
|
||||
{
|
||||
$this->{$property} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'F':
|
||||
case 'N':
|
||||
case 'D':
|
||||
case 'S':
|
||||
case 'W':
|
||||
case 'B':
|
||||
if( is_array($value) )
|
||||
{
|
||||
$this->{$property} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
break;
|
||||
|
||||
default: // non-existant or non-editable property
|
||||
break;
|
||||
}
|
||||
|
||||
return $retVar;
|
||||
}
|
||||
|
||||
/**
|
||||
* append a value to a property's array
|
||||
*
|
||||
* @param (string) $property name
|
||||
* @param (mixed) $value to add to the array
|
||||
*
|
||||
* @return (Boolean) TRUE on success, FALSE otherwise
|
||||
*/ public function append($property, $value)
|
||||
{
|
||||
if( ! is_array($value) )
|
||||
{
|
||||
switch($property)
|
||||
{
|
||||
case 'Z':
|
||||
if( ! is_a($value, __NAMESPACE__."\Person") ) {
|
||||
break;
|
||||
}
|
||||
case 'N':
|
||||
case 'D':
|
||||
case 'S':
|
||||
case 'W':
|
||||
case 'B':
|
||||
case 'F':
|
||||
$this->{$property}[] = $value;
|
||||
return TRUE;
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->set($property, $value);
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the data has changed
|
||||
*
|
||||
* @return (bool) True if the data has changed since last load/save
|
||||
*/
|
||||
public function is_changed()
|
||||
{
|
||||
return $this->change_hash !== $this->generate_hash();
|
||||
}
|
||||
|
||||
// public function init()
|
||||
// {
|
||||
// $this->change_hash = $this->generate_hash();
|
||||
// }
|
||||
|
||||
|
||||
/********************************************************
|
||||
*
|
||||
* Private functions
|
||||
*
|
||||
********************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* generate hash for the whole person object
|
||||
*
|
||||
* @returns (string) md5 hash of all attributes
|
||||
*/
|
||||
protected function generate_hash()
|
||||
{
|
||||
$data = array(
|
||||
$this->tuneID,
|
||||
$this->settingID,
|
||||
//TODO: take into account the transcribers
|
||||
//$this->Z,
|
||||
$this->N,
|
||||
$this->D,
|
||||
$this->S,
|
||||
$this->W,
|
||||
$this->B,
|
||||
$this->F,
|
||||
$this->P,
|
||||
$this->Q,
|
||||
$this->L,
|
||||
$this->M,
|
||||
$this->K,
|
||||
$this->music,
|
||||
);
|
||||
|
||||
return md5(serialize($data));
|
||||
}
|
||||
|
||||
/********************************************************
|
||||
*
|
||||
* Static functions
|
||||
*
|
||||
********************************************************/
|
||||
|
||||
|
||||
|
||||
protected static function validate_line($key, $data)
|
||||
{
|
||||
$result = TRUE;
|
||||
if(isset(Setting::${"regex_$key"}))
|
||||
{
|
||||
$regex = Setting::${"regex_$key"};
|
||||
$result = preg_match($regex, serialize($data));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,254 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Memory;
|
||||
|
||||
class Tune extends BaseIterator {
|
||||
|
||||
protected $tuneID = 0;
|
||||
// private $position = 0;
|
||||
|
||||
protected $X = "1"; //index
|
||||
protected $T = array(); //titles
|
||||
protected $A = array(); //Author of Lyrics
|
||||
protected $C = array(); //Composer
|
||||
protected $G = array(); //Group
|
||||
protected $H = array(); //History - multiline string
|
||||
protected $O = array(); //Origin
|
||||
protected $R = array(); //Rhythm
|
||||
|
||||
// private $settings = array();
|
||||
|
||||
//Use md5 hashes to check if data has been modified
|
||||
// protected $change_hash;
|
||||
|
||||
public static $propertyNames = array(
|
||||
'X' => 'id',
|
||||
'T' => 'Title',
|
||||
'A' => 'Author',
|
||||
'C' => 'Composer',
|
||||
'G' => 'Group,',
|
||||
'H' => 'History',
|
||||
'O' => 'Origin',
|
||||
'R' => 'Rhythm',
|
||||
);
|
||||
|
||||
/**
|
||||
* Tune constructor
|
||||
*
|
||||
* @param $params (array) list of parameters to set at initialization
|
||||
*/
|
||||
function __construct(array $params = array()) {
|
||||
//Init parameters
|
||||
foreach($params as $key=>$val)
|
||||
{
|
||||
$this->set($key, $val);
|
||||
}
|
||||
|
||||
$this->change_hash = $this->generate_hash();
|
||||
}
|
||||
|
||||
|
||||
/********************************************************
|
||||
*
|
||||
* public functions
|
||||
*
|
||||
********************************************************/
|
||||
|
||||
/**
|
||||
* get the value of a private property
|
||||
*
|
||||
* @return (mixed) value of the property or FALSE if property does not exist
|
||||
*/
|
||||
public function get($property)
|
||||
{
|
||||
if(isset($this->{$property})) { return $this->{$property}; }
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* magic setter
|
||||
* just passes to regular set method which contains validation
|
||||
*/
|
||||
public function __set($key, $value)
|
||||
{
|
||||
$this->set($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* set new value for a property
|
||||
* overwrites the existing value
|
||||
*
|
||||
* @return (boolean) TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function set($property, $value)
|
||||
{
|
||||
$retVar = FALSE;
|
||||
|
||||
switch($property)
|
||||
{
|
||||
case 'tuneID':
|
||||
if($this->tuneID !== 0) { // if tuneID is not 0 don't allow setting it
|
||||
$retVar = FALSE;
|
||||
break;
|
||||
}
|
||||
//follow through since this is also a numeric value
|
||||
case 'X':
|
||||
if(is_numeric($value) && $value > 0)
|
||||
{
|
||||
$this->{$property} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'A':
|
||||
case 'C':
|
||||
if( is_array($value) && count($value) > 0)
|
||||
{
|
||||
$contains_only = TRUE;
|
||||
foreach($value as $key=>$person)
|
||||
{
|
||||
if(! is_a($person, __NAMESPACE__ . '\\' ."Person"))
|
||||
{
|
||||
$contains_only = FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( $contains_only )
|
||||
{
|
||||
$this->{$property} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
case 'G':
|
||||
case 'H':
|
||||
case 'O':
|
||||
case 'R':
|
||||
if(is_array($value))
|
||||
{
|
||||
$this->{$property} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'collection':
|
||||
if( is_array($value) && count($value) > 0)
|
||||
{
|
||||
$only_settings = TRUE;
|
||||
foreach($value as $key=>$setting)
|
||||
{
|
||||
if(! is_a($setting, __NAMESPACE__ . '\\' ."Setting"))
|
||||
{
|
||||
$only_settings = FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( $only_settings )
|
||||
{
|
||||
$this->{$property} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
default: // non-existant or non-editable property
|
||||
break;
|
||||
}
|
||||
|
||||
return $retVar;
|
||||
}
|
||||
|
||||
/**
|
||||
* append a value to a property's array
|
||||
*
|
||||
* @param (string) $property name
|
||||
* @param (mixed) $value to add to the array
|
||||
*
|
||||
* @return (Boolean) TRUE on success, FALSE otherwise
|
||||
*/ public function append($property, $value)
|
||||
{
|
||||
if( ! is_array($value) )
|
||||
{
|
||||
switch($property)
|
||||
{
|
||||
case 'T':
|
||||
case 'A':
|
||||
case 'C':
|
||||
case 'G':
|
||||
case 'H':
|
||||
case 'O':
|
||||
case 'R':
|
||||
$this->{$property}[] = $value;
|
||||
return TRUE;
|
||||
break;
|
||||
|
||||
case 'collection':
|
||||
if( is_a($value, __NAMESPACE__ . '\\' ."Setting") )
|
||||
{
|
||||
$this->{$property}[] = $value;
|
||||
return TRUE;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->set($property, $value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test if the data has changed
|
||||
*
|
||||
* @return (bool) True if the data has changed since last load/save
|
||||
*/
|
||||
public function is_changed()
|
||||
{
|
||||
return $this->change_hash !== $this->generate_hash();
|
||||
}
|
||||
|
||||
/********************************************************
|
||||
*
|
||||
* Private functions
|
||||
*
|
||||
********************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* generate hash for the whole person object
|
||||
*
|
||||
* @returns (string) md5 hash of all attributes
|
||||
*/
|
||||
protected function generate_hash()
|
||||
{
|
||||
$data = array(
|
||||
$this->tuneID,
|
||||
$this->X,
|
||||
$this->T,
|
||||
// $this->A,
|
||||
// $this->C,
|
||||
$this->G,
|
||||
$this->H,
|
||||
$this->O,
|
||||
$this->R,
|
||||
// $this->collection,
|
||||
);
|
||||
|
||||
return md5(serialize($data));
|
||||
}
|
||||
|
||||
/********************************************************
|
||||
*
|
||||
* Static functions
|
||||
*
|
||||
********************************************************/
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,316 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Memory;
|
||||
|
||||
use Enzyme\Axiom\Models\ModelInterface;
|
||||
|
||||
/**
|
||||
* TuneCollection represents a collection of tunes, such a book, manuscript
|
||||
* or abc file.
|
||||
*
|
||||
* a collection can have various parameters to define the collection
|
||||
* in addition to the list of tunes
|
||||
*/
|
||||
class TuneCollection extends BaseIterator implements ModelInterface
|
||||
{
|
||||
|
||||
private $tc_id = 0; // collection id
|
||||
private $cOwner; // collection owner. The user that uploaded it
|
||||
|
||||
// private $tunes = array();
|
||||
|
||||
//abc tunebook header fields
|
||||
private $A = []; //Author / Area
|
||||
private $B = []; //Book
|
||||
private $C = []; //Composer
|
||||
private $D = []; //Discography
|
||||
private $F = []; //file url
|
||||
private $H = []; //History
|
||||
private $L; //note length
|
||||
private $M; //Meter
|
||||
private $N = []; //Notes
|
||||
private $O = []; //Origin
|
||||
private $R; //Rhythm
|
||||
private $S = []; //Source
|
||||
private $Z = []; //Transcription
|
||||
|
||||
|
||||
// public function __construct()
|
||||
// {}
|
||||
|
||||
/********************************************************
|
||||
*
|
||||
* public functions
|
||||
*
|
||||
********************************************************/
|
||||
|
||||
/**
|
||||
* add a tune to the collection
|
||||
*
|
||||
* @param Tune the tune to add
|
||||
*/
|
||||
public function push(Tune $tune)
|
||||
{
|
||||
$this->collection[] = $tune;
|
||||
}
|
||||
|
||||
/**
|
||||
* pop last tune from the end of the collection
|
||||
*
|
||||
* @return Tune
|
||||
*/
|
||||
public function pop()
|
||||
{
|
||||
return array_pop($this->collection);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* magic setter
|
||||
* just passes to regular set method which contains validation
|
||||
*/
|
||||
public function __set($key, $value)
|
||||
{
|
||||
$this->set($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* set new value for a property
|
||||
* overwrites the existing value
|
||||
*
|
||||
* @return (boolean) TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function set($property, $value)
|
||||
{
|
||||
$retVar = FALSE;
|
||||
|
||||
switch($property)
|
||||
{
|
||||
case 'change_hash':
|
||||
$this->generate_hash();
|
||||
break;
|
||||
|
||||
case 'tc_id':
|
||||
if($this->tc_id !== 0) { // if tc_ID is not 0 don't allow setting it
|
||||
$retVar = FALSE;
|
||||
break;
|
||||
}
|
||||
|
||||
if(is_numeric($value) && $value > 0)
|
||||
{
|
||||
$this->{$property} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'A':
|
||||
case 'C':
|
||||
case 'Z':
|
||||
if( is_array($value) && count($value) > 0)
|
||||
{
|
||||
$contains_only = TRUE;
|
||||
foreach($value as $key=>$person)
|
||||
{
|
||||
if(! is_a($person, __NAMESPACE__ . '\\' ."Person"))
|
||||
{
|
||||
$contains_only = FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( $contains_only )
|
||||
{
|
||||
$this->{$property} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'B':
|
||||
case 'D':
|
||||
case 'F':
|
||||
case 'H':
|
||||
case 'N':
|
||||
case 'O':
|
||||
case 'S':
|
||||
if(is_array($value))
|
||||
{
|
||||
$this->{$property} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'collection':
|
||||
if( is_array($value) && count($value) > 0)
|
||||
{
|
||||
$contains_only_tunes = TRUE;
|
||||
foreach($value as $key=>$tune)
|
||||
{
|
||||
if(! is_a($tune, __NAMESPACE__ . '\\' ."Tune"))
|
||||
{
|
||||
$contains_only_tunes = FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( $contains_only_tunes )
|
||||
{
|
||||
$this->{$property} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'L' :
|
||||
case 'M' :
|
||||
case 'R' :
|
||||
if(is_numeric($value)) { break; }
|
||||
case 'cOwner' :
|
||||
if(! is_array($value) )
|
||||
{
|
||||
$this->{$property} = $value;
|
||||
$retVar = TRUE;
|
||||
}
|
||||
break;
|
||||
|
||||
default: // non-existant or non-editable property
|
||||
break;
|
||||
}
|
||||
|
||||
return $retVar;
|
||||
}
|
||||
|
||||
/**
|
||||
* append a value to a property's array
|
||||
*
|
||||
* @param (string) $property name
|
||||
* @param (mixed) $value to add to the array
|
||||
*
|
||||
* @return (Boolean) TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function append($property, $value)
|
||||
{
|
||||
switch($property)
|
||||
{
|
||||
case 'A':
|
||||
case 'C':
|
||||
case 'Z':
|
||||
if(! is_a($value, __NAMESPACE__ . '\\' .'Person')) { break; }
|
||||
//Fall through if we pass the check
|
||||
case 'B':
|
||||
case 'D':
|
||||
case 'F':
|
||||
case 'H':
|
||||
case 'N':
|
||||
case 'O':
|
||||
case 'S':
|
||||
$this->{$property}[] = $value;
|
||||
return TRUE;
|
||||
break;
|
||||
|
||||
case 'collection':
|
||||
if( is_a($value, __NAMESPACE__."\Tune") )
|
||||
{
|
||||
$this->{$property}[] = $value;
|
||||
return TRUE;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return $this->set($property, $value);
|
||||
break;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the value of a private property
|
||||
*
|
||||
* @return (mixed) value of the property or FALSE if property does not exist
|
||||
*/
|
||||
public function get($property)
|
||||
{
|
||||
if(isset($this->{$property})) { return $this->{$property}; }
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/********************************************************
|
||||
*
|
||||
* private functions
|
||||
*
|
||||
********************************************************/
|
||||
|
||||
/**
|
||||
* generate hash for the whole person object
|
||||
*
|
||||
* @returns (string) md5 hash of all attributes
|
||||
*/
|
||||
protected function generate_hash()
|
||||
{
|
||||
$data = array(
|
||||
$this->tc_id,
|
||||
$this->cOwner,
|
||||
$this->A,
|
||||
$this->B,
|
||||
$this->C,
|
||||
$this->D,
|
||||
$this->F,
|
||||
$this->H,
|
||||
$this->L,
|
||||
$this->M,
|
||||
$this->N,
|
||||
$this->O,
|
||||
$this->R,
|
||||
$this->S,
|
||||
$this->Z,
|
||||
// $this->collection,
|
||||
);
|
||||
|
||||
return md5(serialize($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the unique identity for this mode.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function identity()
|
||||
{
|
||||
//TODO:
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether this model has the given attribute set.
|
||||
*
|
||||
* @param string $attribute
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function has($attribute)
|
||||
{
|
||||
//TODO:
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Get the value associated with the given attribute.
|
||||
// *
|
||||
// * @param string $attribute
|
||||
// *
|
||||
// * @return mixed
|
||||
// */
|
||||
// public function get($attribute)
|
||||
// {
|
||||
// //TODO:
|
||||
// }
|
||||
|
||||
/**
|
||||
* Get all the attributes associated with this model.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAll()
|
||||
{
|
||||
//TODO:
|
||||
}
|
||||
}
|
||||
150
src/Parser.php
150
src/Parser.php
@@ -1,150 +0,0 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser;
|
||||
|
||||
use XaiCorp\AbcParser\Interfaces\Builder;
|
||||
|
||||
class Parser {
|
||||
/**
|
||||
* the abc passed to the parser might be a single tune or
|
||||
* it might be a collection of tunes, with extra data outside
|
||||
* of each tune. This external data should be applied to each
|
||||
* tune and the collection.
|
||||
*
|
||||
* When reading the abc, the parser should create a new collection
|
||||
* and a new Tune object for each tune encountered in the abc.
|
||||
* External tools can then deal with that collection as they like.
|
||||
*
|
||||
*/
|
||||
|
||||
//regexp patterns for different lines
|
||||
const LN_VERSION = '/^%abc(-\d\.\d)?$/';
|
||||
const LN_BLANK = '/^\s*$/';
|
||||
const LN_FIELD = '/^\w:(.+)$/';
|
||||
|
||||
const MODE_NO_DATA = 0;
|
||||
const MODE_FILE_HEADER = 1;
|
||||
const MODE_TUNE_HEADER = 2;
|
||||
const MODE_TUNE_BODY = 3;
|
||||
|
||||
const SINGLE_VALUE_KEYS = ['P','Q','L','M','K'];
|
||||
|
||||
public function __construct(Builder $builder)
|
||||
{
|
||||
$this->builder = $builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* extract the attributes from the raw abc
|
||||
* @param <string> $abc string containing the raw abc
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public function parseABC($abc)
|
||||
{
|
||||
//tune is either new or existing data has been loaded
|
||||
//create new setting
|
||||
$mode = self::MODE_FILE_HEADER;
|
||||
$this->builder->newCollection();
|
||||
$tune = null;
|
||||
$setting = null;
|
||||
$inHistory = false;
|
||||
$inTunes = false;
|
||||
$music = "";
|
||||
$keys_for_Settings = '/K|L|M|P|Q|B|N|D|S|W|Z|F/';
|
||||
$keys_for_Tunes = '/H|A|C|G|R|T|O/';
|
||||
//scan abc by line and see what the first char is
|
||||
foreach (preg_split("/(\r?\n)/", $abc) as $line) {
|
||||
|
||||
if (preg_match(self::LN_BLANK, $line)) {
|
||||
if ($mode === self::MODE_TUNE_BODY || $mode === self::MODE_TUNE_HEADER) {
|
||||
$this->builder->storeTune($music);
|
||||
$music = '';
|
||||
}
|
||||
|
||||
$mode = self::MODE_NO_DATA;
|
||||
} elseif ($mode === self::MODE_TUNE_BODY) {
|
||||
$music .= $line . PHP_EOL;
|
||||
} elseif (preg_match(self::LN_VERSION, $line)) {
|
||||
//this line might contain the abc version.
|
||||
//What do we want to do with it?
|
||||
} elseif (preg_match(self::LN_FIELD, $line) || $inHistory) {
|
||||
if (preg_match(self::LN_FIELD, $line)) { $inHistory = FALSE; }
|
||||
//split the line "key:data"
|
||||
if($inHistory) {
|
||||
$key = 'H';
|
||||
$data = $line;
|
||||
} else {
|
||||
$key = substr($line, 0, 1);
|
||||
$data = trim(substr($line, 2));
|
||||
}
|
||||
|
||||
if ($key === 'X') {
|
||||
$mode = self::MODE_TUNE_HEADER;
|
||||
$inTunes = true;
|
||||
$this->builder->newTune();
|
||||
$this->builder->newSetting();
|
||||
$music = '';
|
||||
|
||||
$this->builder->setOnTune('X', $data);
|
||||
|
||||
} elseif (preg_match($keys_for_Tunes, $key)) {
|
||||
if ($key === 'H') {
|
||||
$inHistory = TRUE;
|
||||
} else {
|
||||
$inHistory = FALSE;
|
||||
}
|
||||
if ($key === 'A' || $key === 'C') {
|
||||
$data = $this->builder->newPerson(['name'=>$data]);
|
||||
}
|
||||
if ($mode === self::MODE_FILE_HEADER) {
|
||||
if (in_array($key, self::SINGLE_VALUE_KEYS)) {
|
||||
$this->builder->setOnCollection($key, $data);
|
||||
} else {
|
||||
$this->builder->appendToCollection($key, $data);
|
||||
}
|
||||
} elseif($inTunes) {
|
||||
if (in_array($key, self::SINGLE_VALUE_KEYS)) {
|
||||
$this->builder->setOnTune($key, $data);
|
||||
} else {
|
||||
$this->builder->appendToTune($key, $data);
|
||||
}
|
||||
}
|
||||
|
||||
} elseif (preg_match($keys_for_Settings, $key)) {
|
||||
if ($key === 'K' && $mode === self::MODE_TUNE_HEADER) {
|
||||
$mode = self::MODE_TUNE_BODY;
|
||||
}
|
||||
|
||||
if ($key === 'Z') {
|
||||
$data = $this->builder->newPerson(['name'=>$data]);
|
||||
}
|
||||
|
||||
|
||||
if ($mode == self::MODE_FILE_HEADER) {
|
||||
if (in_array($key, self::SINGLE_VALUE_KEYS)) {
|
||||
$this->builder->setOnCollection($key, $data);
|
||||
} else {
|
||||
$this->builder->appendToCollection($key, $data);
|
||||
}
|
||||
} elseif($inTunes){
|
||||
if (in_array($key, self::SINGLE_VALUE_KEYS)) {
|
||||
$this->builder->setOnSetting($key, $data);
|
||||
} else {
|
||||
$this->builder->appendToSetting($key, $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return $this->builder->getCollection();
|
||||
}
|
||||
|
||||
// private static function _storeTune($music, &$setting, &$tune, &$tuneCollection) {
|
||||
// $setting -> set('music', trim($music));
|
||||
// $tune -> append('collection', $setting);
|
||||
// $tuneCollection -> append('collection', $tune);
|
||||
// }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user