-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
279 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace Plank\Metable; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
trait MetableAttributes | ||
{ | ||
public function getAttribute($key) | ||
{ | ||
if ($this->isMetaAttribute($key)) { | ||
return $this->getMeta($this->metaAttributeToKey($key)); | ||
} | ||
|
||
return parent::getAttribute($key); | ||
} | ||
|
||
public function setAttribute($key, $value) | ||
{ | ||
if ($this->isMetaAttribute($key)) { | ||
$this->setMeta($this->metaAttributeToKey($key), $value); | ||
return; | ||
} | ||
|
||
parent::setAttribute($key, $value); | ||
} | ||
|
||
public function fill(array $attributes) | ||
{ | ||
foreach ($attributes as $key => $value) { | ||
if ($this->isMetaAttribute($key)) { | ||
$this->setMeta($this->metaAttributeToKey($key), $value); | ||
unset($attributes[$key]); | ||
} | ||
} | ||
|
||
parent::fill($attributes); | ||
} | ||
|
||
public function getMetaAttributes(): Collection | ||
{ | ||
$attributes = []; | ||
foreach ($this->getAllMeta() as $key => $value) { | ||
$attributes[$this->metaKeyToAttribute($key)] = $value; | ||
} | ||
return collect($attributes); | ||
} | ||
|
||
public function offsetExists($key): bool | ||
{ | ||
if ($this->isMetaAttribute($key)) { | ||
return $this->hasMeta($this->metaAttributeToKey($key)); | ||
} | ||
|
||
return parent::isset($key); | ||
} | ||
|
||
public function offsetUnset($key): void | ||
{ | ||
if ($this->isMetaAttribute($key)) { | ||
$this->removeMeta($this->metaAttributeToKey($key)); | ||
return; | ||
} | ||
|
||
parent::offsetUnset($key); | ||
} | ||
|
||
protected function isMetaAttribute($key): bool | ||
{ | ||
return str_starts_with($key, 'meta_') | ||
&& !array_key_exists($key, $this->attributes) | ||
&& !array_key_exists($key, $this->casts); | ||
} | ||
|
||
public function toArray() | ||
{ | ||
if (property_exists($this, 'includeMetaInArray') | ||
&& !$this->includeMetaInArray | ||
) { | ||
return parent::toArray(); | ||
} | ||
|
||
return array_merge( | ||
parent::toArray(), | ||
$this->getArrayableItems($this->getMetaAttributes()->toArray()) | ||
); | ||
} | ||
|
||
public function makeMetaHidden(): void | ||
{ | ||
$this->hidden = array_merge( | ||
$this->hidden, | ||
array_keys($this->getMetaAttributes()->toArray()) | ||
); | ||
} | ||
|
||
protected function metaAttributeToKey(string $attribute): string | ||
{ | ||
return substr($attribute, 5); | ||
} | ||
|
||
protected function metaKeyToAttribute(string $key): string | ||
{ | ||
return 'meta_' . $key; | ||
} | ||
|
||
abstract public function getMeta(string $key, mixed $default = null): mixed; | ||
|
||
abstract public function setMeta(string $key, mixed $value, bool $encrypt = false): void; | ||
|
||
abstract public function removeMeta(string $key): void; | ||
|
||
abstract public function getAllMeta(): \Illuminate\Support\Collection; | ||
|
||
abstract protected function getArrayableItems(array $values); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Plank\Metable\Tests\Integration; | ||
|
||
use Plank\Metable\Tests\Mocks\SampleMetable; | ||
use Plank\Metable\Tests\TestCase; | ||
|
||
class MetableAttributesTest extends TestCase | ||
{ | ||
public function test_it_mutates_meta_attributes() | ||
{ | ||
$this->useDatabase(); | ||
|
||
$model = $this->createMetable(); | ||
$this->assertFalse($model->hasMeta('var')); | ||
$this->assertFalse(isset($model->meta_var)); | ||
$this->assertNull($model->getAttribute('meta_var')); | ||
|
||
$model->setAttribute('meta_var', 'bar'); | ||
$this->assertEquals('bar', $model->getMeta('var')); | ||
$this->assertTrue(isset($model->meta_var)); | ||
$this->assertEquals('bar', $model->getAttribute('meta_var')); | ||
|
||
$model->meta_var = 'baz'; | ||
$this->assertEquals('baz', $model->getMeta('var')); | ||
$this->assertTrue(isset($model->meta_var)); | ||
$this->assertEquals('baz', $model->getAttribute('meta_var')); | ||
|
||
$model->offsetUnset('meta_var'); | ||
$this->assertFalse($model->hasMeta('var')); | ||
$this->assertFalse(isset($model->meta_var)); | ||
$this->assertNull($model->getAttribute('meta_var')); | ||
|
||
$model->fill(['meta_var' => 'qux']); | ||
$this->assertEquals('qux', $model->getMeta('var')); | ||
$this->assertTrue(isset($model->meta_var)); | ||
$this->assertEquals('qux', $model->getAttribute('meta_var')); | ||
|
||
unset($model['meta_var']); | ||
$this->assertFalse($model->hasMeta('var')); | ||
$this->assertFalse(isset($model->meta_var)); | ||
$this->assertNull($model->getAttribute('meta_var')); | ||
} | ||
|
||
public function test_it_doesnt_overwrite_existing_attributes() | ||
{ | ||
$this->useDatabase(); | ||
|
||
$model = $this->createMetable(); | ||
$model->meta_attribute = 'bar'; | ||
$this->assertFalse($model->hasMeta('attribute')); | ||
|
||
$model->setAttribute('meta_attribute', 'baz'); | ||
$this->assertFalse($model->hasMeta('attribute')); | ||
} | ||
|
||
public function test_it_converts_to_array() | ||
{ | ||
$this->useDatabase(); | ||
$model = $this->createMetable(); | ||
$model->meta_var = 'foo'; | ||
$model->meta_var2 = 'foo2'; | ||
|
||
$this->assertEquals( | ||
collect([ | ||
'meta_var' => 'foo', | ||
'meta_var2' => 'foo2', | ||
'meta_foo' => 'bar' // default value | ||
]), | ||
$model->getMetaAttributes() | ||
); | ||
|
||
$model->makeHidden('meta_var2', 'created_at', 'updated_at', 'meta'); | ||
|
||
$array = $model->toArray(); | ||
$this->assertEquals([ | ||
'meta_attribute' => '', | ||
'id' => $model->getKey(), | ||
'meta_foo' => 'bar', | ||
'meta_var' => 'foo' | ||
], $array); | ||
|
||
$model->makeMetaHidden(); | ||
|
||
$array = $model->toArray(); | ||
$this->assertEquals([ | ||
'meta_attribute' => '', | ||
'id' => $model->getKey(), | ||
], $array); | ||
} | ||
|
||
private function createMetable(array $attributes = []): SampleMetable | ||
{ | ||
return factory(SampleMetable::class)->create($attributes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters