Skip to content

Commit

Permalink
Fixed trait/class collision, added mutator support
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Oct 5, 2017
1 parent 10c3ab4 commit 3d18c2d
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 12 deletions.
54 changes: 46 additions & 8 deletions src/CastsEnums.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@

namespace Konekt\Enum\Eloquent;


use Illuminate\Database\Eloquent\Concerns\HasAttributes;

trait CastsEnums
{
use HasAttributes {
getAttributeValue as baseGetAttributeValue;
}

/**
* Get a plain attribute (not a relationship).
Expand All @@ -29,14 +23,58 @@ trait CastsEnums
*/
public function getAttributeValue($key)
{
if (isset($this->enums[$key])) {
if ($this->isEnumAttribute($key)) {
$class = $this->enums[$key];

return $class::create($this->getAttributeFromArray($key));
}

return $this->baseGetAttributeValue($key);
return parent::getAttributeValue($key);
}

/**
* Get an attribute from the model.
*
* @param string $key
* @return mixed
*/
public function getAttribute($key)
{
if ($this->isEnumAttribute($key)) {
return $this->getAttributeValue($key);
}

return parent::getAttribute($key);
}

/**
* Set a given attribute on the model.
*
* @param string $key
* @param mixed $value
* @return $this
*/
public function setAttribute($key, $value)
{
if ($this->isEnumAttribute($key)) {
$this->attributes[$key] = $value instanceof $this->enums[$key] ? $value->value() : $value;

return $this;
}

parent::setAttribute($key, $value);
}

/**
* Returns whether the attribute was marked as enum
*
* @param $key
*
* @return bool
*/
private function isEnumAttribute($key)
{
return isset($this->enums[$key]);
}

}
22 changes: 18 additions & 4 deletions tests/AttributeAsEnumCastTest.php → tests/EnumAccessorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Contains the AttributeAsEnumCastTest class.
* Contains the EnumAccessorTest class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
Expand All @@ -16,7 +16,7 @@
use Konekt\Enum\Eloquent\Tests\Models\Order;
use Konekt\Enum\Eloquent\Tests\Models\OrderStatus;

class AttributeAsEnumCastTest extends TestCase
class EnumAccessorTest extends TestCase
{

/**
Expand All @@ -33,6 +33,19 @@ public function it_casts_marked_attributes_to_their_proper_enum_class()
$this->assertInstanceOf(OrderStatus::class, $order->status);
}

/**
* @test
*/
public function it_returns_the_enum_default_when_attribute_is_null()
{
$order = new Order([
'number' => 'PLGU7S5'
]);

$this->assertInstanceOf(OrderStatus::class, $order->status);
$this->assertEquals(OrderStatus::__default, $order->status->value());
}

/**
* @test
*/
Expand All @@ -53,8 +66,9 @@ public function it_can_still_read_basic_properties_as_well()
public function it_can_still_read_casted_fields()
{
$order = Order::create([
'number' => 'KH8FRWAD',
'status' => OrderStatus::PROCESSING
'number' => 'KH8FRWAD',
'status' => OrderStatus::PROCESSING,
'is_active' => 1
]);

$this->assertNotNull($order->id);
Expand Down
56 changes: 56 additions & 0 deletions tests/EnumMutatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Contains the EnumMutatorTest class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
* @license MIT
* @since 2017-10-05
*
*/


namespace Konekt\Enum\Eloquent\Tests;


use Konekt\Enum\Eloquent\Tests\Models\Order;
use Konekt\Enum\Eloquent\Tests\Models\OrderStatus;

class EnumMutatorTest extends TestCase
{
/**
* @test
*/
public function it_accepts_enum_object_when_setting_value()
{
$order = Order::create([
'number' => 'NMLM1HQ',
'status' => OrderStatus::SUBMITTED
]);

$this->assertTrue($order->status->equals(OrderStatus::SUBMITTED()));

$order->status = OrderStatus::PROCESSING();
$order->save();

$this->assertTrue($order->status->equals(OrderStatus::PROCESSING()));
}

/**
* @test
*/
public function it_accepts_enums_primitive_value_as_well_when_setting_value()
{
$order = Order::create([
'number' => 'TFOD578',
'status' => OrderStatus::SUBMITTED
]);

$order->status = OrderStatus::PROCESSING;
$order->save();

$this->assertTrue($order->status->equals(OrderStatus::PROCESSING()));

}

}

0 comments on commit 3d18c2d

Please sign in to comment.