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 publication field classes to reduce complexity #792

Merged
merged 10 commits into from
Dec 30, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use function implode;
use function in_array;
use InvalidArgumentException;
use function is_array;
use LaravelZero\Framework\Commands\Command;
use function str_starts_with;

Expand Down Expand Up @@ -152,7 +151,7 @@ protected function captureArrayFieldInput(PublicationField $field): ArrayField
{
$this->line(InputStreamHandler::formatMessage($field->name));

return new ArrayField('', useArrayLiteral: InputStreamHandler::call());
return new ArrayField(InputStreamHandler::call());
}

protected function captureImageFieldInput(PublicationField $field): ?ImageField
Expand Down Expand Up @@ -187,10 +186,6 @@ protected function captureTagFieldInput(PublicationField $field): ?TagField
true
);

if (is_array($choice)) {
return new TagField('', useArrayLiteral: $choice);
}

return new TagField($choice);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@
namespace Hyde\Framework\Features\Publications\Models\PublicationFieldValues;

use Hyde\Framework\Features\Publications\PublicationFieldTypes;
use function is_array;

final class ArrayField extends PublicationFieldValue
{
public const TYPE = PublicationFieldTypes::Array;
public const PARSE_FROM_CSV = 4;
public const PARSE_FROM_NEWLINES = 8;

protected static function parseInput(string $input, int $options = 0, ?array $useArrayLiteral = null): array
public function __construct(string|array $value)
{
if ($useArrayLiteral !== null) {
return $useArrayLiteral;
}

if ($options & self::PARSE_FROM_CSV) {
return explode(', ', $input);
}
$this->value = self::parseInput($value);
}

if ($options & self::PARSE_FROM_NEWLINES) {
return explode("\n", $input);
protected static function parseInput(string|array $input): array
{
if (is_array($input)) {
return $input;
}

return (array) $input;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ abstract class PublicationFieldValue

protected mixed $value;

final public function __construct(string $value, ...$args)
public function __construct(string $value)
{
$this->value = static::parseInput($value, ...$args);
$this->value = static::parseInput($value);
}

final public function getValue(): mixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@
namespace Hyde\Framework\Features\Publications\Models\PublicationFieldValues;

use Hyde\Framework\Features\Publications\PublicationFieldTypes;
use function is_array;

final class TagField extends PublicationFieldValue
{
public const TYPE = PublicationFieldTypes::Tag;

protected static function parseInput(string $input, ?array $useArrayLiteral = null): array
public function __construct(string|array $value)
{
if ($useArrayLiteral !== null) {
return $useArrayLiteral;
$this->value = self::parseInput($value);
}

protected static function parseInput(string|array $input): array
{
if (is_array($input)) {
return $input;
}

return (array) $input;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function testWithArrayType()
]]);

$fieldData = Collection::make([
'tags' => new TagField('', useArrayLiteral: ['tag1', 'tag2', 'foo bar']),
'tags' => new TagField(['tag1', 'tag2', 'foo bar']),
]);

(new CreatesNewPublicationPage($pubType, $fieldData))->create();
Expand Down Expand Up @@ -173,7 +173,7 @@ public function testItCreatesValidYaml()
$fieldData = Collection::make([
'title' => new StringField('Hello World'),
'description' => new TextField("This is a description.\nIt can be multiple lines.\n"),
'tags' => new TagField('', useArrayLiteral: ['tag1', 'tag2', 'foo bar']),
'tags' => new TagField(['tag1', 'tag2', 'foo bar']),
]);

(new CreatesNewPublicationPage($pubType, $fieldData))->create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ public function testArrayFieldToYaml()
$this->assertSame("- foo\n", $this->getYaml(new ArrayField('foo')));
}

public function testArrayFieldWithArrayInput()
{
$this->assertSame(['foo'], (new ArrayField(['foo']))->getValue());
}

public function testArrayFieldParsingOptions()
{
$this->assertSame(['foo'], (new ArrayField('foo'))->getValue());
Expand All @@ -310,16 +315,6 @@ public function testArrayFieldParsingOptions()
$this->assertSame(['-10'], (new ArrayField('-10'))->getValue());
}

public function testArrayParsingWithCommaSeparatedValues()
{
$this->assertSame(['foo', 'bar'], (new ArrayField('foo, bar', ArrayField::PARSE_FROM_CSV))->getValue());
}

public function testArrayParsingWithNewlineSeparatedValues()
{
$this->assertSame(['foo', 'bar'], (new ArrayField("foo\nbar", ArrayField::PARSE_FROM_NEWLINES))->getValue());
}

// TextField tests

public function testTextFieldConstruct()
Expand Down Expand Up @@ -453,6 +448,11 @@ public function testTagFieldToYaml()
$this->assertSame("- foo\n", $this->getYaml(new TagField('foo')));
}

public function testTagFieldWithArrayInput()
{
$this->assertSame(['foo'], (new TagField(['foo']))->getValue());
}

public function testTagFieldParsingOptions()
{
$this->assertSame(['foo'], (new TagField('foo'))->getValue());
Expand Down