eloquent-meta/src/Meta.php

143 lines
3.1 KiB
PHP
Raw Normal View History

2016-04-17 17:29:50 +00:00
<?php
namespace Glmdev\Meta;
use Glmdev\Foundation\FoundationModel;
use Glmdev\Meta\Contracts\MetaContract;
2016-04-17 17:29:50 +00:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Ramsey\Uuid\Uuid;
class Meta extends Model implements FoundationModel, MetaContract
2016-04-17 17:29:50 +00:00
{
/**
* Allow the changing of the meta and uuid database fields.
*
* @var array
*/
2016-04-17 17:29:50 +00:00
protected $fillable = ['meta', 'uuid'];
/**
* 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([]);
2016-06-25 22:10:21 +00:00
$args['uuid'] = (isset($args['uuid'])) ? $args['uuid'] : Uuid::uuid4();
return parent::create($args);
}
/**
* Read the serialized value.
*
* @param string|int $key
*
* @return string|int|float|array|bool|null
*/
public function read( $key ){
2016-04-17 17:29:50 +00:00
$meta = unserialize( $this->meta );
2016-06-18 20:58:40 +00:00
if ( array_key_exists( $key, $meta ) ){
return $meta[ $key ];
}
return null;
2016-04-17 17:29:50 +00:00
}
/**
* 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 ){
2016-04-17 17:29:50 +00:00
$meta = unserialize( $this->meta );
$meta[ $key ] = $value;
$meta = serialize( $meta );
$this->meta = $meta;
$this->save();
}
/**
* Get the serialized value of the meta info.
*
* @return string
*/
public function readRaw(){
2016-04-17 17:29:50 +00:00
return $this->meta;
}
/**
* Set (override) the entire meta with an unmodified string.
*
* @param string $serialized
*
* @return void
*/
public function writeRaw( $serialized ){
2016-04-17 17:29:50 +00:00
$this->meta = $serialized;
$this->save();
}
/**
* Get the universal identifier of the model.
*
* @return \Ramsey\Uuid\Uuid
*/
2016-04-17 17:29:50 +00:00
public function getUUID(){
return $this->uuid;
}
/**
* Set the universal identifier of the model (can only be set once).
*
* @return void
*/
2016-04-17 17:29:50 +00:00
public function setUuid(){
if( is_null($this->uuid) || $this->uuid === "" ){
2016-04-17 17:29:50 +00:00
$this->uuid = Uuid::uuid4();
}
}
/**
* 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 ){
2016-04-17 17:29:50 +00:00
$this->uuid = $uuid;
$this->save();
}
/**
* Add the UUID and Meta columns to the table.
*
* @param \Illuminate\Database\Schema\Blueprint
*
* @return void
*/
2016-04-17 17:29:50 +00:00
public static function formTable( Blueprint $table ){
$table->uuid('uuid');
$table->text('meta');
}
2016-06-25 01:37:24 +00:00
public static function route(){}
2016-04-17 17:29:50 +00:00
}