Skip to content

Commit 2a02699

Browse files
authored
Merge pull request #681 from hydephp/add-publication-fields-class
Add publication fields class
2 parents 4fe2bb8 + 4296a74 commit 2a02699

File tree

7 files changed

+186
-6
lines changed

7 files changed

+186
-6
lines changed

packages/framework/src/Console/Commands/MakePublicationTypeCommand.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ private function captureFieldsDefinitions(): Collection
120120
$this->line(' 7 - Array');
121121
$this->line(' 8 - Text');
122122
$this->line(' 9 - Local Image');
123-
$type = (int) PublicationHelper::askWithValidation($this, 'type', 'Field type (1-7)', ['required', 'integer', 'between:1,9'], 1);
123+
$type = (int) PublicationHelper::askWithValidation($this, 'type', 'Field type (1-9)', ['required', 'integer', 'between:1,9'], 1);
124124
do {
125+
// TODO This should only be done for types that can have length restrictions right?
125126
$field->min = PublicationHelper::askWithValidation($this, 'min', 'Min value (for strings, this refers to string length)', ['required', 'string'], 0);
126127
$field->max = PublicationHelper::askWithValidation($this, 'max', 'Max value (for strings, this refers to string length)', ['required', 'string'], 0);
127128
$lengthsValid = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\Framework\Features\Publications\Models;
6+
7+
use Hyde\Support\Concerns\JsonSerializesArrayable;
8+
use Illuminate\Contracts\Support\Arrayable;
9+
use InvalidArgumentException;
10+
use JsonSerializable;
11+
use function strtolower;
12+
13+
/**
14+
* @see \Hyde\Framework\Testing\Feature\PublicationFieldTest
15+
*/
16+
class PublicationField implements JsonSerializable, Arrayable
17+
{
18+
use JsonSerializesArrayable;
19+
20+
public final const TYPES = ['string', 'boolean', 'integer', 'float', 'datetime', 'url', 'array', 'text', 'image'];
21+
22+
public readonly string $type;
23+
24+
public function __construct(string $type, public readonly string $name, public readonly ?int $min, public readonly ?int $max)
25+
{
26+
$this->type = strtolower($type);
27+
28+
if (! in_array(strtolower($type), self::TYPES)) {
29+
throw new InvalidArgumentException(sprintf("The type '$type' is not a valid type. Valid types are: %s.", implode(', ', self::TYPES)));
30+
}
31+
32+
if (($min !== null) && ($max !== null) && $max < $min) {
33+
throw new InvalidArgumentException("The 'max' value cannot be less than the 'min' value.");
34+
}
35+
}
36+
37+
public function toArray(): array
38+
{
39+
return [
40+
'type' => $this->type,
41+
'name' => $this->name,
42+
'min' => $this->min,
43+
'max' => $this->max,
44+
];
45+
}
46+
47+
public function validateInputAgainstRules(string $input): bool
48+
{
49+
// TODO: Implement this method.
50+
51+
return true;
52+
}
53+
}

packages/framework/src/Framework/Features/Publications/Models/PublicationType.php

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Hyde\Support\Concerns\JsonSerializesArrayable;
1313
use Illuminate\Contracts\Support\Arrayable;
1414
use Illuminate\Contracts\Support\Jsonable;
15+
use Illuminate\Support\Collection;
1516
use Illuminate\Support\Str;
1617
use function json_decode;
1718
use JsonSerializable;
@@ -106,6 +107,14 @@ public function getDirectory(): string
106107
return $this->directory;
107108
}
108109

