Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Refactor canonicable publication field internals #794

Merged
merged 31 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4cdbfc4
StringField implements Stringable
caendesilva Dec 30, 2022
b2bb3dc
Create Canonicable.php
caendesilva Dec 30, 2022
372e1bc
Define interface Canonicable that extends Stringable
caendesilva Dec 30, 2022
7586a94
Class StringField implements Canonicable instead of Stringable directly
caendesilva Dec 30, 2022
7c69edb
Create CanonicableTrait.php
caendesilva Dec 30, 2022
be66418
Use CanonicableTrait
caendesilva Dec 30, 2022
a119b30
Implement the trait methods
caendesilva Dec 30, 2022
ee16fcd
Limit canonicable string length to 64 characters
caendesilva Dec 30, 2022
a9642da
Throw exception if the canonical value is empty
caendesilva Dec 30, 2022
317a8c7
Deprecate canonicable method
caendesilva Dec 30, 2022
c1ca25e
Update all canonicable types to implement the interface
caendesilva Dec 30, 2022
021c691
Apply fixes from StyleCI
StyleCIBot Dec 30, 2022
6a2d4e4
Move trait into Concerns sub-namespace
caendesilva Dec 31, 2022
6c8a52b
Apply fixes from StyleCI
StyleCIBot Dec 31, 2022
40eb089
Merge branch 'publications-feature' into refactor-publication-field-c…
caendesilva Dec 31, 2022
cc445cc
Create CanonicableTraitTest.php
caendesilva Dec 31, 2022
db83e90
Generate method stubs
caendesilva Dec 31, 2022
5d37d89
Create a test class to test the trait
caendesilva Dec 31, 2022
127861d
Reorder test methods to match subject
caendesilva Dec 31, 2022
e38a449
Implement the base test
caendesilva Dec 31, 2022
8591abb
Add additional tests
caendesilva Dec 31, 2022
058be0a
Fix test not calling the subject method
caendesilva Dec 31, 2022
2938d58
Replace qualifier with an import
caendesilva Dec 31, 2022
6ce2042
Remove test for deprecated method
caendesilva Dec 31, 2022
467199c
Revert "Inline fieldClass helper method"
caendesilva Dec 31, 2022
97ad6af
Merge fieldClass helper method changes
caendesilva Dec 31, 2022
72e317f
Check instanceof instead of in_array
caendesilva Dec 31, 2022
72a9ce3
Use is_a instead of instanceof
caendesilva Dec 31, 2022
c12e327
Remove unused deprecated canonicable method
caendesilva Dec 31, 2022
603c858
Inline local variable
caendesilva Dec 31, 2022
e0ba5f0
Apply fixes from StyleCI
StyleCIBot Dec 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Concerns;

use RuntimeException;
use function substr;

trait CanonicableTrait
{
public function __toString(): string
{
return $this->getCanonicalValue();
}

public function getCanonicalValue(): string
{
return substr($this->value, 0, 64) ?: throw new RuntimeException('Canonical value cannot be empty');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Contracts;

use Stringable;

interface Canonicable extends Stringable
{
public function getCanonicalValue(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
namespace Hyde\Framework\Features\Publications\Models\PublicationFieldValues;

use DateTime;
use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Concerns\CanonicableTrait;
use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Contracts\Canonicable;
use Hyde\Framework\Features\Publications\PublicationFieldTypes;

final class DatetimeField extends PublicationFieldValue
final class DatetimeField extends PublicationFieldValue implements Canonicable
{
use CanonicableTrait;

public const TYPE = PublicationFieldTypes::Datetime;

protected static function parseInput(string $input): DateTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

namespace Hyde\Framework\Features\Publications\Models\PublicationFieldValues;

use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Concerns\CanonicableTrait;
use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Contracts\Canonicable;
use Hyde\Framework\Features\Publications\PublicationFieldTypes;

final class IntegerField extends PublicationFieldValue
final class IntegerField extends PublicationFieldValue implements Canonicable
{
use CanonicableTrait;

public const TYPE = PublicationFieldTypes::Integer;

protected static function parseInput(string $input): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

namespace Hyde\Framework\Features\Publications\Models\PublicationFieldValues;

use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Concerns\CanonicableTrait;
use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Contracts\Canonicable;
use Hyde\Framework\Features\Publications\PublicationFieldTypes;

final class StringField extends PublicationFieldValue
final class StringField extends PublicationFieldValue implements Canonicable
{
use CanonicableTrait;

public const TYPE = PublicationFieldTypes::String;

protected static function parseInput(string $input): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace Hyde\Framework\Features\Publications\Models\PublicationFieldValues;

use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Concerns\CanonicableTrait;
use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Contracts\Canonicable;
use Hyde\Framework\Features\Publications\PublicationFieldTypes;
use function trim;

final class TextField extends PublicationFieldValue
final class TextField extends PublicationFieldValue implements Canonicable
{
use CanonicableTrait;

public const TYPE = PublicationFieldTypes::Text;

protected static function parseInput(string $input): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Hyde\Framework\Features\Publications;

use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\PublicationFieldValue;
use Hyde\Framework\Features\Publications\Validation\BooleanRule;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

/**
* The supported field types for publication types.
Expand Down Expand Up @@ -63,18 +65,10 @@ public static function getRules(self $type): array
};
}

/**
* 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
public function fieldClass(): string
{
return [
self::String,
self::Integer,
self::Datetime,
self::Text,
];
$namespace = Str::beforeLast(PublicationFieldValue::class, '\\');

return "$namespace\\{$this->name}Field";
}
}
10 changes: 0 additions & 10 deletions packages/framework/tests/Feature/PublicationFieldTypesEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Testing\Features\Publications\Models\PublicationFieldValues\Concerns;

use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Concerns\CanonicableTrait;
use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Contracts\Canonicable;
use Hyde\Framework\Features\Publications\Models\PublicationFieldValues\PublicationFieldValue;
use Hyde\Testing\TestCase;
use RuntimeException;

/**
* @covers \Hyde\Framework\Features\Publications\Models\PublicationFieldValues\Concerns\CanonicableTrait
*/
class CanonicableTraitTest extends TestCase
{
public function test__toString()
{
$class = new CanonicableTraitTestClass('foo');

$this->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;
}
}