You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
4.2 KiB

<?php
namespace Glmdev\Meta;
use Glmdev\Meta\Contracts\MetaContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Ramsey\Uuid\Uuid;
class Meta extends Model implements 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 Model
*/
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');
}
/**
* Allows the searching of models by a meta value.
*
* @param mixed $key
* @param mixed $value
*
* @return \Illuminate\Support\Collection
*/
public static function metaWhere($key, $value){
$return = array();
$models = self::all();
foreach( $models as $model ){
if ( $model->read($key) === $value ){
array_push($return, $model);
}
}
return collect($return);
}
/**
* UNIMPLEMENTED
*/
public static function route(){}
}