diff --git a/packages/framework/src/Console/Commands/MakePublicationCommand.php b/packages/framework/src/Console/Commands/MakePublicationCommand.php index 547b652e576..cffb11b1f2e 100644 --- a/packages/framework/src/Console/Commands/MakePublicationCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationCommand.php @@ -18,7 +18,6 @@ use Hyde\Framework\Features\Publications\PublicationFieldTypes; use Hyde\Framework\Features\Publications\PublicationService; use Illuminate\Support\Collection; -use Illuminate\Support\Str; use function implode; use function in_array; use InvalidArgumentException; @@ -188,8 +187,7 @@ protected function captureOtherFieldInput(PublicationField $field): ?Publication return null; } - $namespace = Str::beforeLast(PublicationFieldValue::class, '\\'); - $className = "$namespace\\{$field->type->name}Field"; + $className = $field->type->fieldClass(); return new $className($selection); } diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index cf0f4fa2e95..aede583902b 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -8,11 +8,11 @@ use Hyde\Console\Concerns\ValidatingCommand; use Hyde\Framework\Actions\CreatesNewPublicationType; use Hyde\Framework\Features\Publications\Models\PublicationField; +use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Contracts\Canonicable; use Hyde\Framework\Features\Publications\PublicationFieldTypes; use Hyde\Hyde; use Illuminate\Support\Collection; use Illuminate\Support\Str; -use function in_array; use InvalidArgumentException; use function is_dir; use function is_file; @@ -130,7 +130,7 @@ protected function getFieldType(): PublicationFieldTypes protected function getCanonicalField(): PublicationField { $selectableFields = $this->fields->reject(function (PublicationField $field): bool { - return in_array($field, PublicationFieldTypes::canonicable()); + return ! is_a($field->type->fieldClass(), Canonicable::class, true); }); if ($this->option('use-defaults')) { diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldValues/Concerns/CanonicableTrait.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldValues/Concerns/CanonicableTrait.php new file mode 100644 index 00000000000..86b33d58cb9 --- /dev/null +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldValues/Concerns/CanonicableTrait.php @@ -0,0 +1,21 @@ +getCanonicalValue(); + } + + public function getCanonicalValue(): string + { + return substr($this->value, 0, 64) ?: throw new RuntimeException('Canonical value cannot be empty'); + } +} diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldValues/Contracts/Canonicable.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldValues/Contracts/Canonicable.php new file mode 100644 index 00000000000..04a4e1316b0 --- /dev/null +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationFieldValues/Contracts/Canonicable.php @@ -0,0 +1,12 @@ +name}Field"; } } diff --git a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php index 29cbbbb822a..299d94eb4e9 100644 --- a/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php +++ b/packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php @@ -79,14 +79,4 @@ public function testNamesReturnsArrayOfCaseNames() 9 => 'Tag', ], PublicationFieldTypes::names()); } - - public function testCanonicable() - { - $this->assertSame([ - PublicationFieldTypes::String, - PublicationFieldTypes::Integer, - PublicationFieldTypes::Datetime, - PublicationFieldTypes::Text, - ], PublicationFieldTypes::canonicable()); - } } diff --git a/packages/framework/tests/Features/Publications/Models/PublicationFieldValues/Concerns/CanonicableTraitTest.php b/packages/framework/tests/Features/Publications/Models/PublicationFieldValues/Concerns/CanonicableTraitTest.php new file mode 100644 index 00000000000..3686da740c0 --- /dev/null +++ b/packages/framework/tests/Features/Publications/Models/PublicationFieldValues/Concerns/CanonicableTraitTest.php @@ -0,0 +1,66 @@ +assertSame('foo', (string) $class); + } + + public function testGetCanonicalValue() + { + $class = new CanonicableTraitTestClass('foo'); + + $this->assertSame('foo', $class->getCanonicalValue()); + } + + public function test__toStringReturnsGetCanonicalValue() + { + $class = new CanonicableTraitTestClass('foo'); + + $this->assertSame($class->getCanonicalValue(), $class->__toString()); + } + + public function testGetCanonicalValueTruncatesValuesLongerThan64Characters() + { + $class = new CanonicableTraitTestClass(str_repeat('a', 65)); + + $this->assertSame(str_repeat('a', 64), $class->getCanonicalValue()); + $this->assertSame(64, strlen($class->getCanonicalValue())); + } + + public function testCanonicableValueCannotBeEmpty() + { + $class = new CanonicableTraitTestClass(''); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Canonical value cannot be empty'); + + $class->getCanonicalValue(); + } +} + +class CanonicableTraitTestClass extends PublicationFieldValue implements Canonicable +{ + use CanonicableTrait; + + protected static function parseInput(string $input): string + { + return $input; + } +}