initial commit

This commit is contained in:
2016-04-15 21:38:56 -04:00
commit 1d3e8e3117
79 changed files with 16223 additions and 0 deletions

31
src/AbstractBuilder.php Normal file
View File

@@ -0,0 +1,31 @@
<?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);
}
}

23
src/Factory.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
namespace XaiCorp\AbcParser;
class Factory
{
protected $classMap = [
'TuneCollection' => null,
];
public function __construct(array $config = [])
{
}
public static function create($type)
{
return call_user_func([self, 'createInstance']);
}
public function createInstance($type)
{
return isset($this->classMap[$type])? new $this->classMap[$type]() : null;
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace XaiCorp\AbcParser\Interfaces;
/**
* outline the functions and parameters available in the concrete builders
* Class AbstractBuilder
*/
interface Builder
{
public function newCollection();
public function appendToCollection($key, $data);
public function setOnCollection($key, $data);
public function getCollection();
public function newTune();
public function appendToTune($key, $data);
public function setOnTune($key, $data);
public function newSetting();
public function appendToSetting($key, $data);
public function setOnSetting($key, $data);
public function newPerson(array $data);
public function storeTune($music);
}

View File

@@ -0,0 +1,9 @@
<?php
namespace XaiCorp\AbcParser\Interfaces;
interface Exporter
{
public function toAbc();
public function toJson();
}

View File

@@ -0,0 +1,9 @@
<?php
namespace XaiCorp\AbcParser\Interfaces;
interface Manipulator
{
public function getTunes();
public function removeTune($tuneIndex);
}

93
src/Models/Arr/Abc.php Normal file
View File

@@ -0,0 +1,93 @@
<?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);
}
}

133
src/Models/Laravel5/Abc.php Normal file
View File

@@ -0,0 +1,133 @@
<?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 Tune
*/
public $tune;
/**
* @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->tune->$key;
$array[] = $data;
$this->setOnTune($key, $array);
}
public function setOnTune($key, $data)
{
$this->tune->$key = $data;
}
public function newSetting()
{
$this->setting = new TuneSetting();
}
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();
}
}

View File

@@ -0,0 +1,75 @@
<?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 save(array $options = [])
{
$class = new \ReflectionClass($this->attributesClass);
foreach ($class->getStaticPropertyValue('types') as $attribute) {
$this->saveAttr($attribute);
}
$saved = parent::save($options);
return $saved;
}
}

View File

@@ -0,0 +1,228 @@
<?php
namespace XaiCorp\AbcParser\Models\Laravel5;
class Collection extends ValidatingModel implements \Countable
{
use AttributesTrait;
/**
* 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 = [
'Author',
'Composer',
'Discography',
'Rhythm',
'History',
'File',
'Book',
'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;
}
/**************************************************************
* mutators and accessors
*/
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;
}
}
}
foreach ($arr as $key => $val) {
if (empty($val)) {
unset($arr[$key]);
}
}
return $arr;
}
}

View File

@@ -0,0 +1,190 @@
<?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|integer|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);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace XaiCorp\AbcParser\Models\Laravel5;
use Illuminate\Database\Eloquent\Model as BaseModel;
use XaiCorp\AbcParser\Traits\ValidationTrait;
class Person extends BaseModel
{
use ValidationTrait;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'persons';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email'];
protected $hidden = ['email'];
//Relationships
public function toArray()
{
return $this->name;
}
}

View File

@@ -0,0 +1,121 @@
<?php
namespace XaiCorp\AbcParser\Models\Laravel5;
use Illuminate\Database\Eloquent\Model as BaseModel;
use XaiCorp\AbcParser\Traits\ValidationTrait;
class Tune extends ValidatingModel
{
use AttributesTrait;
/**
* 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 = ['x_id', 'Title', 'Group', 'Origin', 'Rhythm', 'History'];
protected $appends = ['T', 'G', 'O', 'R', 'H'];
/**************************************************************
* 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;
}
}

View File

@@ -0,0 +1,134 @@
<?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|integer|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);
}
}

View File

@@ -0,0 +1,168 @@
<?php
namespace XaiCorp\AbcParser\Models\Laravel5;
use Illuminate\Database\Eloquent\Model as BaseModel;
use XaiCorp\AbcParser\Traits\ValidationTrait;
class TuneSetting extends ValidatingModel
{
use AttributesTrait;
/**
* 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|integer|min:1|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 = ['Transcriber', 'Note', 'Discography', 'Source', 'Word', 'Book'];
protected $appends = ['Z', 'N', 'D', 'S', 'W', 'B'];
/**************************************************************
* 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);
}
}

View File

@@ -0,0 +1,145 @@
<?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|integer|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);
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace XaiCorp\AbcParser\Models\Laravel5;
use Illuminate\Database\Eloquent\Model as BaseModel;
use XaiCorp\AbcParser\Traits\ValidationTrait;
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;
}
}

67
src/Models/Memory/Abc.php Normal file
View File

@@ -0,0 +1,67 @@
<?php
namespace XaiCorp\AbcParser\Models\Memory;
use XaiCorp\AbcParser\Interfaces\Builder;
use XaiCorp\AbcParser\Interfaces\Exporter;
use XaiCorp\AbcParser\Interfaces\Manipulator;
class Abc implements Builder
{
protected $collection;
protected $currentTune;
protected $currentSetting;
public function newCollection()
{
$this->collection = new TuneCollection();
}
public function appendToCollection($key, $data)
{
$this->collection->append($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 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);
}
}

View File

@@ -0,0 +1,113 @@
<?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;
}
}

View File

@@ -0,0 +1,34 @@
<?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();
}

View File

@@ -0,0 +1,130 @@
<?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
*
********************************************************/
}

View File

@@ -0,0 +1,281 @@
<?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;
}
}

254
src/Models/Memory/Tune.php Normal file
View File

@@ -0,0 +1,254 @@
<?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
*
********************************************************/
}

View File

@@ -0,0 +1,271 @@
<?php
namespace XaiCorp\AbcParser\Models\Memory;
/**
* 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 {
private $tc_id = 0; // collection id
private $cOwner; // collection owner. The user that uploaded it
// private $tunes = array();
//abc tunebook header fields
private $A = array(); //Author / Area
private $B = array(); //Book
private $C = array(); //Composer
private $D = array(); //Discography
private $F = array(); //file url
private $H = array(); //History
private $L; //note length
private $M; //Meter
private $N = array(); //Notes
private $O = array(); //Origin
private $R; //Rhythm
private $S = array(); //Source
private $Z = array(); //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));
}
}

149
src/Parser.php Normal file
View File

@@ -0,0 +1,149 @@
<?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 attibutes 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;
} else if ($mode === self::MODE_TUNE_BODY) {
$music .= $line . PHP_EOL;
} else if (preg_match(self::LN_VERSION, $line)) {
//this line might contain the abc version.
//What do we want to do with it?
} else if (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);
} else if (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);
}
} else if($inTunes) {
if (in_array($key, self::SINGLE_VALUE_KEYS)) {
$this->builder->setOnTune($key, $data);
} else {
$this->builder->appendToTune($key, $data);
}
}
} else if (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);
}
} else if($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);
// }
}

View File

@@ -0,0 +1,60 @@
<?php
namespace XaiCorp\AbcParser\Traits;
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();
}
}