-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for resolving enum class from 'ClassName@method' notation
- Loading branch information
1 parent
0fb9d7c
commit f60ecb7
Showing
16 changed files
with
259 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
preset: psr2 | ||
|
||
enabled: | ||
- align_phpdoc | ||
- align_double_arrow | ||
- align_equals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,4 @@ public function php_version_satisfies_requirements() | |
. PHP_VERSION . ' found.'); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,4 +95,4 @@ public function it_doesnt_break_related_properties() | |
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,4 +79,4 @@ public function it_doesnt_accept_scalars_that_arent_valid_enum_values() | |
$order->status = 'wtf'; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,4 @@ class Client extends Model | |
{ | ||
protected $guarded = ['id']; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,4 @@ public function client() | |
return $this->belongsTo(Client::class); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,4 @@ class OrderStatus extends Enum | |
const SHIPPING = 'shipping'; | ||
const COMPLETED = 'completed'; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters