tests and laravel models
This commit is contained in:
@@ -13,11 +13,6 @@ class Abc implements Builder, Manipulator, Exporter
|
||||
*/
|
||||
public $collection;
|
||||
|
||||
/**
|
||||
* @var Tune
|
||||
*/
|
||||
public $tune;
|
||||
|
||||
/**
|
||||
* @var TuneSetting
|
||||
*/
|
||||
@@ -65,20 +60,24 @@ class Abc implements Builder, Manipulator, Exporter
|
||||
|
||||
public function appendToTune($key, $data)
|
||||
{
|
||||
$array = $this->tune->$key;
|
||||
$array = $this->setting->tune->$key;
|
||||
$array[] = $data;
|
||||
$this->setOnTune($key, $array);
|
||||
}
|
||||
|
||||
public function setOnTune($key, $data)
|
||||
{
|
||||
$this->tune->$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)
|
||||
|
||||
@@ -62,7 +62,7 @@ trait AttributesTrait
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function save(array $options = [])
|
||||
public function saveAttributes(array $options = [])
|
||||
{
|
||||
$class = new \ReflectionClass($this->attributesClass);
|
||||
foreach ($class->getStaticPropertyValue('types') as $attribute) {
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace XaiCorp\AbcParser\Models\Laravel5;
|
||||
|
||||
class Collection extends ValidatingModel implements \Countable
|
||||
{
|
||||
use AttributesTrait;
|
||||
use AttributesTrait, UuidTrait;
|
||||
|
||||
/**
|
||||
* for AttributesTrait
|
||||
@@ -28,6 +28,7 @@ class Collection extends ValidatingModel implements \Countable
|
||||
protected $fillable = ['name'];
|
||||
|
||||
protected $hidden = [
|
||||
'id',
|
||||
'Author',
|
||||
'Composer',
|
||||
'Discography',
|
||||
@@ -35,6 +36,7 @@ class Collection extends ValidatingModel implements \Countable
|
||||
'History',
|
||||
'File',
|
||||
'Book',
|
||||
'meter',
|
||||
'Note',
|
||||
'Origin',
|
||||
'Source',
|
||||
@@ -63,11 +65,31 @@ class Collection extends ValidatingModel implements \Countable
|
||||
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()
|
||||
{
|
||||
@@ -216,6 +238,7 @@ class Collection extends ValidatingModel implements \Countable
|
||||
}
|
||||
}
|
||||
|
||||
$arr['M'] = $this->meter;
|
||||
foreach ($arr as $key => $val) {
|
||||
if (empty($val)) {
|
||||
unset($arr[$key]);
|
||||
|
||||
@@ -16,7 +16,7 @@ class CollectionAttribute extends ValidatingModel
|
||||
* @var array
|
||||
*/
|
||||
protected $validationRules = [
|
||||
'collection_id' => 'required|integer|exists:collections,id',
|
||||
'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'
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Laravel5;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model as BaseModel;
|
||||
use XaiCorp\AbcParser\Traits\ValidationTrait;
|
||||
|
||||
class Person extends BaseModel
|
||||
class Person extends ValidatingModel
|
||||
{
|
||||
use ValidationTrait;
|
||||
use UuidTrait;
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
@@ -22,6 +19,12 @@ class Person extends BaseModel
|
||||
*/
|
||||
protected $fillable = ['name', 'email'];
|
||||
|
||||
protected $validationRules = [
|
||||
'id' => 'required',
|
||||
'name' => 'required|string|max:63',
|
||||
'email' => 'required|email|max:255'
|
||||
];
|
||||
|
||||
protected $hidden = ['email'];
|
||||
|
||||
//Relationships
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Laravel5;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model as BaseModel;
|
||||
use XaiCorp\AbcParser\Traits\ValidationTrait;
|
||||
|
||||
class Tune extends ValidatingModel
|
||||
{
|
||||
use AttributesTrait;
|
||||
use AttributesTrait, UuidTrait;
|
||||
|
||||
/**
|
||||
* for AttributesTrait
|
||||
@@ -35,10 +32,28 @@ class Tune extends ValidatingModel
|
||||
*/
|
||||
protected $fillable = ['x_id', 'X', 'T'];
|
||||
|
||||
protected $hidden = ['x_id', 'Title', 'Group', 'Origin', 'Rhythm', 'History'];
|
||||
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
|
||||
|
||||
@@ -16,7 +16,7 @@ class TuneAttribute extends ValidatingModel
|
||||
* @var array
|
||||
*/
|
||||
protected $validationRules = [
|
||||
'tune_id' => 'required|integer|exists:tunes,id',
|
||||
'tune_id' => 'required|exists:tunes,id',
|
||||
'type' => 'required|string|in:Title,Group,Origin,Rhythm,History',
|
||||
'string' => 'required|string|max:255',
|
||||
'ordering' => 'required|integer'
|
||||
|
||||
@@ -6,7 +6,7 @@ use XaiCorp\AbcParser\Traits\ValidationTrait;
|
||||
|
||||
class TuneSetting extends ValidatingModel
|
||||
{
|
||||
use AttributesTrait;
|
||||
use AttributesTrait, UuidTrait;
|
||||
|
||||
/**
|
||||
* for AttributesTrait
|
||||
@@ -25,7 +25,7 @@ class TuneSetting extends ValidatingModel
|
||||
* @var array
|
||||
*/
|
||||
protected $validationRules = [
|
||||
'tune_id' => 'required|integer|min:1|exists:tunes,id',
|
||||
'tune_id' => 'required|exists:tunes,id',
|
||||
'meter' => 'string|max:3',
|
||||
'keysig' => 'string|max:5',
|
||||
'filename' => 'string|max:255',
|
||||
@@ -47,10 +47,15 @@ class TuneSetting extends ValidatingModel
|
||||
*/
|
||||
protected $fillable = ['tune_id', 'meter', 'keysig', 'filename', 'tempo', 'L', 'music', 'parts'];
|
||||
|
||||
protected $hidden = ['Transcriber', 'Note', 'Discography', 'Source', 'Word', 'Book'];
|
||||
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
|
||||
|
||||
@@ -16,7 +16,7 @@ class TuneSettingAttribute extends ValidatingModel
|
||||
* @var array
|
||||
*/
|
||||
protected $validationRules = [
|
||||
'setting_id' => 'required|integer|exists:tune_settings,id',
|
||||
'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'
|
||||
|
||||
32
src/Models/Laravel5/UuidTrait.php
Normal file
32
src/Models/Laravel5/UuidTrait.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace XaiCorp\AbcParser\Models\Laravel5;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,12 +34,13 @@ class Parser {
|
||||
}
|
||||
|
||||
/**
|
||||
* extract the attibutes from the raw abc
|
||||
* extract the attributes from the raw abc
|
||||
* @param <string> $abc string containing the raw abc
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public function parseABC($abc) {
|
||||
public function parseABC($abc)
|
||||
{
|
||||
//tune is either new or existing data has been loaded
|
||||
//create new setting
|
||||
$mode = self::MODE_FILE_HEADER;
|
||||
|
||||
Reference in New Issue
Block a user