Skip to content

Remove range values from publications to reduce complexity #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions packages/framework/src/Console/Commands/MakePublicationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,6 @@ protected function generateFieldRules(PublicationField $field): Collection
$fieldRules = Collection::make($field->type->rules());
if ($fieldRules->contains('between')) {
$fieldRules->forget($fieldRules->search('between'));
if ($field->min && $field->max) {
switch ($field->type) {
case 'string':
case 'integer':
case 'float':
$fieldRules->add("between:$field->min,$field->max");
break;
case 'datetime':
$fieldRules->add("after:$field->min");
$fieldRules->add("before:$field->max");
break;
}
}
}

return $fieldRules;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ protected function captureFieldsDefinitions(): Collection
$type = $this->getFieldType();

if ($type < 10) {
do {
$fieldData['min'] = trim($this->askWithValidation('min', 'Min value (see Documentation)', ['required', 'string'], 0));
$fieldData['max'] = trim($this->askWithValidation('max', 'Max value (see Documentation)', ['required', 'string'], 0));
$lengthsValid = $this->validateLengths($fieldData['min'], $fieldData['max']);
} while (! $lengthsValid);
} else {
$fieldData = $this->getFieldDataForTag($fieldData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function safeHandle(): int
foreach ($publication->type->fields as $field) {
$countFields++;
$fieldName = $field['name'];
$pubTypeField = new PublicationField($field['type'], $fieldName, $field['min'], $field['max'], $field['tagGroup'] ?? null, $pubType);
$pubTypeField = new PublicationField($field['type'], $fieldName, $field['tagGroup'] ?? null, $pubType);

try {
if ($verbose) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
use Hyde\Framework\Features\Publications\PublicationService;
use Hyde\Support\Concerns\Serializable;
use Hyde\Support\Contracts\SerializableContract;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use InvalidArgumentException;
use JetBrains\PhpStorm\Deprecated;
use Rgasch\Collection\Collection;
use function strtolower;

Expand All @@ -27,10 +24,6 @@ class PublicationField implements SerializableContract
use Serializable;

public readonly PublicationFieldTypes $type;
/** @deprecated https://github.com/hydephp/develop/pull/685#issuecomment-1361565809 */
public readonly string $max;
/** @deprecated https://github.com/hydephp/develop/pull/685#issuecomment-1361565809 */
public readonly string $min;
public readonly string $name;
public readonly ?string $tagGroup;
public readonly ?PublicationType $publicationType; // Only used for validation command, interactive command doesn't need this
Expand All @@ -40,27 +33,19 @@ public static function fromArray(array $array): static
return new static(...$array);
}

public function __construct(PublicationFieldTypes|string $type, string $name, #[Deprecated] int | string | null $min = '0', #[Deprecated] int | string | null $max = '0', ?string $tagGroup = null, PublicationType $publicationType = null)
public function __construct(PublicationFieldTypes|string $type, string $name, ?string $tagGroup = null, PublicationType $publicationType = null)
{
$this->type = $type instanceof PublicationFieldTypes ? $type : PublicationFieldTypes::from(strtolower($type));
$this->name = Str::kebab($name);
$this->min = (string) $min;
$this->max = (string) $max;
$this->tagGroup = $tagGroup;
$this->publicationType = $publicationType;

if ($max < $min && $max !== '0') {
throw new InvalidArgumentException("The 'max' value cannot be less than the 'min' value.");
}
}

public function toArray(): array
{
return [
'type' => $this->type->value,
'name' => $this->name,
'min' => $this->min,
'max' => $this->max,
'tagGroup' => $this->tagGroup,
];
}
Expand All @@ -74,41 +59,20 @@ public function getValidationRules(bool $reload = true): Collection
$defaultRules = Collection::create(PublicationFieldTypes::values());
$fieldRules = Collection::create($defaultRules->get($this->type->value));

$useRange = true;
// The trim command used to process the min/max input results in a string, so
// we need to test both int and string values to determine required status.
if (($this->min && ! $this->max) || ($this->min == '0' && $this->max == '0')) {
$fieldRules->forget($fieldRules->search('required'));
$useRange = false;
}

switch ($this->type->value) {
case 'array':
$fieldRules->add('array');
break;
case 'datetime':
$fieldRules->add('date');
if ($this->min) {
$dateMin = Carbon::parse($this->min);
$fieldRules->add("after:$dateMin");
}
if ($this->max) {
$dateMax = Carbon::parse($this->max);
$fieldRules->add("before:$dateMax");
}

break;
case 'float':
$fieldRules->add('numeric');
if ($useRange) {
$fieldRules->add("between:$this->min,$this->max");
}
break;
case 'integer':
case 'string':
case 'text':
if ($useRange) {
$fieldRules->add("between:$this->min,$this->max");
}
break;
case 'image':
$mediaFiles = PublicationService::getMediaForPubType($this->publicationType, $reload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function getFieldRules(bool $reload = false): Collection
public function getCanonicalFieldDefinition(): PublicationField
{
if (str_starts_with($this->canonicalField, '__')) {
return new PublicationField('string', $this->canonicalField, 0, 0);
return new PublicationField('string', $this->canonicalField);
}

return $this->getFields()->filter(fn (PublicationField $field): bool => $field->name === $this->canonicalField)->first();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,9 @@ public function testWithTextType()
$pubType = $this->makePublicationType([[
'type' => 'string',
'name' => 'title',
'min' => 0,
'max' => 128,
], [
'type' => 'text',
'name' => 'description',
'min' => 0,
'max' => 128,
]]);

$fieldData = Collection::make([
Expand Down Expand Up @@ -99,13 +95,9 @@ public function testWithArrayType()
$pubType = $this->makePublicationType([[
'type' => 'string',
'name' => 'title',
'min' => 0,
'max' => 128,
], [
'type' => 'array',
'name' => 'tags',
'min' => 0,
'max' => 128,
]]);

$fieldData = Collection::make([
Expand Down Expand Up @@ -146,13 +138,9 @@ public function testCreateWithoutSupplyingRequiredField()
$pubType = $this->makePublicationType([[
'type' => 'string',
'name' => 'title',
'min' => 0,
'max' => 128,
], [
'type' => 'string',
'name' => 'slug',
'min' => 0,
'max' => 128,
]]);

$fieldData = Collection::make([
Expand Down Expand Up @@ -181,19 +169,13 @@ public function testItCreatesValidYaml()
$pubType = $this->makePublicationType([[
'type' => 'string',
'name' => 'title',
'min' => 0,
'max' => 128,
], [
'type' => 'text',
'name' => 'description',
'min' => 0,
'max' => 128,
], [

'type' => 'array',
'name' => 'tags',
'min' => 0,
'max' => 128,
]]);

$fieldData = Collection::make([
Expand Down Expand Up @@ -242,8 +224,6 @@ protected function makePublicationType(array $fields = [
[
'type' => 'string',
'name' => 'title',
'min' => 0,
'max' => 128,
],
]): PublicationType
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ public function test_command_with_text_input()
'fields' => [[
'type' => 'text',
'name' => 'description',
'min' => '0',
'max' => '0',
],
],
]);
Expand Down Expand Up @@ -196,8 +194,6 @@ public function test_command_with_array_input()
'fields' => [[
'type' => 'array',
'name' => 'tags',
'min' => '0',
'max' => '0',
],
],
]);
Expand Down Expand Up @@ -237,8 +233,6 @@ protected function makeSchemaFile(array $merge = []): void
'fields' => [
[
'name' => 'title',
'min' => '0',
'max' => '0',
'type' => 'string',
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public function test_command_creates_publication_type()
9 => 'Local Image',
10 => 'Tag (select value from list)',
])
->expectsQuestion('Min value (see Documentation)', '0')
->expectsQuestion('Max value (see Documentation)', '0')
->expectsQuestion('<bg=magenta;fg=white>Add another field (y/n)</>', 'n')
->expectsChoice('Choose the default field you wish to sort by', 'dateCreated (meta field)', [
'dateCreated (meta field)',
Expand Down Expand Up @@ -78,8 +76,6 @@ public function test_command_creates_publication_type()
{
"type": "string",
"name": "publication-title",
"min": "0",
"max": "0",
"tagGroup": null
}
]
Expand All @@ -92,26 +88,4 @@ public function test_command_creates_publication_type()

Filesystem::deleteDirectory('test-publication');
}

public function test_cannot_create_field_with_lower_max_than_min_value()
{
$this->artisan('make:publicationType test-publication')
->expectsQuestion('Field name', 'foo')
->expectsQuestion('Field type', 'foo')
->expectsQuestion('Min value (see Documentation)', 10)
->expectsQuestion('Max value (see Documentation)', 5)
->expectsQuestion('Min value (see Documentation)', 5)
->expectsQuestion('Max value (see Documentation)', 10)

->expectsQuestion('<bg=magenta;fg=white>Add another field (y/n)</>', 'n')
->expectsQuestion('Choose the default field you wish to sort by', 'foo')
->expectsQuestion('Choose the default sort direction', 'Ascending (oldest items first if sorting by dateCreated)')
->expectsQuestion('Enter the pageSize (0 for no limit)', 10)
->expectsQuestion('Generate previous/next links in detail view (y/n)', 'n')
->expectsQuestion('Choose a canonical name field (the values of this field have to be unique!)', 'foo')
->expectsOutputToContain('Creating a new Publication Type!')
->assertSuccessful();

Filesystem::deleteDirectory('test-publication');
}
}
Loading