|
4 | 4 |
|
5 | 5 | namespace Hyde\Framework\Features\Publications\Models;
|
6 | 6 |
|
| 7 | +use function dirname; |
| 8 | +use Exception; |
| 9 | +use function file_get_contents; |
| 10 | +use Hyde\Framework\Concerns\InteractsWithDirectories; |
7 | 11 | use Hyde\Hyde;
|
| 12 | +use Hyde\Support\Concerns\JsonSerializesArrayable; |
| 13 | +use Illuminate\Contracts\Support\Arrayable; |
| 14 | +use Illuminate\Contracts\Support\Jsonable; |
| 15 | +use Illuminate\Support\Str; |
| 16 | +use function json_decode; |
| 17 | +use JsonSerializable; |
| 18 | +use RuntimeException; |
8 | 19 |
|
9 |
| -class PublicationType |
| 20 | +/** |
| 21 | + * @see \Hyde\Framework\Testing\Feature\PublicationTypeTest |
| 22 | + */ |
| 23 | +class PublicationType implements JsonSerializable, Jsonable, Arrayable |
10 | 24 | {
|
11 |
| - protected string $schemaFile; |
| 25 | + use JsonSerializesArrayable; |
| 26 | + use InteractsWithDirectories; |
| 27 | + |
12 | 28 | protected string $directory;
|
13 |
| - protected array $schema; |
14 | 29 |
|
15 |
| - public static function get(string $name): self |
| 30 | + public string $name; |
| 31 | + public string $canonicalField; |
| 32 | + public string $sortField; |
| 33 | + public string $sortDirection; |
| 34 | + public int $pageSize; |
| 35 | + public bool $prevNextLinks; |
| 36 | + public string $detailTemplate; |
| 37 | + public string $listTemplate; |
| 38 | + public array $fields; |
| 39 | + |
| 40 | + public static function get(string $name): static |
| 41 | + { |
| 42 | + return static::fromFile(Hyde::path("$name/schema.json")); |
| 43 | + } |
| 44 | + |
| 45 | + public static function fromFile(string $schemaFile): static |
| 46 | + { |
| 47 | + try { |
| 48 | + return new static(...array_merge( |
| 49 | + static::parseSchemaFile($schemaFile), |
| 50 | + static::getRelativeDirectoryName($schemaFile)) |
| 51 | + ); |
| 52 | + } catch (Exception $exception) { |
| 53 | + throw new RuntimeException("Could not parse schema file $schemaFile", 0, $exception); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public function __construct(string $name, string $canonicalField, string $sortField, string $sortDirection, int $pageSize, bool $prevNextLinks, string $detailTemplate, string $listTemplate, array $fields, ?string $directory = null) |
16 | 58 | {
|
17 |
| - return new self(Hyde::path("$name/schema.json")); |
| 59 | + $this->name = $name; |
| 60 | + $this->canonicalField = $canonicalField; |
| 61 | + $this->sortField = $sortField; |
| 62 | + $this->sortDirection = $sortDirection; |
| 63 | + $this->pageSize = $pageSize; |
| 64 | + $this->prevNextLinks = $prevNextLinks; |
| 65 | + $this->detailTemplate = $detailTemplate; |
| 66 | + $this->listTemplate = $listTemplate; |
| 67 | + $this->fields = $fields; |
| 68 | + |
| 69 | + if ($directory) { |
| 70 | + $this->directory = $directory; |
| 71 | + } |
18 | 72 | }
|
19 | 73 |
|
20 |
| - public function __construct(string $schemaFile) |
| 74 | + public function toArray(): array |
21 | 75 | {
|
22 |
| - $this->schemaFile = $schemaFile; |
23 |
| - $this->directory = Hyde::pathToRelative(dirname($schemaFile)); |
24 |
| - $this->schema = static::parseSchema($schemaFile); |
| 76 | + return [ |
| 77 | + 'name' => $this->name, |
| 78 | + 'canonicalField' => $this->canonicalField, |
| 79 | + 'sortField' => $this->sortField, |
| 80 | + 'sortDirection' => $this->sortDirection, |
| 81 | + 'pageSize' => $this->pageSize, |
| 82 | + 'prevNextLinks' => $this->prevNextLinks, |
| 83 | + 'detailTemplate' => $this->detailTemplate, |
| 84 | + 'listTemplate' => $this->listTemplate, |
| 85 | + 'fields' => $this->fields, |
| 86 | + ]; |
25 | 87 | }
|
26 | 88 |
|
27 |
| - public function __get(string $name): mixed |
| 89 | + public function toJson($options = JSON_PRETTY_PRINT): string |
28 | 90 | {
|
29 |
| - return $this->$name ?? $this->schema[$name] ?? null; |
| 91 | + return json_encode($this->toArray(), $options); |
| 92 | + } |
| 93 | + |
| 94 | + public function getIdentifier(): string |
| 95 | + { |
| 96 | + return $this->directory ?? Str::slug($this->name); |
30 | 97 | }
|
31 | 98 |
|
32 | 99 | public function getSchemaFile(): string
|
33 | 100 | {
|
34 |
| - return $this->schemaFile; |
| 101 | + return "$this->directory/schema.json"; |
35 | 102 | }
|
36 | 103 |
|
37 | 104 | public function getDirectory(): string
|
38 | 105 | {
|
39 | 106 | return $this->directory;
|
40 | 107 | }
|
41 | 108 |
|
42 |
| - public function getSchema(): array |
| 109 | + public function save(?string $path = null): void |
43 | 110 | {
|
44 |
| - return $this->schema; |
| 111 | + $path ??= $this->getSchemaFile(); |
| 112 | + $this->needsParentDirectory($path); |
| 113 | + file_put_contents($path, json_encode($this->toArray(), JSON_PRETTY_PRINT)); |
45 | 114 | }
|
46 | 115 |
|
47 |
| - protected static function parseSchema(string $schemaFile): array |
| 116 | + protected static function parseSchemaFile(string $schemaFile): array |
48 | 117 | {
|
49 | 118 | return json_decode(file_get_contents($schemaFile), true, 512, JSON_THROW_ON_ERROR);
|
50 | 119 | }
|
51 | 120 |
|
52 |
| - // TODO build list pages and detail pages for each publication type |
| 121 | + protected static function getRelativeDirectoryName(string $schemaFile): array |
| 122 | + { |
| 123 | + return ['directory' => Hyde::pathToRelative(dirname($schemaFile))]; |
| 124 | + } |
53 | 125 | }
|
0 commit comments