-
-
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 #681 from hydephp/add-publication-fields-class
Add publication fields class
- Loading branch information
Showing
7 changed files
with
186 additions
and
6 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
53 changes: 53 additions & 0 deletions
53
packages/framework/src/Framework/Features/Publications/Models/PublicationField.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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Features\Publications\Models; | ||
|
||
use Hyde\Support\Concerns\JsonSerializesArrayable; | ||
use Illuminate\Contracts\Support\Arrayable; | ||
use InvalidArgumentException; | ||
use JsonSerializable; | ||
use function strtolower; | ||
|
||
/** | ||
* @see \Hyde\Framework\Testing\Feature\PublicationFieldTest | ||
*/ | ||
class PublicationField implements JsonSerializable, Arrayable | ||
{ | ||
use JsonSerializesArrayable; | ||
|
||
public final const TYPES = ['string', 'boolean', 'integer', 'float', 'datetime', 'url', 'array', 'text', 'image']; | ||
|
||
public readonly string $type; | ||
|
||
public function __construct(string $type, public readonly string $name, public readonly ?int $min, public readonly ?int $max) | ||
{ | ||
$this->type = strtolower($type); | ||
|
||
if (! in_array(strtolower($type), self::TYPES)) { | ||
throw new InvalidArgumentException(sprintf("The type '$type' is not a valid type. Valid types are: %s.", implode(', ', self::TYPES))); | ||
} | ||
|
||
if (($min !== null) && ($max !== null) && $max < $min) { | ||
throw new InvalidArgumentException("The 'max' value cannot be less than the 'min' value."); | ||
} | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'type' => $this->type, | ||
'name' => $this->name, | ||
'min' => $this->min, | ||
'max' => $this->max, | ||
]; | ||
} | ||
|
||
public function validateInputAgainstRules(string $input): bool | ||
{ | ||
// TODO: Implement this method. | ||
|
||
return true; | ||
} | ||
} |
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
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,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Testing\Feature; | ||
|
||
use Hyde\Framework\Features\Publications\Models\PublicationField; | ||
use Hyde\Testing\TestCase; | ||
use InvalidArgumentException; | ||
|
||
/** | ||
* @covers \Hyde\Framework\Features\Publications\Models\PublicationField | ||
*/ | ||
class PublicationFieldTest extends TestCase | ||
{ | ||
public function test_can_instantiate_class() | ||
{ | ||
$field = $this->makeField(); | ||
$this->assertInstanceOf(PublicationField::class, $field); | ||
|
||
$this->assertSame('string', $field->type); | ||
$this->assertSame('test', $field->name); | ||
$this->assertSame(1, $field->min); | ||
$this->assertSame(10, $field->max); | ||
} | ||
|
||
public function test_can_get_field_as_array() | ||
{ | ||
$this->assertSame([ | ||
'type' => 'string', | ||
'name' => 'test', | ||
'min' => 1, | ||
'max' => 10, | ||
], $this->makeField()->toArray()); | ||
} | ||
|
||
public function test_can_encode_field_as_json() | ||
{ | ||
$this->assertSame('{"type":"string","name":"test","min":1,"max":10}', json_encode($this->makeField())); | ||
} | ||
|
||
public function test_range_values_can_be_null() | ||
{ | ||
$field = new PublicationField('string', 'test', null, null); | ||
$this->assertNull($field->min); | ||
$this->assertNull($field->max); | ||
} | ||
|
||
public function test_max_value_cannot_be_less_than_min_value() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage("The 'max' value cannot be less than the 'min' value."); | ||
|
||
new PublicationField('string', 'test', 10, 1); | ||
} | ||
|
||
public function test_types_constant() | ||
{ | ||
$this->assertSame([ | ||
'string', | ||
'boolean', | ||
'integer', | ||
'float', | ||
'datetime', | ||
'url', | ||
'array', | ||
'text', | ||
'image', | ||
], PublicationField::TYPES); | ||
} | ||
|
||
public function test_type_must_be_valid() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage("The type 'invalid' is not a valid type. Valid types are: string, boolean, integer, float, datetime, url, array, text, image."); | ||
|
||
new PublicationField('invalid', 'test', 1, 10); | ||
} | ||
|
||
public function test_type_input_is_case_insensitive() | ||
{ | ||
$field = new PublicationField('STRING', 'test', 1, 10); | ||
$this->assertSame('string', $field->type); | ||
} | ||
|
||
public function test_validate_input_against_rules() | ||
{ | ||
$this->markTestIncomplete('TODO: Implement this method.'); | ||
} | ||
|
||
protected function makeField(): PublicationField | ||
{ | ||
return new PublicationField('string', 'test', 1, 10); | ||
} | ||
} |
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