Skip to content

Commit

Permalink
Merge 74bf9f2 into 9f2f32c
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Oppermann committed Nov 3, 2016
2 parents 9f2f32c + 74bf9f2 commit daca2b8
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 19 deletions.
7 changes: 6 additions & 1 deletion src/Contracts/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ public function getValue();
* @return string
*/
public function getKey();

/**
* Get the meta attribute group.
*
* @return string
*/
public function getMetaGroup();
/**
* Set value of the meta attribute.
*
Expand Down
3 changes: 2 additions & 1 deletion src/Contracts/AttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

interface AttributeBag
{
public function set($key, $value);
public function set($key, $value, $group = null);
public function getValue($key);
public function getMetaByGroup($group);
}
2 changes: 1 addition & 1 deletion src/Contracts/Metable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public function getMetaAttributes();
public function getMetaAttributesArray();
public function hasMeta($key);
public function getMeta($key);
public function setMeta($key, $value);
public function setMeta($key, $value, $group = null);
public function getAllowedMeta();
}
15 changes: 12 additions & 3 deletions src/Metable.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,17 +404,26 @@ public function getMeta($key)
{
return $this->getMetaAttributes()->getValue($key);
}

/**
* Get meta attribute values by group.
*
* @param string $key
* @return mixed
*/
public function getMetaByGroup($group)
{
return $this->getMetaAttributes()->getMetaByGroup($group);
}
/**
* Set meta attribute.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function setMeta($key, $value)
public function setMeta($key, $value, $group = null)
{
$this->getMetaAttributes()->set($key, $value);
$this->getMetaAttributes()->set($key, $value, $group);
}

/**
Expand Down
33 changes: 29 additions & 4 deletions src/Metable/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ class Attribute extends Model implements AttributeContract
*
* @var array
*/
protected $visible = ['meta_key', 'meta_value', 'meta_type'];
protected $visible = ['meta_key', 'meta_value', 'meta_type', 'meta_group'];

/**
* Create new attribute instance.
*
* @param string|array $key
* @param mixed $value
*/
public function __construct($key = null, $value = null)
public function __construct($key = null, $value = null, $group = null)
{
// default behaviour
if (is_array($key)) {
Expand All @@ -89,7 +89,7 @@ public function __construct($key = null, $value = null)
parent::__construct();

if (is_string($key)) {
$this->set($key, $value);
$this->set($key, $value, $group);
}
}
}
Expand Down Expand Up @@ -119,11 +119,13 @@ protected static function boot()
*
* @param string $key
* @param mixed $value
* @param string $group
*/
protected function set($key, $value)
protected function set($key, $value, $group = 'default')
{
$this->setMetaKey($key);
$this->setValue($value);
$this->setMetaGroup($group);
}

/**
Expand Down Expand Up @@ -161,6 +163,15 @@ public function getMetaKey()
return $this->attributes['meta_key'];
}

/**
* Get the meta attribute group.
*
* @return string
*/
public function getMetaGroup()
{
return $this->attributes['meta_group'];
}
/**
* Cast value to proper type.
*
Expand Down Expand Up @@ -194,7 +205,21 @@ protected function setMetaKey($key)

$this->attributes['meta_key'] = $key;
}
/**
* Set group of the meta attribute.
*
* @param string $group
*
* @throws \InvalidArgumentException
*/
public function setMetaGroup($group = null)
{
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $group) && $group !== null) {
throw new InvalidArgumentException("Provided group [{$group}] is not valid variable name.");
}