110+
/** @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Publications\Models\PublicationField */
111+
public function getFields(): Collection
112+
{
113+
return collect($this->fields)->map(function (array $data) {
114+
return new PublicationField(...$data);
115+
});
116+
}
117+
109118
public function save(?string $path = null): void
110119
{
111120
$path ??= $this->getSchemaFile();

packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function test_command_creates_publication()
1818
$this->artisan('make:publicationType')
1919
->expectsQuestion('Publication type name', 'Test Publication')
2020
->expectsQuestion('Field name', 'Title')
21-
->expectsQuestion('Field type (1-7)', 1)
21+
->expectsQuestion('Field type (1-9)', 1)
2222
->expectsQuestion('Min value (for strings, this refers to string length)', 'default')
2323
->expectsQuestion('Max value (for strings, this refers to string length)', 'default')
2424
->expectsQuestion('Add another field (y/n)', 'n')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\Framework\Testing\Feature;
6+
7+
use Hyde\Framework\Features\Publications\Models\PublicationField;
8+
use Hyde\Testing\TestCase;
9+
use InvalidArgumentException;
10+
11+
/**
12+
* @covers \Hyde\Framework\Features\Publications\Models\PublicationField
13+
*/
14+
class PublicationFieldTest extends TestCase
15+
{
16+
public function test_can_instantiate_class()
17+
{
18+
$field = $this->makeField();
19+
$this->assertInstanceOf(PublicationField::class, $field);
20+
21+
$this->assertSame('string', $field->type);
22+
$this->assertSame('test', $field->name);
23+
$this->assertSame(1, $field->min);
24+
$this->assertSame(10, $field->max);
25+
}
26+
27+
public function test_can_get_field_as_array()
28+
{
29+
$this->assertSame([
30+
'type' => 'string',
31+
'name' => 'test',
32+
'min' => 1,
33+
'max' => 10,
34+
], $this->makeField()->toArray());
35+
}
36+
37+
public function test_can_encode_field_as_json()
38+
{
39+
$this->assertSame('{"type":"string","name":"test","min":1,"max":10}', json_encode($this->makeField()));
40+
}
41+
42+
public function test_range_values_can_be_null()
43+
{
44+
$field = new PublicationField('string', 'test', null, null);
45+
$this->assertNull($field->min);
46+
$this->assertNull($field->max);
47+
}
48+
49+
public function test_max_value_cannot_be_less_than_min_value()
50+
{
51+
$this->expectException(InvalidArgumentException::class);
52+
$this->expectExceptionMessage("The 'max' value cannot be less than the 'min' value.");
53+
54+
new PublicationField('string', 'test', 10, 1);
55+
}
56+
57+
public function test_types_constant()
58+
{
59+
$this->assertSame([
60+
'string',
61+
'boolean',
62+
'integer',
63+
'float',
64+
'datetime',
65+
'url',
66+
'array',
67+
'text',
68+
'image',
69+
], PublicationField::TYPES);
70+
}
71+
72+
public function test_type_must_be_valid()
73+
{
74+
$this->expectException(InvalidArgumentException::class);
75+
$this->expectExceptionMessage("The type 'invalid' is not a valid type. Valid types are: string, boolean, integer, float, datetime, url, array, text, image.");
76+
77+
new PublicationField('invalid', 'test', 1, 10);
78+
}
79+
80+
public function test_type_input_is_case_insensitive()
81+
{
82+
$field = new PublicationField('STRING', 'test', 1, 10);
83+
$this->assertSame('string', $field->type);
84+
}
85+
86+
public function test_validate_input_against_rules()
87+
{
88+
$this->markTestIncomplete('TODO: Implement this method.');
89+
}
90+
91+
protected function makeField(): PublicationField
92+
{
93+
return new PublicationField('string', 'test', 1, 10);
94+
}
95+
}

packages/framework/tests/Feature/PublicationTypeTest.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
namespace Hyde\Framework\Testing\Feature;
66

77
use function array_merge;
8+
use Hyde\Framework\Features\Publications\Models\PublicationField;
89
use Hyde\Framework\Features\Publications\Models\PublicationType;
910
use Hyde\Hyde;
1011
use Hyde\Testing\TestCase;
12+
use Illuminate\Support\Collection;
1113
use Illuminate\Support\Facades\File;
1214

1315
/**
@@ -91,6 +93,16 @@ public function test_can_load_from_json_file()
9193
$this->assertEquals($publicationType, PublicationType::fromFile(Hyde::path('tests/fixtures/test-publication-schema.json')));
9294
}
9395

96+
public function test_get_fields_method_returns_collection_of_field_objects()
97+
{
98+
$publicationType = new PublicationType(...$this->getTestDataWithPathInformation());
99+
$collection = $publicationType->getFields();
100+
$this->assertCount(1, $collection);
101+
$this->assertInstanceOf(Collection::class, $collection);
102+
$this->assertInstanceOf(PublicationField::class, $collection->first());
103+
$this->assertEquals(new Collection([new PublicationField('string', 'test', 0, 128)]), $collection);
104+
}
105+
94106
protected function getTestData(): array
95107
{
96108
return [
@@ -103,7 +115,12 @@ protected function getTestData(): array
103115
'detailTemplate' => 'detail',
104116
'listTemplate' => 'list',
105117
'fields' => [
106-
'foo' => 'bar',
118+
[
119+
'type' => 'string',
120+
'name' => 'test',
121+
'min' => 0,
122+
'max' => 128,
123+
],
107124
],
108125
];
109126
}

tests/fixtures/test-publication-schema.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
"prevNextLinks": true,
88
"detailTemplate": "detail",
99
"listTemplate": "list",
10-
"fields": {
11-
"foo": "bar"
12-
}
10+
"fields": [
11+
{
12+
"type": "string",
13+
"name": "test",
14+
"min": "0",
15+
"max": "128"
16+
}
17+
]
1318
}

0 commit comments

Comments
 (0)