diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 5e302bb576d..589a0efeaf8 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -120,8 +120,9 @@ private function captureFieldsDefinitions(): Collection $this->line(' 7 - Array'); $this->line(' 8 - Text'); $this->line(' 9 - Local Image'); - $type = (int) PublicationHelper::askWithValidation($this, 'type', 'Field type (1-7)', ['required', 'integer', 'between:1,9'], 1); + $type = (int) PublicationHelper::askWithValidation($this, 'type', 'Field type (1-9)', ['required', 'integer', 'between:1,9'], 1); do { + // TODO This should only be done for types that can have length restrictions right? $field->min = PublicationHelper::askWithValidation($this, 'min', 'Min value (for strings, this refers to string length)', ['required', 'string'], 0); $field->max = PublicationHelper::askWithValidation($this, 'max', 'Max value (for strings, this refers to string length)', ['required', 'string'], 0); $lengthsValid = true; diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php new file mode 100644 index 00000000000..fda5374ff30 --- /dev/null +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -0,0 +1,53 @@ +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; + } +} diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php index 2edabc268bc..c4b5d73aba3 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php @@ -12,6 +12,7 @@ use Hyde\Support\Concerns\JsonSerializesArrayable; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; +use Illuminate\Support\Collection; use Illuminate\Support\Str; use function json_decode; use JsonSerializable; @@ -106,6 +107,14 @@ public function getDirectory(): string return $this->directory; } + /** @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Publications\Models\PublicationField */ + public function getFields(): Collection + { + return collect($this->fields)->map(function (array $data) { + return new PublicationField(...$data); + }); + } + public function save(?string $path = null): void { $path ??= $this->getSchemaFile(); diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 79b7ad64464..5ed98141a90 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -18,7 +18,7 @@ public function test_command_creates_publication() $this->artisan('make:publicationType') ->expectsQuestion('Publication type name', 'Test Publication') ->expectsQuestion('Field name', 'Title') - ->expectsQuestion('Field type (1-7)', 1) + ->expectsQuestion('Field type (1-9)', 1) ->expectsQuestion('Min value (for strings, this refers to string length)', 'default') ->expectsQuestion('Max value (for strings, this refers to string length)', 'default') ->expectsQuestion('Add another field (y/n)', 'n') diff --git a/packages/framework/tests/Feature/PublicationFieldTest.php b/packages/framework/tests/Feature/PublicationFieldTest.php new file mode 100644 index 00000000000..5c1db57da4e --- /dev/null +++ b/packages/framework/tests/Feature/PublicationFieldTest.php @@ -0,0 +1,95 @@ +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); + } +} diff --git a/packages/framework/tests/Feature/PublicationTypeTest.php b/packages/framework/tests/Feature/PublicationTypeTest.php index eeb80597b8b..8f66af0f4b0 100644 --- a/packages/framework/tests/Feature/PublicationTypeTest.php +++ b/packages/framework/tests/Feature/PublicationTypeTest.php @@ -5,9 +5,11 @@ namespace Hyde\Framework\Testing\Feature; use function array_merge; +use Hyde\Framework\Features\Publications\Models\PublicationField; use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Hyde; use Hyde\Testing\TestCase; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; /** @@ -91,6 +93,16 @@ public function test_can_load_from_json_file() $this->assertEquals($publicationType, PublicationType::fromFile(Hyde::path('tests/fixtures/test-publication-schema.json'))); } + public function test_get_fields_method_returns_collection_of_field_objects() + { + $publicationType = new PublicationType(...$this->getTestDataWithPathInformation()); + $collection = $publicationType->getFields(); + $this->assertCount(1, $collection); + $this->assertInstanceOf(Collection::class, $collection); + $this->assertInstanceOf(PublicationField::class, $collection->first()); + $this->assertEquals(new Collection([new PublicationField('string', 'test', 0, 128)]), $collection); + } + protected function getTestData(): array { return [ @@ -103,7 +115,12 @@ protected function getTestData(): array 'detailTemplate' => 'detail', 'listTemplate' => 'list', 'fields' => [ - 'foo' => 'bar', + [ + 'type' => 'string', + 'name' => 'test', + 'min' => 0, + 'max' => 128, + ], ], ]; } diff --git a/tests/fixtures/test-publication-schema.json b/tests/fixtures/test-publication-schema.json index 4ed5899266e..6e75544d127 100644 --- a/tests/fixtures/test-publication-schema.json +++ b/tests/fixtures/test-publication-schema.json @@ -7,7 +7,12 @@ "prevNextLinks": true, "detailTemplate": "detail", "listTemplate": "list", - "fields": { - "foo": "bar" - } + "fields": [ + { + "type": "string", + "name": "test", + "min": "0", + "max": "128" + } + ] } \ No newline at end of file