Allow users to write multiple items at once

master
Jake Mitchell 8 years ago
parent e5b48176c4
commit 110ff0a15c

@ -25,12 +25,12 @@ interface MetaContract
/**
* Adds the key-value pair into the serialized data set.
*
* @param string|int $key
* @param string|int|array $keyOrArray
* @param string|int|float|array|bool|null $value
*
* @return void
*/
public function write( $key, $value );
public function write( $keyOrArray, $value = null );
/**
* Get the serialized value of the meta info.

@ -52,14 +52,22 @@ class Meta extends Model implements FoundationModel, MetaContract
/**
* Adds the key-value pair into the serialized data set.
*
* @param string|int $key
* @param string|int|array $keyOrArray
* @param string|int|float|array|bool|null $value
*
* @return void
*/
public function write( $key, $value ){
public function write( $keyOrArray, $value = null ){
$meta = unserialize( $this->meta );
$meta[ $key ] = $value;
if( is_array( $keyOrArray ) ){
foreach( $keyOrArray as $key => $val ){
$meta[ $key ] = $val;
}
} else {
$meta[ $keyOrArray ] = $value;
}
$meta = serialize( $meta );
$this->meta = $meta;
$this->save();

@ -13,11 +13,21 @@ class MetaTest extends \PHPUnit_Framework_TestCase
$model->write('name', 'John');
$model->write('age', 15);
$model->write('email', 'Johnny@appleseed.com');
$model->write('true', false);
$model->write([
'food' => 'taco',
'old' => 'spice',
'new' => 1935
]);
$this->assertEquals('John', $model->read('name'));
$this->assertEquals(15, $model->read('age'));
$this->assertEquals('Johnny@appleseed.com', $model->read('email'));
$this->assertEquals(false, $model->read('true'));
$this->assertEquals('taco', $model->read('food'));
$this->assertEquals('spice', $model->read('old'));
$this->assertEquals(1935, $model->read('new'));
}
public function testUuid ()

Loading…
Cancel
Save