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);
|
||||
// }
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,12 +1,5 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: richard
|
||||
* Date: 11/19/17
|
||||
* Time: 4:49 PM
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Application;
|
||||
namespace Tests\Unit\AbcParser\Application;
|
||||
|
||||
use Enzyme\Axiom\Atoms\StringAtom;
|
||||
use XaiCorp\AbcParser\Application\PersonFactory;
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
use XaiCorp\AbcParser\Application\UseCases\ExtractTuneFromCollection;
|
||||
use XaiCorp\AbcParser\Application\UseCases\ImportAbcFile;
|
||||
|
||||
class ExtractTuneFromCollectionTest extends \Codeception\Test\Unit
|
||||
{
|
||||
/**
|
||||
* @param string $filename
|
||||
* @return bool|string
|
||||
*/
|
||||
private function getAbc($filename = '/abc/valid_abc_1.abc')
|
||||
{
|
||||
return file_get_contents(codecept_data_dir($filename));
|
||||
}
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$abc = $this->getAbc();
|
||||
$tunes = ImportAbcFile::create()->import($abc);
|
||||
|
||||
$extractor = ExtractTuneFromCollection::create($tunes);
|
||||
|
||||
$this->assertInstanceOf(ExtractTuneFromCollection::class, $extractor);
|
||||
}
|
||||
|
||||
public function testExtractTuneByTitle()
|
||||
{
|
||||
$title = "Emmett's Hedgehog";
|
||||
|
||||
$abc = $this->getAbc('/abc/jigs.abc');
|
||||
$tunes = ImportAbcFile::create()->import($abc);
|
||||
|
||||
$extractor = ExtractTuneFromCollection::create($tunes);
|
||||
|
||||
$result = $extractor->extractTuneByTitle($title);
|
||||
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertNotEquals($tunes, $result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\AbcParser\Application\UseCases;
|
||||
|
||||
use XaiCorp\AbcParser\Application\UseCases\ImportAbcFile;
|
||||
use XaiCorp\AbcParser\Domain\Core\TuneCollection;
|
||||
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
|
||||
|
||||
class ImportAbcFileTest extends \Codeception\Test\Unit
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @return bool|string
|
||||
*/
|
||||
private function getAbc($filename = '/abc/valid_abc_1.abc')
|
||||
{
|
||||
return file_get_contents(codecept_data_dir($filename));
|
||||
}
|
||||
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$importer = ImportAbcFile::create();
|
||||
|
||||
$this->assertInstanceOf(ImportAbcFile::class, $importer);
|
||||
}
|
||||
|
||||
public function testCreateInjectingBuilder()
|
||||
{
|
||||
$builder = new Builder();
|
||||
$importer = ImportAbcFile::create($builder);
|
||||
|
||||
$this->assertInstanceOf(ImportAbcFile::class, $importer);
|
||||
}
|
||||
|
||||
public function testImport()
|
||||
{
|
||||
$abc = $this->getAbc('/abc/jigs.abc');
|
||||
$importer = ImportAbcFile::create();
|
||||
|
||||
$result = $importer->import($abc);
|
||||
|
||||
$this->assertInstanceOf(TuneCollection::class, $result);
|
||||
$this->assertGreaterThan(1, $result->count());
|
||||
}
|
||||
|
||||
public function testImport2FilesConsecutively()
|
||||
{
|
||||
$abc2 = $this->getAbc('/abc/valid_abc_2.abc');
|
||||
$abc3 = $this->getAbc('/abc/valid_abc_3.abc');
|
||||
$importer = ImportAbcFile::create();
|
||||
|
||||
$result1 = $importer->import($abc2);
|
||||
$result2 = $importer->import($abc3);
|
||||
|
||||
$this->assertNotEquals($result1, $result2);
|
||||
}
|
||||
|
||||
public function testImportMultiple()
|
||||
{
|
||||
$abc1 = $this->getAbc('/abc/valid_abc_1.abc');
|
||||
$abc2 = $this->getAbc('/abc/valid_abc_2.abc');
|
||||
$abc3 = $this->getAbc('/abc/valid_abc_3.abc');
|
||||
$importer = ImportAbcFile::create();
|
||||
|
||||
$result = $importer->importMultiple([$abc1, $abc2, $abc3]);
|
||||
|
||||
$this->assertInstanceOf(TuneCollection::class, $result);
|
||||
$this->assertGreaterThan(1, $result->count());
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
<?php
|
||||
namespace Tests\Unit;
|
||||
|
||||
use \UnitTester;
|
||||
use \Codeception\Util\Stub;
|
||||
use \XaiCorp\AbcParser\Parser;
|
||||
use \XaiCorp\AbcParser\Interfaces\Builder;
|
||||
|
||||
class ParserCest
|
||||
{
|
||||
/**
|
||||
* @var \XaiCorp\AbcParser\Interfaces\Builder
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
/**
|
||||
* @var Parser;
|
||||
*/
|
||||
protected $parser;
|
||||
|
||||
public function _before(UnitTester $I)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function _after(UnitTester $I)
|
||||
{
|
||||
unset($builder);
|
||||
}
|
||||
|
||||
// tests: trying to...
|
||||
public function createParser(UnitTester $I)
|
||||
{
|
||||
$builder = \Mockery::mock(Builder::Class)
|
||||
->shouldReceive('newCollection')->once()
|
||||
->shouldreceive('getCollection')->once()->andreturn(true)
|
||||
->mock();
|
||||
|
||||
$parser = new Parser($builder);
|
||||
|
||||
$I->assertInstanceOf(Parser::class, $parser);
|
||||
}
|
||||
|
||||
public function seeParseABCCallNewCollection(UnitTester $I)
|
||||
{
|
||||
$abc = '';
|
||||
$builder = \Mockery::mock(Builder::Class)
|
||||
->shouldReceive('newCollection')->once()
|
||||
->shouldreceive('getCollection')->once()->andreturn(true)
|
||||
->mock();
|
||||
|
||||
$parser = new Parser($builder);
|
||||
|
||||
$result = $parser->parseABC($abc);
|
||||
$I->assertTrue($result);
|
||||
}
|
||||
|
||||
public function seeParseABCExample1(UnitTester $I)
|
||||
{
|
||||
$abc = file_get_contents(__DIR__.'/../_data/abc/valid_abc_1.abc');
|
||||
$builder = \Mockery::mock(Builder::Class)
|
||||
->shouldReceive('newCollection')->once()
|
||||
->shouldReceive('newPerson')->once()
|
||||
->shouldReceive('newTune')->once()
|
||||
->shouldReceive('newSetting')->once()
|
||||
->shouldReceive('appendToSetting')->once()
|
||||
->shouldReceive('setOnSetting')->once()
|
||||
->shouldReceive('storeTune')->once()
|
||||
->shouldreceive('getCollection')->once()->andreturn(true);
|
||||
$builder->shouldReceive('setOnTune')
|
||||
->with('X', '3')
|
||||
->atMost()->times(4);
|
||||
$builder = $builder->mock();
|
||||
|
||||
|
||||
$parser = new Parser($builder);
|
||||
|
||||
$result = $parser->parseABC($abc);
|
||||
$I->assertTrue($result);
|
||||
}
|
||||
}
|
||||
@@ -1,156 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Arr;
|
||||
|
||||
use UnitTester;
|
||||
use XaiCorp\AbcParser\Models\Arr\Abc;
|
||||
use XaiCorp\AbcParser\Parser;
|
||||
|
||||
class AbcCest
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $dataDir;
|
||||
|
||||
/**
|
||||
* @var \XaiCorp\AbcParser\Interfaces\Builder
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
/**
|
||||
* @var Parser;
|
||||
*/
|
||||
protected $parser;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->dataDir = codecept_data_dir();
|
||||
}
|
||||
|
||||
public function _before(UnitTester $I)
|
||||
{
|
||||
$this->builder = new Abc();
|
||||
}
|
||||
|
||||
public function _after(UnitTester $I)
|
||||
{
|
||||
unset($builder);
|
||||
}
|
||||
|
||||
// tests: trying to...
|
||||
public function createParser(UnitTester $I)
|
||||
{
|
||||
$parser = new Parser($this->builder);
|
||||
|
||||
$I->assertInstanceOf(Parser::class, $parser);
|
||||
}
|
||||
|
||||
public function seeParseEmptyAbcWithoutErrors(UnitTester $I)
|
||||
{
|
||||
$abc = '';
|
||||
$parser = new Parser($this->builder);
|
||||
|
||||
$result = $parser->parseABC($abc);
|
||||
$I->assertEmpty($result);
|
||||
}
|
||||
|
||||
public function seeParseABCExample1(UnitTester $I)
|
||||
{
|
||||
$abc = file_get_contents($this->dataDir . '/abc/valid_abc_1.abc');
|
||||
$parser = new Parser($this->builder);
|
||||
$expected = [
|
||||
1 => [
|
||||
'X' => '3',
|
||||
'M' => '4/4',
|
||||
'K' => 'C',
|
||||
'music' => 'gaab babc :|',
|
||||
],
|
||||
];
|
||||
|
||||
$result = $parser->parseABC($abc);
|
||||
$I->assertNotEmpty($result);
|
||||
|
||||
$I->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function seeParseABCExample2(UnitTester $I)
|
||||
{
|
||||
$abc = file_get_contents($this->dataDir . '/abc/valid_abc_2.abc');
|
||||
$parser = new Parser($this->builder);
|
||||
$expected = [
|
||||
1 => [
|
||||
'X' => '40',
|
||||
'T' => ['Abbotts Bromley Horn Dance'],
|
||||
'A' => ['trad'],
|
||||
'C' => ['trad.'],
|
||||
'D' => ['None'],
|
||||
'G' => ['lute'],
|
||||
'H' => [
|
||||
'Thousand year old tradition',
|
||||
'The Horns live in the church and are removed for the dance once a year',
|
||||
],
|
||||
'R' => ['Jig'],
|
||||
'F' => ['none.abc'],
|
||||
'B' => ['Traditional English tunes'],
|
||||
'W' => ['None'],
|
||||
'Q' => '1/8=80',
|
||||
'L' => '1/8',
|
||||
'M' => '6/8',
|
||||
'N' => ['Traditional English'],
|
||||
'O' => ['England'],
|
||||
'Z' => ['Andy Hornby'],
|
||||
'S' => ['http://www.leeds.ac.uk/music/Info/RRTuneBk/gettune/00000dab.html'],
|
||||
'P' => 'ABC',
|
||||
'K' => 'G',
|
||||
'music' => 'e|B2e G2e|B2e E2G|FGA GAB|1AGF G2:|2AGF E2|
|
||||
|e|c2e cde|A2c ABc|FGA GFE|_E=EF B,2g|
|
||||
e2g efg|c2e cde|dcB AGF|E3E2
|
||||
|:A|BcB e3|BcB f3|BcB AGF|1G2F G2:|2E3-E2|]',
|
||||
],
|
||||
];
|
||||
|
||||
$result = $parser->parseABC($abc);
|
||||
$I->assertNotEmpty($result);
|
||||
|
||||
$I->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
|
||||
public function seeParseABCExample3(UnitTester $I)
|
||||
{
|
||||
$abc = file_get_contents($this->dataDir . '/abc/valid_abc_3.abc');
|
||||
$parser = new Parser($this->builder);
|
||||
$expected = [
|
||||
'A' => ['trad'],
|
||||
'C' => ['trad.'],
|
||||
'D' => ['None'],
|
||||
'H' => [
|
||||
'Thousand year old tradition',
|
||||
'The Horns live in the church and are removed for the dance once a year',
|
||||
],
|
||||
'R' => ['Jig'],
|
||||
'F' => ['none.abc'],
|
||||
'B' => ['Traditional English tunes'],
|
||||
'L' => '1/8',
|
||||
'M' => '6/8',
|
||||
'N' => ['Traditional English'],
|
||||
'O' => ['England'],
|
||||
'S' => ['http://www.leeds.ac.uk/music/Info/RRTuneBk/gettune/00000dab.html'],
|
||||
'Z' => ['Andy Hornby'],
|
||||
|
||||
1 => [
|
||||
'X' => '3',
|
||||
'M' => '4/4',
|
||||
'K' => 'C',
|
||||
'music' => 'gaab babc :|',
|
||||
'T' => ['Short and Sweet']
|
||||
],
|
||||
];
|
||||
|
||||
$result = $parser->parseABC($abc);
|
||||
$I->assertNotEmpty($result);
|
||||
|
||||
$I->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Memory;
|
||||
|
||||
use XaiCorp\AbcParser\Interfaces\Repository;
|
||||
use XaiCorp\AbcParser\Models\Memory\Abc;
|
||||
use XaiCorp\AbcParser\Models\Memory\Person;
|
||||
use XaiCorp\AbcParser\Parser as AbcParser;
|
||||
use Enzyme\Axiom\Repositories\RepositoryInterface;
|
||||
|
||||
class AbcParserTest extends \Codeception\TestCase\Test
|
||||
{
|
||||
private $facade;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setup();
|
||||
|
||||
$repository = \Mockery::mock(RepositoryInterface::class)
|
||||
->shouldReceive('add')->once()
|
||||
->getMock();
|
||||
$this->facade = new AbcParser(new Abc($repository));
|
||||
}
|
||||
|
||||
private function getValidAbc($filename = '/abc/valid_abc_1.abc')
|
||||
{
|
||||
return file_get_contents(codecept_data_dir($filename));
|
||||
}
|
||||
|
||||
public function testNotHeaderDataNoCrash()
|
||||
{
|
||||
$result = $this->facade->parseABC($this->getValidAbc());
|
||||
$this->assertInstanceOf(\XaiCorp\AbcParser\Models\Memory\TuneCollection::class, $result);
|
||||
}
|
||||
|
||||
|
||||
public function testAllParametersForTune()
|
||||
{
|
||||
$result = $this->facade->parseABC($this->getValidAbc('/abc/valid_abc_2.abc'));
|
||||
|
||||
// Tune details
|
||||
$tune = $result[0];
|
||||
$this->assertEquals('40', $result[0]->get('X'));
|
||||
$this->assertEquals('Abbotts Bromley Horn Dance', current($tune->get('T')));
|
||||
$this->assertEquals('England', current($tune->get('O')));
|
||||
$this->assertEquals(new Person(['name' => 'trad']), current($tune->get('A')));
|
||||
$this->assertEquals(new Person(['name' => 'trad.']), current($tune->get('C')));
|
||||
$this->assertEquals('England', current($tune->get('O')));
|
||||
$this->assertEquals('lute', current($tune->get('G')));
|
||||
$history = $tune->get('H');
|
||||
$this->assertEquals(2, count($history));
|
||||
$this->assertEquals('Thousand year old tradition', $history[0]);
|
||||
$this->assertEquals('The Horns live in the church and are removed for the dance once a year', $history[1]);
|
||||
$this->assertEquals('Jig', current($tune->get('R')));
|
||||
|
||||
// setting details
|
||||
$setting = $tune[0];
|
||||
$this->assertEquals('Traditional English', current($setting->get('N')));
|
||||
$this->assertEquals('1/8', $setting->get('L'));
|
||||
$this->assertEquals('6/8', $setting->get('M'));
|
||||
$this->assertEquals('G', $setting->get('K'));
|
||||
$expectedMusic = "
|
||||
e|B2e G2e|B2e E2G|FGA GAB|1AGF G2:|2AGF E2|
|
||||
|e|c2e cde|A2c ABc|FGA GFE|_E=EF B,2g|
|
||||
e2g efg|c2e cde|dcB AGF|E3E2
|
||||
|:A|BcB e3|BcB f3|BcB AGF|1G2F G2:|2E3-E2|]
|
||||
";
|
||||
$this->assertEquals(trim($expectedMusic), $setting->get('music'), "music strings should match");
|
||||
|
||||
$Z = new Person(['name' => 'Andy Hornby']);
|
||||
$this->assertEquals($Z, current($setting->get('Z')));
|
||||
|
||||
$this->assertEquals('None', current($setting->get('D')));
|
||||
$this->assertEquals(
|
||||
'http://www.leeds.ac.uk/music/Info/RRTuneBk/gettune/00000dab.html',
|
||||
current($setting->get('S'))
|
||||
);
|
||||
$this->assertEquals('None', current($setting->get('W')));
|
||||
$this->assertEquals('Traditional English tunes', current($setting->get('B')));
|
||||
$this->assertEquals('none.abc', current($setting->get('F')));
|
||||
$this->assertEquals('ABC', $setting->get('P'));
|
||||
$this->assertEquals('1/8=80', $setting->get('Q'));
|
||||
}
|
||||
|
||||
|
||||
public function testAllParametersForCollection()
|
||||
{
|
||||
$collection = $this->facade->parseABC($this->getValidAbc('/abc/valid_abc_3.abc'));
|
||||
|
||||
$this->assertEquals(new Person(['name' => 'trad']), current($collection->get('A')));
|
||||
$this->assertEquals('Traditional English tunes', current($collection->get('B')));
|
||||
$this->assertEquals(new Person(['name' => 'trad.']), current($collection->get('C')));
|
||||
$this->assertEquals('None', current($collection->get('D')));
|
||||
$this->assertEquals('none.abc', current($collection->get('F')));
|
||||
$this->assertEquals('Thousand year old tradition', current($collection->get('H')));
|
||||
$this->assertEquals('1/8', $collection->get('L'));
|
||||
$this->assertEquals('6/8', $collection->get('M'));
|
||||
$this->assertEquals('Traditional English', current($collection->get('N')));
|
||||
$this->assertEquals('England', current($collection->get('O')));
|
||||
$this->assertEquals('Jig', $collection->get('R'));
|
||||
$this->assertEquals(
|
||||
'http://www.leeds.ac.uk/music/Info/RRTuneBk/gettune/00000dab.html',
|
||||
current($collection->get('S'))
|
||||
);
|
||||
$this->assertEquals(new Person(['name' => 'Andy Hornby']), current($collection->get('Z')));
|
||||
}
|
||||
}
|
||||
@@ -1,176 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Memory;
|
||||
|
||||
use Codeception\TestCase\Test;
|
||||
use XaiCorp\AbcParser\Models\Memory\Person;
|
||||
|
||||
class PersonTest extends Test
|
||||
{
|
||||
private $person;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->person = new Person();
|
||||
}
|
||||
|
||||
/**
|
||||
* test the model contains the correct attributes
|
||||
*/
|
||||
public function testModelDefinition()
|
||||
{
|
||||
$this->assertClassHasAttribute('person_id', Person::class);
|
||||
$this->assertClassHasAttribute('name', Person::class);
|
||||
$this->assertClassHasAttribute('email', Person::class);
|
||||
}
|
||||
|
||||
public function testConstruct()
|
||||
{
|
||||
$params = ['person_id' => 5];
|
||||
$this->person = new Person($params);
|
||||
|
||||
$this->assertEquals(5, $this->person->get('person_id'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerSet
|
||||
*/
|
||||
public function testMagicSet($property, $value, $expected)
|
||||
{
|
||||
$this->testSet($property, $value, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* test the set property method
|
||||
*
|
||||
* @param (string) $property, the name of the property to set
|
||||
* @param (mixed) value to set
|
||||
* @param (Boolean) expected value for return value of set method
|
||||
*
|
||||
* @dataProvider providerSet
|
||||
*/
|
||||
public function testSet($property, $value, $expected)
|
||||
{
|
||||
$success = $this->person->set($property, $value);
|
||||
$this->assertEquals($expected, $success);
|
||||
if ($success) {
|
||||
$this->assertEquals($value, $this->person->get($property));
|
||||
}
|
||||
}
|
||||
|
||||
public function providerSet()
|
||||
{
|
||||
return [
|
||||
['Y', 'anything', false],
|
||||
['person_id', 'not', false],
|
||||
['person_id', 11, true],
|
||||
['person_id', '11', true],
|
||||
['name', null, false],
|
||||
['name', 1, false],
|
||||
['name', 'anything', true],
|
||||
['email', 'anything', false],
|
||||
['email', 1, false],
|
||||
['email', 'mail@example.com', true],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerPersonGet
|
||||
*/
|
||||
public function testPersonGet($key, $expected)
|
||||
{
|
||||
$result = $this->person->set($key, $expected);
|
||||
$result = $this->person->get($key);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function providerPersonGet()
|
||||
{
|
||||
return [
|
||||
['', false],
|
||||
['me', false],
|
||||
[['of', 'me'], false],
|
||||
[new Person(), false],
|
||||
['name', 'Richard Morgan'],
|
||||
['email', 'r_morgan@sympatico.ca'],
|
||||
['person_id', 0],
|
||||
];
|
||||
}
|
||||
|
||||
public function testIsChanged()
|
||||
{
|
||||
$person = new Person(['name' => 'Richard Morgan']);
|
||||
|
||||
$this->assertInstanceOf(Person::class, $person);
|
||||
|
||||
$this->assertFalse($person->is_changed(), 'after load should be no changes');
|
||||
|
||||
$person->set('name', 'Richard Armitage');
|
||||
$this->assertTrue($person->is_changed(), 'updating the name changes the data');
|
||||
}
|
||||
|
||||
/**
|
||||
* test the static function equals
|
||||
* returns true if the data properties of 2 Person objects
|
||||
* are identical
|
||||
*
|
||||
* @param (array) $params list of properties to set on the objects
|
||||
* @param (boolean) $expected result of running equals
|
||||
*
|
||||
* @dataProvider providerEquals
|
||||
*/
|
||||
public function testEquals($params)
|
||||
{
|
||||
$ob1 = new Person();
|
||||
$ob2 = new Person();
|
||||
|
||||
foreach ($params as $key => $val) {
|
||||
if (is_array($ob1->get($key))) {
|
||||
$ob1->append($key, $val);
|
||||
$ob2->append($key, $val);
|
||||
} else {
|
||||
$ob1->set($key, $val);
|
||||
$ob2->set($key, $val);
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertTrue(Person::equals($ob1, $ob2));
|
||||
}
|
||||
|
||||
/**
|
||||
* test the static function equals
|
||||
* returns true if the data properties of 2 Person objects
|
||||
* are identical
|
||||
*
|
||||
* @param (array) $params list of properties to set on the objects
|
||||
* @param (boolean) $expected result of running equals
|
||||
*
|
||||
* @dataProvider providerEquals
|
||||
*/
|
||||
public function testEqualsFails($params)
|
||||
{
|
||||
$ob1 = new Person();
|
||||
$ob2 = new Person();
|
||||
|
||||
foreach ($params as $key => $val) {
|
||||
if (is_array($ob1->get($key))) {
|
||||
$ob1->append($key, $val);
|
||||
$ob2->append($key, $val . " hello");
|
||||
} else {
|
||||
$ob1->set($key, $val);
|
||||
$ob2->set($key, $val + 1);
|
||||
}
|
||||
}
|
||||
// print_r($ob1); print_r($ob2); die;
|
||||
$this->assertFalse(Person::equals($ob1, $ob2));
|
||||
}
|
||||
|
||||
public function providerEquals()
|
||||
{
|
||||
return [
|
||||
// array( array('person_id'=>'2') ),
|
||||
[['name' => 'a title']],
|
||||
[['email' => 'mail@example.com']],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,354 +0,0 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Memory;
|
||||
|
||||
use Codeception\TestCase\Test as TestCase;
|
||||
use XaiCorp\AbcParser\Models\Memory\Person;
|
||||
use XaiCorp\AbcParser\Models\Memory\Setting;
|
||||
|
||||
/**
|
||||
* @group Lib
|
||||
*/
|
||||
class SettingTest extends TestCase
|
||||
{
|
||||
private $setting;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->setting = new Setting();
|
||||
}
|
||||
|
||||
/**
|
||||
* test the model contains the correct attributes
|
||||
*/
|
||||
public function testModelDefinition()
|
||||
{
|
||||
$this->assertClassHasAttribute('settingID', Setting::class);
|
||||
$this->assertClassHasAttribute('tuneID', Setting::class);
|
||||
|
||||
$this->assertClassHasAttribute('Z', Setting::class);
|
||||
$this->assertClassHasAttribute('N', Setting::class);
|
||||
$this->assertClassHasAttribute('D', Setting::class);
|
||||
$this->assertClassHasAttribute('S', Setting::class);
|
||||
$this->assertClassHasAttribute('W', Setting::class);
|
||||
$this->assertClassHasAttribute('B', Setting::class);
|
||||
|
||||
$this->assertClassHasAttribute('F', Setting::class);
|
||||
$this->assertClassHasAttribute('P', Setting::class);
|
||||
$this->assertClassHasAttribute('Q', Setting::class);
|
||||
$this->assertClassHasAttribute('L', Setting::class);
|
||||
$this->assertClassHasAttribute('M', Setting::class);
|
||||
$this->assertClassHasAttribute('K', Setting::class);
|
||||
$this->assertClassHasAttribute('music', Setting::class);
|
||||
// $this->assertClassHasAttribute('music_hash', Setting::class);
|
||||
|
||||
$this->assertClassHasAttribute('propertyNames', Setting::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* test the set property method
|
||||
*
|
||||
* also tests the validate_line protected method
|
||||
*
|
||||
* @param (string) $property, the name of the property to set
|
||||
* @param (mixed) value to set
|
||||
* @param (Boolean) expected value for return value of set method
|
||||
*
|
||||
* @dataProvider providerSet
|
||||
*/
|
||||
public function testSet($property, $value, $expected_success, $expected_value = false)
|
||||
{
|
||||
$success = $this->setting->set($property, $value);
|
||||
$this->assertEquals($expected_success, $success);
|
||||
if ($success) {
|
||||
if (!$expected_value) {
|
||||
$expected_value = $value;
|
||||
}
|
||||
$this->assertEquals($expected_value, $this->setting->get($property));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerSet
|
||||
*/
|
||||
public function testMagicSet($property, $value, $expected_success, $expected_value = false)
|
||||
{
|
||||
$this->setting->$property = $value;
|
||||
if ($expected_success) {
|
||||
$this->assertEquals($value, $this->setting->get($property));
|
||||
}
|
||||
}
|
||||
|
||||
public function providerSet()
|
||||
{
|
||||
return [
|
||||
['Y', 'anything', false],
|
||||
['settingID', 1, true],
|
||||
['tuneID', ['alpha'], false],
|
||||
['tuneID', 'alpha', false],
|
||||
['tuneID', '1', true],
|
||||
['tuneID', 1, true],
|
||||
['f', 1, false],
|
||||
['F', 1, false],
|
||||
['F', '1', false],
|
||||
['F', ['aplha'], true],
|
||||
['P', ['string'], false],
|
||||
['P', 10, false],
|
||||
['P', 'string', true],
|
||||
['Q', ['string'], false],
|
||||
['Q', 10, false],
|
||||
['Q', 'string', false],
|
||||
['Q', '1/8=200', true],
|
||||
['L', ['string'], false],
|
||||
['L', 10, false],
|
||||
['L', 'string', false],
|
||||
['L', '1/8', true],
|
||||
['M', ['string'], false],
|
||||
['M', 10, false],
|
||||
['M', 'string', false],
|
||||
['M', '6/8', true],
|
||||
['M', 'C', true],
|
||||
['M', 'c', true],
|
||||
['M', 'C|', true],
|
||||
['K', ['string'], false],
|
||||
['K', 10, false],
|
||||
['K', 'C', true],
|
||||
['K', 'Am', true],
|
||||
['K', 'A minor', true],
|
||||
['K', 'E dorian', true],
|
||||
['music', 'GABc:|\\', true],
|
||||
['Z', new Person(), false],
|
||||
['Z', [new Person(), 'not a person'], false],
|
||||
['Z', [new Person()], true],
|
||||
['Z', 111, false],
|
||||
['N', ['a string'], true],
|
||||
['D', ['a string'], true],
|
||||
['S', ['a string'], true],
|
||||
['W', ['a string'], true],
|
||||
['B', ['a string'], true],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* the settingID can only be set if it's value is currently 0
|
||||
* this ensures that we can't change a setting's id once it's set.
|
||||
* the default, unset value for settingID is 0
|
||||
*/
|
||||
public function testSetSettingID()
|
||||
{
|
||||
$success = $this->setting->set('settingID', 1);
|
||||
$this->assertTrue($success, 'first attempt should succeed');
|
||||
|
||||
$success = $this->setting->set('settingID', 2);
|
||||
$this->assertFalse($success, 'second attempt should fail');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* test the append value method
|
||||
*
|
||||
* @param (string) $property, the name of the property to set
|
||||
* @param (mixed) value to append
|
||||
* @param (Boolean) expected value for return value of append method
|
||||
*
|
||||
* @dataProvider providerAppend
|
||||
*/
|
||||
public function testAppend($property, $value, $expected)
|
||||
{
|
||||
$success = $this->setting->append($property, $value);
|
||||
$this->assertEquals($expected, $success);
|
||||
if ($success) {
|
||||
$this->assertContains($value, $this->setting->get($property));
|
||||
}
|
||||
}
|
||||
|
||||
public function providerAppend()
|
||||
{
|
||||
return [
|
||||
['Y', 'anything', false],
|
||||
['settingID', 1, false],
|
||||
['tuneID', ['alpha'], false],
|
||||
['tuneID', 'alpha', false],
|
||||
['tuneID', '1', false],
|
||||
['tuneID', 1, false],
|
||||
['f', 1, false],
|
||||
['F', 1, true],
|
||||
['F', '1', true],
|
||||
['F', ['aplha'], false],
|
||||
['P', ['string'], false],
|
||||
['P', 10, false],
|
||||
['P', 'string', false],
|
||||
['Q', ['string'], false],
|
||||
['Q', 10, false],
|
||||
['Q', 'string', false],
|
||||
['Q', '1/8=200', false],
|
||||
['L', ['string'], false],
|
||||
['L', 10, false],
|
||||
['L', 'string', false],
|
||||
['L', '1/8', false],
|
||||
['M', ['string'], false],
|
||||
['M', 10, false],
|
||||
['M', 'string', false],
|
||||
['M', '6/8', false],
|
||||
['M', 'C', false],
|
||||
['M', 'c', false],
|
||||
['M', 'C|', false],
|
||||
['K', ['string'], false],
|
||||
['K', 10, false],
|
||||
['K', 'C', false],
|
||||
['K', 'Am', false],
|
||||
['K', 'A minor', false],
|
||||
['K', 'E dorian', false],
|
||||
['music', 'GABc:|\\', false],
|
||||
|
||||
['Z', new Person(), true],
|
||||
['Z', [new Person()], false],
|
||||
['Z', 111, false],
|
||||
['N', ['a string'], false],
|
||||
['D', ['a string'], false],
|
||||
['S', ['a string'], false],
|
||||
['W', ['a string'], false],
|
||||
['B', ['a string'], false],
|
||||
['N', 'more notes', true],
|
||||
['D', 'another record', true],
|
||||
['S', 'another source', true],
|
||||
['W', 'words', true],
|
||||
['B', 'another book', true],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerGet
|
||||
*/
|
||||
public function testGet($key, $expected)
|
||||
{
|
||||
$result = $this->setting->set($key, $expected);
|
||||
$result = $this->setting->get($key);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function providerGet()
|
||||
{
|
||||
return [
|
||||
['', false],
|
||||
['me', false],
|
||||
['settingID', 1],
|
||||
['tuneID', 2],
|
||||
['Q', '1/8=100'],
|
||||
['L', '1/8'],
|
||||
['M', '6/8'],
|
||||
['K', 'Am'],
|
||||
['music', 'GABc:|\\'],
|
||||
|
||||
['Z', [new Person()]],
|
||||
['N', ['a string']],
|
||||
['D', ['a string']],
|
||||
['S', ['a string']],
|
||||
['W', ['a string']],
|
||||
['B', ['a string']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* test the static function equals
|
||||
* returns true if the data properties of 2 Setting Objects
|
||||
* are identical
|
||||
*
|
||||
* @param (array) $params list of properties to set on the objects
|
||||
* @param (boolean) $expected result of running equals
|
||||
*
|
||||
* @dataProvider providerEquals
|
||||
*/
|
||||
public function testEquals($params)
|
||||
{
|
||||
$ob1 = new Setting();
|
||||
$ob2 = new Setting();
|
||||
|
||||
foreach ($params as $key => $val) {
|
||||
if (is_array($ob1->get($key))) {
|
||||
$ob1->append($key, $val);
|
||||
$ob2->append($key, $val);
|
||||
} else {
|
||||
$ob1->set($key, $val);
|
||||
$ob2->set($key, $val);
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertTrue(Setting::equals($ob1, $ob2));
|
||||
}
|
||||
|
||||
/**
|
||||
* test the static function equals
|
||||
* returns true if the data properties of 2 Abc_Tunes
|
||||
* are identical
|
||||
*
|
||||
* @param (array) $params list of properties to set on the objects
|
||||
* @param (boolean) $expected result of running equals
|
||||
*
|
||||
* @dataProvider providerEquals
|
||||
*/
|
||||
public function testEqualsFails($params)
|
||||
{
|
||||
$ob1 = new Setting();
|
||||
$ob2 = new Setting();
|
||||
|
||||
foreach ($params as $key => $val) {
|
||||
if (is_array($ob1->get($key))) {
|
||||
$ob1->append($key, $val);
|
||||
// $ob2->append($key, $val);
|
||||
} else {
|
||||
$ob1->set($key, $val);
|
||||
$ob2->set($key, $val + 1);
|
||||
}
|
||||
}
|
||||
$this->assertFalse(Setting::equals($ob1, $ob2));
|
||||
}
|
||||
|
||||
public function providerEquals()
|
||||
{
|
||||
return [
|
||||
[['settingID' => '2']],
|
||||
[['tuneID' => '2']],
|
||||
[['Z' => new Person()]],
|
||||
[['N' => 'an author']],
|
||||
[['D' => 'a composer']],
|
||||
[['S' => 'a group']],
|
||||
[['W' => 'some history']],
|
||||
[['B' => 'Plymouth']],
|
||||
[['F' => 'Jig']],
|
||||
[['P' => 'ABB']],
|
||||
[['Q' => '1/8=100']],
|
||||
[['L' => '1/4']],
|
||||
[['M' => '2/4']],
|
||||
[['K' => 'G']],
|
||||
[['music' => 'a|cab|']],
|
||||
];
|
||||
}
|
||||
|
||||
public function testIsChanged()
|
||||
{
|
||||
$setting = new Setting();
|
||||
|
||||
$this->assertInstanceOf(Setting::class, $setting);
|
||||
|
||||
$this->assertFalse($setting->is_changed(), 'after load should be no changes');
|
||||
|
||||
$setting->set('K', 'G');
|
||||
$this->assertTrue($setting->is_changed(), 'updating the name changes the data');
|
||||
}
|
||||
|
||||
|
||||
public function testConstruct()
|
||||
{
|
||||
$params = [
|
||||
'settingID' => 3,
|
||||
'Z' => [new Person()],
|
||||
'P' => 'ABC',
|
||||
];
|
||||
|
||||
$this->setting = new Setting($params);
|
||||
|
||||
foreach ($params as $key => $val) {
|
||||
$this->assertEquals($val, $this->setting->get($key));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,203 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Memory;
|
||||
|
||||
use XaiCorp\AbcParser\Models\Memory\Person;
|
||||
use XaiCorp\AbcParser\Models\Memory\Tune;
|
||||
use XaiCorp\AbcParser\Models\Memory\TuneCollection;
|
||||
use Codeception\TestCase\Test as TestCase;
|
||||
|
||||
//use App\Libraries\memory\Setting;
|
||||
|
||||
class TuneCollectionTest extends TestCase
|
||||
{
|
||||
private $tunes;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->tunes = new TuneCollection();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* test the implementation of the Iterator interface
|
||||
* 100% coverage! ;)
|
||||
*/
|
||||
public function testIterator()
|
||||
{
|
||||
$t = new Tune();
|
||||
$this->assertEquals(0, count($this->tunes));
|
||||
$this->tunes[] = $t;
|
||||
$this->tunes->push($t);
|
||||
$this->assertEquals(2, count($this->tunes));
|
||||
|
||||
$this->assertInstanceOf(Tune::class, $this->tunes[0], 'first tune');
|
||||
$this->assertInstanceOf(Tune::class, $this->tunes[1], 'second tune');
|
||||
|
||||
foreach ($this->tunes as $key => $val) {
|
||||
$this->assertInternalType('scalar', $key);
|
||||
$this->assertInstanceOf(Tune::class, $val);
|
||||
$this->assertTrue(isset($this->tunes[$key]), 'key is set');
|
||||
|
||||
unset($this->tunes[$key]);
|
||||
$this->assertFalse(isset($this->tunes[$key]), 'key should have been unset');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* test the ability to remove the last tune from collection
|
||||
*
|
||||
* tests the implementation of the Countable interface
|
||||
*/
|
||||
public function testPop()
|
||||
{
|
||||
$this->assertEquals(0, count($this->tunes), 'no tunes in collection to begin with');
|
||||
|
||||
$t = new Tune();
|
||||
$this->tunes->push($t);
|
||||
|
||||
$this->assertEquals(1, count($this->tunes), '1 tune added to collection');
|
||||
|
||||
$result = $this->tunes->pop();
|
||||
$this->assertEquals(0, count($this->tunes), 'no tunes left in collection');
|
||||
$this->assertInstanceOf(Tune::class, $result);
|
||||
}
|
||||
|
||||
// public function test_construct()
|
||||
// {
|
||||
// $params = array(
|
||||
// 'tc_id' => 5,
|
||||
// 'cOwner' => 'me',
|
||||
// 'B' => array('my tune book'),
|
||||
// 'R' => 'reel'
|
||||
// );
|
||||
//
|
||||
// $this->_tunes = new TuneCollection($params);
|
||||
//
|
||||
// foreach($params as $key => $val)
|
||||
// {
|
||||
// $this->assertEquals($val, $this->_tunes->get($key));
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* test the set property method
|
||||
*
|
||||
* @param (string) $property, the name of the property to set
|
||||
* @param (mixed) value to set
|
||||
* @param (Boolean) expected value for return value of set method
|
||||
*
|
||||
* @dataProvider providerSet
|
||||
*/
|
||||
public function testSet($property, $value, $expected)
|
||||
{
|
||||
$success = $this->tunes->set($property, $value);
|
||||
$this->assertEquals($expected, $success);
|
||||
if ($success) {
|
||||
$this->assertEquals($value, $this->tunes->get($property));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* test the set property method
|
||||
*
|
||||
* @param (string) $property, the name of the property to set
|
||||
* @param (mixed) value to set
|
||||
* @param (Boolean) expected value for return value of set method
|
||||
*
|
||||
* @dataProvider providerSet
|
||||
*/
|
||||
public function testMagicSetter($property, $value, $expected)
|
||||
{
|
||||
$this->tunes->$property = $value;
|
||||
if ($expected) {
|
||||
$this->assertEquals($value, $this->tunes->get($property));
|
||||
}
|
||||
}
|
||||
|
||||
public function providerSet()
|
||||
{
|
||||
return [
|
||||
['Y', 'anything', false],
|
||||
['A', 10, false],
|
||||
['A', ['1', '2'], false],
|
||||
['A', [new Person(), new Person()], true],
|
||||
['C', 10, false],
|
||||
['C', ['1', '2'], false],
|
||||
['C', [new Person(), new Person()], true],
|
||||
['H', 10, false],
|
||||
['H', ['1', '2'], true],
|
||||
['O', 10, false],
|
||||
['O', ['1', '2'], true],
|
||||
['R', 10, false],
|
||||
['R', 'reel', true],
|
||||
['collection', 10, false],
|
||||
['collection', '10', false],
|
||||
['collection', [], false],
|
||||
['collection', ['1', '2'], false],
|
||||
['collection', [new Tune(), 'not a tune'], false],
|
||||
['collection', [new Tune()], true],
|
||||
['tc_id', 'abc', false],
|
||||
['tc_id', '11', true],
|
||||
['tc_id', 15, true],
|
||||
['cOwner', 'me', true],
|
||||
];
|
||||
}
|
||||
|
||||
public function testSetTcId()
|
||||
{
|
||||
//we should start with tc_id = 0
|
||||
$this->assertEquals(0, $this->tunes->get('tc_id'), 'should start at 0');
|
||||
|
||||
//because tc_id == 0 we can change value
|
||||
$this->tunes->set('tc_id', 4);
|
||||
$this->assertEquals(4, $this->tunes->get('tc_id'), 'should have changed to 4');
|
||||
|
||||
//tc_id is not 0 so we cannot change value
|
||||
$this->tunes->set('tc_id', 11);
|
||||
$this->assertEquals(4, $this->tunes->get('tc_id'), 'should not have changed from 4');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* test the append value method
|
||||
*
|
||||
* @param (string) $property, the name of the property to set
|
||||
* @param (mixed) value to append
|
||||
* @param (Boolean) expected value for return value of append method
|
||||
*
|
||||
* @dataProvider providerAppend
|
||||
*/
|
||||
public function testAppend($property, $value, $expected)
|
||||
{
|
||||
$success = $this->tunes->append($property, $value);
|
||||
$this->assertEquals($expected, $success);
|
||||
if ($success) {
|
||||
$this->assertContains($value, $this->tunes->get($property));
|
||||
}
|
||||
}
|
||||
|
||||
public function providerAppend()
|
||||
{
|
||||
return [
|
||||
['Y', 1, false],
|
||||
['H', 'history', true],
|
||||
['R', 'reel', true],
|
||||
['r', 'reel', false],
|
||||
['collection', 'setting', false],
|
||||
['collection', new Tune(), true],
|
||||
['collection', [new Tune()], false],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
//R is a valid property so we should get it's value
|
||||
$this->tunes->set('R', 'reel');
|
||||
$this->assertEquals('reel', $this->tunes->get('R'));
|
||||
|
||||
//X is not a property, so we should get FALSE
|
||||
$this->assertFalse($this->tunes->get('X'));
|
||||
}
|
||||
}
|
||||
@@ -1,301 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Memory;
|
||||
|
||||
use XaiCorp\AbcParser\Models\Memory\Person;
|
||||
use XaiCorp\AbcParser\Models\Memory\Setting;
|
||||
use XaiCorp\AbcParser\Models\Memory\Tune;
|
||||
use Codeception\TestCase\Test as TestCase;
|
||||
|
||||
/**
|
||||
* @group Lib
|
||||
* @group Unit
|
||||
*/
|
||||
class TuneTest extends TestCase
|
||||
{
|
||||
private $tune;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->tune = new Tune();
|
||||
}
|
||||
|
||||
/**
|
||||
* test the model contains the correct attributes
|
||||
*/
|
||||
public function testModelDefinition()
|
||||
{
|
||||
//Private attributes, probably shouldn't be tested
|
||||
$this->assertClassHasAttribute('tuneID', Tune::class);
|
||||
$this->assertClassHasAttribute('A', Tune::class);
|
||||
$this->assertClassHasAttribute('C', Tune::class);
|
||||
$this->assertClassHasAttribute('T', Tune::class);
|
||||
$this->assertClassHasAttribute('G', Tune::class);
|
||||
$this->assertClassHasAttribute('O', Tune::class);
|
||||
$this->assertClassHasAttribute('R', Tune::class);
|
||||
$this->assertClassHasAttribute('H', Tune::class);
|
||||
$this->assertClassHasAttribute('X', Tune::class);
|
||||
$this->assertClassHasAttribute('propertyNames', Tune::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* test the set property method
|
||||
*
|
||||
* @param (string) $property, the name of the property to set
|
||||
* @param (mixed) value to set
|
||||
* @param (Boolean) expected value for return value of set method
|
||||
*
|
||||
* @dataProvider providerSet
|
||||
*/
|
||||
public function testSet($property, $value, $expected)
|
||||
{
|
||||
$success = $this->tune->set($property, $value);
|
||||
$this->assertEquals($expected, $success);
|
||||
if ($success) {
|
||||
$this->assertEquals($value, $this->tune->get($property));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* test the set property method
|
||||
*
|
||||
* @param (string) $property, the name of the property to set
|
||||
* @param (mixed) value to set
|
||||
* @param (Boolean) expected value for return value of set method
|
||||
*
|
||||
* @dataProvider providerSet
|
||||
*/
|
||||
public function testMagicSetter($property, $value, $expected)
|
||||
{
|
||||
$this->tune->$property = $value;
|
||||
// $this->assertEquals($expected, $success);
|
||||
if ($expected) {
|
||||
$this->assertEquals($value, $this->tune->get($property));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerSet
|
||||
*/
|
||||
public function testConstruct($property, $value, $expected)
|
||||
{
|
||||
$params = [$property => $value];
|
||||
$this->tune = new Tune($params);
|
||||
|
||||
if ($expected) {
|
||||
$this->assertEquals($value, $this->tune->get($property));
|
||||
}
|
||||
}
|
||||
|
||||
public function providerSet()
|
||||
{
|
||||
return [
|
||||
['Y', 'anything', false],
|
||||
['x', 1, false],
|
||||
['X', 1, true],
|
||||
['X', 'aplha', false],
|
||||
['X', '2', true],
|
||||
['T', 'string', false],
|
||||
['T', 10, false],
|
||||
['T', ['1', '2'], true],
|
||||
['A', 10, false],
|
||||
['A', ['1', '2'], false],
|
||||
['A', [new Person(), new Person()], true],
|
||||
['C', 10, false],
|
||||
['C', ['1', '2'], false],
|
||||
['C', [new Person(), new Person()], true],
|
||||
['G', 10, false],
|
||||
['G', ['1', '2'], true],
|
||||
['H', 10, false],
|
||||
['H', ['1', '2'], true],
|
||||
['O', 10, false],
|
||||
['O', ['1', '2'], true],
|
||||
['R', 10, false],
|
||||
['R', ['1', '2'], true],
|
||||
['collection', 10, false],
|
||||
['collection', '10', false],
|
||||
['collection', [], false],
|
||||
['collection', ['1', '2'], false],
|
||||
['collection', [new Setting()], true],
|
||||
['tuneHash', 10, false],
|
||||
['tuneHash', '10', false],
|
||||
['tuneHash', [], false],
|
||||
['tuneHash', ['1', '2'], false],
|
||||
['tuneID', 'abc', false],
|
||||
['tuneID', '11', true],
|
||||
['tuneID', 15, true],
|
||||
];
|
||||
}
|
||||
|
||||
public function testSetTuneID()
|
||||
{
|
||||
//we should start with tuneID = 0
|
||||
$this->assertEquals(0, $this->tune->get('tuneID'), 'should start at 0');
|
||||
|
||||
//because tuneID == 0 we can change value
|
||||
$this->tune->set('tuneID', 4);
|
||||
$this->assertEquals(4, $this->tune->get('tuneID'), 'should have changed to 4');
|
||||
|
||||
//tuneID is not 0 so we cannot change value
|
||||
$this->tune->set('tuneID', 11);
|
||||
$this->assertEquals(4, $this->tune->get('tuneID'), 'should not have changed from 4');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* test the append value method
|
||||
*
|
||||
* @param (string) $property, the name of the property to set
|
||||
* @param (mixed) value to append
|
||||
* @param (Boolean) expected value for return value of append method
|
||||
*
|
||||
* @dataProvider providerAppend
|
||||
*/
|
||||
public function testAppend($property, $value, $expected)
|
||||
{
|
||||
$success = $this->tune->append($property, $value);
|
||||
$this->assertEquals($expected, $success);
|
||||
if ($success) {
|
||||
$this->assertContains($value, $this->tune->get($property));
|
||||
}
|
||||
}
|
||||
|
||||
public function providerAppend()
|
||||
{
|
||||
return [
|
||||
['Y', 1, false],
|
||||
['X', 1, false],
|
||||
['T', 'title', true],
|
||||
['T', ['of', 'titles'], false],
|
||||
['A', 'author', true],
|
||||
['C', 'composer', true],
|
||||
['G', 'Group', true],
|
||||
['H', 'history', true],
|
||||
['O', 'Origin', true],
|
||||
['R', 'Rhythm', true],
|
||||
['collection', 'setting', false],
|
||||
['collection', new Setting(), true],
|
||||
['collection', [new Setting()], false],
|
||||
['tuneHash', 'tuneHash', false],
|
||||
['tuneHash', new Setting(), false],
|
||||
['tuneHash', [new Setting()], false],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* test the static function equals
|
||||
* returns true if the data properties of 2 Abc_Tunes
|
||||
* are identical
|
||||
*
|
||||
* @param (array) $params list of properties to set on the objects
|
||||
* @param (boolean) $expected result of running equals
|
||||
*
|
||||
* @dataProvider providerEquals
|
||||
*/
|
||||
public function testEquals($params)
|
||||
{
|
||||
// $this->markTestIncomplete();
|
||||
|
||||
$ob1 = new Tune();
|
||||
$ob2 = new Tune();
|
||||
|
||||
foreach ($params as $key => $val) {
|
||||
if (is_array($ob1->get($key))) {
|
||||
$ob1->append($key, $val);
|
||||
$ob2->append($key, $val);
|
||||
} else {
|
||||
$ob1->set($key, $val);
|
||||
$ob2->set($key, $val);
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertTrue(Tune::equals($ob1, $ob2));
|
||||
}
|
||||
|
||||
/**
|
||||
* test the static function equals
|
||||
* returns true if the data properties of 2 Abc_Tunes
|
||||
* are identical
|
||||
*
|
||||
* @param (array) $params list of properties to set on the objects
|
||||
* @param (boolean) $expected result of running equals
|
||||
*
|
||||
* @dataProvider providerEquals
|
||||
*/
|
||||
public function testEqualsFails($params)
|
||||
{
|
||||
$ob1 = new Tune();
|
||||
$ob2 = new Tune();
|
||||
|
||||
foreach ($params as $key => $val) {
|
||||
if ($key === 'collection') {
|
||||
$ob1->set($key, $val);
|
||||
} elseif (is_array($ob1->get($key))) {
|
||||
$ob1->append($key, $val);
|
||||
} else {
|
||||
$ob1->set($key, $val);
|
||||
}
|
||||
}
|
||||
$this->assertFalse(Tune::equals($ob1, $ob2));
|
||||
}
|
||||
|
||||
public function providerEquals()
|
||||
{
|
||||
$setting1 = new Setting(['settingID' => 5, 'tuneID' => 444]);
|
||||
$setting2 = new Setting(['settingID' => 3, 'tuneID' => 2244]);
|
||||
return [
|
||||
[['X' => '2']],
|
||||
[['T' => 'a title']],
|
||||
[['A' => 'an author']],
|
||||
[['C' => 'a composer']],
|
||||
[['G' => 'a group']],
|
||||
[['H' => 'some history']],
|
||||
[['O' => 'Plymouth']],
|
||||
[['R' => 'Jig']],
|
||||
[['collection' => [$setting1, $setting2]]],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function testIsChanged()
|
||||
{
|
||||
$tune = new Tune();
|
||||
|
||||
$this->assertInstanceOf(Tune::class, $tune);
|
||||
|
||||
$this->assertFalse($tune->is_changed(), 'after load should be no changes');
|
||||
|
||||
$tune->set('X', '5');
|
||||
$this->assertTrue($tune->is_changed(), 'updating the name changes the data');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerGet
|
||||
*/
|
||||
public function testGet($key, $expected)
|
||||
{
|
||||
$result = $this->tune->set($key, $expected);
|
||||
$result = $this->tune->get($key);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function providerGet()
|
||||
{
|
||||
return [
|
||||
['', false],
|
||||
['me', false],
|
||||
['tuneID', 2],
|
||||
['X', '2'],
|
||||
['T', ['1', '2']],
|
||||
['A', [new Person(), new Person()]],
|
||||
['C', [new Person(), new Person()]],
|
||||
['G', ['1', '2']],
|
||||
['H', ['1', '2']],
|
||||
['O', ['1', '2']],
|
||||
['R', ['1', '2']],
|
||||
['collection', [new Setting()]],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user