-
-
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.
Merge pull request #733 from hydephp/extract-publication-type-enum
Create enum for publication types
- Loading branch information
Showing
6 changed files
with
135 additions
and
61 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
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
60 changes: 60 additions & 0 deletions
60
packages/framework/src/Framework/Features/Publications/Concerns/PublicationFieldTypes.php
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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Features\Publications\Concerns; | ||
|
||
use BadMethodCallException; | ||
use Illuminate\Support\Collection; | ||
|
||
/** | ||
* The supported field types for publication types. | ||
* | ||
* @see \Hyde\Framework\Features\Publications\Models\PublicationFieldType | ||
* @see \Hyde\Framework\Testing\Feature\PublicationFieldTypesEnumTest | ||
*/ | ||
enum PublicationFieldTypes: string | ||
{ | ||
case String = 'string'; | ||
case Boolean = 'boolean'; | ||
case Integer = 'integer'; | ||
case Float = 'float'; | ||
case Datetime = 'datetime'; | ||
case Url = 'url'; | ||
case Array = 'array'; | ||
case Text = 'text'; | ||
case Image = 'image'; | ||
case Tag = 'tag'; | ||
|
||
public function rules(): array | ||
{ | ||
return self::getRules($this); | ||
} | ||
|
||
public static function collect(): Collection | ||
{ | ||
return collect(self::cases()); | ||
} | ||
|
||
public static function values(): array | ||
{ | ||
return self::collect()->pluck('value')->toArray(); | ||
} | ||
|
||
public static function getRules(self $type): array | ||
{ | ||
/** @noinspection PhpDuplicateMatchArmBodyInspection */ | ||
return match ($type) { | ||
self::String => ['required', 'string', 'between'], | ||
self::Boolean => ['required', 'boolean'], | ||
self::Integer => ['required', 'integer', 'between'], | ||
self::Float => ['required', 'numeric', 'between'], | ||
self::Datetime => ['required', 'datetime', 'between'], | ||
self::Url => ['required', 'url'], | ||
self::Text => ['required', 'string', 'between'], | ||
self::Array => throw new BadMethodCallException('This type has no validation rules'), | ||
self::Image => throw new BadMethodCallException('This type has no validation rules'), | ||
self::Tag => throw new BadMethodCallException('This type has no validation rules'), | ||
}; | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Testing\Feature; | ||
|
||
use BadMethodCallException; | ||
use Hyde\Framework\Features\Publications\Concerns\PublicationFieldTypes; | ||
use Hyde\Testing\TestCase; | ||
|
||
/** | ||
* @covers \Hyde\Framework\Features\Publications\Concerns\PublicationFieldTypes | ||
*/ | ||
class PublicationFieldTypesEnumTest extends TestCase | ||
{ | ||
public function testCanGetRulesForEnum() | ||
{ | ||
$this->assertSame([ | ||
'required', | ||
'string', | ||
'between', | ||
], PublicationFieldTypes::String->rules()); | ||
} | ||
|
||
public function testCollectCreatesCollectionOfCases() | ||
{ | ||
$this->assertEquals(collect(PublicationFieldTypes::cases()), PublicationFieldTypes::collect()); | ||
} | ||
|
||
public function testValuesReturnsArrayOfCaseValues() | ||
{ | ||
$this->assertSame([ | ||
0 => 'string', | ||
1 => 'boolean', | ||
2 => 'integer', | ||
3 => 'float', | ||
4 => 'datetime', | ||
5 => 'url', | ||
6 => 'array', | ||
7 => 'text', | ||
8 => 'image', | ||
9 => 'tag', | ||
], PublicationFieldTypes::values()); | ||
} | ||
|
||
public function testCanGetRulesForEnumWithNoRules() | ||
{ | ||
$this->expectException(BadMethodCallException::class); | ||
$this->expectExceptionMessage('This type has no validation rules'); | ||
PublicationFieldTypes::Tag->rules(); | ||
} | ||
} |