diff --git a/src/CastsEnums.php b/src/CastsEnums.php index 58827cc..c3ca3b9 100644 --- a/src/CastsEnums.php +++ b/src/CastsEnums.php @@ -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). @@ -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]); + } } \ No newline at end of file diff --git a/tests/AttributeAsEnumCastTest.php b/tests/EnumAccessorTest.php similarity index 70% rename from tests/AttributeAsEnumCastTest.php rename to tests/EnumAccessorTest.php index aeaf652..3dbf2e1 100644 --- a/tests/AttributeAsEnumCastTest.php +++ b/tests/EnumAccessorTest.php @@ -1,6 +1,6 @@ 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 */ @@ -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); diff --git a/tests/EnumMutatorTest.php b/tests/EnumMutatorTest.php new file mode 100644 index 0000000..9552ce8 --- /dev/null +++ b/tests/EnumMutatorTest.php @@ -0,0 +1,56 @@ + '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())); + + } + +} \ No newline at end of file