From 505f17156c1d0f960679319542b5664a12d56aca Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 12:58:21 +0100 Subject: [PATCH 001/130] Use the enum types --- .../src/Console/Commands/MakePublicationTypeCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index a006042690e..09712952b45 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -158,9 +158,9 @@ protected function getCanonicalField(Collection $fields): string { $options = $fields->reject(function (PublicationField $field): bool { // Temporary verbose check to see code coverage - if ($field->type === 'image') { + if ($field->type === PublicationFieldTypes::Image) { return true; - } elseif ($field->type === 'tag') { + } elseif ($field->type === PublicationFieldTypes::Tag) { return true; } else { return false; From f6b805bb5fc5a4a837b598979518dc215d75dc5b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 12:58:56 +0100 Subject: [PATCH 002/130] Remove unused length validation --- .../Console/Commands/MakePublicationTypeCommand.php | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 09712952b45..ace3a7d13ab 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -170,17 +170,6 @@ protected function getCanonicalField(Collection $fields): string return $this->choice('Choose a canonical name field (the values of this field have to be unique!)', $options->toArray(), $options->first()); } - protected function validateLengths(string $min, string $max): bool - { - if ($max < $min) { - $this->error('Field length [max] cannot be less than [min]'); - - return false; - } - - return true; - } - protected function getFieldDataForTag(array $fieldData): array { $allTags = PublicationService::getAllTags(); From 8205f09fdfcc393e758aee85132d914f9d603daf Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 13:03:11 +0100 Subject: [PATCH 003/130] Sort PublicationFieldTypes enum cases --- .../Publications/PublicationFieldTypes.php | 24 +++++++++---------- .../Feature/PublicationFieldTypesEnumTest.php | 16 ++++++------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index 41323c2d6c8..34a8532b70d 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -14,16 +14,16 @@ */ enum PublicationFieldTypes: string { - case String = 'string'; + case Array = 'array'; case Boolean = 'boolean'; - case Integer = 'integer'; - case Float = 'float'; case Datetime = 'datetime'; - case Url = 'url'; - case Array = 'array'; - case Text = 'text'; + case Float = 'float'; case Image = 'image'; + case Integer = 'integer'; + case String = 'string'; case Tag = 'tag'; + case Text = 'text'; + case Url = 'url'; public function rules(): array { @@ -44,16 +44,16 @@ public static function getRules(self $type): array { /** @noinspection PhpDuplicateMatchArmBodyInspection */ return match ($type) { - self::String => ['string'], + self::Array => ['array'], self::Boolean => ['boolean'], - self::Integer => ['integer', 'numeric'], - self::Float => ['numeric'], self::Datetime => ['date'], - self::Url => ['url'], - self::Text => ['string'], - self::Array => ['array'], + self::Float => ['numeric'], self::Image => [], + self::Integer => ['integer', 'numeric'], + self::String => ['string'], self::Tag => [], + self::Text => ['string'], + self::Url => ['url'], }; } } diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index 5c0e2a51908..4cbd68b1d2d 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -50,16 +50,16 @@ public function testCollectCreatesCollectionOfCases() public function testValuesReturnsArrayOfCaseValues() { $this->assertSame([ - 0 => 'string', + 0 => 'array', 1 => 'boolean', - 2 => 'integer', + 2 => 'datetime', 3 => 'float', - 4 => 'datetime', - 5 => 'url', - 6 => 'array', - 7 => 'text', - 8 => 'image', - 9 => 'tag', + 4 => 'image', + 5 => 'integer', + 6 => 'string', + 7 => 'tag', + 8 => 'text', + 9 => 'url', ], PublicationFieldTypes::values()); } } From 422e2b74b377b06844f84b72b1dbc02f3dcef260 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 13:11:39 +0100 Subject: [PATCH 004/130] Add array of the field types that can be canonical --- .../Publications/PublicationFieldTypes.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index 34a8532b70d..e2d49ca0dab 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -56,4 +56,19 @@ public static function getRules(self $type): array self::Url => ['url'], }; } + + /** + * The types that can be used for canonical fields (used to generate file names). + * + * @return \Hyde\Framework\Features\Publications\PublicationFieldTypes[] + */ + public static function canonicable(): array + { + return [ + self::String, + self::Integer, + self::Datetime, + self::Text, + ]; + } } From 64cf0f1d5b70da73935dda742c80d91ea091a872 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 13:15:21 +0100 Subject: [PATCH 005/130] Use a more readable approach for overriding the labels --- .../Commands/MakePublicationTypeCommand.php | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index ace3a7d13ab..51b07510f30 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -104,13 +104,23 @@ protected function captureFieldsDefinitions(): Collection protected function getFieldType(): int { $options = PublicationFieldTypes::cases(); + + $labels = [ + 'Array' => 'Array', + 'Boolean' => 'Boolean', + 'Datetime' => 'Datetime (YYYY-MM-DD (HH:MM:SS))', + 'Float' => 'Float', + 'Image' => 'Local Image', + 'Integer' => 'Integer', + 'String' => 'String', + 'Tag' => 'Tag (select value from list)', + 'Text' => 'Text', + 'Url' => 'URL', + ]; + foreach ($options as $key => $value) { - $options[$key] = $value->name; + $options[$key] = $labels[$value->name]; } - $options[4] = 'Datetime (YYYY-MM-DD (HH:MM:SS))'; - $options[5] = 'URL'; - $options[8] = 'Local Image'; - $options[9] = 'Tag (select value from list)'; return (int) $this->choice('Field type', $options, 1) + 1; } From 44518035a8a247703f3275df7cdbd80bce91b69c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 13:25:21 +0100 Subject: [PATCH 006/130] Remove label overrides to reduce complexity and as cases should already be self-describing --- .../Commands/MakePublicationTypeCommand.php | 19 +------------------ .../MakePublicationTypeCommandTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 51b07510f30..340c28a4bcd 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -103,24 +103,7 @@ protected function captureFieldsDefinitions(): Collection protected function getFieldType(): int { - $options = PublicationFieldTypes::cases(); - - $labels = [ - 'Array' => 'Array', - 'Boolean' => 'Boolean', - 'Datetime' => 'Datetime (YYYY-MM-DD (HH:MM:SS))', - 'Float' => 'Float', - 'Image' => 'Local Image', - 'Integer' => 'Integer', - 'String' => 'String', - 'Tag' => 'Tag (select value from list)', - 'Text' => 'Text', - 'Url' => 'URL', - ]; - - foreach ($options as $key => $value) { - $options[$key] = $labels[$value->name]; - } + $options = PublicationFieldTypes::collect()->pluck('name')->toArray(); return (int) $this->choice('Field type', $options, 1) + 1; } diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 8dca69f48e9..c24815d67d0 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -32,12 +32,12 @@ public function test_command_creates_publication_type() 2 => 'Boolean', 3 => 'Integer', 4 => 'Float', - 5 => 'Datetime (YYYY-MM-DD (HH:MM:SS))', - 6 => 'URL', + 5 => 'Datetime', + 6 => 'Url', 7 => 'Array', 8 => 'Text', - 9 => 'Local Image', - 10 => 'Tag (select value from list)', + 9 => 'Image', + 10 => 'Tag', ]) ->expectsQuestion('Add another field (y/n)', 'n') ->expectsChoice('Choose the default field you wish to sort by', 'dateCreated (meta field)', [ From 493233931c3dc5ad460265159547df217281c768 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 14:13:44 +0100 Subject: [PATCH 007/130] Update helper method to return the enum instance --- .../src/Console/Commands/MakePublicationTypeCommand.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 340c28a4bcd..47da61f4636 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -61,7 +61,7 @@ public function safeHandle(): int $canonicalField = $this->getCanonicalField($fields); - $creator = new CreatesNewPublicationType($title, $fields, $canonicalField, $sortField, $sortAscending, $prevNextLinks, $pageSize, $this->output); + $creator = new CreatesNewPublicationType($title, $fields, $canonicalField->name, $sortField, $sortAscending, $prevNextLinks, $pageSize, $this->output); $creator->create(); $this->info('Publication type created successfully!'); @@ -147,7 +147,7 @@ protected function getPrevNextLinks(): bool ); } - protected function getCanonicalField(Collection $fields): string + protected function getCanonicalField(Collection $fields): PublicationField { $options = $fields->reject(function (PublicationField $field): bool { // Temporary verbose check to see code coverage @@ -160,7 +160,9 @@ protected function getCanonicalField(Collection $fields): string } })->pluck('name'); - return $this->choice('Choose a canonical name field (the values of this field have to be unique!)', $options->toArray(), $options->first()); + return $fields->firstWhere('name', + $this->choice('Choose a canonical name field (the values of this field have to be unique!)', $options->toArray(), $options->first()) + ); } protected function getFieldDataForTag(array $fieldData): array From dadf3cbea2e2f18e5da8bb93070e3a5adc958c38 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 14:15:30 +0100 Subject: [PATCH 008/130] Update expected order --- .../MakePublicationTypeCommandTest.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index c24815d67d0..353cc3b5dfe 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -28,16 +28,16 @@ public function test_command_creates_publication_type() ->expectsQuestion('Publication type name', 'Test Publication') ->expectsQuestion('Field name', 'Publication Title') ->expectsChoice('Field type', 'String', [ - 1 => 'String', - 2 => 'Boolean', - 3 => 'Integer', - 4 => 'Float', - 5 => 'Datetime', - 6 => 'Url', - 7 => 'Array', - 8 => 'Text', - 9 => 'Image', - 10 => 'Tag', + 'Array', + 'Boolean', + 'Datetime', + 'Float', + 'Image', + 'Integer', + 'String', + 'Tag', + 'Text', + 'Url', ]) ->expectsQuestion('Add another field (y/n)', 'n') ->expectsChoice('Choose the default field you wish to sort by', 'dateCreated (meta field)', [ From fc9c3ba369ff821d00cd83074106cfdd42ed281b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 14:18:10 +0100 Subject: [PATCH 009/130] Remove overcounter --- .../src/Console/Commands/MakePublicationTypeCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 47da61f4636..932d4b82981 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -92,7 +92,7 @@ protected function captureFieldsDefinitions(): Collection $addAnother = $this->askWithValidation('addAnother', 'Add another field (y/n)', ['required', 'string', 'in:y,n'], 'n'); // map field choice to actual field type - $fieldData['type'] = PublicationFieldTypes::values()[$type - 1]; + $fieldData['type'] = PublicationFieldTypes::values()[$type]; $fields->add(PublicationField::fromArray($fieldData)); $count++; @@ -105,7 +105,7 @@ protected function getFieldType(): int { $options = PublicationFieldTypes::collect()->pluck('name')->toArray(); - return (int) $this->choice('Field type', $options, 1) + 1; + return (int) $this->choice('Field type', $options, 0); } protected function getSortField(Collection $fields): string From 50875fd6a1ba9d848ab98c9a4bc0207f1c4efb0c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 14:19:08 +0100 Subject: [PATCH 010/130] Rename local parameter to clarify it --- .../src/Console/Commands/MakePublicationTypeCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 932d4b82981..1defc87806c 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -147,9 +147,9 @@ protected function getPrevNextLinks(): bool ); } - protected function getCanonicalField(Collection $fields): PublicationField + protected function getCanonicalField(Collection $selectedFields): PublicationField { - $options = $fields->reject(function (PublicationField $field): bool { + $options = $selectedFields->reject(function (PublicationField $field): bool { // Temporary verbose check to see code coverage if ($field->type === PublicationFieldTypes::Image) { return true; @@ -160,7 +160,7 @@ protected function getCanonicalField(Collection $fields): PublicationField } })->pluck('name'); - return $fields->firstWhere('name', + return $selectedFields->firstWhere('name', $this->choice('Choose a canonical name field (the values of this field have to be unique!)', $options->toArray(), $options->first()) ); } From 4007c5075ea002ec467acb03af4dd2a1374f7e7c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 14:36:09 +0100 Subject: [PATCH 011/130] Refactor to return the enum type --- .../Console/Commands/MakePublicationTypeCommand.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 1defc87806c..008db910a22 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -86,13 +86,13 @@ protected function captureFieldsDefinitions(): Collection $type = $this->getFieldType(); - if ($type === 10) { + if ($type === PublicationFieldTypes::Tag) { $fieldData = $this->getFieldDataForTag($fieldData); } $addAnother = $this->askWithValidation('addAnother', 'Add another field (y/n)', ['required', 'string', 'in:y,n'], 'n'); // map field choice to actual field type - $fieldData['type'] = PublicationFieldTypes::values()[$type]; + $fieldData['type'] = $type; $fields->add(PublicationField::fromArray($fieldData)); $count++; @@ -101,11 +101,13 @@ protected function captureFieldsDefinitions(): Collection return $fields; } - protected function getFieldType(): int + protected function getFieldType(): PublicationFieldTypes { $options = PublicationFieldTypes::collect()->pluck('name')->toArray(); - return (int) $this->choice('Field type', $options, 0); + $choice = $this->choice('Field type', $options, 'String'); + + return PublicationFieldTypes::from(strtolower($choice)); } protected function getSortField(Collection $fields): string From 9df715ffe151d234f0298a75a699689b0fed8bd4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 14:41:37 +0100 Subject: [PATCH 012/130] Use the canonicable array to see if field can be canonical --- .../Console/Commands/MakePublicationTypeCommand.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 008db910a22..cae4da1579d 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -16,6 +16,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Str; use InvalidArgumentException; +use function in_array; use function is_dir; use LaravelZero\Framework\Commands\Command; use function scandir; @@ -152,14 +153,7 @@ protected function getPrevNextLinks(): bool protected function getCanonicalField(Collection $selectedFields): PublicationField { $options = $selectedFields->reject(function (PublicationField $field): bool { - // Temporary verbose check to see code coverage - if ($field->type === PublicationFieldTypes::Image) { - return true; - } elseif ($field->type === PublicationFieldTypes::Tag) { - return true; - } else { - return false; - } + return in_array($field, PublicationFieldTypes::canonicable()); })->pluck('name'); return $selectedFields->firstWhere('name', From b5b8718d5609e7ff5465a515b6f99e6254a4fc4b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 14:41:59 +0100 Subject: [PATCH 013/130] Warn and default to meta field when there are no selectable options --- .../src/Console/Commands/MakePublicationTypeCommand.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index cae4da1579d..13f539b1278 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -156,6 +156,14 @@ protected function getCanonicalField(Collection $selectedFields): PublicationFie return in_array($field, PublicationFieldTypes::canonicable()); })->pluck('name'); + if ($options->isEmpty()) { + $this->warn('There are no fields that can be canonical. Using __createdAt instead.'); + return PublicationField::fromArray([ + 'name' => '__createdAt', + 'type' => PublicationFieldTypes::Datetime, + ]); + } + return $selectedFields->firstWhere('name', $this->choice('Choose a canonical name field (the values of this field have to be unique!)', $options->toArray(), $options->first()) ); From 56ebb79c7842e72b08b163d569bf576d38ddceca Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 14:42:11 +0100 Subject: [PATCH 014/130] Update warning message --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 13f539b1278..60addca07ec 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -157,7 +157,7 @@ protected function getCanonicalField(Collection $selectedFields): PublicationFie })->pluck('name'); if ($options->isEmpty()) { - $this->warn('There are no fields that can be canonical. Using __createdAt instead.'); + $this->warn('There are no fields that can be canonical. Defaulting to __createdAt instead.'); return PublicationField::fromArray([ 'name' => '__createdAt', 'type' => PublicationFieldTypes::Datetime, From 198638daeaacf6a346fe62ce3c441c78d928d984 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 14:44:03 +0100 Subject: [PATCH 015/130] Update question message --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- .../tests/Feature/Commands/MakePublicationTypeCommandTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 60addca07ec..9311855a3c5 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -165,7 +165,7 @@ protected function getCanonicalField(Collection $selectedFields): PublicationFie } return $selectedFields->firstWhere('name', - $this->choice('Choose a canonical name field (the values of this field have to be unique!)', $options->toArray(), $options->first()) + $this->choice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', $options->toArray(), $options->first()) ); } diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 353cc3b5dfe..5392ed1063e 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -50,7 +50,7 @@ public function test_command_creates_publication_type() ]) ->expectsQuestion('Enter the pageSize (0 for no limit)', 10) ->expectsQuestion('Generate previous/next links in detail view (y/n)', 'n') - ->expectsChoice('Choose a canonical name field (the values of this field have to be unique!)', 'publication-title', [ + ->expectsChoice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', 'publication-title', [ 'publication-title', ]) ->expectsOutputToContain('Creating a new Publication Type!') From 215819cc2102df0ef2a4a81a1dec824a483e776b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 14:44:30 +0100 Subject: [PATCH 016/130] Split comma-separated values into multiple lines --- .../src/Console/Commands/MakePublicationTypeCommand.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 9311855a3c5..61563cbdcdf 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -165,8 +165,10 @@ protected function getCanonicalField(Collection $selectedFields): PublicationFie } return $selectedFields->firstWhere('name', - $this->choice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', $options->toArray(), $options->first()) - ); + $this->choice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', + $options->toArray(), + $options->first() + )); } protected function getFieldDataForTag(array $fieldData): array From 66555a9cc22ceace725be7e89f0338e925612b75 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 23 Dec 2022 14:00:26 +0000 Subject: [PATCH 017/130] Apply fixes from StyleCI --- .../src/Console/Commands/MakePublicationTypeCommand.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 61563cbdcdf..47f8f36a65a 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -15,8 +15,8 @@ use Hyde\Framework\Features\Publications\PublicationService; use Illuminate\Support\Collection; use Illuminate\Support\Str; -use InvalidArgumentException; use function in_array; +use InvalidArgumentException; use function is_dir; use LaravelZero\Framework\Commands\Command; use function scandir; @@ -158,6 +158,7 @@ protected function getCanonicalField(Collection $selectedFields): PublicationFie if ($options->isEmpty()) { $this->warn('There are no fields that can be canonical. Defaulting to __createdAt instead.'); + return PublicationField::fromArray([ 'name' => '__createdAt', 'type' => PublicationFieldTypes::Datetime, From a009421fc14fa1958d011af3a392df691119cc34 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 16:20:28 +0100 Subject: [PATCH 018/130] Test the canonicable method --- .../tests/Feature/PublicationFieldTypesEnumTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index 4cbd68b1d2d..f8db901a01c 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -62,4 +62,14 @@ public function testValuesReturnsArrayOfCaseValues() 9 => 'url', ], PublicationFieldTypes::values()); } + + public function testCanonicable() + { + $this->assertSame([ + PublicationFieldTypes::String, + PublicationFieldTypes::Integer, + PublicationFieldTypes::Datetime, + PublicationFieldTypes::Text, + ], PublicationFieldTypes::canonicable()); + } } From ef6ed8050d7cb33b6852f6d1b4c427b3b2f46153 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 16:21:37 +0100 Subject: [PATCH 019/130] Clean up filesystem in teardown method --- .../Feature/Commands/MakePublicationTypeCommandTest.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 5392ed1063e..4b707feb27d 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -22,6 +22,13 @@ protected function setUp(): void config(['app.throw_on_console_exception' => true]); } + protected function tearDown(): void + { + Filesystem::deleteDirectory('test-publication'); + + parent::tearDown(); + } + public function test_command_creates_publication_type() { $this->artisan('make:publicationType') @@ -84,7 +91,5 @@ public function test_command_creates_publication_type() ); // TODO: Assert Blade templates were created? - - Filesystem::deleteDirectory('test-publication'); } } From 3aec858e9cbfedf31b72cf93276f5c96e74d3337 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 16:29:43 +0100 Subject: [PATCH 020/130] Use built in confirmation helper --- .../src/Console/Commands/MakePublicationTypeCommand.php | 4 ++-- .../tests/Feature/Commands/MakePublicationTypeCommandTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 47f8f36a65a..ddc4c281e73 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -90,14 +90,14 @@ protected function captureFieldsDefinitions(): Collection if ($type === PublicationFieldTypes::Tag) { $fieldData = $this->getFieldDataForTag($fieldData); } - $addAnother = $this->askWithValidation('addAnother', 'Add another field (y/n)', ['required', 'string', 'in:y,n'], 'n'); + $addAnother = $this->confirm('Add another field?'); // map field choice to actual field type $fieldData['type'] = $type; $fields->add(PublicationField::fromArray($fieldData)); $count++; - } while (strtolower($addAnother) !== 'n'); + } while ($addAnother); return $fields; } diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 4b707feb27d..0cf6449ba5c 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -46,7 +46,7 @@ public function test_command_creates_publication_type() 'Text', 'Url', ]) - ->expectsQuestion('Add another field (y/n)', 'n') + ->expectsConfirmation('Add another field?', 'n') ->expectsChoice('Choose the default field you wish to sort by', 'dateCreated (meta field)', [ 'dateCreated (meta field)', 'publication-title', From 08ffd7ca252a31dcb6f0fe50c246a34194ec2d55 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 16:53:01 +0100 Subject: [PATCH 021/130] Test command with multiple fields of the same name --- .../Feature/Commands/MakePublicationTypeCommandTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 0cf6449ba5c..790e89f0fa4 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -92,4 +92,9 @@ public function test_command_creates_publication_type() // TODO: Assert Blade templates were created? } + + public function test_with_multiple_fields_of_the_same_name() + { + $this->markTestIncomplete('Unable to test this because of a bug in Mockery'); + } } From aadbbd67fc127e5b04fdb37967dc71456de4b326 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 16:57:52 +0100 Subject: [PATCH 022/130] Use strict array validation --- .../tests/Feature/Commands/MakePublicationTypeCommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 790e89f0fa4..3ede3d2aab5 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -45,7 +45,7 @@ public function test_command_creates_publication_type() 'Tag', 'Text', 'Url', - ]) + ], true) ->expectsConfirmation('Add another field?', 'n') ->expectsChoice('Choose the default field you wish to sort by', 'dateCreated (meta field)', [ 'dateCreated (meta field)', From af3d9e659ebfc9f1247892200fd37c5c8cc09cf9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 16:58:49 +0100 Subject: [PATCH 023/130] Sort options for better ease of access --- .../Console/Commands/MakePublicationTypeCommand.php | 13 ++++++++++++- .../Commands/MakePublicationTypeCommandTest.php | 10 +++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index ddc4c281e73..0eb084a3f41 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -104,7 +104,18 @@ protected function captureFieldsDefinitions(): Collection protected function getFieldType(): PublicationFieldTypes { - $options = PublicationFieldTypes::collect()->pluck('name')->toArray(); + $options = [ + 'String', + 'Datetime', + 'Boolean', + 'Integer', + 'Float', + 'Image', + 'Array', + 'Text', + 'Url', + 'Tag', + ]; $choice = $this->choice('Field type', $options, 'String'); diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 3ede3d2aab5..966d9484012 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -35,16 +35,16 @@ public function test_command_creates_publication_type() ->expectsQuestion('Publication type name', 'Test Publication') ->expectsQuestion('Field name', 'Publication Title') ->expectsChoice('Field type', 'String', [ - 'Array', - 'Boolean', + 'String', 'Datetime', + 'Boolean', + 'Integer', 'Float', 'Image', - 'Integer', - 'String', - 'Tag', + 'Array', 'Text', 'Url', + 'Tag', ], true) ->expectsConfirmation('Add another field?', 'n') ->expectsChoice('Choose the default field you wish to sort by', 'dateCreated (meta field)', [ From 6cc96cccd36dc8509001ac8f8fc7475f65edc8ff Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:01:02 +0100 Subject: [PATCH 024/130] Use the built in confirm helper --- .../src/Console/Commands/MakePublicationTypeCommand.php | 7 +------ .../Feature/Commands/MakePublicationTypeCommandTest.php | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 0eb084a3f41..cada5d43fda 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -153,12 +153,7 @@ protected function getPageSize(): int protected function getPrevNextLinks(): bool { - return (bool) $this->askWithValidation( - 'prevNextLinks', - 'Generate previous/next links in detail view (y/n)', - ['required', 'string', 'in:y,n'], - 'y' - ); + return $this->confirm('Generate previous/next links in detail view?', true); } protected function getCanonicalField(Collection $selectedFields): PublicationField diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 966d9484012..631d4630cdb 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -56,7 +56,7 @@ public function test_command_creates_publication_type() 'Descending (newest 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('Generate previous/next links in detail view?', 'n') ->expectsChoice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', 'publication-title', [ 'publication-title', ]) From 4a5d8e3c18908a0702ddbefdf2d354abc6df35df Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:11:46 +0100 Subject: [PATCH 025/130] Add option to use default values --- .../Console/Commands/MakePublicationTypeCommand.php | 5 +++-- .../Commands/MakePublicationTypeCommandTest.php | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index cada5d43fda..9a0829b2238 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -33,7 +33,8 @@ class MakePublicationTypeCommand extends ValidatingCommand { /** @var string */ protected $signature = 'make:publicationType - {title? : The name of the Publication Type to create. Will be used to generate the storage directory}'; + {title? : The name of the Publication Type to create. Will be used to generate the storage directory} + {--use-defaults : Select the default options wherever possible}'; /** @var string */ protected $description = 'Create a new publication type definition'; @@ -90,7 +91,7 @@ protected function captureFieldsDefinitions(): Collection if ($type === PublicationFieldTypes::Tag) { $fieldData = $this->getFieldDataForTag($fieldData); } - $addAnother = $this->confirm('Add another field?'); + $addAnother = $this->option('use-defaults') || $this->confirm('Add another field?'); // map field choice to actual field type $fieldData['type'] = $type; diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 631d4630cdb..846ba18d68d 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Testing\Feature\Commands; +use Hyde\Framework\Features\Publications\PublicationFieldTypes; use function config; use Hyde\Facades\Filesystem; use Hyde\Hyde; @@ -93,6 +94,17 @@ public function test_command_creates_publication_type() // TODO: Assert Blade templates were created? } + public function test_with_default_values() + { + $this->artisan('make:publicationType --use-defaults') + ->expectsQuestion('Publication type name', 'Test Publication') + ->expectsQuestion('Field name', 'Title') + ->expectsChoice('Field type', 'string', PublicationFieldTypes::collect()->pluck('name')->toArray()) + ->expectsOutput('Saving publication data to [test-publication/schema.json]') + ->expectsOutput('Publication type created successfully!') + ->assertExitCode(0); + } + public function test_with_multiple_fields_of_the_same_name() { $this->markTestIncomplete('Unable to test this because of a bug in Mockery'); From 5a9bb3f2358081eac79cdf888309ece29d6a4f26 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:11:58 +0100 Subject: [PATCH 026/130] Revert "Add option to use default values" This reverts commit 4a5d8e3c18908a0702ddbefdf2d354abc6df35df. --- .../Console/Commands/MakePublicationTypeCommand.php | 5 ++--- .../Commands/MakePublicationTypeCommandTest.php | 12 ------------ 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 9a0829b2238..cada5d43fda 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -33,8 +33,7 @@ class MakePublicationTypeCommand extends ValidatingCommand { /** @var string */ protected $signature = 'make:publicationType - {title? : The name of the Publication Type to create. Will be used to generate the storage directory} - {--use-defaults : Select the default options wherever possible}'; + {title? : The name of the Publication Type to create. Will be used to generate the storage directory}'; /** @var string */ protected $description = 'Create a new publication type definition'; @@ -91,7 +90,7 @@ protected function captureFieldsDefinitions(): Collection if ($type === PublicationFieldTypes::Tag) { $fieldData = $this->getFieldDataForTag($fieldData); } - $addAnother = $this->option('use-defaults') || $this->confirm('Add another field?'); + $addAnother = $this->confirm('Add another field?'); // map field choice to actual field type $fieldData['type'] = $type; diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 846ba18d68d..631d4630cdb 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -4,7 +4,6 @@ namespace Hyde\Framework\Testing\Feature\Commands; -use Hyde\Framework\Features\Publications\PublicationFieldTypes; use function config; use Hyde\Facades\Filesystem; use Hyde\Hyde; @@ -94,17 +93,6 @@ public function test_command_creates_publication_type() // TODO: Assert Blade templates were created? } - public function test_with_default_values() - { - $this->artisan('make:publicationType --use-defaults') - ->expectsQuestion('Publication type name', 'Test Publication') - ->expectsQuestion('Field name', 'Title') - ->expectsChoice('Field type', 'string', PublicationFieldTypes::collect()->pluck('name')->toArray()) - ->expectsOutput('Saving publication data to [test-publication/schema.json]') - ->expectsOutput('Publication type created successfully!') - ->assertExitCode(0); - } - public function test_with_multiple_fields_of_the_same_name() { $this->markTestIncomplete('Unable to test this because of a bug in Mockery'); From ecd1eb7fc47ed407b9d156f629586879b9114c0b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:19:02 +0100 Subject: [PATCH 027/130] Update argument name 'title' to 'name' to match rest of the code --- .../src/Console/Commands/MakePublicationTypeCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index cada5d43fda..e448fa200fa 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -33,7 +33,7 @@ class MakePublicationTypeCommand extends ValidatingCommand { /** @var string */ protected $signature = 'make:publicationType - {title? : The name of the Publication Type to create. Will be used to generate the storage directory}'; + {name? : The name of the Publication Type to create. Will be used to generate the storage directory}'; /** @var string */ protected $description = 'Create a new publication type definition'; @@ -42,7 +42,7 @@ public function safeHandle(): int { $this->title('Creating a new Publication Type!'); - $title = $this->argument('title'); + $title = $this->argument('name'); if (! $title) { $title = trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); $dirname = Str::slug($title); From 8340439c885653dc84c240f99483920c52854638 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:23:08 +0100 Subject: [PATCH 028/130] Display the field number --- .../src/Console/Commands/MakePublicationTypeCommand.php | 9 ++++----- .../Feature/Commands/MakePublicationTypeCommandTest.php | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index e448fa200fa..d8d6da37fd3 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -77,15 +77,14 @@ protected function captureFieldsDefinitions(): Collection $fields = Collection::make(); do { $this->line(''); - $this->output->writeln("Field #$count:"); $fieldData = []; do { - $fieldData['name'] = Str::kebab(trim($this->askWithValidation('name', 'Field name', ['required']))); + $fieldData['name'] = Str::kebab(trim($this->askWithValidation('name', "Enter name for field #$count", ['required']))); $duplicate = $this->checkIfFieldIsDuplicate($fields, $fieldData['name']); } while ($duplicate); - $type = $this->getFieldType(); + $type = $this->getFieldType($count); if ($type === PublicationFieldTypes::Tag) { $fieldData = $this->getFieldDataForTag($fieldData); @@ -102,7 +101,7 @@ protected function captureFieldsDefinitions(): Collection return $fields; } - protected function getFieldType(): PublicationFieldTypes + protected function getFieldType(int $count): PublicationFieldTypes { $options = [ 'String', @@ -117,7 +116,7 @@ protected function getFieldType(): PublicationFieldTypes 'Tag', ]; - $choice = $this->choice('Field type', $options, 'String'); + $choice = $this->choice("Enter type for field #$count", $options, 'String'); return PublicationFieldTypes::from(strtolower($choice)); } diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 631d4630cdb..92ed5ef91f9 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -33,8 +33,8 @@ public function test_command_creates_publication_type() { $this->artisan('make:publicationType') ->expectsQuestion('Publication type name', 'Test Publication') - ->expectsQuestion('Field name', 'Publication Title') - ->expectsChoice('Field type', 'String', [ + ->expectsQuestion('Enter name for field #1', 'Publication Title') + ->expectsChoice('Enter type for field #1', 'String', [ 'String', 'Datetime', 'Boolean', From d122dae34919812ec8513b7d5ab414c3b7e719c9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:46:08 +0100 Subject: [PATCH 029/130] Update MakePublicationTypeCommandTest.php --- .../Feature/Commands/MakePublicationTypeCommandTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 92ed5ef91f9..c6be9497627 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Testing\Feature\Commands; +use Hyde\Framework\Features\Publications\PublicationFieldTypes; use function config; use Hyde\Facades\Filesystem; use Hyde\Hyde; @@ -95,6 +96,11 @@ public function test_command_creates_publication_type() public function test_with_multiple_fields_of_the_same_name() { - $this->markTestIncomplete('Unable to test this because of a bug in Mockery'); + $this->artisan('make:publicationType "Test Publication"') + ->expectsQuestion('Enter name for field #1', 'foo') + ->expectsChoice('Enter type for field #1', 'String', PublicationFieldTypes::collect()->pluck('name')->toArray()) + ->expectsConfirmation('Add another field?', 'y') + ->expectsQuestion('Enter name for field #2', 'foo') + ->assertExitCode(0); } } From fd23b4cf3ad1c6ec2e20a9d259bddc42ec4e895b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:47:12 +0100 Subject: [PATCH 030/130] Convert variable to class property --- .../Console/Commands/MakePublicationTypeCommand.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index d8d6da37fd3..7b36a106e5b 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -37,6 +37,7 @@ class MakePublicationTypeCommand extends ValidatingCommand /** @var string */ protected $description = 'Create a new publication type definition'; + protected int $count; public function safeHandle(): int { @@ -73,18 +74,18 @@ public function safeHandle(): int protected function captureFieldsDefinitions(): Collection { $this->output->writeln('You now need to define the fields in your publication type:'); - $count = 1; + $this->count = 1; $fields = Collection::make(); do { $this->line(''); $fieldData = []; do { - $fieldData['name'] = Str::kebab(trim($this->askWithValidation('name', "Enter name for field #$count", ['required']))); + $fieldData['name'] = Str::kebab(trim($this->askWithValidation('name', "Enter name for field #$this->count", ['required']))); $duplicate = $this->checkIfFieldIsDuplicate($fields, $fieldData['name']); } while ($duplicate); - $type = $this->getFieldType($count); + $type = $this->getFieldType(); if ($type === PublicationFieldTypes::Tag) { $fieldData = $this->getFieldDataForTag($fieldData); @@ -95,13 +96,13 @@ protected function captureFieldsDefinitions(): Collection $fieldData['type'] = $type; $fields->add(PublicationField::fromArray($fieldData)); - $count++; + $this->count++; } while ($addAnother); return $fields; } - protected function getFieldType(int $count): PublicationFieldTypes + protected function getFieldType(): PublicationFieldTypes { $options = [ 'String', @@ -116,7 +117,7 @@ protected function getFieldType(int $count): PublicationFieldTypes 'Tag', ]; - $choice = $this->choice("Enter type for field #$count", $options, 'String'); + $choice = $this->choice("Enter type for field #$this->count", $options, 'String'); return PublicationFieldTypes::from(strtolower($choice)); } From 6d4375bdcac3d4ff4948cb095a428306895f0ba5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:47:21 +0100 Subject: [PATCH 031/130] Use default value --- .../src/Console/Commands/MakePublicationTypeCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 7b36a106e5b..2ae79203127 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -37,7 +37,7 @@ class MakePublicationTypeCommand extends ValidatingCommand /** @var string */ protected $description = 'Create a new publication type definition'; - protected int $count; + protected int $count = 1; public function safeHandle(): int { @@ -74,7 +74,6 @@ public function safeHandle(): int protected function captureFieldsDefinitions(): Collection { $this->output->writeln('You now need to define the fields in your publication type:'); - $this->count = 1; $fields = Collection::make(); do { $this->line(''); From 992ba9b8acf0c39a080cf63a65380b9ba4c6bf65 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:47:55 +0100 Subject: [PATCH 032/130] Use standard output styling --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 2ae79203127..6c5074fe6c5 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -73,7 +73,7 @@ public function safeHandle(): int protected function captureFieldsDefinitions(): Collection { - $this->output->writeln('You now need to define the fields in your publication type:'); + $this->line('You now need to define the fields in your publication type:'); $fields = Collection::make(); do { $this->line(''); From 1efc07a969994c4a0140519335a2e842ecfa4c5f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:48:45 +0100 Subject: [PATCH 033/130] Extract method to get the pagination settings --- .../Commands/MakePublicationTypeCommand.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 6c5074fe6c5..e9ed5b4f4fd 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -54,12 +54,7 @@ public function safeHandle(): int $fields = $this->captureFieldsDefinitions(); - $sortField = $this->getSortField($fields); - - $sortAscending = $this->getSortDirection(); - - $pageSize = $this->getPageSize(); - $prevNextLinks = $this->getPrevNextLinks(); + list($sortField, $sortAscending, $pageSize, $prevNextLinks) = $this->getPaginationSettings($fields); $canonicalField = $this->getCanonicalField($fields); @@ -203,4 +198,13 @@ protected function checkIfFieldIsDuplicate(Collection $fields, $name): bool return (bool) $duplicate; } + + protected function getPaginationSettings(Collection $fields): array + { + $sortField = $this->getSortField($fields); + $sortAscending = $this->getSortDirection(); + $pageSize = $this->getPageSize(); + $prevNextLinks = $this->getPrevNextLinks(); + return array($sortField, $sortAscending, $pageSize, $prevNextLinks); + } } From 2bc77919096f46db62ebb69dbcfa12e2fa19ac40 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:49:03 +0100 Subject: [PATCH 034/130] Reorder methods --- .../Commands/MakePublicationTypeCommand.php | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index e9ed5b4f4fd..49547bd4d8c 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -116,40 +116,6 @@ protected function getFieldType(): PublicationFieldTypes return PublicationFieldTypes::from(strtolower($choice)); } - protected function getSortField(Collection $fields): string - { - $options = array_merge(['dateCreated (meta field)'], $fields->pluck('name')->toArray()); - - $selected = $this->choice('Choose the default field you wish to sort by', $options, 'dateCreated (meta field)'); - - return $selected === 'dateCreated (meta field)' ? '__createdAt' : $options[(array_flip($options)[$selected])]; - } - - protected function getSortDirection(): bool - { - $options = [ - 'Ascending (oldest items first if sorting by dateCreated)' => true, - 'Descending (newest items first if sorting by dateCreated)' => false, - ]; - - return $options[$this->choice('Choose the default sort direction', array_keys($options), 'Ascending (oldest items first if sorting by dateCreated)')]; - } - - protected function getPageSize(): int - { - return (int) $this->askWithValidation( - 'pageSize', - 'Enter the pageSize (0 for no limit)', - ['required', 'integer', 'between:0,100'], - 25 - ); - } - - protected function getPrevNextLinks(): bool - { - return $this->confirm('Generate previous/next links in detail view?', true); - } - protected function getCanonicalField(Collection $selectedFields): PublicationField { $options = $selectedFields->reject(function (PublicationField $field): bool { @@ -207,4 +173,38 @@ protected function getPaginationSettings(Collection $fields): array $prevNextLinks = $this->getPrevNextLinks(); return array($sortField, $sortAscending, $pageSize, $prevNextLinks); } + + protected function getSortField(Collection $fields): string + { + $options = array_merge(['dateCreated (meta field)'], $fields->pluck('name')->toArray()); + + $selected = $this->choice('Choose the default field you wish to sort by', $options, 'dateCreated (meta field)'); + + return $selected === 'dateCreated (meta field)' ? '__createdAt' : $options[(array_flip($options)[$selected])]; + } + + protected function getSortDirection(): bool + { + $options = [ + 'Ascending (oldest items first if sorting by dateCreated)' => true, + 'Descending (newest items first if sorting by dateCreated)' => false, + ]; + + return $options[$this->choice('Choose the default sort direction', array_keys($options), 'Ascending (oldest items first if sorting by dateCreated)')]; + } + + protected function getPageSize(): int + { + return (int) $this->askWithValidation( + 'pageSize', + 'Enter the pageSize (0 for no limit)', + ['required', 'integer', 'between:0,100'], + 25 + ); + } + + protected function getPrevNextLinks(): bool + { + return $this->confirm('Generate previous/next links in detail view?', true); + } } From 1bbeaa6c9dbf113b91c2130eefa444d15d2eba3a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:50:39 +0100 Subject: [PATCH 035/130] Convert array to short associative syntax --- .../src/Console/Commands/MakePublicationTypeCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 49547bd4d8c..56cefb3b7af 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -54,7 +54,7 @@ public function safeHandle(): int $fields = $this->captureFieldsDefinitions(); - list($sortField, $sortAscending, $pageSize, $prevNextLinks) = $this->getPaginationSettings($fields); + list($sortField, $sortAscending, $pageSize, $prevNextLinks) = array_values($this->getPaginationSettings($fields)); $canonicalField = $this->getCanonicalField($fields); @@ -171,7 +171,7 @@ protected function getPaginationSettings(Collection $fields): array $sortAscending = $this->getSortDirection(); $pageSize = $this->getPageSize(); $prevNextLinks = $this->getPrevNextLinks(); - return array($sortField, $sortAscending, $pageSize, $prevNextLinks); + return ['sortField' => $sortField, 'sortAscending' => $sortAscending, 'pageSize' => $pageSize, 'prevNextLinks' => $prevNextLinks]; } protected function getSortField(Collection $fields): string From 4b3e7d32952380b6d6f5046ca154be884c7ed6e0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:54:06 +0100 Subject: [PATCH 036/130] Create array of default values --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 56cefb3b7af..4bb39750eaf 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -167,6 +167,8 @@ protected function checkIfFieldIsDuplicate(Collection $fields, $name): bool protected function getPaginationSettings(Collection $fields): array { + $paginationDefaults = ['sortField' => '__createdAt', 'sortAscending' => true, 'pageSize' => 25, 'prevNextLinks' => true]; + $sortField = $this->getSortField($fields); $sortAscending = $this->getSortDirection(); $pageSize = $this->getPageSize(); From 5f2faad202a24f59f6840481728a47b5a4172d0c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 17:55:30 +0100 Subject: [PATCH 037/130] Partially revert "Revert "Add option to use default values"" This partially reverts commit 5a9bb3f2358081eac79cdf888309ece29d6a4f26. --- .../Console/Commands/MakePublicationTypeCommand.php | 3 ++- .../Commands/MakePublicationTypeCommandTest.php | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 4bb39750eaf..1bff20bf675 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -33,7 +33,8 @@ class MakePublicationTypeCommand extends ValidatingCommand { /** @var string */ protected $signature = 'make:publicationType - {name? : The name of the Publication Type to create. Will be used to generate the storage directory}'; + {name? : The name of the Publication Type to create. Will be used to generate the storage directory} + {--use-defaults : Select the default options wherever possible}'; /** @var string */ protected $description = 'Create a new publication type definition'; diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index c6be9497627..53adf57cf0f 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -94,6 +94,17 @@ public function test_command_creates_publication_type() // TODO: Assert Blade templates were created? } + public function test_with_default_values() + { + $this->artisan('make:publicationType --use-defaults') + ->expectsQuestion('Publication type name', 'Test Publication') + ->expectsQuestion('Enter name for field #1', 'foo') + ->expectsChoice('Enter type for field #1', 'String', PublicationFieldTypes::collect()->pluck('name')->toArray()) + ->expectsOutput('Saving publication data to [test-publication/schema.json]') + ->expectsOutput('Publication type created successfully!') + ->assertExitCode(0); + } + public function test_with_multiple_fields_of_the_same_name() { $this->artisan('make:publicationType "Test Publication"') From 5a6e3cc01ce081b123d089e171b4b2fe3571e7ba Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:01:09 +0100 Subject: [PATCH 038/130] Return paginationDefaults when use-defaults flag is set --- .../src/Console/Commands/MakePublicationTypeCommand.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 1bff20bf675..b2f0a42f023 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -169,7 +169,9 @@ protected function checkIfFieldIsDuplicate(Collection $fields, $name): bool protected function getPaginationSettings(Collection $fields): array { $paginationDefaults = ['sortField' => '__createdAt', 'sortAscending' => true, 'pageSize' => 25, 'prevNextLinks' => true]; - + if ($this->option('use-defaults')) { + return $paginationDefaults; + } $sortField = $this->getSortField($fields); $sortAscending = $this->getSortDirection(); $pageSize = $this->getPageSize(); From eb3319a2a4aff97642a802a45fae26e0bb828f14 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:01:21 +0100 Subject: [PATCH 039/130] Don't ask to add another when use-defaults flag is set --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index b2f0a42f023..cb8b8278283 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -85,7 +85,7 @@ protected function captureFieldsDefinitions(): Collection if ($type === PublicationFieldTypes::Tag) { $fieldData = $this->getFieldDataForTag($fieldData); } - $addAnother = $this->confirm('Add another field?'); + $addAnother = (! $this->hasOption('use-defaults')) && $this->confirm('Add another field?'); // map field choice to actual field type $fieldData['type'] = $type; From 108997cfc5895f1ee11cc1c41665b64aa5d1ef72 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:07:03 +0100 Subject: [PATCH 040/130] Use __createdAt as canonical when use-defaults flag is set --- .../src/Console/Commands/MakePublicationTypeCommand.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index cb8b8278283..f81c323548f 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -119,6 +119,13 @@ protected function getFieldType(): PublicationFieldTypes protected function getCanonicalField(Collection $selectedFields): PublicationField { + if ($this->hasOption('use-defaults')) { + return PublicationField::fromArray([ + 'name' => '__createdAt', + 'type' => PublicationFieldTypes::Datetime, + ]); + } + $options = $selectedFields->reject(function (PublicationField $field): bool { return in_array($field, PublicationFieldTypes::canonicable()); })->pluck('name'); From 21a877193b97ad35568ca708be64019e7e0a48aa Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:07:50 +0100 Subject: [PATCH 041/130] Add todo --- .../src/Console/Commands/MakePublicationTypeCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index f81c323548f..0e62b57855d 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -119,6 +119,7 @@ protected function getFieldType(): PublicationFieldTypes protected function getCanonicalField(Collection $selectedFields): PublicationField { + // todo Use the first field as canonical when use-defaults flag is set if ($this->hasOption('use-defaults')) { return PublicationField::fromArray([ 'name' => '__createdAt', From 1c3a8c6a472ce8199617a609ab3d63efc15e34a8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:10:19 +0100 Subject: [PATCH 042/130] Split out if block --- .../src/Console/Commands/MakePublicationTypeCommand.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 0e62b57855d..308d13b4513 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -85,7 +85,12 @@ protected function captureFieldsDefinitions(): Collection if ($type === PublicationFieldTypes::Tag) { $fieldData = $this->getFieldDataForTag($fieldData); } - $addAnother = (! $this->hasOption('use-defaults')) && $this->confirm('Add another field?'); + + if ($this->option('use-defaults') === true) { + $addAnother = false; + } else { + $addAnother = $this->confirm('Add another field?'); + } // map field choice to actual field type $fieldData['type'] = $type; From 9c98578547a1f2290806a03e7e177a3e097c0f2b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:14:26 +0100 Subject: [PATCH 043/130] Refactor to add date meta field as the zeroth field --- .../src/Console/Commands/MakePublicationTypeCommand.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 308d13b4513..8cca5758971 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -38,7 +38,7 @@ class MakePublicationTypeCommand extends ValidatingCommand /** @var string */ protected $description = 'Create a new publication type definition'; - protected int $count = 1; + protected int $count = 0; public function safeHandle(): int { @@ -71,6 +71,13 @@ protected function captureFieldsDefinitions(): Collection { $this->line('You now need to define the fields in your publication type:'); $fields = Collection::make(); + + $fields->add(PublicationField::fromArray([ + 'name' => '__createdAt', + 'type' => PublicationFieldTypes::Datetime, + ])); + $this->count++; + do { $this->line(''); From 3dc9eb41e2eba9d6db176fc7e52f5bacb6c18c91 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:16:10 +0100 Subject: [PATCH 044/130] Use the first field as canonical when use-defaults flag is set --- .../Commands/MakePublicationTypeCommand.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 8cca5758971..393f0bc1c35 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -131,17 +131,15 @@ protected function getFieldType(): PublicationFieldTypes protected function getCanonicalField(Collection $selectedFields): PublicationField { - // todo Use the first field as canonical when use-defaults flag is set + $selectableFields = $selectedFields->reject(function (PublicationField $field): bool { + return in_array($field, PublicationFieldTypes::canonicable()); + }); + if ($this->hasOption('use-defaults')) { - return PublicationField::fromArray([ - 'name' => '__createdAt', - 'type' => PublicationFieldTypes::Datetime, - ]); + return $selectableFields->first(); } - $options = $selectedFields->reject(function (PublicationField $field): bool { - return in_array($field, PublicationFieldTypes::canonicable()); - })->pluck('name'); + $options = $selectableFields->pluck('name'); if ($options->isEmpty()) { $this->warn('There are no fields that can be canonical. Defaulting to __createdAt instead.'); From 1c0c2a6bd98d5132871d9964ebca29321e9cc669 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:16:26 +0100 Subject: [PATCH 045/130] Unwrap if-block as array is now never empty --- .../src/Console/Commands/MakePublicationTypeCommand.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 393f0bc1c35..d8cca17399d 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -141,15 +141,6 @@ protected function getCanonicalField(Collection $selectedFields): PublicationFie $options = $selectableFields->pluck('name'); - if ($options->isEmpty()) { - $this->warn('There are no fields that can be canonical. Defaulting to __createdAt instead.'); - - return PublicationField::fromArray([ - 'name' => '__createdAt', - 'type' => PublicationFieldTypes::Datetime, - ]); - } - return $selectedFields->firstWhere('name', $this->choice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', $options->toArray(), From 4bde7c2d8d65b71a7c6028a8cfd514d8ca434b7b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:22:22 +0100 Subject: [PATCH 046/130] Use option instead of hasOption --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index d8cca17399d..95fa23ced1e 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -135,7 +135,7 @@ protected function getCanonicalField(Collection $selectedFields): PublicationFie return in_array($field, PublicationFieldTypes::canonicable()); }); - if ($this->hasOption('use-defaults')) { + if ($this->option('use-defaults')) { return $selectableFields->first(); } From 225437b80a3c6731b177ca6d0624bdc4d6118669 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:25:47 +0100 Subject: [PATCH 047/130] Refactor sort label logic --- .../src/Console/Commands/MakePublicationTypeCommand.php | 7 ++++--- .../Feature/Commands/MakePublicationTypeCommandTest.php | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 95fa23ced1e..6ed7d7d776d 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -20,6 +20,7 @@ use function is_dir; use LaravelZero\Framework\Commands\Command; use function scandir; +use function str_replace; use function strtolower; use function trim; @@ -190,11 +191,11 @@ protected function getPaginationSettings(Collection $fields): array protected function getSortField(Collection $fields): string { - $options = array_merge(['dateCreated (meta field)'], $fields->pluck('name')->toArray()); + $options = $fields->pluck('name')->toArray(); + $options[0] = 'Date created (meta field)'; $selected = $this->choice('Choose the default field you wish to sort by', $options, 'dateCreated (meta field)'); - - return $selected === 'dateCreated (meta field)' ? '__createdAt' : $options[(array_flip($options)[$selected])]; + return str_replace('Date created (meta field)', '__createdAt', $selected); } protected function getSortDirection(): bool diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 53adf57cf0f..1daf103bff1 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -49,7 +49,7 @@ public function test_command_creates_publication_type() ], true) ->expectsConfirmation('Add another field?', 'n') ->expectsChoice('Choose the default field you wish to sort by', 'dateCreated (meta field)', [ - 'dateCreated (meta field)', + 'Date created (meta field)', 'publication-title', ]) ->expectsChoice('Choose the default sort direction', 'Ascending (oldest items first if sorting by dateCreated)', [ From 61852d0306e4358290de21f17eee4854b5b1fccc Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:29:27 +0100 Subject: [PATCH 048/130] Refactor add primitive array data and only construct objects at the end --- .../src/Console/Commands/MakePublicationTypeCommand.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 6ed7d7d776d..c3897f0092e 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -55,6 +55,7 @@ public function safeHandle(): int } $fields = $this->captureFieldsDefinitions(); + $fields = $fields->map(fn (array $field) => PublicationField::fromArray($field)); list($sortField, $sortAscending, $pageSize, $prevNextLinks) = array_values($this->getPaginationSettings($fields)); @@ -73,10 +74,10 @@ protected function captureFieldsDefinitions(): Collection $this->line('You now need to define the fields in your publication type:'); $fields = Collection::make(); - $fields->add(PublicationField::fromArray([ + $fields->add([ 'name' => '__createdAt', 'type' => PublicationFieldTypes::Datetime, - ])); + ]); $this->count++; do { @@ -103,7 +104,7 @@ protected function captureFieldsDefinitions(): Collection // map field choice to actual field type $fieldData['type'] = $type; - $fields->add(PublicationField::fromArray($fieldData)); + $fields->add($fieldData); $this->count++; } while ($addAnother); From 9b9f3b8152a0ef1c7e896ae1a8c90b66d48228b5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:32:57 +0100 Subject: [PATCH 049/130] Normalize string at the end --- .../Console/Commands/MakePublicationTypeCommand.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index c3897f0092e..eb26d0e3909 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -55,9 +55,13 @@ public function safeHandle(): int } $fields = $this->captureFieldsDefinitions(); - $fields = $fields->map(fn (array $field) => PublicationField::fromArray($field)); list($sortField, $sortAscending, $pageSize, $prevNextLinks) = array_values($this->getPaginationSettings($fields)); + $fields->put(0, [ + 'name' => 'Date created (meta field)', + 'type' => PublicationFieldTypes::Datetime, + ]); + $fields = $fields->map(fn (array $field) => PublicationField::fromArray($field)); $canonicalField = $this->getCanonicalField($fields); @@ -75,7 +79,7 @@ protected function captureFieldsDefinitions(): Collection $fields = Collection::make(); $fields->add([ - 'name' => '__createdAt', + 'name' => 'Date created (meta field)', 'type' => PublicationFieldTypes::Datetime, ]); $this->count++; @@ -193,10 +197,8 @@ protected function getPaginationSettings(Collection $fields): array protected function getSortField(Collection $fields): string { $options = $fields->pluck('name')->toArray(); - $options[0] = 'Date created (meta field)'; - $selected = $this->choice('Choose the default field you wish to sort by', $options, 'dateCreated (meta field)'); - return str_replace('Date created (meta field)', '__createdAt', $selected); + return $this->choice('Choose the default field you wish to sort by', $options, 'dateCreated (meta field)'); } protected function getSortDirection(): bool From 600a31e62f78734d166fce634f479c704b97d4cf Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:33:04 +0100 Subject: [PATCH 050/130] Revert "Normalize string at the end" This reverts commit 9b9f3b8152a0ef1c7e896ae1a8c90b66d48228b5. --- .../Console/Commands/MakePublicationTypeCommand.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index eb26d0e3909..c3897f0092e 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -55,13 +55,9 @@ public function safeHandle(): int } $fields = $this->captureFieldsDefinitions(); + $fields = $fields->map(fn (array $field) => PublicationField::fromArray($field)); list($sortField, $sortAscending, $pageSize, $prevNextLinks) = array_values($this->getPaginationSettings($fields)); - $fields->put(0, [ - 'name' => 'Date created (meta field)', - 'type' => PublicationFieldTypes::Datetime, - ]); - $fields = $fields->map(fn (array $field) => PublicationField::fromArray($field)); $canonicalField = $this->getCanonicalField($fields); @@ -79,7 +75,7 @@ protected function captureFieldsDefinitions(): Collection $fields = Collection::make(); $fields->add([ - 'name' => 'Date created (meta field)', + 'name' => '__createdAt', 'type' => PublicationFieldTypes::Datetime, ]); $this->count++; @@ -197,8 +193,10 @@ protected function getPaginationSettings(Collection $fields): array protected function getSortField(Collection $fields): string { $options = $fields->pluck('name')->toArray(); + $options[0] = 'Date created (meta field)'; - return $this->choice('Choose the default field you wish to sort by', $options, 'dateCreated (meta field)'); + $selected = $this->choice('Choose the default field you wish to sort by', $options, 'dateCreated (meta field)'); + return str_replace('Date created (meta field)', '__createdAt', $selected); } protected function getSortDirection(): bool From b9c91e6700091e5c58537b3eba44e63ebeb958a2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:33:08 +0100 Subject: [PATCH 051/130] Revert "Refactor add primitive array data and only construct objects at the end" This reverts commit 61852d0306e4358290de21f17eee4854b5b1fccc. --- .../src/Console/Commands/MakePublicationTypeCommand.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index c3897f0092e..6ed7d7d776d 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -55,7 +55,6 @@ public function safeHandle(): int } $fields = $this->captureFieldsDefinitions(); - $fields = $fields->map(fn (array $field) => PublicationField::fromArray($field)); list($sortField, $sortAscending, $pageSize, $prevNextLinks) = array_values($this->getPaginationSettings($fields)); @@ -74,10 +73,10 @@ protected function captureFieldsDefinitions(): Collection $this->line('You now need to define the fields in your publication type:'); $fields = Collection::make(); - $fields->add([ + $fields->add(PublicationField::fromArray([ 'name' => '__createdAt', 'type' => PublicationFieldTypes::Datetime, - ]); + ])); $this->count++; do { @@ -104,7 +103,7 @@ protected function captureFieldsDefinitions(): Collection // map field choice to actual field type $fieldData['type'] = $type; - $fields->add($fieldData); + $fields->add(PublicationField::fromArray($fieldData)); $this->count++; } while ($addAnother); From 46673951cc233217abb0909974d6768a9b0cb9e4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:37:39 +0100 Subject: [PATCH 052/130] Remove label overrides for __createdAt meta field --- .../src/Console/Commands/MakePublicationTypeCommand.php | 4 +--- .../Feature/Commands/MakePublicationTypeCommandTest.php | 5 +++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 6ed7d7d776d..845aebf3325 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -192,10 +192,8 @@ protected function getPaginationSettings(Collection $fields): array protected function getSortField(Collection $fields): string { $options = $fields->pluck('name')->toArray(); - $options[0] = 'Date created (meta field)'; - $selected = $this->choice('Choose the default field you wish to sort by', $options, 'dateCreated (meta field)'); - return str_replace('Date created (meta field)', '__createdAt', $selected); + return $this->choice('Choose the default field you wish to sort by', $options, '__dateCreated'); } protected function getSortDirection(): bool diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 1daf103bff1..5392d7b48c2 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -48,8 +48,8 @@ public function test_command_creates_publication_type() 'Tag', ], true) ->expectsConfirmation('Add another field?', 'n') - ->expectsChoice('Choose the default field you wish to sort by', 'dateCreated (meta field)', [ - 'Date created (meta field)', + ->expectsChoice('Choose the default field you wish to sort by', '__created-at', [ + '__created-at', 'publication-title', ]) ->expectsChoice('Choose the default sort direction', 'Ascending (oldest items first if sorting by dateCreated)', [ @@ -59,6 +59,7 @@ public function test_command_creates_publication_type() ->expectsQuestion('Enter the pageSize (0 for no limit)', 10) ->expectsQuestion('Generate previous/next links in detail view?', 'n') ->expectsChoice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', 'publication-title', [ + '__created-at', 'publication-title', ]) ->expectsOutputToContain('Creating a new Publication Type!') From d8a484292855b8da375af69d1027298751c6753a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:38:14 +0100 Subject: [PATCH 053/130] Add option to not normalize the name --- .../Features/Publications/Models/PublicationField.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 88f7ef7f2e9..98b8aebb9d7 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -34,10 +34,10 @@ public static function fromArray(array $array): static return new static(...$array); } - public function __construct(PublicationFieldTypes|string $type, string $name, array $rules = []) + public function __construct(PublicationFieldTypes|string $type, string $name, array $rules = [], bool $normalizeName = true) { $this->type = $type instanceof PublicationFieldTypes ? $type : PublicationFieldTypes::from(strtolower($type)); - $this->name = Str::kebab($name); + $this->name = $normalizeName ? Str::kebab($name) : $name; $this->rules = $rules; } From 2812656608051293524e2f7790f868f4d9a176fb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:39:32 +0100 Subject: [PATCH 054/130] Don't normalize the __createdAt name --- .../src/Console/Commands/MakePublicationTypeCommand.php | 1 + .../Feature/Commands/MakePublicationTypeCommandTest.php | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 845aebf3325..45a3bba899b 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -76,6 +76,7 @@ protected function captureFieldsDefinitions(): Collection $fields->add(PublicationField::fromArray([ 'name' => '__createdAt', 'type' => PublicationFieldTypes::Datetime, + 'normalizeName' => false ])); $this->count++; diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 5392d7b48c2..1200cb950de 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -48,8 +48,8 @@ public function test_command_creates_publication_type() 'Tag', ], true) ->expectsConfirmation('Add another field?', 'n') - ->expectsChoice('Choose the default field you wish to sort by', '__created-at', [ - '__created-at', + ->expectsChoice('Choose the default field you wish to sort by', '__createdAt', [ + '__createdAt', 'publication-title', ]) ->expectsChoice('Choose the default sort direction', 'Ascending (oldest items first if sorting by dateCreated)', [ @@ -59,7 +59,7 @@ public function test_command_creates_publication_type() ->expectsQuestion('Enter the pageSize (0 for no limit)', 10) ->expectsQuestion('Generate previous/next links in detail view?', 'n') ->expectsChoice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', 'publication-title', [ - '__created-at', + '__createdAt', 'publication-title', ]) ->expectsOutputToContain('Creating a new Publication Type!') From 6eeac7e345642a6c24ceeea0b3308e033e694c85 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:41:55 +0100 Subject: [PATCH 055/130] Expect the meta field to be added --- .../tests/Feature/Commands/MakePublicationTypeCommandTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 1200cb950de..5dd2a7a7b49 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -82,6 +82,10 @@ public function test_command_creates_publication_type() "pageSize": 10 }, "fields": [ + { + "type": "datetime", + "name": "__createdAt" + }, { "type": "string", "name": "publication-title" From cf71ce54ca19a19f1e989716f9376d966af11971 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:49:18 +0100 Subject: [PATCH 056/130] Remove descriptors for sort directions --- .../src/Console/Commands/MakePublicationTypeCommand.php | 6 +++--- .../Feature/Commands/MakePublicationTypeCommandTest.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 45a3bba899b..fb7a5de0fe7 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -200,11 +200,11 @@ protected function getSortField(Collection $fields): string protected function getSortDirection(): bool { $options = [ - 'Ascending (oldest items first if sorting by dateCreated)' => true, - 'Descending (newest items first if sorting by dateCreated)' => false, + 'Ascending' => true, + 'Descending' => false, ]; - return $options[$this->choice('Choose the default sort direction', array_keys($options), 'Ascending (oldest items first if sorting by dateCreated)')]; + return $options[$this->choice('Choose the default sort direction', array_keys($options), 'Ascending')]; } protected function getPageSize(): int diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 5dd2a7a7b49..a7968bed919 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -52,9 +52,9 @@ public function test_command_creates_publication_type() '__createdAt', 'publication-title', ]) - ->expectsChoice('Choose the default sort direction', 'Ascending (oldest items first if sorting by dateCreated)', [ - 'Ascending (oldest items first if sorting by dateCreated)', - 'Descending (newest items first if sorting by dateCreated)', + ->expectsChoice('Choose the default sort direction', 'Ascending', [ + 'Ascending', + 'Descending', ]) ->expectsQuestion('Enter the pageSize (0 for no limit)', 10) ->expectsQuestion('Generate previous/next links in detail view?', 'n') From e35fc6ff5fdf5ee71083ff746dcf4cf07508e5ad Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 18:52:01 +0100 Subject: [PATCH 057/130] Ask if user wants to configure pagination settings --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- .../tests/Feature/Commands/MakePublicationTypeCommandTest.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index fb7a5de0fe7..a5d4f9ae6ce 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -180,7 +180,7 @@ protected function checkIfFieldIsDuplicate(Collection $fields, $name): bool protected function getPaginationSettings(Collection $fields): array { $paginationDefaults = ['sortField' => '__createdAt', 'sortAscending' => true, 'pageSize' => 25, 'prevNextLinks' => true]; - if ($this->option('use-defaults')) { + if ($this->option('use-defaults') || ! $this->confirm('Do you want to configure pagination settings?')) { return $paginationDefaults; } $sortField = $this->getSortField($fields); diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index a7968bed919..83a940ba707 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -48,6 +48,7 @@ public function test_command_creates_publication_type() 'Tag', ], true) ->expectsConfirmation('Add another field?', 'n') + ->expectsConfirmation('Do you want to configure pagination settings?', 'yes') ->expectsChoice('Choose the default field you wish to sort by', '__createdAt', [ '__createdAt', 'publication-title', @@ -117,6 +118,7 @@ public function test_with_multiple_fields_of_the_same_name() ->expectsChoice('Enter type for field #1', 'String', PublicationFieldTypes::collect()->pluck('name')->toArray()) ->expectsConfirmation('Add another field?', 'y') ->expectsQuestion('Enter name for field #2', 'foo') + ->expectsConfirmation('Do you want to configure pagination settings?', 'n') ->assertExitCode(0); } } From 4b7aec53eed503e5052f6d94ca1ab475c901f4b6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 19:04:13 +0100 Subject: [PATCH 058/130] Add count prefixes to ask messages as mockery seems completely unable to handle multiple expectations without utterly falling apart --- .../Commands/MakePublicationTypeCommand.php | 9 ++++++-- .../MakePublicationTypeCommandTest.php | 21 +++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index a5d4f9ae6ce..50578cab72d 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -85,7 +85,12 @@ protected function captureFieldsDefinitions(): Collection $fieldData = []; do { - $fieldData['name'] = Str::kebab(trim($this->askWithValidation('name', "Enter name for field #$this->count", ['required']))); + if ($duplicate ?? false) { + $tryMsg = 'Try again: '; + } else { + $tryMsg = ''; + } + $fieldData['name'] = Str::kebab(trim($this->askWithValidation('name', "{$tryMsg}Enter name for field #$this->count", ['required']))); $duplicate = $this->checkIfFieldIsDuplicate($fields, $fieldData['name']); } while ($duplicate); @@ -98,7 +103,7 @@ protected function captureFieldsDefinitions(): Collection if ($this->option('use-defaults') === true) { $addAnother = false; } else { - $addAnother = $this->confirm('Add another field?'); + $addAnother = $this->confirm("Field #$this->count added! Add another field?"); } // map field choice to actual field type diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 83a940ba707..920e8a12178 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -114,11 +114,20 @@ public function test_with_default_values() public function test_with_multiple_fields_of_the_same_name() { $this->artisan('make:publicationType "Test Publication"') - ->expectsQuestion('Enter name for field #1', 'foo') - ->expectsChoice('Enter type for field #1', 'String', PublicationFieldTypes::collect()->pluck('name')->toArray()) - ->expectsConfirmation('Add another field?', 'y') - ->expectsQuestion('Enter name for field #2', 'foo') - ->expectsConfirmation('Do you want to configure pagination settings?', 'n') - ->assertExitCode(0); + ->expectsQuestion('Enter name for field #1', 'foo') + ->expectsChoice('Enter type for field #1', 'String', []) + + ->expectsConfirmation('Field #1 added! Add another field?', 'yes') + + ->expectsQuestion('Enter name for field #2', 'foo') + ->expectsOutput('Field name [foo] already exists!') + ->expectsQuestion('Try again: Enter name for field #2', 'bar') + ->expectsChoice('Enter type for field #2', 'String', []) + + ->expectsConfirmation('Field #2 added! Add another field?', 'no') + + ->expectsConfirmation('Do you want to configure pagination settings?', 'n') + ->expectsChoice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', 'foo', []) + ->assertExitCode(0); } } From 5346f4e870d787234da359c17c172e56d9a56f23 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 19:16:34 +0100 Subject: [PATCH 059/130] Update MakePublicationTypeCommandTest.php --- .../Commands/MakePublicationTypeCommandTest.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 920e8a12178..6e66bac28c2 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -115,19 +115,23 @@ public function test_with_multiple_fields_of_the_same_name() { $this->artisan('make:publicationType "Test Publication"') ->expectsQuestion('Enter name for field #1', 'foo') - ->expectsChoice('Enter type for field #1', 'String', []) + ->expectsChoice('Enter type for field #1', 'String', PublicationFieldTypes::collect()->pluck('name')->toArray()) ->expectsConfirmation('Field #1 added! Add another field?', 'yes') ->expectsQuestion('Enter name for field #2', 'foo') ->expectsOutput('Field name [foo] already exists!') ->expectsQuestion('Try again: Enter name for field #2', 'bar') - ->expectsChoice('Enter type for field #2', 'String', []) + ->expectsChoice('Enter type for field #2', 'String', PublicationFieldTypes::collect()->pluck('name')->toArray()) ->expectsConfirmation('Field #2 added! Add another field?', 'no') ->expectsConfirmation('Do you want to configure pagination settings?', 'n') - ->expectsChoice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', 'foo', []) + ->expectsChoice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', 'foo', [ + '__createdAt', + 'bar', + 'foo', + ]) ->assertExitCode(0); } } From e2fcdc27ccef89e08a5f14e66ddea693751e3470 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 19:16:55 +0100 Subject: [PATCH 060/130] Update MakePublicationTypeCommandTest.php --- .../tests/Feature/Commands/MakePublicationTypeCommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 6e66bac28c2..8b4cc67a4de 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -47,7 +47,7 @@ public function test_command_creates_publication_type() 'Url', 'Tag', ], true) - ->expectsConfirmation('Add another field?', 'n') + ->expectsConfirmation('Field #1 added! Add another field?', 'n') ->expectsConfirmation('Do you want to configure pagination settings?', 'yes') ->expectsChoice('Choose the default field you wish to sort by', '__createdAt', [ '__createdAt', From c162debc5818f9a230990f4940a7515230dd1415 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 23 Dec 2022 18:17:06 +0000 Subject: [PATCH 061/130] Apply fixes from StyleCI --- .../Console/Commands/MakePublicationTypeCommand.php | 10 ++++------ .../Commands/MakePublicationTypeCommandTest.php | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 50578cab72d..1866e67f662 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -4,9 +4,7 @@ namespace Hyde\Console\Commands; -use function array_flip; use function array_keys; -use function array_merge; use function file_exists; use Hyde\Console\Concerns\ValidatingCommand; use Hyde\Framework\Actions\CreatesNewPublicationType; @@ -20,7 +18,6 @@ use function is_dir; use LaravelZero\Framework\Commands\Command; use function scandir; -use function str_replace; use function strtolower; use function trim; @@ -56,7 +53,7 @@ public function safeHandle(): int $fields = $this->captureFieldsDefinitions(); - list($sortField, $sortAscending, $pageSize, $prevNextLinks) = array_values($this->getPaginationSettings($fields)); + [$sortField, $sortAscending, $pageSize, $prevNextLinks] = array_values($this->getPaginationSettings($fields)); $canonicalField = $this->getCanonicalField($fields); @@ -76,7 +73,7 @@ protected function captureFieldsDefinitions(): Collection $fields->add(PublicationField::fromArray([ 'name' => '__createdAt', 'type' => PublicationFieldTypes::Datetime, - 'normalizeName' => false + 'normalizeName' => false, ])); $this->count++; @@ -143,7 +140,7 @@ protected function getCanonicalField(Collection $selectedFields): PublicationFie }); if ($this->option('use-defaults')) { - return $selectableFields->first(); + return $selectableFields->first(); } $options = $selectableFields->pluck('name'); @@ -192,6 +189,7 @@ protected function getPaginationSettings(Collection $fields): array $sortAscending = $this->getSortDirection(); $pageSize = $this->getPageSize(); $prevNextLinks = $this->getPrevNextLinks(); + return ['sortField' => $sortField, 'sortAscending' => $sortAscending, 'pageSize' => $pageSize, 'prevNextLinks' => $prevNextLinks]; } diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 8b4cc67a4de..e4f1be68171 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -4,9 +4,9 @@ namespace Hyde\Framework\Testing\Feature\Commands; -use Hyde\Framework\Features\Publications\PublicationFieldTypes; use function config; use Hyde\Facades\Filesystem; +use Hyde\Framework\Features\Publications\PublicationFieldTypes; use Hyde\Hyde; use Hyde\Testing\TestCase; From 800d09a2b1992761fd03a4f2540bbcba82379b2e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 20:46:35 +0100 Subject: [PATCH 062/130] Refactor to use class property instead of throwing around local variable --- .../Commands/MakePublicationTypeCommand.php | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 1866e67f662..5fb9cc073df 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -38,6 +38,8 @@ class MakePublicationTypeCommand extends ValidatingCommand protected $description = 'Create a new publication type definition'; protected int $count = 0; + protected Collection $fields; + public function safeHandle(): int { $this->title('Creating a new Publication Type!'); @@ -51,13 +53,13 @@ public function safeHandle(): int } } - $fields = $this->captureFieldsDefinitions(); + $this->fields = $this->captureFieldsDefinitions(); - [$sortField, $sortAscending, $pageSize, $prevNextLinks] = array_values($this->getPaginationSettings($fields)); + [$sortField, $sortAscending, $pageSize, $prevNextLinks] = array_values($this->getPaginationSettings()); - $canonicalField = $this->getCanonicalField($fields); + $canonicalField = $this->getCanonicalField($this->fields); - $creator = new CreatesNewPublicationType($title, $fields, $canonicalField->name, $sortField, $sortAscending, $prevNextLinks, $pageSize, $this->output); + $creator = new CreatesNewPublicationType($title, $this->fields, $canonicalField->name, $sortField, $sortAscending, $prevNextLinks, $pageSize, $this->output); $creator->create(); $this->info('Publication type created successfully!'); @@ -68,9 +70,9 @@ public function safeHandle(): int protected function captureFieldsDefinitions(): Collection { $this->line('You now need to define the fields in your publication type:'); - $fields = Collection::make(); + $this->fields = Collection::make(); - $fields->add(PublicationField::fromArray([ + $this->fields->add(PublicationField::fromArray([ 'name' => '__createdAt', 'type' => PublicationFieldTypes::Datetime, 'normalizeName' => false, @@ -88,7 +90,7 @@ protected function captureFieldsDefinitions(): Collection $tryMsg = ''; } $fieldData['name'] = Str::kebab(trim($this->askWithValidation('name', "{$tryMsg}Enter name for field #$this->count", ['required']))); - $duplicate = $this->checkIfFieldIsDuplicate($fields, $fieldData['name']); + $duplicate = $this->checkIfFieldIsDuplicate($fieldData['name']); } while ($duplicate); $type = $this->getFieldType(); @@ -106,11 +108,11 @@ protected function captureFieldsDefinitions(): Collection // map field choice to actual field type $fieldData['type'] = $type; - $fields->add(PublicationField::fromArray($fieldData)); + $this->fields->add(PublicationField::fromArray($fieldData)); $this->count++; } while ($addAnother); - return $fields; + return $this->fields; } protected function getFieldType(): PublicationFieldTypes @@ -169,9 +171,9 @@ protected function getFieldDataForTag(array $fieldData): array return $fieldData; } - protected function checkIfFieldIsDuplicate(Collection $fields, $name): bool + protected function checkIfFieldIsDuplicate($name): bool { - $duplicate = $fields->where('name', $name)->count(); + $duplicate = $this->fields->where('name', $name)->count(); if ($duplicate) { $this->error("Field name [$name] already exists!"); } @@ -179,13 +181,13 @@ protected function checkIfFieldIsDuplicate(Collection $fields, $name): bool return (bool) $duplicate; } - protected function getPaginationSettings(Collection $fields): array + protected function getPaginationSettings(): array { $paginationDefaults = ['sortField' => '__createdAt', 'sortAscending' => true, 'pageSize' => 25, 'prevNextLinks' => true]; if ($this->option('use-defaults') || ! $this->confirm('Do you want to configure pagination settings?')) { return $paginationDefaults; } - $sortField = $this->getSortField($fields); + $sortField = $this->getSortField(); $sortAscending = $this->getSortDirection(); $pageSize = $this->getPageSize(); $prevNextLinks = $this->getPrevNextLinks(); @@ -193,9 +195,9 @@ protected function getPaginationSettings(Collection $fields): array return ['sortField' => $sortField, 'sortAscending' => $sortAscending, 'pageSize' => $pageSize, 'prevNextLinks' => $prevNextLinks]; } - protected function getSortField(Collection $fields): string + protected function getSortField(): string { - $options = $fields->pluck('name')->toArray(); + $options = $this->fields->pluck('name')->toArray(); return $this->choice('Choose the default field you wish to sort by', $options, '__dateCreated'); } From 8d844b30a04df6140e8a043df8a27c825aefc15c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 20:46:52 +0100 Subject: [PATCH 063/130] Join comma-separated values into a single line --- .../Console/Commands/MakePublicationTypeCommand.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 5fb9cc073df..92897fbd5fa 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -117,18 +117,7 @@ protected function captureFieldsDefinitions(): Collection protected function getFieldType(): PublicationFieldTypes { - $options = [ - 'String', - 'Datetime', - 'Boolean', - 'Integer', - 'Float', - 'Image', - 'Array', - 'Text', - 'Url', - 'Tag', - ]; + $options = ['String', 'Datetime', 'Boolean', 'Integer', 'Float', 'Image', 'Array', 'Text', 'Url', 'Tag']; $choice = $this->choice("Enter type for field #$this->count", $options, 'String'); From 0b18dae15764cc392af38a351e87da9149ef6a85 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 20:49:59 +0100 Subject: [PATCH 064/130] Use the class property --- .../src/Console/Commands/MakePublicationTypeCommand.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 92897fbd5fa..876a3b43a66 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -57,7 +57,7 @@ public function safeHandle(): int [$sortField, $sortAscending, $pageSize, $prevNextLinks] = array_values($this->getPaginationSettings()); - $canonicalField = $this->getCanonicalField($this->fields); + $canonicalField = $this->getCanonicalField(); $creator = new CreatesNewPublicationType($title, $this->fields, $canonicalField->name, $sortField, $sortAscending, $prevNextLinks, $pageSize, $this->output); $creator->create(); @@ -124,9 +124,9 @@ protected function getFieldType(): PublicationFieldTypes return PublicationFieldTypes::from(strtolower($choice)); } - protected function getCanonicalField(Collection $selectedFields): PublicationField + protected function getCanonicalField(): PublicationField { - $selectableFields = $selectedFields->reject(function (PublicationField $field): bool { + $selectableFields = $this->fields->reject(function (PublicationField $field): bool { return in_array($field, PublicationFieldTypes::canonicable()); }); @@ -136,7 +136,7 @@ protected function getCanonicalField(Collection $selectedFields): PublicationFie $options = $selectableFields->pluck('name'); - return $selectedFields->firstWhere('name', + return $this->fields->firstWhere('name', $this->choice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', $options->toArray(), $options->first() From 99cfabba66bc423d31f58671076f119306e9f278 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 20:51:11 +0100 Subject: [PATCH 065/130] Simplify list return values --- .../src/Console/Commands/MakePublicationTypeCommand.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 876a3b43a66..0001768809b 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -55,7 +55,7 @@ public function safeHandle(): int $this->fields = $this->captureFieldsDefinitions(); - [$sortField, $sortAscending, $pageSize, $prevNextLinks] = array_values($this->getPaginationSettings()); + [$sortField, $sortAscending, $pageSize, $prevNextLinks] = ($this->getPaginationSettings()); $canonicalField = $this->getCanonicalField(); @@ -172,16 +172,15 @@ protected function checkIfFieldIsDuplicate($name): bool protected function getPaginationSettings(): array { - $paginationDefaults = ['sortField' => '__createdAt', 'sortAscending' => true, 'pageSize' => 25, 'prevNextLinks' => true]; if ($this->option('use-defaults') || ! $this->confirm('Do you want to configure pagination settings?')) { - return $paginationDefaults; + return ['__createdAt', true, 25, true]; } $sortField = $this->getSortField(); $sortAscending = $this->getSortDirection(); $pageSize = $this->getPageSize(); $prevNextLinks = $this->getPrevNextLinks(); - return ['sortField' => $sortField, 'sortAscending' => $sortAscending, 'pageSize' => $pageSize, 'prevNextLinks' => $prevNextLinks]; + return [$sortField, $sortAscending, $pageSize, $prevNextLinks]; } protected function getSortField(): string From 62d418af40b83a3512223f69ef6f2037bfb40cec Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 20:51:38 +0100 Subject: [PATCH 066/130] Split comma-separated values into multiple lines and inline variables --- .../Console/Commands/MakePublicationTypeCommand.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 0001768809b..32e6b90ab1a 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -175,12 +175,13 @@ protected function getPaginationSettings(): array if ($this->option('use-defaults') || ! $this->confirm('Do you want to configure pagination settings?')) { return ['__createdAt', true, 25, true]; } - $sortField = $this->getSortField(); - $sortAscending = $this->getSortDirection(); - $pageSize = $this->getPageSize(); - $prevNextLinks = $this->getPrevNextLinks(); - return [$sortField, $sortAscending, $pageSize, $prevNextLinks]; + return [ + $this->getSortField(), + $this->getSortDirection(), + $this->getPageSize(), + $this->getPrevNextLinks() + ]; } protected function getSortField(): string From 222965d6aece6864d7e94969a07637014be8b776 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 20:52:21 +0100 Subject: [PATCH 067/130] Invert 'if' statement --- .../Commands/MakePublicationTypeCommand.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 32e6b90ab1a..87527d1151e 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -172,16 +172,16 @@ protected function checkIfFieldIsDuplicate($name): bool protected function getPaginationSettings(): array { - if ($this->option('use-defaults') || ! $this->confirm('Do you want to configure pagination settings?')) { - return ['__createdAt', true, 25, true]; + if (!$this->option('use-defaults') && $this->confirm('Do you want to configure pagination settings?')) { + return [ + $this->getSortField(), + $this->getSortDirection(), + $this->getPageSize(), + $this->getPrevNextLinks() + ]; } - return [ - $this->getSortField(), - $this->getSortDirection(), - $this->getPageSize(), - $this->getPrevNextLinks() - ]; + return ['__createdAt', true, 25, true]; } protected function getSortField(): string From 9101a36b84f3631dc856bec57832a4c632c3a1eb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 20:52:24 +0100 Subject: [PATCH 068/130] Revert "Invert 'if' statement" This reverts commit 222965d6aece6864d7e94969a07637014be8b776. --- .../Commands/MakePublicationTypeCommand.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 87527d1151e..32e6b90ab1a 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -172,16 +172,16 @@ protected function checkIfFieldIsDuplicate($name): bool protected function getPaginationSettings(): array { - if (!$this->option('use-defaults') && $this->confirm('Do you want to configure pagination settings?')) { - return [ - $this->getSortField(), - $this->getSortDirection(), - $this->getPageSize(), - $this->getPrevNextLinks() - ]; + if ($this->option('use-defaults') || ! $this->confirm('Do you want to configure pagination settings?')) { + return ['__createdAt', true, 25, true]; } - return ['__createdAt', true, 25, true]; + return [ + $this->getSortField(), + $this->getSortDirection(), + $this->getPageSize(), + $this->getPrevNextLinks() + ]; } protected function getSortField(): string From 4315cefae4ca75ac290ddbfb327b73b6b8d4e31c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 20:52:34 +0100 Subject: [PATCH 069/130] Join comma-separated values into a single line --- .../src/Console/Commands/MakePublicationTypeCommand.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 32e6b90ab1a..2aa07acd040 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -176,12 +176,7 @@ protected function getPaginationSettings(): array return ['__createdAt', true, 25, true]; } - return [ - $this->getSortField(), - $this->getSortDirection(), - $this->getPageSize(), - $this->getPrevNextLinks() - ]; + return [$this->getSortField(), $this->getSortDirection(), $this->getPageSize(), $this->getPrevNextLinks()]; } protected function getSortField(): string From f4f4d111cf2df3648a62f7eb63b2f329a830300a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 20:53:00 +0100 Subject: [PATCH 070/130] Inline local variable --- .../src/Console/Commands/MakePublicationTypeCommand.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 2aa07acd040..56cdb772c3c 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -181,9 +181,7 @@ protected function getPaginationSettings(): array protected function getSortField(): string { - $options = $this->fields->pluck('name')->toArray(); - - return $this->choice('Choose the default field you wish to sort by', $options, '__dateCreated'); + return $this->choice('Choose the default field you wish to sort by', $this->fields->pluck('name')->toArray(), '__dateCreated'); } protected function getSortDirection(): bool From 20553143e5d95ca13b29543cc944e168013854aa Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 21:04:28 +0100 Subject: [PATCH 071/130] Use stricter comparison --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 56cdb772c3c..31ae92a8d02 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -163,7 +163,7 @@ protected function getFieldDataForTag(array $fieldData): array protected function checkIfFieldIsDuplicate($name): bool { $duplicate = $this->fields->where('name', $name)->count(); - if ($duplicate) { + if ($duplicate > 0) { $this->error("Field name [$name] already exists!"); } From 4ec96a51fab5be7c1a2b3cfcf05d183c55a05926 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 21:05:38 +0100 Subject: [PATCH 072/130] Simplify return statement and inline variable --- .../src/Console/Commands/MakePublicationTypeCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 31ae92a8d02..9d155caa6dc 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -162,12 +162,12 @@ protected function getFieldDataForTag(array $fieldData): array protected function checkIfFieldIsDuplicate($name): bool { - $duplicate = $this->fields->where('name', $name)->count(); - if ($duplicate > 0) { + if ($this->fields->where('name', $name)->count() > 0) { $this->error("Field name [$name] already exists!"); + return true; } - return (bool) $duplicate; + return false; } protected function getPaginationSettings(): array From 22505fde826d3022e08cd9ca6ec369e1ed21634b Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 23 Dec 2022 20:06:18 +0000 Subject: [PATCH 073/130] Apply fixes from StyleCI --- .../src/Console/Commands/MakePublicationTypeCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 9d155caa6dc..899c51e4f72 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -164,6 +164,7 @@ protected function checkIfFieldIsDuplicate($name): bool { if ($this->fields->where('name', $name)->count() > 0) { $this->error("Field name [$name] already exists!"); + return true; } From 589d83decdd6476916a6393fabf645ba2f4f845f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 21:09:21 +0100 Subject: [PATCH 074/130] Extract method --- .../Commands/MakePublicationTypeCommand.php | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 899c51e4f72..dc9423e8a45 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -83,15 +83,7 @@ protected function captureFieldsDefinitions(): Collection $this->line(''); $fieldData = []; - do { - if ($duplicate ?? false) { - $tryMsg = 'Try again: '; - } else { - $tryMsg = ''; - } - $fieldData['name'] = Str::kebab(trim($this->askWithValidation('name', "{$tryMsg}Enter name for field #$this->count", ['required']))); - $duplicate = $this->checkIfFieldIsDuplicate($fieldData['name']); - } while ($duplicate); + $fieldData['name'] = $this->getFieldName(); $type = $this->getFieldType(); @@ -115,6 +107,21 @@ protected function captureFieldsDefinitions(): Collection return $this->fields; } + protected function getFieldName(): string + { + do { + if ($duplicate ?? false) { + $tryMsg = 'Try again: '; + } else { + $tryMsg = ''; + } + $name = Str::kebab(trim($this->askWithValidation('name', "{$tryMsg}Enter name for field #$this->count", ['required']))); + $duplicate = $this->checkIfFieldIsDuplicate($name); + } while ($duplicate); + + return $name; + } + protected function getFieldType(): PublicationFieldTypes { $options = ['String', 'Datetime', 'Boolean', 'Integer', 'Float', 'Image', 'Array', 'Text', 'Url', 'Tag']; From 095f9abfe65193e17e832fd284b7648cc8a79fa5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 21:09:39 +0100 Subject: [PATCH 075/130] Rename local variable --- .../src/Console/Commands/MakePublicationTypeCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index dc9423e8a45..72985778fee 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -115,11 +115,11 @@ protected function getFieldName(): string } else { $tryMsg = ''; } - $name = Str::kebab(trim($this->askWithValidation('name', "{$tryMsg}Enter name for field #$this->count", ['required']))); - $duplicate = $this->checkIfFieldIsDuplicate($name); + $selected = Str::kebab(trim($this->askWithValidation('name', "{$tryMsg}Enter name for field #$this->count", ['required']))); + $duplicate = $this->checkIfFieldIsDuplicate($selected); } while ($duplicate); - return $name; + return $selected; } protected function getFieldType(): PublicationFieldTypes From 32d26f7013abc6a7425176c291f02a95836a9293 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 21:10:54 +0100 Subject: [PATCH 076/130] Initialize variable --- .../src/Console/Commands/MakePublicationTypeCommand.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 72985778fee..99776fd8a14 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -109,8 +109,9 @@ protected function captureFieldsDefinitions(): Collection protected function getFieldName(): string { + $duplicate = false; do { - if ($duplicate ?? false) { + if ($duplicate) { $tryMsg = 'Try again: '; } else { $tryMsg = ''; From a0106ee276240a937cdf0b7327d1c35f9d892537 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 21:11:02 +0100 Subject: [PATCH 077/130] Simplify 'if' --- .../src/Console/Commands/MakePublicationTypeCommand.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 99776fd8a14..ed568f7de89 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -111,11 +111,7 @@ protected function getFieldName(): string { $duplicate = false; do { - if ($duplicate) { - $tryMsg = 'Try again: '; - } else { - $tryMsg = ''; - } + $tryMsg = $duplicate ? 'Try again: ' : ''; $selected = Str::kebab(trim($this->askWithValidation('name', "{$tryMsg}Enter name for field #$this->count", ['required']))); $duplicate = $this->checkIfFieldIsDuplicate($selected); } while ($duplicate); From dcc5c6b999960e42e9154ec05330c46470219db0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 21:11:15 +0100 Subject: [PATCH 078/130] Inline variable --- .../src/Console/Commands/MakePublicationTypeCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index ed568f7de89..6cf95258b9e 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -111,8 +111,7 @@ protected function getFieldName(): string { $duplicate = false; do { - $tryMsg = $duplicate ? 'Try again: ' : ''; - $selected = Str::kebab(trim($this->askWithValidation('name', "{$tryMsg}Enter name for field #$this->count", ['required']))); + $selected = Str::kebab(trim($this->askWithValidation('name', ($duplicate ? 'Try again: ' : '')."Enter name for field #$this->count", ['required']))); $duplicate = $this->checkIfFieldIsDuplicate($selected); } while ($duplicate); From dbe44840e2c6709b78c5353d7edbb77b6d77e0d8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 21:12:43 +0100 Subject: [PATCH 079/130] Convert concatenation to 'sprintf()' call --- .../src/Console/Commands/MakePublicationTypeCommand.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 6cf95258b9e..b998f2a53e7 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -111,7 +111,11 @@ protected function getFieldName(): string { $duplicate = false; do { - $selected = Str::kebab(trim($this->askWithValidation('name', ($duplicate ? 'Try again: ' : '')."Enter name for field #$this->count", ['required']))); + $selected = Str::kebab(trim($this->askWithValidation('name', + sprintf("%sEnter name for field #%d", + $duplicate ? 'Try again: ' : '', $this->count + ), ['required']))); + $duplicate = $this->checkIfFieldIsDuplicate($selected); } while ($duplicate); From a3198b707fae920aee9673ffe05b3bea29f3e17c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 21:14:30 +0100 Subject: [PATCH 080/130] Break out message assembly --- .../src/Console/Commands/MakePublicationTypeCommand.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index b998f2a53e7..63a4b1ca3c1 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -111,11 +111,10 @@ protected function getFieldName(): string { $duplicate = false; do { - $selected = Str::kebab(trim($this->askWithValidation('name', - sprintf("%sEnter name for field #%d", - $duplicate ? 'Try again: ' : '', $this->count - ), ['required']))); - + $message = $duplicate + ? "Try again: Enter name for field #$this->count" + : "Enter name for field #$this->count"; + $selected = Str::kebab(trim($this->askWithValidation('name', $message, ['required']))); $duplicate = $this->checkIfFieldIsDuplicate($selected); } while ($duplicate); From 369f8ddedf7de3ac07831232a2435419441ad9e8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 21:19:49 +0100 Subject: [PATCH 081/130] Replace do while loop with recursion --- .../Commands/MakePublicationTypeCommand.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 63a4b1ca3c1..c56aaae4ad0 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -107,16 +107,17 @@ protected function captureFieldsDefinitions(): Collection return $this->fields; } - protected function getFieldName(): string + protected function getFieldName(bool $duplicate = false): string { - $duplicate = false; - do { - $message = $duplicate - ? "Try again: Enter name for field #$this->count" - : "Enter name for field #$this->count"; - $selected = Str::kebab(trim($this->askWithValidation('name', $message, ['required']))); - $duplicate = $this->checkIfFieldIsDuplicate($selected); - } while ($duplicate); + $message = $duplicate + ? "Try again: Enter name for field #$this->count" + : "Enter name for field #$this->count"; + + $selected = Str::kebab(trim($this->askWithValidation('name', $message, ['required']))); + + if ($this->checkIfFieldIsDuplicate($selected)) { + return $this->getFieldName(true); + } return $selected; } From ce0363c3cb2313c93250dac344535a1894c15428 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 21:36:50 +0100 Subject: [PATCH 082/130] Use the notIn Laravel validation rule --- .../Console/Commands/MakePublicationTypeCommand.php | 12 ++++++------ .../Commands/MakePublicationTypeCommandTest.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index c56aaae4ad0..011169b923a 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -4,6 +4,7 @@ namespace Hyde\Console\Commands; +use Illuminate\Validation\Rule; use function array_keys; use function file_exists; use Hyde\Console\Concerns\ValidatingCommand; @@ -112,14 +113,13 @@ protected function getFieldName(bool $duplicate = false): string $message = $duplicate ? "Try again: Enter name for field #$this->count" : "Enter name for field #$this->count"; - - $selected = Str::kebab(trim($this->askWithValidation('name', $message, ['required']))); - if ($this->checkIfFieldIsDuplicate($selected)) { - return $this->getFieldName(true); + $used = $this->fields->pluck('name')->toArray(); + $validateDuplicates = count($used) > 0; + if ($validateDuplicates) { + $rules = ['required', Rule::notIn($used)]; } - - return $selected; + return Str::kebab(trim($this->askWithValidation('name', $message, $rules ?? ['required']))); } protected function getFieldType(): PublicationFieldTypes diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index e4f1be68171..15a3dd0fb8b 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -121,7 +121,7 @@ public function test_with_multiple_fields_of_the_same_name() ->expectsQuestion('Enter name for field #2', 'foo') ->expectsOutput('Field name [foo] already exists!') - ->expectsQuestion('Try again: Enter name for field #2', 'bar') + ->expectsQuestion('Enter name for field #2', 'bar') ->expectsChoice('Enter type for field #2', 'String', PublicationFieldTypes::collect()->pluck('name')->toArray()) ->expectsConfirmation('Field #2 added! Add another field?', 'no') From 25a77ba386f469ff8a398866d653cbc75741aff4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 21:36:53 +0100 Subject: [PATCH 083/130] Revert "Use the notIn Laravel validation rule" This reverts commit ce0363c3cb2313c93250dac344535a1894c15428. --- .../Console/Commands/MakePublicationTypeCommand.php | 12 ++++++------ .../Commands/MakePublicationTypeCommandTest.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 011169b923a..c56aaae4ad0 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -4,7 +4,6 @@ namespace Hyde\Console\Commands; -use Illuminate\Validation\Rule; use function array_keys; use function file_exists; use Hyde\Console\Concerns\ValidatingCommand; @@ -113,13 +112,14 @@ protected function getFieldName(bool $duplicate = false): string $message = $duplicate ? "Try again: Enter name for field #$this->count" : "Enter name for field #$this->count"; + + $selected = Str::kebab(trim($this->askWithValidation('name', $message, ['required']))); - $used = $this->fields->pluck('name')->toArray(); - $validateDuplicates = count($used) > 0; - if ($validateDuplicates) { - $rules = ['required', Rule::notIn($used)]; + if ($this->checkIfFieldIsDuplicate($selected)) { + return $this->getFieldName(true); } - return Str::kebab(trim($this->askWithValidation('name', $message, $rules ?? ['required']))); + + return $selected; } protected function getFieldType(): PublicationFieldTypes diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 15a3dd0fb8b..e4f1be68171 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -121,7 +121,7 @@ public function test_with_multiple_fields_of_the_same_name() ->expectsQuestion('Enter name for field #2', 'foo') ->expectsOutput('Field name [foo] already exists!') - ->expectsQuestion('Enter name for field #2', 'bar') + ->expectsQuestion('Try again: Enter name for field #2', 'bar') ->expectsChoice('Enter type for field #2', 'String', PublicationFieldTypes::collect()->pluck('name')->toArray()) ->expectsConfirmation('Field #2 added! Add another field?', 'no') From 99f5763592a68e5549f4f5847c092c31709c6b83 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 21:38:21 +0100 Subject: [PATCH 084/130] Merge dynamic message handler with state boolean --- .../Console/Commands/MakePublicationTypeCommand.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index c56aaae4ad0..2dcd94b9038 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -107,16 +107,12 @@ protected function captureFieldsDefinitions(): Collection return $this->fields; } - protected function getFieldName(bool $duplicate = false): string + protected function getFieldName(?string $message = null): string { - $message = $duplicate - ? "Try again: Enter name for field #$this->count" - : "Enter name for field #$this->count"; - - $selected = Str::kebab(trim($this->askWithValidation('name', $message, ['required']))); + $selected = Str::kebab(trim($this->askWithValidation('name', $message ?? "Enter name for field #$this->count", ['required']))); if ($this->checkIfFieldIsDuplicate($selected)) { - return $this->getFieldName(true); + return $this->getFieldName("Try again: Enter name for field #$this->count"); } return $selected; From b5acbe711080b56b97d6fdf14210e55a2fc81ab5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 22:26:58 +0100 Subject: [PATCH 085/130] Remove unused tag logic and display a tip instead --- .../Commands/MakePublicationTypeCommand.php | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 2dcd94b9038..ee8b4fe65e5 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -88,7 +88,7 @@ protected function captureFieldsDefinitions(): Collection $type = $this->getFieldType(); if ($type === PublicationFieldTypes::Tag) { - $fieldData = $this->getFieldDataForTag($fieldData); + $this->comment('Tip: Hyde will look for tags matching the name of the publication!'); } if ($this->option('use-defaults') === true) { @@ -146,23 +146,6 @@ protected function getCanonicalField(): PublicationField )); } - protected function getFieldDataForTag(array $fieldData): array - { - $allTags = PublicationService::getAllTags(); - $offset = 1; - foreach ($allTags as $k => $v) { - $this->line(" $offset - $k"); - $offset++; - } - $offset--; // The above loop overcounts by 1 - $selected = $this->askWithValidation('tagGroup', 'Tag Group', ['required', 'integer', "between:1,$offset"], 0); - $fieldData['tagGroup'] = $allTags->keys()->{$selected - 1}; - $fieldData['min'] = 0; - $fieldData['max'] = 0; - - return $fieldData; - } - protected function checkIfFieldIsDuplicate($name): bool { if ($this->fields->where('name', $name)->count() > 0) { From f20dd8851e0db80e0e1d906c8ef99baf0ecedd16 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 23 Dec 2022 21:29:16 +0000 Subject: [PATCH 086/130] Apply fixes from StyleCI --- .../src/Console/Commands/MakePublicationTypeCommand.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index ee8b4fe65e5..a10f7fcdf97 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -10,7 +10,6 @@ use Hyde\Framework\Actions\CreatesNewPublicationType; use Hyde\Framework\Features\Publications\Models\PublicationField; use Hyde\Framework\Features\Publications\PublicationFieldTypes; -use Hyde\Framework\Features\Publications\PublicationService; use Illuminate\Support\Collection; use Illuminate\Support\Str; use function in_array; From ad34e53e8a5c6ed428603debceb00e3b22f137b8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 22:29:44 +0100 Subject: [PATCH 087/130] Remove code comment --- .../src/Console/Commands/MakePublicationTypeCommand.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index a10f7fcdf97..51a516ad0c8 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -96,7 +96,6 @@ protected function captureFieldsDefinitions(): Collection $addAnother = $this->confirm("Field #$this->count added! Add another field?"); } - // map field choice to actual field type $fieldData['type'] = $type; $this->fields->add(PublicationField::fromArray($fieldData)); From 614604f40e548b41cdfb38a97a45d25c5ef02aef Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 23 Dec 2022 22:30:28 +0100 Subject: [PATCH 088/130] Introduce local variable --- .../Console/Commands/MakePublicationTypeCommand.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 51a516ad0c8..fc0afc875d6 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -137,11 +137,12 @@ protected function getCanonicalField(): PublicationField $options = $selectableFields->pluck('name'); - return $this->fields->firstWhere('name', - $this->choice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', - $options->toArray(), - $options->first() - )); + $selected = $this->choice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', + $options->toArray(), + $options->first() + ); + + return $this->fields->firstWhere('name', $selected); } protected function checkIfFieldIsDuplicate($name): bool From fc5f9c61f60eaa32ce303c9ec04224fbed7d7503 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 10:28:27 +0100 Subject: [PATCH 089/130] Re-sort enum cases for ease of access --- .../Publications/PublicationFieldTypes.php | 20 +++++++++---------- .../Feature/PublicationFieldTypesEnumTest.php | 20 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index e2d49ca0dab..af180cbe3e8 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -14,16 +14,16 @@ */ enum PublicationFieldTypes: string { - case Array = 'array'; - case Boolean = 'boolean'; + case String = 'string'; case Datetime = 'datetime'; + case Boolean = 'boolean'; + case Integer = 'integer'; case Float = 'float'; case Image = 'image'; - case Integer = 'integer'; - case String = 'string'; - case Tag = 'tag'; + case Array = 'array'; case Text = 'text'; case Url = 'url'; + case Tag = 'tag'; public function rules(): array { @@ -44,16 +44,16 @@ public static function getRules(self $type): array { /** @noinspection PhpDuplicateMatchArmBodyInspection */ return match ($type) { - self::Array => ['array'], - self::Boolean => ['boolean'], + self::String => ['string'], self::Datetime => ['date'], + self::Boolean => ['boolean'], + self::Integer => ['integer', 'numeric'], self::Float => ['numeric'], self::Image => [], - self::Integer => ['integer', 'numeric'], - self::String => ['string'], - self::Tag => [], + self::Array => ['array'], self::Text => ['string'], self::Url => ['url'], + self::Tag => [], }; } diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index f8db901a01c..b1bf708eb35 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -50,16 +50,16 @@ public function testCollectCreatesCollectionOfCases() public function testValuesReturnsArrayOfCaseValues() { $this->assertSame([ - 0 => 'array', - 1 => 'boolean', - 2 => 'datetime', - 3 => 'float', - 4 => 'image', - 5 => 'integer', - 6 => 'string', - 7 => 'tag', - 8 => 'text', - 9 => 'url', + 0 => 'string', + 1 => 'datetime', + 2 => 'boolean', + 3 => 'integer', + 4 => 'float', + 5 => 'image', + 6 => 'array', + 7 => 'text', + 8 => 'url', + 9 => 'tag', ], PublicationFieldTypes::values()); } From 9a90f50194b3b9594e3d6792efa54aeecb95dd45 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 10:30:52 +0100 Subject: [PATCH 090/130] Create method to get the enum names --- .../Commands/MakePublicationTypeCommand.php | 2 +- .../Publications/PublicationFieldTypes.php | 5 +++++ .../Commands/MakePublicationTypeCommandTest.php | 6 +++--- .../Feature/PublicationFieldTypesEnumTest.php | 16 ++++++++++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index fc0afc875d6..bf4413af0b8 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -118,7 +118,7 @@ protected function getFieldName(?string $message = null): string protected function getFieldType(): PublicationFieldTypes { - $options = ['String', 'Datetime', 'Boolean', 'Integer', 'Float', 'Image', 'Array', 'Text', 'Url', 'Tag']; + $options = PublicationFieldTypes::names(); $choice = $this->choice("Enter type for field #$this->count", $options, 'String'); diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index af180cbe3e8..2642c83cac2 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -40,6 +40,11 @@ public static function values(): array return self::collect()->pluck('value')->toArray(); } + public static function names(): array + { + return self::collect()->pluck('name')->toArray(); + } + public static function getRules(self $type): array { /** @noinspection PhpDuplicateMatchArmBodyInspection */ diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index e4f1be68171..e8cae24850a 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -105,7 +105,7 @@ public function test_with_default_values() $this->artisan('make:publicationType --use-defaults') ->expectsQuestion('Publication type name', 'Test Publication') ->expectsQuestion('Enter name for field #1', 'foo') - ->expectsChoice('Enter type for field #1', 'String', PublicationFieldTypes::collect()->pluck('name')->toArray()) + ->expectsChoice('Enter type for field #1', 'String', PublicationFieldTypes::names()) ->expectsOutput('Saving publication data to [test-publication/schema.json]') ->expectsOutput('Publication type created successfully!') ->assertExitCode(0); @@ -115,14 +115,14 @@ public function test_with_multiple_fields_of_the_same_name() { $this->artisan('make:publicationType "Test Publication"') ->expectsQuestion('Enter name for field #1', 'foo') - ->expectsChoice('Enter type for field #1', 'String', PublicationFieldTypes::collect()->pluck('name')->toArray()) + ->expectsChoice('Enter type for field #1', 'String', PublicationFieldTypes::names()) ->expectsConfirmation('Field #1 added! Add another field?', 'yes') ->expectsQuestion('Enter name for field #2', 'foo') ->expectsOutput('Field name [foo] already exists!') ->expectsQuestion('Try again: Enter name for field #2', 'bar') - ->expectsChoice('Enter type for field #2', 'String', PublicationFieldTypes::collect()->pluck('name')->toArray()) + ->expectsChoice('Enter type for field #2', 'String', PublicationFieldTypes::names()) ->expectsConfirmation('Field #2 added! Add another field?', 'no') diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index b1bf708eb35..365633691d3 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -63,6 +63,22 @@ public function testValuesReturnsArrayOfCaseValues() ], PublicationFieldTypes::values()); } + public function testNamesReturnsArrayOfCaseNames() + { + $this->assertSame([ + 0 => 'String', + 1 => 'Datetime', + 2 => 'Boolean', + 3 => 'Integer', + 4 => 'Float', + 5 => 'Image', + 6 => 'Array', + 7 => 'Text', + 8 => 'Url', + 9 => 'Tag', + ], PublicationFieldTypes::names()); + } + public function testCanonicable() { $this->assertSame([ From 0b74421c94d6de9ab1600f27076734a5324254fe Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 10:33:16 +0100 Subject: [PATCH 091/130] Revert "Add option to not normalize the name" This reverts commit d8a484292855b8da375af69d1027298751c6753a. --- .../src/Console/Commands/MakePublicationTypeCommand.php | 1 - .../Features/Publications/Models/PublicationField.php | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index bf4413af0b8..16684c35a19 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -74,7 +74,6 @@ protected function captureFieldsDefinitions(): Collection $this->fields->add(PublicationField::fromArray([ 'name' => '__createdAt', 'type' => PublicationFieldTypes::Datetime, - 'normalizeName' => false, ])); $this->count++; diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 98b8aebb9d7..88f7ef7f2e9 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -34,10 +34,10 @@ public static function fromArray(array $array): static return new static(...$array); } - public function __construct(PublicationFieldTypes|string $type, string $name, array $rules = [], bool $normalizeName = true) + public function __construct(PublicationFieldTypes|string $type, string $name, array $rules = []) { $this->type = $type instanceof PublicationFieldTypes ? $type : PublicationFieldTypes::from(strtolower($type)); - $this->name = $normalizeName ? Str::kebab($name) : $name; + $this->name = Str::kebab($name); $this->rules = $rules; } From 17074621eb4d2b015ff4234ac4f0fe215264b226 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 10:35:20 +0100 Subject: [PATCH 092/130] Don't normalize meta field names --- .../Features/Publications/Models/PublicationField.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php index 88f7ef7f2e9..6a0ef005f51 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationField.php @@ -13,6 +13,7 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Collection; use Illuminate\Support\Str; +use function str_starts_with; use function strtolower; /** @@ -37,7 +38,7 @@ public static function fromArray(array $array): static public function __construct(PublicationFieldTypes|string $type, string $name, array $rules = []) { $this->type = $type instanceof PublicationFieldTypes ? $type : PublicationFieldTypes::from(strtolower($type)); - $this->name = Str::kebab($name); + $this->name = str_starts_with($name, '__') ? $name : Str::kebab($name); $this->rules = $rules; } From 12359222314978fe2440d97c7713c36eace7b4d5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 10:39:31 +0100 Subject: [PATCH 093/130] Extract method --- .../Commands/MakePublicationTypeCommand.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 16684c35a19..1f4dd969ad5 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -71,11 +71,7 @@ protected function captureFieldsDefinitions(): Collection $this->line('You now need to define the fields in your publication type:'); $this->fields = Collection::make(); - $this->fields->add(PublicationField::fromArray([ - 'name' => '__createdAt', - 'type' => PublicationFieldTypes::Datetime, - ])); - $this->count++; + $this->addCreatedAtMetaField(); do { $this->line(''); @@ -193,4 +189,15 @@ protected function getPrevNextLinks(): bool { return $this->confirm('Generate previous/next links in detail view?', true); } + + protected function addCreatedAtMetaField(): void + { + $this->fields->add( + PublicationField::fromArray([ + 'name' => '__createdAt', + 'type' => PublicationFieldTypes::Datetime, + ]) + ); + $this->count++; + } } From 7aec7fcf41c47978847ba18294a5936c0c4fb7cd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 10:41:05 +0100 Subject: [PATCH 094/130] Ask to create another after the first one has been added --- .../src/Console/Commands/MakePublicationTypeCommand.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 1f4dd969ad5..87a2026ce3e 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -85,15 +85,16 @@ protected function captureFieldsDefinitions(): Collection $this->comment('Tip: Hyde will look for tags matching the name of the publication!'); } + $fieldData['type'] = $type; + + $this->fields->add(PublicationField::fromArray($fieldData)); + if ($this->option('use-defaults') === true) { $addAnother = false; } else { $addAnother = $this->confirm("Field #$this->count added! Add another field?"); } - $fieldData['type'] = $type; - - $this->fields->add(PublicationField::fromArray($fieldData)); $this->count++; } while ($addAnother); From 6fa612b945b485b7bdf968eecd89c2c61ca77716 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 10:41:44 +0100 Subject: [PATCH 095/130] Add marker --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 87a2026ce3e..2fcc6d3fe36 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -87,6 +87,8 @@ protected function captureFieldsDefinitions(): Collection $fieldData['type'] = $type; + // TODO: Here we could collect other data like the "rules" array for the field. + $this->fields->add(PublicationField::fromArray($fieldData)); if ($this->option('use-defaults') === true) { From 4cc59b8f4281777244362a8a330d4fc4db843bb4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 10:44:14 +0100 Subject: [PATCH 096/130] Split out local array variable --- .../src/Console/Commands/MakePublicationTypeCommand.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 2fcc6d3fe36..058fbc628e5 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -76,8 +76,7 @@ protected function captureFieldsDefinitions(): Collection do { $this->line(''); - $fieldData = []; - $fieldData['name'] = $this->getFieldName(); + $fieldName = $this->getFieldName(); $type = $this->getFieldType(); @@ -85,11 +84,11 @@ protected function captureFieldsDefinitions(): Collection $this->comment('Tip: Hyde will look for tags matching the name of the publication!'); } - $fieldData['type'] = $type; + $fieldType = $type; // TODO: Here we could collect other data like the "rules" array for the field. - $this->fields->add(PublicationField::fromArray($fieldData)); + $this->fields->add(new PublicationField($fieldType, $fieldName)); if ($this->option('use-defaults') === true) { $addAnother = false; From 80880685c7a13293bfc8fd60795457bc0a85b8b5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 10:44:53 +0100 Subject: [PATCH 097/130] Merge local variables --- .../src/Console/Commands/MakePublicationTypeCommand.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 058fbc628e5..7c41aa2efa2 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -78,14 +78,12 @@ protected function captureFieldsDefinitions(): Collection $fieldName = $this->getFieldName(); - $type = $this->getFieldType(); + $fieldType = $this->getFieldType(); - if ($type === PublicationFieldTypes::Tag) { + if ($fieldType === PublicationFieldTypes::Tag) { $this->comment('Tip: Hyde will look for tags matching the name of the publication!'); } - $fieldType = $type; - // TODO: Here we could collect other data like the "rules" array for the field. $this->fields->add(new PublicationField($fieldType, $fieldName)); From eeecfd4a496d5ad74f7725d6e576cea2156572c6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 10:50:16 +0100 Subject: [PATCH 098/130] Extract method --- .../Commands/MakePublicationTypeCommand.php | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 7c41aa2efa2..578cc92bd7c 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -74,19 +74,7 @@ protected function captureFieldsDefinitions(): Collection $this->addCreatedAtMetaField(); do { - $this->line(''); - - $fieldName = $this->getFieldName(); - - $fieldType = $this->getFieldType(); - - if ($fieldType === PublicationFieldTypes::Tag) { - $this->comment('Tip: Hyde will look for tags matching the name of the publication!'); - } - - // TODO: Here we could collect other data like the "rules" array for the field. - - $this->fields->add(new PublicationField($fieldType, $fieldName)); + $this->fields->add($this->captureFieldDefinition()); if ($this->option('use-defaults') === true) { $addAnother = false; @@ -100,6 +88,23 @@ protected function captureFieldsDefinitions(): Collection return $this->fields; } + protected function captureFieldDefinition(): PublicationField + { + $this->line(''); + + $fieldName = $this->getFieldName(); + + $fieldType = $this->getFieldType(); + + if ($fieldType === PublicationFieldTypes::Tag) { + $this->comment('Tip: Hyde will look for tags matching the name of the publication!'); + } + + // TODO: Here we could collect other data like the "rules" array for the field. + + return (new PublicationField($fieldType, $fieldName)); + } + protected function getFieldName(?string $message = null): string { $selected = Str::kebab(trim($this->askWithValidation('name', $message ?? "Enter name for field #$this->count", ['required']))); From c59883ae40112d572a73a432a6d1563626f3493d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 10:50:43 +0100 Subject: [PATCH 099/130] Move up helper method --- .../Commands/MakePublicationTypeCommand.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 578cc92bd7c..8552ca7b018 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -156,6 +156,17 @@ protected function checkIfFieldIsDuplicate($name): bool return false; } + protected function addCreatedAtMetaField(): void + { + $this->fields->add( + PublicationField::fromArray([ + 'name' => '__createdAt', + 'type' => PublicationFieldTypes::Datetime, + ]) + ); + $this->count++; + } + protected function getPaginationSettings(): array { if ($this->option('use-defaults') || ! $this->confirm('Do you want to configure pagination settings?')) { @@ -194,15 +205,4 @@ protected function getPrevNextLinks(): bool { return $this->confirm('Generate previous/next links in detail view?', true); } - - protected function addCreatedAtMetaField(): void - { - $this->fields->add( - PublicationField::fromArray([ - 'name' => '__createdAt', - 'type' => PublicationFieldTypes::Datetime, - ]) - ); - $this->count++; - } } From d9c49b8bb7f1fb6fb63eae5f442a50ba0d8d8f0d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 10:54:11 +0100 Subject: [PATCH 100/130] Simplify object construction --- .../src/Console/Commands/MakePublicationTypeCommand.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 8552ca7b018..8b2de65f8d8 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -158,12 +158,7 @@ protected function checkIfFieldIsDuplicate($name): bool protected function addCreatedAtMetaField(): void { - $this->fields->add( - PublicationField::fromArray([ - 'name' => '__createdAt', - 'type' => PublicationFieldTypes::Datetime, - ]) - ); + $this->fields->add(new PublicationField(PublicationFieldTypes::Datetime, '__createdAt')); $this->count++; } From 23436fe42d6880cf630914c34e2d6825d855954d Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 24 Dec 2022 09:57:19 +0000 Subject: [PATCH 101/130] Apply fixes from StyleCI --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 8b2de65f8d8..76ad24751b1 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -102,7 +102,7 @@ protected function captureFieldDefinition(): PublicationField // TODO: Here we could collect other data like the "rules" array for the field. - return (new PublicationField($fieldType, $fieldName)); + return new PublicationField($fieldType, $fieldName); } protected function getFieldName(?string $message = null): string From bcf0c158519aa838d3c733dbaabeeffdc24a1809 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 10:57:46 +0100 Subject: [PATCH 102/130] Add method to count added items --- .../src/Console/Commands/MakePublicationTypeCommand.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 76ad24751b1..ffb75b48f6c 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -200,4 +200,9 @@ protected function getPrevNextLinks(): bool { return $this->confirm('Generate previous/next links in detail view?', true); } + + protected function getCount(): int + { + return $this->fields->count(); + } } From 1586164190fefeb67475bff2483716362169a19b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:02:33 +0100 Subject: [PATCH 103/130] Refactor to use the actual data count instead of class property --- .../Console/Commands/MakePublicationTypeCommand.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index ffb75b48f6c..3f22d8e5520 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -35,7 +35,6 @@ class MakePublicationTypeCommand extends ValidatingCommand /** @var string */ protected $description = 'Create a new publication type definition'; - protected int $count = 0; protected Collection $fields; @@ -79,10 +78,9 @@ protected function captureFieldsDefinitions(): Collection if ($this->option('use-defaults') === true) { $addAnother = false; } else { - $addAnother = $this->confirm("Field #$this->count added! Add another field?"); + $addAnother = $this->confirm("Field #".($this->getCount() - 1)." added! Add another field?"); } - $this->count++; } while ($addAnother); return $this->fields; @@ -107,10 +105,10 @@ protected function captureFieldDefinition(): PublicationField protected function getFieldName(?string $message = null): string { - $selected = Str::kebab(trim($this->askWithValidation('name', $message ?? "Enter name for field #$this->count", ['required']))); + $selected = Str::kebab(trim($this->askWithValidation('name', $message ?? "Enter name for field #{$this->getCount()}", ['required']))); if ($this->checkIfFieldIsDuplicate($selected)) { - return $this->getFieldName("Try again: Enter name for field #$this->count"); + return $this->getFieldName("Try again: Enter name for field #{$this->getCount()}"); } return $selected; @@ -120,7 +118,7 @@ protected function getFieldType(): PublicationFieldTypes { $options = PublicationFieldTypes::names(); - $choice = $this->choice("Enter type for field #$this->count", $options, 'String'); + $choice = $this->choice("Enter type for field #{$this->getCount()}", $options, 'String'); return PublicationFieldTypes::from(strtolower($choice)); } @@ -159,7 +157,6 @@ protected function checkIfFieldIsDuplicate($name): bool protected function addCreatedAtMetaField(): void { $this->fields->add(new PublicationField(PublicationFieldTypes::Datetime, '__createdAt')); - $this->count++; } protected function getPaginationSettings(): array From 80bd4bcec90613565acd482c29d69234289ee3ba Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:03:22 +0100 Subject: [PATCH 104/130] Add offset parameter to support string interpolation --- .../src/Console/Commands/MakePublicationTypeCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 3f22d8e5520..ab9573cfd9d 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -78,7 +78,7 @@ protected function captureFieldsDefinitions(): Collection if ($this->option('use-defaults') === true) { $addAnother = false; } else { - $addAnother = $this->confirm("Field #".($this->getCount() - 1)." added! Add another field?"); + $addAnother = $this->confirm("Field #{$this->getCount(-1)} added! Add another field?"); } } while ($addAnother); @@ -198,8 +198,8 @@ protected function getPrevNextLinks(): bool return $this->confirm('Generate previous/next links in detail view?', true); } - protected function getCount(): int + protected function getCount(int $offset = 0): int { - return $this->fields->count(); + return $this->fields->count() + $offset; } } From be6f212f9a73f39840f7c2c74cf6453cdd0a4b3b Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 24 Dec 2022 10:03:38 +0000 Subject: [PATCH 105/130] Apply fixes from StyleCI --- .../src/Console/Commands/MakePublicationTypeCommand.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index ab9573cfd9d..254685b2976 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -80,7 +80,6 @@ protected function captureFieldsDefinitions(): Collection } else { $addAnother = $this->confirm("Field #{$this->getCount(-1)} added! Add another field?"); } - } while ($addAnother); return $this->fields; From ab471752ed0903190c1706b64b1c6f5c29edc18a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:15:28 +0100 Subject: [PATCH 106/130] Mark setting as deprecation candidate --- .../src/Console/Commands/MakePublicationTypeCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 254685b2976..4d1b23e6491 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -192,6 +192,7 @@ protected function getPageSize(): int ); } + /** @deprecated This setting might be deprecated as its unlikely one would enable page size limits without a way to traverse them */ protected function getPrevNextLinks(): bool { return $this->confirm('Generate previous/next links in detail view?', true); From a97de952ac8385ee4d9474b2b8b303ea8047f1b2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:20:30 +0100 Subject: [PATCH 107/130] Break out code from if block so it also validates argument data --- .../src/Console/Commands/MakePublicationTypeCommand.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 4d1b23e6491..41d862a1b52 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -45,10 +45,10 @@ public function safeHandle(): int $title = $this->argument('name'); if (! $title) { $title = trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); - $dirname = Str::slug($title); - if (file_exists($dirname) && is_dir($dirname) && count(scandir($dirname)) > 2) { - throw new InvalidArgumentException("Storage path [$dirname] already exists"); - } + } + $dirname = Str::slug($title); + if (file_exists($dirname) && is_dir($dirname) && count(scandir($dirname)) > 2) { + throw new InvalidArgumentException("Storage path [$dirname] already exists"); } $this->fields = $this->captureFieldsDefinitions(); From 9b1554fb2c79f2b98492b41d6638142b84302681 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:23:21 +0100 Subject: [PATCH 108/130] Test existence of files and directories independently of each other --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 41d862a1b52..fe37616fcee 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -47,7 +47,7 @@ public function safeHandle(): int $title = trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); } $dirname = Str::slug($title); - if (file_exists($dirname) && is_dir($dirname) && count(scandir($dirname)) > 2) { + if (file_exists($dirname) || (is_dir($dirname) && count(scandir($dirname)) > 2)) { throw new InvalidArgumentException("Storage path [$dirname] already exists"); } From f424ef3afc67700fd4cc833c01c9b60272fe8c6b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:23:36 +0100 Subject: [PATCH 109/130] Use absolute paths for filesystem calls --- .../src/Console/Commands/MakePublicationTypeCommand.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index fe37616fcee..26a298180d4 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -4,6 +4,7 @@ namespace Hyde\Console\Commands; +use Hyde\Hyde; use function array_keys; use function file_exists; use Hyde\Console\Concerns\ValidatingCommand; @@ -47,7 +48,7 @@ public function safeHandle(): int $title = trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); } $dirname = Str::slug($title); - if (file_exists($dirname) || (is_dir($dirname) && count(scandir($dirname)) > 2)) { + if (file_exists(Hyde::path($dirname)) || (is_dir(Hyde::path($dirname)) && count(scandir($dirname)) > 2)) { throw new InvalidArgumentException("Storage path [$dirname] already exists"); } From dc7cc6e58111ac957873eceaf9f282172ca38315 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:24:48 +0100 Subject: [PATCH 110/130] Use is_file instead of file_exists --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 26a298180d4..6223abcc1e7 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -48,7 +48,7 @@ public function safeHandle(): int $title = trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); } $dirname = Str::slug($title); - if (file_exists(Hyde::path($dirname)) || (is_dir(Hyde::path($dirname)) && count(scandir($dirname)) > 2)) { + if (is_file(Hyde::path($dirname)) || (is_dir(Hyde::path($dirname)) && count(scandir($dirname)) > 2)) { throw new InvalidArgumentException("Storage path [$dirname] already exists"); } From 4ffa47241605e532b5ec92b7166de7101e7122de Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:25:12 +0100 Subject: [PATCH 111/130] Test conflict handling --- .../MakePublicationTypeCommandTest.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index e8cae24850a..67e9ecaec24 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -134,4 +134,27 @@ public function test_with_multiple_fields_of_the_same_name() ]) ->assertExitCode(0); } + + public function test_with_existing_file_of_the_same_name() + { + config(['app.throw_on_console_exception' => false]); + + $this->file('test-publication'); + + $this->artisan('make:publicationType "Test Publication"') + ->expectsOutput('Error: Storage path [test-publication] already exists') + ->assertExitCode(1); + } + + public function test_with_existing_publication_of_the_same_name() + { + config(['app.throw_on_console_exception' => false]); + + $this->directory('test-publication'); + $this->file('test-publication/foo'); + + $this->artisan('make:publicationType "Test Publication"') + ->expectsOutput('Error: Storage path [test-publication] already exists') + ->assertExitCode(1); + } } From c19f7229866a6b8b650e4b2fd5ebd45b922f66ef Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 24 Dec 2022 10:25:30 +0000 Subject: [PATCH 112/130] Apply fixes from StyleCI --- .../src/Console/Commands/MakePublicationTypeCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 6223abcc1e7..24ebaf1a0a3 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -4,13 +4,12 @@ namespace Hyde\Console\Commands; -use Hyde\Hyde; use function array_keys; -use function file_exists; use Hyde\Console\Concerns\ValidatingCommand; use Hyde\Framework\Actions\CreatesNewPublicationType; use Hyde\Framework\Features\Publications\Models\PublicationField; use Hyde\Framework\Features\Publications\PublicationFieldTypes; +use Hyde\Hyde; use Illuminate\Support\Collection; use Illuminate\Support\Str; use function in_array; From 22d14edfcf254ba242a27327dedd7067b0cdf4dc Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:29:10 +0100 Subject: [PATCH 113/130] Extract method --- .../Console/Commands/MakePublicationTypeCommand.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 24ebaf1a0a3..e4f6ff378c5 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -46,10 +46,7 @@ public function safeHandle(): int if (! $title) { $title = trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); } - $dirname = Str::slug($title); - if (is_file(Hyde::path($dirname)) || (is_dir(Hyde::path($dirname)) && count(scandir($dirname)) > 2)) { - throw new InvalidArgumentException("Storage path [$dirname] already exists"); - } + $this->validateStorageDirectory($title); $this->fields = $this->captureFieldsDefinitions(); @@ -202,4 +199,12 @@ protected function getCount(int $offset = 0): int { return $this->fields->count() + $offset; } + + protected function validateStorageDirectory(string $title): void + { + $dirname = Str::slug($title); + if (is_file(Hyde::path($dirname)) || (is_dir(Hyde::path($dirname)) && count(scandir($dirname)) > 2)) { + throw new InvalidArgumentException("Storage path [$dirname] already exists"); + } + } } From 72e6c5d5ca68e37f59eb1160f160c15a9ae4ae9c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:29:37 +0100 Subject: [PATCH 114/130] Shift slug call --- .../src/Console/Commands/MakePublicationTypeCommand.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index e4f6ff378c5..a72abac251c 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -46,7 +46,7 @@ public function safeHandle(): int if (! $title) { $title = trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); } - $this->validateStorageDirectory($title); + $this->validateStorageDirectory(Str::slug($title)); $this->fields = $this->captureFieldsDefinitions(); @@ -200,9 +200,8 @@ protected function getCount(int $offset = 0): int return $this->fields->count() + $offset; } - protected function validateStorageDirectory(string $title): void + protected function validateStorageDirectory(string $dirname): void { - $dirname = Str::slug($title); if (is_file(Hyde::path($dirname)) || (is_dir(Hyde::path($dirname)) && count(scandir($dirname)) > 2)) { throw new InvalidArgumentException("Storage path [$dirname] already exists"); } From 4c4b005ea67f222e12341e6caa1e101484aff3cd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:29:56 +0100 Subject: [PATCH 115/130] Expand variable name abbreviation --- .../src/Console/Commands/MakePublicationTypeCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index a72abac251c..c933206341a 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -200,10 +200,10 @@ protected function getCount(int $offset = 0): int return $this->fields->count() + $offset; } - protected function validateStorageDirectory(string $dirname): void + protected function validateStorageDirectory(string $directoryName): void { - if (is_file(Hyde::path($dirname)) || (is_dir(Hyde::path($dirname)) && count(scandir($dirname)) > 2)) { - throw new InvalidArgumentException("Storage path [$dirname] already exists"); + if (is_file(Hyde::path($directoryName)) || (is_dir(Hyde::path($directoryName)) && count(scandir($directoryName)) > 2)) { + throw new InvalidArgumentException("Storage path [$directoryName] already exists"); } } } From 359d99023f430f826d656368d7d445a12d89692d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:30:31 +0100 Subject: [PATCH 116/130] Shorten and normalize argument description --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index c933206341a..691ba2b070c 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -30,7 +30,7 @@ class MakePublicationTypeCommand extends ValidatingCommand { /** @var string */ protected $signature = 'make:publicationType - {name? : The name of the Publication Type to create. Will be used to generate the storage directory} + {name? : The name of the publication type to create} {--use-defaults : Select the default options wherever possible}'; /** @var string */ From 7990cf5e394e0ec07b09b0a3bd9b8caca223b26d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:31:58 +0100 Subject: [PATCH 117/130] Simplify if --- .../src/Console/Commands/MakePublicationTypeCommand.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 691ba2b070c..07c164b7690 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -42,10 +42,8 @@ public function safeHandle(): int { $this->title('Creating a new Publication Type!'); - $title = $this->argument('name'); - if (! $title) { - $title = trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); - } + $title = $this->argument('name') ?: trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); + $this->validateStorageDirectory(Str::slug($title)); $this->fields = $this->captureFieldsDefinitions(); From d928bb2ab19293be51d4c3a640ba8cc13d8cb88b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:32:27 +0100 Subject: [PATCH 118/130] Extract method --- .../src/Console/Commands/MakePublicationTypeCommand.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 07c164b7690..9103f6d0ed4 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -42,7 +42,7 @@ public function safeHandle(): int { $this->title('Creating a new Publication Type!'); - $title = $this->argument('name') ?: trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); + $title = $this->getTitle(); $this->validateStorageDirectory(Str::slug($title)); @@ -198,6 +198,11 @@ protected function getCount(int $offset = 0): int return $this->fields->count() + $offset; } + protected function getTitle(): string + { + return $this->argument('name') ?: trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); + } + protected function validateStorageDirectory(string $directoryName): void { if (is_file(Hyde::path($directoryName)) || (is_dir(Hyde::path($directoryName)) && count(scandir($directoryName)) > 2)) { From 0da2de872933a2f05d6e39a03dd3d0ff909cf0d9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:32:41 +0100 Subject: [PATCH 119/130] Add clarifying parentheses --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 9103f6d0ed4..80f53b58803 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -205,7 +205,7 @@ protected function getTitle(): string protected function validateStorageDirectory(string $directoryName): void { - if (is_file(Hyde::path($directoryName)) || (is_dir(Hyde::path($directoryName)) && count(scandir($directoryName)) > 2)) { + if (is_file(Hyde::path($directoryName)) || (is_dir(Hyde::path($directoryName)) && (count(scandir($directoryName)) > 2))) { throw new InvalidArgumentException("Storage path [$directoryName] already exists"); } } From 7655b050ed8bbd9c593a8e2c45e2b7ad4708110f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:33:08 +0100 Subject: [PATCH 120/130] Move deprecation candidate notice to data object --- .../src/Console/Commands/MakePublicationTypeCommand.php | 1 - .../Features/Publications/Models/PaginationSettings.php | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 80f53b58803..46c96bd14a1 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -187,7 +187,6 @@ protected function getPageSize(): int ); } - /** @deprecated This setting might be deprecated as its unlikely one would enable page size limits without a way to traverse them */ protected function getPrevNextLinks(): bool { return $this->confirm('Generate previous/next links in detail view?', true); diff --git a/packages/framework/src/Framework/Features/Publications/Models/PaginationSettings.php b/packages/framework/src/Framework/Features/Publications/Models/PaginationSettings.php index d133e104991..81a2f5cee4f 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PaginationSettings.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PaginationSettings.php @@ -13,6 +13,7 @@ class PaginationSettings implements SerializableContract public string $sortField = '__createdAt'; public bool $sortAscending = true; + /** @deprecated This setting might be deprecated as its unlikely one would enable page size limits without a way to traverse them */ public bool $prevNextLinks = true; public int $pageSize = 25; From 24881f0911a67a0e221ae86347641e5a65a4a77e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:34:09 +0100 Subject: [PATCH 121/130] Expand property name in question output to make message more fluent --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- .../tests/Feature/Commands/MakePublicationTypeCommandTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 46c96bd14a1..2863e336cf1 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -181,7 +181,7 @@ protected function getPageSize(): int { return (int) $this->askWithValidation( 'pageSize', - 'Enter the pageSize (0 for no limit)', + 'Enter the page size (0 for no limit)', ['required', 'integer', 'between:0,100'], 25 ); diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 67e9ecaec24..63d582195b4 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -57,7 +57,7 @@ public function test_command_creates_publication_type() 'Ascending', 'Descending', ]) - ->expectsQuestion('Enter the pageSize (0 for no limit)', 10) + ->expectsQuestion('Enter the page size (0 for no limit)', 10) ->expectsQuestion('Generate previous/next links in detail view?', 'n') ->expectsChoice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', 'publication-title', [ '__createdAt', From 754bbaed9f4a1c01be0eb7295349870cbe4707a9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:35:25 +0100 Subject: [PATCH 122/130] Remove redundant arguments --- .../Feature/Commands/MakePublicationTypeCommandTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index 63d582195b4..ce976b018e0 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -47,7 +47,7 @@ public function test_command_creates_publication_type() 'Url', 'Tag', ], true) - ->expectsConfirmation('Field #1 added! Add another field?', 'n') + ->expectsConfirmation('Field #1 added! Add another field?') ->expectsConfirmation('Do you want to configure pagination settings?', 'yes') ->expectsChoice('Choose the default field you wish to sort by', '__createdAt', [ '__createdAt', @@ -124,9 +124,9 @@ public function test_with_multiple_fields_of_the_same_name() ->expectsQuestion('Try again: Enter name for field #2', 'bar') ->expectsChoice('Enter type for field #2', 'String', PublicationFieldTypes::names()) - ->expectsConfirmation('Field #2 added! Add another field?', 'no') + ->expectsConfirmation('Field #2 added! Add another field?') - ->expectsConfirmation('Do you want to configure pagination settings?', 'n') + ->expectsConfirmation('Do you want to configure pagination settings?') ->expectsChoice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', 'foo', [ '__createdAt', 'bar', From 5d2df5b84df45d635d35c86652f39c48a959e888 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:36:31 +0100 Subject: [PATCH 123/130] Use expectsConfirmation instead --- .../tests/Feature/Commands/MakePublicationTypeCommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index ce976b018e0..dc38a708419 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -58,7 +58,7 @@ public function test_command_creates_publication_type() 'Descending', ]) ->expectsQuestion('Enter the page size (0 for no limit)', 10) - ->expectsQuestion('Generate previous/next links in detail view?', 'n') + ->expectsConfirmation('Generate previous/next links in detail view?', 'yes') ->expectsChoice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', 'publication-title', [ '__createdAt', 'publication-title', From bd8b153c3b87105910ae104242b280c4071a2bef Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:37:02 +0100 Subject: [PATCH 124/130] Join comma-separated values into a single line --- .../src/Console/Commands/MakePublicationTypeCommand.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 2863e336cf1..ae30eef92a1 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -169,10 +169,7 @@ protected function getSortField(): string protected function getSortDirection(): bool { - $options = [ - 'Ascending' => true, - 'Descending' => false, - ]; + $options = ['Ascending' => true, 'Descending' => false]; return $options[$this->choice('Choose the default sort direction', array_keys($options), 'Ascending')]; } From cb978a83cf8fac5ebc4f9a394defcc24b71156d4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:37:52 +0100 Subject: [PATCH 125/130] Move up helper methods --- .../Commands/MakePublicationTypeCommand.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index ae30eef92a1..ca0c206424e 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -60,6 +60,18 @@ public function safeHandle(): int return Command::SUCCESS; } + protected function getTitle(): string + { + return $this->argument('name') ?: trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); + } + + protected function validateStorageDirectory(string $directoryName): void + { + if (is_file(Hyde::path($directoryName)) || (is_dir(Hyde::path($directoryName)) && (count(scandir($directoryName)) > 2))) { + throw new InvalidArgumentException("Storage path [$directoryName] already exists"); + } + } + protected function captureFieldsDefinitions(): Collection { $this->line('You now need to define the fields in your publication type:'); @@ -193,16 +205,4 @@ protected function getCount(int $offset = 0): int { return $this->fields->count() + $offset; } - - protected function getTitle(): string - { - return $this->argument('name') ?: trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); - } - - protected function validateStorageDirectory(string $directoryName): void - { - if (is_file(Hyde::path($directoryName)) || (is_dir(Hyde::path($directoryName)) && (count(scandir($directoryName)) > 2))) { - throw new InvalidArgumentException("Storage path [$directoryName] already exists"); - } - } } From b304347a5c884a1d098b05c5bf0edb79ea8c7d45 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:38:11 +0100 Subject: [PATCH 126/130] Import used function --- .../src/Console/Commands/MakePublicationTypeCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index ca0c206424e..ca1940eae4d 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -16,6 +16,7 @@ use InvalidArgumentException; use function is_dir; use LaravelZero\Framework\Commands\Command; +use function is_file; use function scandir; use function strtolower; use function trim; From 550b0e3953d8b986f97058b725c44b775e3b313e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:38:52 +0100 Subject: [PATCH 127/130] Unwrap line --- .../src/Console/Commands/MakePublicationTypeCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index ca1940eae4d..25faf683d91 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -189,8 +189,7 @@ protected function getSortDirection(): bool protected function getPageSize(): int { - return (int) $this->askWithValidation( - 'pageSize', + return (int) $this->askWithValidation('pageSize', 'Enter the page size (0 for no limit)', ['required', 'integer', 'between:0,100'], 25 From 3de00f3bb46a2449bb328108b83fdae223e2682c Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 24 Dec 2022 10:39:28 +0000 Subject: [PATCH 128/130] Apply fixes from StyleCI --- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 25faf683d91..d0a6753379d 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -15,8 +15,8 @@ use function in_array; use InvalidArgumentException; use function is_dir; -use LaravelZero\Framework\Commands\Command; use function is_file; +use LaravelZero\Framework\Commands\Command; use function scandir; use function strtolower; use function trim; From 3dba236de3e1e4bcb6ff8f9e071e99059f016ab8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:41:09 +0100 Subject: [PATCH 129/130] Make action pagination parameters nullable and shift defaults to there --- .../Commands/MakePublicationTypeCommand.php | 2 +- .../Actions/CreatesNewPublicationType.php | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index d0a6753379d..3c35db41422 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -169,7 +169,7 @@ protected function addCreatedAtMetaField(): void protected function getPaginationSettings(): array { if ($this->option('use-defaults') || ! $this->confirm('Do you want to configure pagination settings?')) { - return ['__createdAt', true, 25, true]; + return [null, null, null, null]; } return [$this->getSortField(), $this->getSortDirection(), $this->getPageSize(), $this->getPrevNextLinks()]; diff --git a/packages/framework/src/Framework/Actions/CreatesNewPublicationType.php b/packages/framework/src/Framework/Actions/CreatesNewPublicationType.php index 7886e3c0e3a..d48e0d73cbe 100644 --- a/packages/framework/src/Framework/Actions/CreatesNewPublicationType.php +++ b/packages/framework/src/Framework/Actions/CreatesNewPublicationType.php @@ -24,10 +24,10 @@ public function __construct( protected string $name, protected Collection $fields, protected string $canonicalField, - protected string $sortField, - protected bool $sortAscending, - protected bool $prevNextLinks, - protected int $pageSize, + protected ?string $sortField, + protected ?bool $sortAscending, + protected ?bool $prevNextLinks, + protected ?int $pageSize, protected ?OutputStyle $output = null, ) { $this->dirName = $this->formatStringForStorage($this->name); @@ -42,10 +42,10 @@ protected function handleCreate(): void "{$this->dirName}_detail", "{$this->dirName}_list", [ - $this->sortField, - $this->sortAscending, - $this->prevNextLinks, - $this->pageSize, + $this->sortField ?? '__createdAt', + $this->sortAscending ?? true, + $this->prevNextLinks ?? true, + $this->pageSize ?? 25, ], $this->fields->toArray() ); From 72c9b2583efff3957af813055e4d49f300a06529 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 24 Dec 2022 11:43:05 +0100 Subject: [PATCH 130/130] Reorder questions to match data order --- .../Commands/MakePublicationTypeCommand.php | 14 +++++++------- .../Commands/MakePublicationTypeCommandTest.php | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 3c35db41422..a8476e27b4a 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -49,7 +49,7 @@ public function safeHandle(): int $this->fields = $this->captureFieldsDefinitions(); - [$sortField, $sortAscending, $pageSize, $prevNextLinks] = ($this->getPaginationSettings()); + [$sortField, $sortAscending, $prevNextLinks, $pageSize] = ($this->getPaginationSettings()); $canonicalField = $this->getCanonicalField(); @@ -172,7 +172,7 @@ protected function getPaginationSettings(): array return [null, null, null, null]; } - return [$this->getSortField(), $this->getSortDirection(), $this->getPageSize(), $this->getPrevNextLinks()]; + return [$this->getSortField(), $this->getSortDirection(), $this->getPrevNextLinks(), $this->getPageSize()]; } protected function getSortField(): string @@ -187,6 +187,11 @@ protected function getSortDirection(): bool return $options[$this->choice('Choose the default sort direction', array_keys($options), 'Ascending')]; } + protected function getPrevNextLinks(): bool + { + return $this->confirm('Generate previous/next links in detail view?', true); + } + protected function getPageSize(): int { return (int) $this->askWithValidation('pageSize', @@ -196,11 +201,6 @@ protected function getPageSize(): int ); } - protected function getPrevNextLinks(): bool - { - return $this->confirm('Generate previous/next links in detail view?', true); - } - protected function getCount(int $offset = 0): int { return $this->fields->count() + $offset; diff --git a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php index dc38a708419..682eca863c3 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationTypeCommandTest.php @@ -57,8 +57,8 @@ public function test_command_creates_publication_type() 'Ascending', 'Descending', ]) - ->expectsQuestion('Enter the page size (0 for no limit)', 10) ->expectsConfirmation('Generate previous/next links in detail view?', 'yes') + ->expectsQuestion('Enter the page size (0 for no limit)', 10) ->expectsChoice('Choose a canonical name field (this will be used to generate filenames, so the values need to be unique)', 'publication-title', [ '__createdAt', 'publication-title',