Skip to content

Commit 67ca3ce

Browse files
author
github-actions
committed
Merge pull request #398 from hydephp/improve-serialization
Improve model serializations hydephp/develop@4048f59
1 parent aea7f85 commit 67ca3ce

File tree

6 files changed

+67
-6
lines changed

6 files changed

+67
-6
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Hyde\Framework\Concerns;
4+
5+
trait JsonSerializesArrayable
6+
{
7+
/** @inheritDoc */
8+
public function jsonSerialize()
9+
{
10+
return $this->toArray();
11+
}
12+
}

src/Helpers/Features.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Hyde\Framework\Helpers;
44

5+
use Hyde\Framework\Concerns\JsonSerializesArrayable;
56
use Hyde\Framework\Hyde;
7+
use Illuminate\Contracts\Support\Arrayable;
8+
use Illuminate\Support\Str;
69

710
/**
811
* Allows features to be enabled and disabled in a simple object-oriented manner.
@@ -12,8 +15,10 @@
1215
* Based entirely on Laravel Jetstream (License MIT)
1316
* @see https://jetstream.laravel.com/
1417
*/
15-
class Features
18+
class Features implements Arrayable, \JsonSerializable
1619
{
20+
use JsonSerializesArrayable;
21+
1722
/**
1823
* Determine if the given specified is enabled.
1924
*
@@ -154,4 +159,17 @@ public static function rss(): bool
154159
&& config('hyde.generate_rss_feed', true)
155160
&& extension_loaded('simplexml');
156161
}
162+
163+
/** @inheritDoc */
164+
public function toArray()
165+
{
166+
$array = [];
167+
foreach (get_class_methods(static::class) as $method) {
168+
if (str_starts_with($method, 'has')) {
169+
$array[Str::kebab(substr($method, 3))] = static::{$method}();
170+
}
171+
}
172+
173+
return $array;
174+
}
157175
}

src/HydeKernel.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
namespace Hyde\Framework;
44

55
use Composer\InstalledVersions;
6+
use Hyde\Framework\Concerns\JsonSerializesArrayable;
67
use Hyde\Framework\Contracts\HydeKernelContract;
78
use Hyde\Framework\Contracts\RouteContract;
89
use Hyde\Framework\Foundation\Filesystem;
910
use Hyde\Framework\Foundation\Hyperlinks;
1011
use Hyde\Framework\Helpers\Features;
12+
use Illuminate\Contracts\Support\Arrayable;
1113
use Illuminate\Support\Facades\View;
1214
use Illuminate\Support\Str;
1315
use Illuminate\Support\Traits\Macroable;
@@ -23,9 +25,10 @@
2325
*
2426
* @link https://hydephp.com/
2527
*/
26-
class HydeKernel implements HydeKernelContract
28+
class HydeKernel implements HydeKernelContract, Arrayable, \JsonSerializable
2729
{
2830
use Macroable;
31+
use JsonSerializesArrayable;
2932

3033
protected string $basePath;
3134
protected Filesystem $filesystem;
@@ -207,4 +210,15 @@ public function pathToRelative(string $path): string
207210
{
208211
return $this->filesystem->pathToRelative($path);
209212
}
213+
214+
/** @inheritDoc */
215+
public function toArray()
216+
{
217+
return [
218+
'basePath' => $this->basePath,
219+
'features' => $this->features(),
220+
'pages' => $this->pages(),
221+
'routes' => $this->routes(),
222+
];
223+
}
210224
}

src/Models/FrontMatter.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Hyde\Framework\Models;
44

55
use Hyde\Framework\Actions\ConvertsArrayToFrontMatter;
6+
use Hyde\Framework\Concerns\JsonSerializesArrayable;
67
use Illuminate\Contracts\Support\Arrayable;
78
use Illuminate\Support\Arr;
89

@@ -19,8 +20,10 @@
1920
*
2021
* @see \Hyde\Framework\Testing\Unit\FrontMatterModelTest
2122
*/
22-
class FrontMatter implements Arrayable, \Stringable
23+
class FrontMatter implements Arrayable, \Stringable, \JsonSerializable
2324
{
25+
use JsonSerializesArrayable;
26+
2427
public array $data;
2528

2629
public function __construct(array $matter = [])

src/Models/Route.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22

33
namespace Hyde\Framework\Models;
44

5+
use Hyde\Framework\Concerns\JsonSerializesArrayable;
56
use Hyde\Framework\Contracts\PageContract;
67
use Hyde\Framework\Contracts\RouteContract;
78
use Hyde\Framework\Contracts\RouteFacadeContract;
89
use Hyde\Framework\Exceptions\RouteNotFoundException;
910
use Hyde\Framework\Hyde;
1011
use Hyde\Framework\Services\RoutingService;
12+
use Illuminate\Contracts\Support\Arrayable;
1113
use Illuminate\Support\Collection;
1214

1315
/**
1416
* @see \Hyde\Framework\Testing\Feature\RouteTest
1517
*/
16-
class Route implements RouteContract, RouteFacadeContract, \Stringable
18+
class Route implements RouteContract, RouteFacadeContract, \Stringable, \JsonSerializable, Arrayable
1719
{
20+
use JsonSerializesArrayable;
21+
1822
/**
1923
* The source model for the route.
2024
*
@@ -36,6 +40,16 @@ public function __construct(PageContract $sourceModel)
3640
$this->routeKey = $this->constructRouteKey();
3741
}
3842

43+
/** @inheritDoc */
44+
public function toArray()
45+
{
46+
return [
47+
'routeKey' => $this->routeKey,
48+
'sourceModelPath' => $this->sourceModel->getSourcePath(),
49+
'sourceModelType' => $this->sourceModel::class,
50+
];
51+
}
52+
3953
/** @inheritDoc */
4054
public function getPageType(): string
4155
{

tests/Feature/RouteCollectionTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function test_boot_method_discovers_all_pages()
4141
$this->assertEquals([
4242
'404' => (new Route(new BladePage('404'))),
4343
'index' => (new Route(new BladePage('index'))),
44-
], $collection->toArray());
44+
], $collection->all());
4545
}
4646

4747
public function test_boot_method_discovers_all_page_types()
@@ -63,7 +63,7 @@ public function test_boot_method_discovers_all_page_types()
6363
'markdown' => (new Route(new MarkdownPage('markdown'))),
6464
'posts/post' => (new Route(new MarkdownPost('post'))),
6565
'docs/docs' => (new Route(new DocumentationPage('docs'))),
66-
], $collection->toArray());
66+
], $collection->all());
6767

6868
$this->restoreDefaultPages();
6969
}

0 commit comments

Comments
 (0)