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(){} }