173 lines
3.9 KiB
PHP
173 lines
3.9 KiB
PHP
<?php
|
|
|
|
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;
|
|
|
|
class Meta extends Model implements FoundationModel, MetaContract
|
|
{
|
|
/**
|
|
* Allow the changing of the meta and uuid database fields.
|
|
*
|
|
* @var array
|
|
*/
|
|
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'])) ? ((gettype($args['meta']) === 'array') ? serialize($args['meta']) : $args['meta']) : serialize([]);
|
|
$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 ){
|
|
$meta = unserialize( $this->meta );
|
|
|
|
if ( array_key_exists( $key, $meta ) ){
|
|
return $meta[ $key ];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Adds the key-value pair into the serialized data set.
|
|
*
|
|
* @param string|int|array $keyOrArray
|
|
* @param string|int|float|array|bool|null $value
|
|
*
|
|
* @return void
|
|
*/
|
|
public function write( $keyOrArray, $value = null ){
|
|
$meta = unserialize( $this->meta );
|
|
|
|
if( is_array( $keyOrArray ) ){
|
|
foreach( $keyOrArray as $key => $val ){
|
|
$meta[ $key ] = $val;
|
|
}
|
|
} else {
|
|
$meta[ $keyOrArray ] = $value;
|
|
}
|
|
|
|
$meta = serialize( $meta );
|
|
$this->meta = $meta;
|
|
$this->save();
|
|
}
|
|
|
|
/**
|
|
* Get the serialized value of the meta info.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function readRaw(){
|
|
return $this->meta;
|
|
}
|
|
|
|
/**
|
|
* 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(){
|
|
if( is_null($this->uuid) || $this->uuid === "" ){
|
|
$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 ){
|
|
$this->uuid = $uuid;
|
|
$this->save();
|
|
}
|
|
|
|
/**
|
|
* Get the model that has this UUID.
|
|
*
|
|
* @param string $uuid
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public static function withUuid( $uuid ){
|
|
return parent::where('uuid', $uuid);
|
|
}
|
|
|
|
/**
|
|
* 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(){}
|
|
<<<<<<< HEAD
|
|
=======
|
|
|
|
public static function create( array $args ){
|
|
$args['meta'] = serialize([]);
|
|
$args['uuid'] = '';
|
|
|
|
return parent::create($args);
|
|
}
|
|
|
|
>>>>>>> refs/remotes/origin/master
|
|
} |