Skip to content

Commit

Permalink
Merge pull request #734 from hydephp/Cherry-pick-changes-from-rgasch-…
Browse files Browse the repository at this point in the history
…publications-feature

Cherry pick changes from rgasch:publications-feature
  • Loading branch information
caendesilva authored Dec 5, 2022
2 parents 47b44c6 + 820406f commit bfee47a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ protected function captureFieldsDefinitions(): Collection

if ($type < 10) {
do {
$fieldData['min'] = trim($this->askWithValidation('min', 'Min value (for strings, this refers to string length)', ['required', 'string'], 0));
$fieldData['max'] = trim($this->askWithValidation('max', 'Max value (for strings, this refers to string length)', ['required', 'string'], 0));
$fieldData['min'] = trim($this->askWithValidation('min', 'Min value (see Documentation)', ['required', 'string'], 0));
$fieldData['max'] = trim($this->askWithValidation('max', 'Max value (see Documentation)', ['required', 'string'], 0));
$lengthsValid = $this->validateLengths($fieldData['min'], $fieldData['max']);
} while (! $lengthsValid);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@ class PublicationFieldType implements SerializableContract
public readonly string $min;
public readonly string $name;
public readonly ?string $tagGroup;
public readonly ?PublicationType $publicationType; // Only used for validation command, interactive command doesn't need this

public static function fromArray(array $array): static
{
return new static(...$array);
}

public function __construct(string $type, string $name, int|string|null $min, int|string|null $max, ?string $tagGroup = null)
public function __construct(string $type, string $name, int|string|null $min, int|string|null $max, ?string $tagGroup = null, PublicationType $publicationType = null)
{
$this->type = PublicationFieldTypes::from(strtolower($type));
$this->name = Str::kebab($name);
$this->min = (string) $min;
$this->max = (string) $max;
$this->tagGroup = $tagGroup;
$this->publicationType = $publicationType;

if ($max < $min) {
throw new InvalidArgumentException("The 'max' value cannot be less than the 'min' value.");
Expand All @@ -52,6 +54,7 @@ public function toArray(): array
'name' => $this->name,
'min' => $this->min,
'max' => $this->max,
'tagGroup' => $this->tagGroup,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
use Hyde\Hyde;
use Hyde\Support\Concerns\Serializable;
use Hyde\Support\Contracts\SerializableContract;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use function json_decode;
use Rgasch\Collection\Collection;
use RuntimeException;

/**
Expand Down Expand Up @@ -95,12 +95,14 @@ public function getDirectory(): string
return $this->directory;
}

/** @return \Illuminate\Support\Collection<string, \Hyde\Framework\Features\Publications\Models\PublicationFieldType> */
/** @return \Rgasch\Collection\Collection<string, \Hyde\Framework\Features\Publications\Models\PublicationFieldType> */
public function getFields(): Collection
{
return collect($this->fields)->mapWithKeys(function (array $data): array {
$result = collect($this->fields)->mapWithKeys(function (array $data): array {
return [$data['name'] => new PublicationFieldType(...$data)];
});

return Collection::create($result, false);
}

public function save(?string $path = null): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function test_command_creates_publication_type()
9 => 'Local Image',
10 => 'Tag (select value from list)',
])
->expectsQuestion('Min value (for strings, this refers to string length)', '0')
->expectsQuestion('Max value (for strings, this refers to string length)', '0')
->expectsQuestion('Min value (see Documentation)', '0')
->expectsQuestion('Max value (see Documentation)', '0')
->expectsQuestion('<bg=magenta;fg=white>Add another field (y/n)</>', 'n')
->expectsChoice('Choose the default field you wish to sort by', 'dateCreated (meta field)', [
'dateCreated (meta field)',
Expand Down Expand Up @@ -86,7 +86,8 @@ public function test_command_creates_publication_type()
"type": "string",
"name": "publication-title",
"min": "0",
"max": "0"
"max": "0",
"tagGroup": null
}
]
}
Expand All @@ -102,10 +103,10 @@ public function test_cannot_create_field_with_lower_max_than_min_value()
$this->artisan('make:publicationType test-publication')
->expectsQuestion('Field name', 'foo')
->expectsQuestion('Field type', 'foo')
->expectsQuestion('Min value (for strings, this refers to string length)', 10)
->expectsQuestion('Max value (for strings, this refers to string length)', 5)
->expectsQuestion('Min value (for strings, this refers to string length)', 5)
->expectsQuestion('Max value (for strings, this refers to string length)', 10)
->expectsQuestion('Min value (see Documentation)', 10)
->expectsQuestion('Max value (see Documentation)', 5)
->expectsQuestion('Min value (see Documentation)', 5)
->expectsQuestion('Max value (see Documentation)', 10)

->expectsQuestion('<bg=magenta;fg=white>Add another field (y/n)</>', 'n')
->expectsQuestion('Choose the default field you wish to sort by', 'foo')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ public function test_can_get_field_as_array()
'name' => 'test',
'min' => '1',
'max' => '10',
'tagGroup' => null,
], $this->makeField()->toArray());
}

public function test_can_encode_field_as_json()
{
$this->assertSame('{"type":"string","name":"test","min":"1","max":"10"}', json_encode($this->makeField()));
$this->assertSame('{"type":"string","name":"test","min":"1","max":"10","tagGroup":null}', json_encode($this->makeField()));
}

public function test_null_range_values_are_cast_to_empty_string()
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/tests/Feature/PublicationTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function test_get_fields_method_returns_collection_of_field_objects()
$this->assertCount(1, $collection);
$this->assertInstanceOf(Collection::class, $collection);
$this->assertInstanceOf(PublicationFieldType::class, $collection->first());
$this->assertEquals(new Collection([
$this->assertEquals(new \Rgasch\Collection\Collection([
'title' => new PublicationFieldType('string', 'title', 0, 128),
]), $collection);
}
Expand Down

0 comments on commit bfee47a

Please sign in to comment.