added docblocks, contract, and service provider
This commit is contained in:
80
src/Contracts/MetaContract.php
Normal file
80
src/Contracts/MetaContract.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Glmdev\Meta\Contracts;
|
||||
|
||||
interface MetaContract
|
||||
{
|
||||
/**
|
||||
* Ensure the model recieves valid UUID and meta columns when created.
|
||||
*
|
||||
* @param array $args
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
public static function create( array $args = [] );
|
||||
|
||||
/**
|
||||
* Read the serialized value.
|
||||
*
|
||||
* @param string|int $key
|
||||
*
|
||||
* @return string|int|float|array|bool|null
|
||||
*/
|
||||
public function read( $key );
|
||||
|
||||
/**
|
||||
* 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 );
|
||||
|
||||
/**
|
||||
* Get the serialized value of the meta info.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function readRaw();
|
||||
|
||||
/**
|
||||
* Set (override) the entire meta with an unmodified string.
|
||||
*
|
||||
* @param string $serialized
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function writeRaw( $serialized );
|
||||
|
||||
/**
|
||||
* Get the universal identifier of the model.
|
||||
*
|
||||
* @return \Ramsey\Uuid\Uuid
|
||||
*/
|
||||
public function getUuid();
|
||||
|
||||
/**
|
||||
* Set the universal identifier of the model (can only be set once).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUuid();
|
||||
|
||||
/**
|
||||
* Ask if the model has a universal identifier.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasUuid();
|
||||
|
||||
/**
|
||||
* Set the universal identifier via existing UUID.
|
||||
*
|
||||
* @param \Ramsey\Uuid\Uuid $uuid
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setRawUuid( \Ramsey\Uuid\Uuid $uuid );
|
||||
}
|
||||
121
src/Meta.php
121
src/Meta.php
@@ -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(){}
|
||||
|
||||
}
|
||||
40
src/MetaServiceProvider.php
Normal file
40
src/MetaServiceProvider.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Glmdev\Meta;
|
||||
|
||||
use Glmdev\Meta\Contracts;
|
||||
use Glmdev\Meta\Meta;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class MetaServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Indicates if loading of the provider is deferred.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $defer = true;
|
||||
|
||||
/**
|
||||
* Register any package services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->bind(Contracts\Meta::class, 'Meta');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function provides()
|
||||
{
|
||||
return [
|
||||
Contracts\Meta::class,
|
||||
'Meta',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user