diff --git a/Changelog.md b/Changelog.md index 0e5f491..416d2ab 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,11 @@ # Changelog ### Konekt Enum Eloquent +## Unreleased +##### 2023-XX-YY + +- Fixed issue where CastEnum did not take in account hidden or visible properties from Eloquent + ## 1.9.0 ##### 2023-02-16 diff --git a/src/CastsEnums.php b/src/CastsEnums.php index 3eb6250..c00ab64 100644 --- a/src/CastsEnums.php +++ b/src/CastsEnums.php @@ -81,7 +81,11 @@ public function setAttribute($key, $value) */ public function attributesToArray() { - return $this->addEnumAttributesToArray(parent::attributesToArray()); + return $this->getArrayableItems( + $this->addEnumAttributesToArray( + parent::attributesToArray() + ) + ); } /** diff --git a/tests/EnumToArrayTest.php b/tests/EnumToArrayTest.php index 624cfc0..b8daaa9 100644 --- a/tests/EnumToArrayTest.php +++ b/tests/EnumToArrayTest.php @@ -18,6 +18,8 @@ use Konekt\Enum\Eloquent\Tests\Models\OrderStatus; use Konekt\Enum\Eloquent\Tests\Models\OrderStatusV2; use Konekt\Enum\Eloquent\Tests\Models\OrderV2; +use Konekt\Enum\Eloquent\Tests\Models\Visibility; +use Konekt\Enum\Eloquent\Tests\Models\VisibilityTalk; class EnumToArrayTest extends TestCase { @@ -129,4 +131,64 @@ public function returns_enum_v2_default_string_value_when_attribute_is_null() $this->assertIsString($array['status']); $this->assertEquals($array['status'], OrderStatusV2::__default); } + + /** @test */ + public function returns_no_enum_if_hidden() + { + $normal = new Visibility([ + 'number' => 'na321', + 'talk1' => VisibilityTalk::BLABLA, + 'talk2' => VisibilityTalk::YADDA, + ]); + + $array = $normal->attributesToArray(); + + $this->assertArrayHasKey('number', $array); + $this->assertArrayNotHasKey('talk1', $array); + $this->assertArrayHasKey('talk2', $array); + $this->assertIsString($array['talk2']); + $this->assertEquals($array['talk2'], VisibilityTalk::YADDA); + } + + /** @test */ + public function returns_no_enum_if_hidden_dynamic() + { + $dynamic_hidden = new Visibility([ + 'number' => 'na654', + 'talk1' => VisibilityTalk::BLABLA, + 'talk2' => VisibilityTalk::YADDA, + ]); + + $dynamic_hidden->makeHidden('talk2'); + + $array = $dynamic_hidden->attributesToArray(); + + $this->assertArrayHasKey('number', $array); + $this->assertArrayNotHasKey('talk1', $array); + $this->assertArrayNotHasKey('talk2', $array); + } + + /** @test */ + public function returns_enum_if_hidden_made_visible() + { + $made_visible = new Visibility([ + 'number' => 'na987', + 'talk1' => VisibilityTalk::BLABLA, + 'talk2' => VisibilityTalk::YADDA, + ]); + + $made_visible->makeVisible('talk1'); + + $array = $made_visible->attributesToArray(); + + $this->assertArrayHasKey('number', $array); + + $this->assertArrayHasKey('talk1', $array); + $this->assertIsString($array['talk1']); + $this->assertEquals($array['talk1'], VisibilityTalk::BLABLA); + + $this->assertArrayHasKey('talk2', $array); + $this->assertIsString($array['talk2']); + $this->assertEquals($array['talk2'], VisibilityTalk::YADDA); + } } diff --git a/tests/Models/Visibility.php b/tests/Models/Visibility.php new file mode 100644 index 0000000..7ecdfd6 --- /dev/null +++ b/tests/Models/Visibility.php @@ -0,0 +1,38 @@ + 'boolean' + ]; + + protected $enums = [ + 'talk1' => VisibilityTalk::class, + 'talk2' => VisibilityTalk::class, + ]; + + protected $hidden = [ + 'talk1', + ]; +} diff --git a/tests/Models/VisibilityTalk.php b/tests/Models/VisibilityTalk.php new file mode 100644 index 0000000..2c81f0b --- /dev/null +++ b/tests/Models/VisibilityTalk.php @@ -0,0 +1,25 @@ +