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

Extract class for pagination settings #732

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -55,14 +55,14 @@ public function safeHandle(): int

$sortField = $this->getSortField($fields);

$sortDirection = $this->getSortDirection();
$sortAscending = $this->getSortDirection();

$pageSize = $this->getPageSize();
$prevNextLinks = $this->getPrevNextLinks();

$canonicalField = $this->getCanonicalField($fields);

$creator = new CreatesNewPublicationType($title, $fields, $canonicalField, $sortField, $sortDirection, $pageSize, $prevNextLinks, $this->output);
$creator = new CreatesNewPublicationType($title, $fields, $canonicalField, $sortField, $sortAscending, $prevNextLinks, $pageSize, $this->output);
$creator->create();

$this->info('Publication type created successfully!');
Expand Down Expand Up @@ -131,11 +131,11 @@ protected function getSortField(Collection $fields): string
return $selected === 'dateCreated (meta field)' ? '__createdAt' : $options[(array_flip($options)[$selected])];
}

protected function getSortDirection(): string
protected function getSortDirection(): bool
{
$options = [
'Ascending (oldest items first if sorting by dateCreated)' => 'ASC',
'Descending (newest items first if sorting by dateCreated)' => 'DESC',
'Ascending (oldest items first if sorting by dateCreated)' => true,
'Descending (newest items first if sorting by dateCreated)' => false,
];

return $options[$this->choice('Choose the default sort direction', array_keys($options), 'Ascending (oldest items first if sorting by dateCreated)')];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function __construct(
protected Collection $fields,
protected string $canonicalField,
protected string $sortField,
protected string $sortDirection,
protected int $pageSize,
protected bool $sortAscending,
protected bool $prevNextLinks,
protected int $pageSize,
protected ?OutputStyle $output = null,
) {
$this->dirName = $this->formatStringForStorage($this->name);
Expand All @@ -39,12 +39,14 @@ protected function handleCreate(): void
$type = new PublicationType(
$this->name,
$this->canonicalField,
$this->sortField,
$this->sortDirection,
$this->pageSize,
$this->prevNextLinks,
"{$this->dirName}_detail",
"{$this->dirName}_list",
[
$this->sortField,
$this->sortAscending,
$this->prevNextLinks,
$this->pageSize,
],
$this->fields->toArray()
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Features\Publications\Models;

use Hyde\Support\Concerns\Serializable;
use Hyde\Support\Contracts\SerializableContract;

class PaginationSettings implements SerializableContract
{
use Serializable;

public string $sortField = '__createdAt';
public bool $sortAscending = true;
public bool $prevNextLinks = true;
public int $pageSize = 25;

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

public function __construct(string $sortField = '__createdAt', bool $sortAscending = true, bool $prevNextLinks = true, int $pageSize = 25)
{
$this->sortField = $sortField;
$this->sortAscending = $sortAscending;
$this->prevNextLinks = $prevNextLinks;
$this->pageSize = $pageSize;
}

public function toArray(): array
{
return [
'sortField' => $this->sortField,
'sortAscending' => $this->sortAscending,
'prevNextLinks' => $this->prevNextLinks,
'pageSize' => $this->pageSize,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class PublicationType implements SerializableContract
use Serializable;
use InteractsWithDirectories;

public PaginationSettings|array $pagination = [];
protected string $directory;

/** @var array<array<string, mixed>> */
Expand All @@ -49,30 +50,27 @@ public static function fromFile(string $schemaFile): static
public function __construct(
public string $name,
public string $canonicalField = 'identifier',
public string $sortField = '__createdAt',
public string $sortDirection = 'DESC',
public int $pageSize = 25,
public bool $prevNextLinks = true,
public string $detailTemplate = 'detail',
public string $listTemplate = 'list',
array|PaginationSettings $pagination = [],
array $fields = [],
?string $directory = null
) {
$this->fields = $fields;
$this->directory = $directory ?? Str::slug($name);
$this->pagination = $pagination instanceof PaginationSettings
? $pagination
: PaginationSettings::fromArray($pagination);
}

public function toArray(): array
{
return [
'name' => $this->name,
'canonicalField' => $this->canonicalField,
'sortField' => $this->sortField,
'sortDirection' => $this->sortDirection,
'pageSize' => $this->pageSize,
'prevNextLinks' => $this->prevNextLinks,
'detailTemplate' => $this->detailTemplate,
'listTemplate' => $this->listTemplate,
'pagination' => $this->pagination->toArray(),
'fields' => $this->fields,
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,15 @@ protected function makePublicationType(): PublicationType
return new PublicationType(
'test',
'title',
'sort',
'asc',
10,
true,
'detail',
'list',
[
fields: [
[
'type' => 'string',
'name' => 'title',
'min' => 0,
'max' => 128,
],
],
'test-publication',
directory: 'test-publication',
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CreatesNewPublicationTypeTest extends TestCase
{
public function test_it_creates_a_new_publication_type()
{
$creator = new CreatesNewPublicationType('name', new Collection(), 'canonical', 'sort', 'asc', 10, true);
$creator = new CreatesNewPublicationType('name', new Collection(), 'canonical', 'sort', true, true, 10);
$creator->create();

$this->assertFileExists(Hyde::path('name/schema.json'));
Expand All @@ -28,7 +28,7 @@ public function test_it_creates_a_new_publication_type()
$this->assertStringContainsString('"name": "name"', $result);
$this->assertStringContainsString('"canonicalField": "canonical"', $result);
$this->assertStringContainsString('"sortField": "sort"', $result);
$this->assertStringContainsString('"sortDirection": "asc"', $result);
$this->assertStringContainsString('"sortAscending": true', $result);
$this->assertStringContainsString('"pageSize": 10', $result);
$this->assertStringContainsString('"prevNextLinks": true', $result);
$this->assertStringContainsString('"detailTemplate": "name_detail"', $result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ protected function makeSchemaFile(): void
json_encode([
'name' => 'Test Publication',
'canonicalField' => 'title',
'sortField' => '__createdAt',
'sortDirection' => 'ASC',
'pageSize' => 10,
'prevNextLinks' => true,
'detailTemplate' => 'test-publication_detail',
'listTemplate' => 'test-publication_list',
'pagination' => [
'pageSize' => 10,
'prevNextLinks' => true,
'sortField' => '__createdAt',
'sortAscending' => true,
],
'fields' => [
[
'name' => 'title',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ public function test_command_creates_publication_type()
{
"name": "Test Publication",
"canonicalField": "publication-title",
"sortField": "__createdAt",
"sortDirection": "ASC",
"pageSize": 10,
"prevNextLinks": true,
"detailTemplate": "test-publication_detail",
"listTemplate": "test-publication_list",
"pagination": {
"sortField": "__createdAt",
"sortAscending": true,
"prevNextLinks": true,
"pageSize": 10
},
"fields": [
{
"type": "string",
Expand Down
10 changes: 6 additions & 4 deletions packages/framework/tests/Feature/PublicationListPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ protected function getTestData(): array
return [
'name' => 'test',
'canonicalField' => 'canonical',
'sortField' => 'sort',
'sortDirection' => 'asc',
'pageSize' => 10,
'prevNextLinks' => true,
'detailTemplate' => 'detail',
'listTemplate' => 'list',
'pagination' => [
'sortField' => 'sort',
'sortAscending' => true,
'pageSize' => 10,
'prevNextLinks' => true,
],
'fields' => [
'foo' => 'bar',
],
Expand Down
10 changes: 6 additions & 4 deletions packages/framework/tests/Feature/PublicationPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ protected function createRealPublicationFiles(): void
file_put_contents(Hyde::path('test-publication/schema.json'), '{
"name": "test",
"canonicalField": "slug",
"sortField": "__createdAt",
"sortDirection": "DESC",
"pageSize": 0,
"prevNextLinks": true,
"detailTemplate": "test_detail",
"listTemplate": "test_list",
"pagination": {
"sortField": "__createdAt",
"sortAscending": true,
"pageSize": 0,
"prevNextLinks": true
},
"fields": [
{
"name": "slug",
Expand Down
27 changes: 18 additions & 9 deletions packages/framework/tests/Feature/PublicationTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\Framework\Testing\Feature;

use function array_merge;
use Hyde\Framework\Features\Publications\Models\PaginationSettings;
use Hyde\Framework\Features\Publications\Models\PublicationFieldType;
use Hyde\Framework\Features\Publications\Models\PublicationListPage;
use Hyde\Framework\Features\Publications\Models\PublicationType;
Expand All @@ -24,7 +25,11 @@ public function test_can_construct_new_publication_type()
$publicationType = new PublicationType(...$this->getTestData());

foreach ($this->getTestData() as $key => $property) {
$this->assertEquals($property, $publicationType->$key);
if ($key === 'pagination') {
$this->assertEquals($property, $publicationType->$key->toArray());
} else {
$this->assertEquals($property, $publicationType->$key);
}
}
}

Expand All @@ -34,13 +39,15 @@ public function testConstructWithDefaultValues()

$this->assertEquals('Test Publication', $publicationType->name);
$this->assertEquals('identifier', $publicationType->canonicalField);
$this->assertEquals('__createdAt', $publicationType->sortField);
$this->assertEquals('DESC', $publicationType->sortDirection);
$this->assertEquals(25, $publicationType->pageSize);
$this->assertEquals(true, $publicationType->prevNextLinks);
$this->assertEquals('detail', $publicationType->detailTemplate);
$this->assertEquals('list', $publicationType->listTemplate);
$this->assertEquals([], $publicationType->fields);
$this->assertEquals(PaginationSettings::fromArray([
'sortField' => '__createdAt',
'sortAscending' => true,
'pageSize' => 25,
'prevNextLinks' => true,
]), $publicationType->pagination);

$this->assertEquals('test-publication', $publicationType->getDirectory());
}
Expand Down Expand Up @@ -151,12 +158,14 @@ protected function getTestData(): array
return [
'name' => 'Test Publication',
'canonicalField' => 'title',
'sortField' => '__createdAt',
'sortDirection' => 'DESC',
'pageSize' => 25,
'prevNextLinks' => true,
'detailTemplate' => 'test-publication_detail',
'listTemplate' => 'test-publication_list',
'pagination' => [
'sortField' => '__createdAt',
'sortAscending' => true,
'prevNextLinks' => true,
'pageSize' => 25,
],
'fields' => [
[
'name' => 'title',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,9 @@ protected function pubType(): PublicationType
return new PublicationType(
'name',
'canonicalField',
'sortField',
'sortDirection',
1,
true,
'detailTemplate',
'listTemplate',
['sortField', true, true, 1],
[],
'directory'
);
Expand Down
Loading