added docblocks, contract, and service provider

This commit is contained in:
Jake Mitchell
2016-06-25 21:12:01 +00:00
parent 0985c6c829
commit ea3ec0bacc
13 changed files with 407 additions and 179 deletions

View File

@@ -1,34 +1,63 @@
<?php
/**
* Created by PhpStorm.
* User: garrett
* Date: 4/17/16
* Time: 11:59 AM
*/
namespace Glmdev\Meta;
use Glmdev\Foundation\FoundationModel;
use Glmdev\Meta\Contracts\MetaContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Ramsey\Uuid\Uuid;
use Glmdev\Foundation\FoundationModel;
class Meta extends Model implements FoundationModel
class Meta extends Model implements FoundationModel, MetaContract
{
/**
* Allow the changing of the meta and uuid database fields.
*
* @var array
*/
protected $fillable = ['meta', 'uuid'];
function read( $key ){
/**
* Ensure the model recieves valid UUID and meta columns when created.
*
* @param array $args
*
* @return callable
*/
public static function create( array $args = [] ){
$args['meta'] = (isset($args['meta'])) ? $args['meta'] : serialize([]);
$args['uuid'] = (isset($args['uuid'])) ? $args['uuid'] : '';
return parent::create($args);
}
/**
* Read the serialized value.
*
* @param string|int $key
*
* @return string|int|float|array|bool|null
*/
public function read( $key ){
$meta = unserialize( $this->meta );
if ( array_key_exists( $key, $meta ) ){
return $meta[ $key ];
}
else {
return null;
}
return null;
}
function write( $key, $value ){
/**
* Adds the key-value pair into the serialized data set.
*
* @param string|int $key
* @param string|int|float|array|bool|null $value
*
* @return void
*/
public function write( $key, $value ){
$meta = unserialize( $this->meta );
$meta[ $key ] = $value;
$meta = serialize( $meta );
@@ -36,41 +65,79 @@ class Meta extends Model implements FoundationModel
$this->save();
}
function readRaw(){
/**
* Get the serialized value of the meta info.
*
* @return string
*/
public function readRaw(){
return $this->meta;
}
function writeRaw( $serialized ){
/**
* Set (override) the entire meta with an unmodified string.
*
* @param string $serialized
*
* @return void
*/
public function writeRaw( $serialized ){
$this->meta = $serialized;
$this->save();
}
/**
* Get the universal identifier of the model.
*
* @return \Ramsey\Uuid\Uuid
*/
public function getUUID(){
return $this->uuid;
}
/**
* Set the universal identifier of the model (can only be set once).
*
* @return void
*/
public function setUuid(){
// check if UUID is set
if( !isset($this->uuid) || is_null($this->uuid) || $this->uuid === "" ){
if( is_null($this->uuid) || $this->uuid === "" ){
$this->uuid = Uuid::uuid4();
return true;
}
else {
return false;
}
}
public function rawUuid( Uuid $uuid ){
/**
* Ask if the model has a universal identifier.
*
* @return bool
*/
public function hasUuid(){
return ( isset($this->uuid) && $this->uuid !== "" );
}
/**
* Set the universal identifier via existing UUID.
*
* @param \Ramsey\Uuid\Uuid $uuid
*
* @return void
*/
public function setRawUuid( Uuid $uuid ){
$this->uuid = $uuid;
$this->save();
}
/**
* Add the UUID and Meta columns to the table.
*
* @param \Illuminate\Database\Schema\Blueprint
*
* @return void
*/
public static function formTable( Blueprint $table ){
$table->uuid('uuid');
$table->text('meta');
}
public static function route(){}
}