From eb62bd08adb10129cc2f1fe2b747970663af3769 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 30 Dec 2022 17:26:47 +0100 Subject: [PATCH 01/12] Create BooleanRule.php --- .../Features/Publications/Rules/BooleanRule.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php diff --git a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php new file mode 100644 index 00000000000..bd666652919 --- /dev/null +++ b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php @@ -0,0 +1,10 @@ + Date: Fri, 30 Dec 2022 17:27:19 +0100 Subject: [PATCH 02/12] Implements InvokableRule --- .../Features/Publications/Rules/BooleanRule.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php index bd666652919..06328399518 100644 --- a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php +++ b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php @@ -4,7 +4,20 @@ namespace Hyde\Framework\Features\Publications\Rules; -class BooleanRule +use Illuminate\Contracts\Validation\InvokableRule; + +class BooleanRule implements InvokableRule { - // + /** + * Run the validation rule. + * + * @param string $attribute + * @param mixed $value + * @param \Closure $fail + * @return void + */ + public function __invoke($attribute, $value, $fail) + { + // + } } From edf0d91cd36ac9da23ad9019f2bfa36f5c5866e1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 30 Dec 2022 17:29:39 +0100 Subject: [PATCH 03/12] Implement the rule --- .../Framework/Features/Publications/Rules/BooleanRule.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php index 06328399518..c731a54f2eb 100644 --- a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php +++ b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php @@ -6,6 +6,8 @@ use Illuminate\Contracts\Validation\InvokableRule; +use function in_array; + class BooleanRule implements InvokableRule { /** @@ -18,6 +20,10 @@ class BooleanRule implements InvokableRule */ public function __invoke($attribute, $value, $fail) { - // + $acceptable = ['true', 'false', true, false, 0, 1, '0', '1']; + + if (! in_array($value, $acceptable, true)) { + $fail('The :attribute must be true or false'); + } } } From 83d2a4d677e28bf901387bd56081fd1e4d8d5ef8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 30 Dec 2022 17:29:51 +0100 Subject: [PATCH 04/12] Link to the base rule --- .../src/Framework/Features/Publications/Rules/BooleanRule.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php index c731a54f2eb..ab143c041cd 100644 --- a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php +++ b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php @@ -13,6 +13,7 @@ class BooleanRule implements InvokableRule /** * Run the validation rule. * + * @see https://github.com/illuminate/validation/blob/3f63f1046f67377a64779baaa86d7f1997b5f748/Concerns/ValidatesAttributes.php#L448-L453 * @param string $attribute * @param mixed $value * @param \Closure $fail From 772a8216a295a110237e7f2a16d79e9890b7b72a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 30 Dec 2022 17:30:00 +0100 Subject: [PATCH 05/12] Use the custom boolean rule --- .../Framework/Features/Publications/PublicationFieldTypes.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index ccbc4c5a491..c0d5e3b9142 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Features\Publications; +use Hyde\Framework\Features\Publications\Rules\BooleanRule; use Illuminate\Support\Collection; /** @@ -51,7 +52,7 @@ public static function getRules(self $type): array return match ($type) { self::String => ['string'], self::Datetime => ['date'], - self::Boolean => ['boolean'], + self::Boolean => [BooleanRule::class], self::Integer => ['integer', 'numeric'], self::Float => ['numeric'], self::Image => [], // TODO Rename to media and move down in the list From 0f2e5afa53a80e16ed060515947631c9cec3f135 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 30 Dec 2022 17:30:41 +0100 Subject: [PATCH 06/12] Add 'void' as the function's return type --- .../src/Framework/Features/Publications/Rules/BooleanRule.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php index ab143c041cd..19490dcbfd6 100644 --- a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php +++ b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php @@ -17,9 +17,8 @@ class BooleanRule implements InvokableRule * @param string $attribute * @param mixed $value * @param \Closure $fail - * @return void */ - public function __invoke($attribute, $value, $fail) + public function __invoke($attribute, $value, $fail): void { $acceptable = ['true', 'false', true, false, 0, 1, '0', '1']; From 948f379040c481ce2bc4c1368ed2ee65434aa30d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 30 Dec 2022 17:31:21 +0100 Subject: [PATCH 07/12] Create class PHPDoc --- .../Framework/Features/Publications/Rules/BooleanRule.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php index 19490dcbfd6..2dedaee3b6c 100644 --- a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php +++ b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php @@ -8,12 +8,16 @@ use function in_array; +/** + * Extended boolean rule that allows for 'true' and 'false' strings in order to support console inputs. + * + * @see https://github.com/illuminate/validation/blob/3f63f1046f67377a64779baaa86d7f1997b5f748/Concerns/ValidatesAttributes.php#L448-L453 + */ class BooleanRule implements InvokableRule { /** * Run the validation rule. * - * @see https://github.com/illuminate/validation/blob/3f63f1046f67377a64779baaa86d7f1997b5f748/Concerns/ValidatesAttributes.php#L448-L453 * @param string $attribute * @param mixed $value * @param \Closure $fail From 27b8ed6b2d2bcf705615a6680b49e0547e5f9bf3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 30 Dec 2022 17:33:15 +0100 Subject: [PATCH 08/12] Fix boolean rule syntax --- .../Framework/Features/Publications/PublicationFieldTypes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index c0d5e3b9142..088c516aa5e 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -52,7 +52,7 @@ public static function getRules(self $type): array return match ($type) { self::String => ['string'], self::Datetime => ['date'], - self::Boolean => [BooleanRule::class], + self::Boolean => [new BooleanRule], self::Integer => ['integer', 'numeric'], self::Float => ['numeric'], self::Image => [], // TODO Rename to media and move down in the list From 3473695e127fe43dede0762d8e6f8c5f78164754 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 30 Dec 2022 17:33:34 +0100 Subject: [PATCH 09/12] Remove deprecated method now refactored into custom rule --- .../Commands/MakePublicationCommand.php | 38 ------------------- 1 file changed, 38 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationCommand.php b/packages/framework/src/Console/Commands/MakePublicationCommand.php index 45d638b2534..88492f0f0a7 100644 --- a/packages/framework/src/Console/Commands/MakePublicationCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationCommand.php @@ -131,7 +131,6 @@ protected function captureFieldInput(PublicationField $field): ?PublicationField PublicationFieldTypes::Array => $this->captureArrayFieldInput($field), PublicationFieldTypes::Image => $this->captureImageFieldInput($field), PublicationFieldTypes::Tag => $this->captureTagFieldInput($field), - PublicationFieldTypes::Boolean => $this->captureBooleanFieldInput($field), default => new ($field->type->fieldClass())($this->askWithValidation($field->name, "Enter data for field [$field->name]", $field->getValidationRules()->toArray())), }; @@ -197,43 +196,6 @@ protected function captureTagFieldInput(PublicationField $field): ?TagField return new TagField($choice); } - /** - * @deprecated Will be refactored into a dedicated rule - */ - protected function captureBooleanFieldInput(PublicationField $field, $retryCount = 1): ?BooleanField - { - // Return null when retry count is exceeded to prevent infinite loop - if ($retryCount > 30) { - return null; - } - - // Since the Laravel validation rule for booleans doesn't accept the string input provided by the console, - // we need to do some logic of our own to support validating booleans through the console. - - $rules = $field->type->rules(); - $rules = array_flip($rules); - unset($rules['boolean']); - $rules = array_flip($rules); - - $selection = $this->askWithValidation($field->name, "Enter data for field [$field->name]", $rules); - - if (empty($selection)) { - return null; - } - - $acceptable = ['true', 'false', true, false, 0, 1, '0', '1']; - - // Strict parameter is needed as for some reason `in_array($selection, [true])` is always true no matter what the value of $selection is. - if (in_array($selection, $acceptable, true)) { - return new BooleanField($selection); - } else { - // Match the formatting of the standard Laravel validation error message. - $this->error("The $field->name field must be true or false."); - - return $this->captureBooleanFieldInput($field, $retryCount + 1); - } - } - /** @return null */ protected function handleEmptyOptionsCollection(PublicationField $field, string $type, string $message) { From 6392faf8bdcfb094b83fdbd692741926ff7a0f3a Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 30 Dec 2022 16:33:56 +0000 Subject: [PATCH 10/12] Apply fixes from StyleCI --- .../framework/src/Console/Commands/MakePublicationCommand.php | 2 -- .../src/Framework/Features/Publications/Rules/BooleanRule.php | 1 - 2 files changed, 3 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationCommand.php b/packages/framework/src/Console/Commands/MakePublicationCommand.php index 88492f0f0a7..a1967764644 100644 --- a/packages/framework/src/Console/Commands/MakePublicationCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationCommand.php @@ -4,14 +4,12 @@ namespace Hyde\Console\Commands; -use function array_flip; use Closure; use Hyde\Console\Commands\Helpers\InputStreamHandler; use Hyde\Console\Concerns\ValidatingCommand; use Hyde\Framework\Actions\CreatesNewPublicationPage; use Hyde\Framework\Features\Publications\Models\PublicationField; use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\ArrayField; -use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\BooleanField; use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\ImageField; use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\PublicationFieldValue; use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\TagField; diff --git a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php index 2dedaee3b6c..8b09c669730 100644 --- a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php +++ b/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php @@ -5,7 +5,6 @@ namespace Hyde\Framework\Features\Publications\Rules; use Illuminate\Contracts\Validation\InvokableRule; - use function in_array; /** From f5a4337c8f4b559970f1f7f64d0610a5972c5a95 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 30 Dec 2022 17:39:14 +0100 Subject: [PATCH 11/12] Update PublicationFieldTypesEnumTest.php --- .../framework/tests/Feature/PublicationFieldTypesEnumTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index 365633691d3..fe85406b435 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -5,6 +5,7 @@ namespace Hyde\Framework\Testing\Feature; use Hyde\Framework\Features\Publications\PublicationFieldTypes; +use Hyde\Framework\Features\Publications\Rules\BooleanRule; use Hyde\Testing\TestCase; /** @@ -31,7 +32,7 @@ public function testCases() public function testGetRules() { $this->assertSame(['string'], PublicationFieldTypes::String->rules()); - $this->assertSame(['boolean'], PublicationFieldTypes::Boolean->rules()); + $this->assertEquals([new BooleanRule()], PublicationFieldTypes::Boolean->rules()); $this->assertSame(['integer', 'numeric'], PublicationFieldTypes::Integer->rules()); $this->assertSame(['numeric'], PublicationFieldTypes::Float->rules()); $this->assertSame(['date'], PublicationFieldTypes::Datetime->rules()); From 0fbcff6f9fe9ca63c0bf2682b088c3fe642b5b53 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 30 Dec 2022 17:42:20 +0100 Subject: [PATCH 12/12] Rename validation namespace --- .../Framework/Features/Publications/PublicationFieldTypes.php | 2 +- .../Features/Publications/{Rules => Validation}/BooleanRule.php | 2 +- .../framework/tests/Feature/PublicationFieldTypesEnumTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename packages/framework/src/Framework/Features/Publications/{Rules => Validation}/BooleanRule.php (93%) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php index 088c516aa5e..8e82d50a9d4 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationFieldTypes.php @@ -4,7 +4,7 @@ namespace Hyde\Framework\Features\Publications; -use Hyde\Framework\Features\Publications\Rules\BooleanRule; +use Hyde\Framework\Features\Publications\Validation\BooleanRule; use Illuminate\Support\Collection; /** diff --git a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php b/packages/framework/src/Framework/Features/Publications/Validation/BooleanRule.php similarity index 93% rename from packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php rename to packages/framework/src/Framework/Features/Publications/Validation/BooleanRule.php index 8b09c669730..68b2a3ac727 100644 --- a/packages/framework/src/Framework/Features/Publications/Rules/BooleanRule.php +++ b/packages/framework/src/Framework/Features/Publications/Validation/BooleanRule.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Hyde\Framework\Features\Publications\Rules; +namespace Hyde\Framework\Features\Publications\Validation; use Illuminate\Contracts\Validation\InvokableRule; use function in_array; diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index fe85406b435..29cbbbb822a 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -5,7 +5,7 @@ namespace Hyde\Framework\Testing\Feature; use Hyde\Framework\Features\Publications\PublicationFieldTypes; -use Hyde\Framework\Features\Publications\Rules\BooleanRule; +use Hyde\Framework\Features\Publications\Validation\BooleanRule; use Hyde\Testing\TestCase; /**