Skip to content

Commit

Permalink
Merge pull request #8 from nickrupert7/master
Browse files Browse the repository at this point in the history
Added support for Eloquent toArray()
  • Loading branch information
fulopattila122 authored Nov 3, 2019
2 parents cec28dc + bfe19d3 commit 297971f
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog
### Konekt Enum Eloquent

## 1.3.2
##### 2019-09-17

- Added support for Eloquent `toArray()`

## 1.3.1
##### 2019-09-08

Expand Down
25 changes: 25 additions & 0 deletions src/CastsEnums.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public function setAttribute($key, $value)
parent::setAttribute($key, $value);
}

/**
* Convert the model's attributes to an array.
*
* @return array
*/
public function attributesToArray()
{
return $this->addEnumAttributesToArray(parent::attributesToArray());
}

/**
* Returns whether the attribute was marked as enum
*
Expand All @@ -80,6 +90,21 @@ protected function isEnumAttribute($key)
return isset($this->enums[$key]);
}

/**
* Add enum values to the attributes array
*
* @param $array
* @return array
*/
protected function addEnumAttributesToArray($array)
{
foreach ($this->enums as $key => $value) {
$array[$key] = $this->getAttributeValue($key)->value();
}

return $array;
}

/**
* Returns the enum class. Supports 'FQCN\Class@method()' notation
*
Expand Down
129 changes: 129 additions & 0 deletions tests/EnumToArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/**
* Contains the EnumAccessorTest 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\OrderV2;
use Konekt\Enum\Eloquent\Tests\Models\OrderStatus;

class EnumToArrayTest extends TestCase
{
/**
* @test
*/
public function returns_enum_string_value()
{
$order = new Order([
'number' => 'abc123',
'status' => OrderStatus::SUBMITTED
]);

$array = $order->attributesToArray();

$this->assertArrayHasKey('status', $array);
$this->assertIsString($array['status']);
}

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

$array = $order->attributesToArray();

$this->assertArrayHasKey('number', $array);
$this->assertEquals($array['number'], $order->number);
}

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

$attributesArray = $order->attributesToArray();
$array = $order->toArray();

$this->assertEquals($array, $attributesArray);
}

/**
* @test
*/
public function returns_enum_default_string_value_when_attribute_is_null()
{
// don't test if major version is lower than 3
if ($this->getEnumVersionMajor() < 3) {
$this->assertTrue(true);

return;
}

$order = new Order([
'number' => 'abc123'
]);

$array = $order->attributesToArray();

$this->assertArrayHasKey('status', $array);
$this->assertIsString($array['status']);
$this->assertEquals($array['status'], OrderStatus::__DEFAULT);
}

/**
* @test
*/
public function returns_enum_v2_default_string_value_when_attribute_is_null()
{
// don't test if major version is 3 or higher
if ($this->getEnumVersionMajor() >= 3) {
$this->assertTrue(true);

return;
}

$order = new OrderV2([
'number' => 'abc123'
]);

$array = $order->attributesToArray();

$this->assertArrayHasKey('status', $array);
$this->assertIsString($array['status']);
$this->assertEquals($array['status'], OrderStatus::__DEFAULT);
}

private function getEnumVersion()
{
$raw_version = \PackageVersions\Versions::getVersion('konekt/enum');

$parts = explode('@', $raw_version);

return $parts[0];
}

private function getEnumVersionMajor()
{
$parts = explode('.', $this->getEnumVersion());

return $parts[0];
}
}

0 comments on commit 297971f

Please sign in to comment.