Skip to content

Commit

Permalink
Added support for resolving enum class from 'ClassName@method' notation
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Oct 16, 2017
1 parent 0fb9d7c commit f60ecb7
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .styleci.yml
@@ -0,0 +1,6 @@
preset: psr2

enabled:
- align_phpdoc
- align_double_arrow
- align_equals
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Attila Fulop

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 32 additions & 3 deletions src/CastsEnums.php
Expand Up @@ -24,7 +24,7 @@ trait CastsEnums
public function getAttributeValue($key)
{
if ($this->isEnumAttribute($key)) {
$class = $this->enums[$key];
$class = $this->getEnumClass($key);

return $class::create($this->getAttributeFromArray($key));
}
Expand Down Expand Up @@ -57,7 +57,7 @@ public function getAttribute($key)
public function setAttribute($key, $value)
{
if ($this->isEnumAttribute($key)) {
$enumClass = $this->enums[$key];
$enumClass = $this->getEnumClass($key);
if (! $value instanceof $enumClass) {
$value = new $enumClass($value);
}
Expand All @@ -82,4 +82,33 @@ private function isEnumAttribute($key)
return isset($this->enums[$key]);
}

}
/**
* Returns the enum class. Supports 'FQCN\Class@method()' notation
*
* @param $key
*
* @return mixed
*/
private function getEnumClass($key)
{
$result = $this->enums[$key];
if (strpos($result, '@')) {
$class = str_before($result, '@');
$method = str_after($result, '@');

// If no namespace was set, prepend the Model's namespace to the
// class that resolves the enum class. Prevent this behavior,
// by setting the resolver class with a leading backslash
if (class_basename($class) == $class) {
$class =
str_before(get_class($this), class_basename(get_class($this))) . // namespace of the model
$class;
}

$result = $class::$method();
}

return $result;
}

}
2 changes: 1 addition & 1 deletion tests/AAASmokeTest.php
Expand Up @@ -38,4 +38,4 @@ public function php_version_satisfies_requirements()
. PHP_VERSION . ' found.');
}

}
}
48 changes: 48 additions & 0 deletions tests/DynamicClassResolverTest.php
@@ -0,0 +1,48 @@
<?php
/**
* Contains the DynamicClassResolverTest class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
* @license MIT
* @since 2017-10-16
*
*/


namespace Konekt\Enum\Eloquent\Tests;


use Konekt\Enum\Eloquent\Tests\Models\Address;
use Konekt\Enum\Eloquent\Tests\Models\AddressStatus;
use Konekt\Enum\Eloquent\Tests\Models\AddressType;

class DynamicClassResolverTest extends TestCase
{
/**
* @test
*/
public function it_resolves_fqcn_enum_class_name_from_the_at_notation()
{
$address = Address::create([
'type' => AddressType::SHIPPING,
'address' => 'Richard Avenue 33'
]);

$this->assertInstanceOf(AddressType::class, $address->type);
}

/**
* @test
*/
public function it_resolves_local_enum_class_name_from_the_at_notation()
{
$address = Address::create([
'type' => AddressType::SHIPPING,
'address' => 'Richard Avenue 33'
]);

$this->assertInstanceOf(AddressStatus::class, $address->status);
}

}
2 changes: 1 addition & 1 deletion tests/EnumAccessorTest.php
Expand Up @@ -95,4 +95,4 @@ public function it_doesnt_break_related_properties()

}

}
}
2 changes: 1 addition & 1 deletion tests/EnumMutatorTest.php
Expand Up @@ -79,4 +79,4 @@ public function it_doesnt_accept_scalars_that_arent_valid_enum_values()
$order->status = 'wtf';
}

}
}
30 changes: 30 additions & 0 deletions tests/Models/Address.php
@@ -0,0 +1,30 @@
<?php
/**
* Contains the Address class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
* @license MIT
* @since 2017-10-16
*
*/