$this->attributes['meta_group'] = $group;
}
/**
* Set type of the meta attribute.
*
Expand Down
25 changes: 18 additions & 7 deletions src/Metable/AttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ public function __construct($attributes = [])
* @param mixed $value
* @return $this
*/
public function set($key, $value = null)
public function set($key, $value = null, $group = null)
{
if ($key instanceof Attribute) {
return $this->setInstance($key);
}

if ($this->has($key)) {
$this->update($key, $value);
$this->update($key, $value, $group);
} else {
$this->items[$key] = $this->newAttribute($key, $value);
$this->items[$key] = $this->newAttribute($key, $value, $group);
}

return $this;
Expand Down Expand Up @@ -85,14 +85,16 @@ protected function addInstance(Attribute $attribute)
* @param mixed $value
* @return $this
*/
protected function update($key, $value = null)
protected function update($key, $value = null, $group = null)
{
if ($key instanceof Attribute) {
$value = $key->getValue();
$group = $key->getMetaGroup();
$key = $key->getMetaKey();
}

$this->get($key)->setValue($value);
$this->get($key)->setMetaGroup($group);

return $this;
}
Expand All @@ -104,9 +106,9 @@ protected function update($key, $value = null)
* @param mixed $value
* @return \Sofa\Eloquence\Metable\Attribute
*/
protected function newAttribute($key, $value)
protected function newAttribute($key, $value, $group = null)
{
return new Attribute($key, $value);
return new Attribute($key, $value, $group);
}

/**
Expand All @@ -121,7 +123,16 @@ public function getValue($key)
return $attribute->getValue();
}
}

/**
* Get attribute values by group.
*
* @param string $group
* @return mixed
*/
public function getMetaByGroup($group)
{
return $this->where('meta_group', $group);
}
/**
* Get collection as key-value array.
*
Expand Down
1 change: 1 addition & 0 deletions src/Metable/CreateMetaAttributesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function up()
$table->string('meta_key');
$table->longText('meta_value');
$table->string('meta_type')->default('string');
$table->string('meta_group')->nullable();
$table->morphs('metable');

$table->index('meta_key');
Expand Down
15 changes: 14 additions & 1 deletion tests/AttributeBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ public function it_gets_raw_attribute_value()
$this->assertEquals('bax', $bag->getValue('baz'));
}

/**
* @test
*/
public function it_gets_attributes_by_group()
{
$bag = $this->getBag();

$this->assertEquals([
'baz' => $bag->get('baz'),
'bar' => $bag->get('bar'),
], $bag->getMetaByGroup('group')->toArray());
}
/**
* @test
*/
Expand All @@ -110,7 +122,8 @@ protected function getBag()
{
return new AttributeBag([
new Attribute('foo', 'bar'),
new Attribute('baz', 'bax')
new Attribute('baz', 'bax','group'),
new Attribute('bar', 'baz','group'),
]);
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ public function getters()

$this->assertEquals('color', $attribute->getMetaKey());
$this->assertEquals('red', $attribute->getValue());
$this->assertEquals(NULL, $attribute->getMetaGroup());

$attribute = $this->getAttributeWithGroup('group');

$this->assertEquals('color', $attribute->getMetaKey());
$this->assertEquals('red', $attribute->getValue());
$this->assertEquals('group', $attribute->getMetaGroup());
}

/**
Expand Down Expand Up @@ -132,6 +139,11 @@ protected function getAttribute()
{
return new Attribute('color', 'red');
}

protected function getAttributeWithGroup($group = null)
{
return new Attribute('color', 'red', $group);
}
}

class AttributeStub extends Attribute {
Expand Down
31 changes: 30 additions & 1 deletion tests/MetableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Sofa\Eloquence\Metable;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\ArgumentBag;
use Sofa\Eloquence\Metable\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\Builder as Query;
Expand Down Expand Up @@ -522,7 +523,7 @@ public function it_gets_attribute_from_the_bag()
public function it_sets_attribute_on_the_bag()
{
$bag = $this->getBag();
$bag->shouldReceive('set')->with('color', 'red')->once();
$bag->shouldReceive('set')->with('color', 'red', NULL)->once();

$model = $this->getMetableStub();
$model->shouldReceive('getMetaAttributes')->andReturn($bag);
Expand All @@ -544,6 +545,34 @@ public function it_gets_meta_attributes_as_key_value_array()
$this->assertEquals(['color' => 'red'], $model->getMetaAttributesArray());
}

/**
* @test
*/
public function it_sets_attribute_with_group_on_the_bag()
{
$bag = $this->getBag();
$bag->shouldReceive('set')->with('color', 'red', 'group')->once();

$model = $this->getMetableStub();
$model->shouldReceive('getMetaAttributes')->andReturn($bag);

$model->setMeta('color', 'red', 'group');
}

/**
* @test
*/
public function it_gets_attribute_with_group_on_the_bag()
{
$model = $this->getModel();

$model->setMeta('test','1','group');
$model->setMeta('test2','2',null);

$this->assertEquals($model->getMetaByGroup('group')->toArray(), ['test' => new Attribute('test','1','group')]);

}

/**
* @test
*/
Expand Down

0 comments on commit daca2b8

Please sign in to comment.