diff --git a/src/Contracts/MetaContract.php b/src/Contracts/MetaContract.php index a438266..7de87df 100644 --- a/src/Contracts/MetaContract.php +++ b/src/Contracts/MetaContract.php @@ -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. diff --git a/src/Meta.php b/src/Meta.php index 3c2ec08..8c558ce 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -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(); diff --git a/tests/MetaTest.php b/tests/MetaTest.php index 22d2c03..b155735 100644 --- a/tests/MetaTest.php +++ b/tests/MetaTest.php @@ -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 ()