namespace Konekt\Enum\Eloquent\Tests\Models;


use Illuminate\Database\Eloquent\Model;
use Konekt\Enum\Eloquent\CastsEnums;

class Address extends Model
{
use CastsEnums;

protected $guarded = ['id'];

protected $enums = [
'type' => 'Konekt\\Enum\\Eloquent\\Tests\\Resolvers\\AddressTypeResolver@enumClass',
'status' => 'AddressStatusResolver@enumClass'
];

}
25 changes: 25 additions & 0 deletions tests/Models/AddressStatus.php
@@ -0,0 +1,25 @@
<?php
/**
* Contains the AddressStatus class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
* @license MIT
* @since 2017-10-16
*
*/


namespace Konekt\Enum\Eloquent\Tests\Models;


use Konekt\Enum\Enum;

class AddressStatus extends Enum
{
const __default = self::UNKNOWN;

const UNKNOWN = null;
const VALID = 'valid';
const INVALID = 'invalid';
}
28 changes: 28 additions & 0 deletions tests/Models/AddressStatusResolver.php
@@ -0,0 +1,28 @@
<?php
/**
* Contains the AddressStatusResolver class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
* @license MIT
* @since 2017-10-16
*
*/


namespace Konekt\Enum\Eloquent\Tests\Models;


class AddressStatusResolver
{
/**
* Returns the enum class to use as address type enum
*
* @return string
*/
public static function enumClass()
{
return AddressStatus::class;
}

}
22 changes: 22 additions & 0 deletions tests/Models/AddressType.php
@@ -0,0 +1,22 @@
<?php
/**
* Contains the AddressType class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
* @license MIT
* @since 2017-10-16
*
*/


namespace Konekt\Enum\Eloquent\Tests\Models;


use Konekt\Enum\Enum;

class AddressType extends Enum
{
const BILLING = 'billing';
const SHIPPING = 'shipping';
}
2 changes: 1 addition & 1 deletion tests/Models/Client.php
Expand Up @@ -19,4 +19,4 @@ class Client extends Model
{
protected $guarded = ['id'];

}
}
2 changes: 1 addition & 1 deletion tests/Models/Order.php
Expand Up @@ -38,4 +38,4 @@ public function client()
return $this->belongsTo(Client::class);
}

}
}
2 changes: 1 addition & 1 deletion tests/Models/OrderStatus.php
Expand Up @@ -24,4 +24,4 @@ class OrderStatus extends Enum
const SHIPPING = 'shipping';
const COMPLETED = 'completed';

}
}
30 changes: 30 additions & 0 deletions tests/Resolvers/AddressTypeResolver.php
@@ -0,0 +1,30 @@
<?php
/**
* Contains the AddressTypeResolver class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
* @license MIT
* @since 2017-10-16
*
*/


namespace Konekt\Enum\Eloquent\Tests\Resolvers;


use Konekt\Enum\Eloquent\Tests\Models\AddressType;

class AddressTypeResolver
{
/**
* Returns the enum class to use as address type enum
*
* @return string
*/
public static function enumClass()
{
return AddressType::class;
}

}
12 changes: 11 additions & 1 deletion tests/TestCase.php
Expand Up @@ -41,6 +41,8 @@ protected function setUpDatabase()
$this->capsule->bootEloquent();

$this->capsule->schema()->dropIfExists('orders');
$this->capsule->schema()->dropIfExists('clients');
$this->capsule->schema()->dropIfExists('addresses');

$this->capsule->schema()->create('orders', function (Blueprint $table) {
$table->increments('id');
Expand All @@ -57,7 +59,15 @@ protected function setUpDatabase()
$table->timestamps();
});

$this->capsule->schema()->create('addresses', function (Blueprint $table) {
$table->increments('id');
$table->string('type');
$table->string('status')->nullable();
$table->string('address')->nullable();
$table->timestamps();
});

}


}
}

0 comments on commit f60ecb7

Please sign in to comment.