Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Extensions/Spatie/Fractalistic/Fractal.php
10311 views
1
<?php
2
3
namespace Pterodactyl\Extensions\Spatie\Fractalistic;
4
5
use League\Fractal\Scope;
6
use League\Fractal\TransformerAbstract;
7
use Spatie\Fractal\Fractal as SpatieFractal;
8
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
9
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
10
use Pterodactyl\Extensions\League\Fractal\Serializers\PterodactylSerializer;
11
12
class Fractal extends SpatieFractal
13
{
14
/**
15
* Create fractal data.
16
*
17
* @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation
18
* @throws \Spatie\Fractalistic\Exceptions\NoTransformerSpecified
19
*/
20
public function createData(): Scope
21
{
22
// Set the serializer by default.
23
if (is_null($this->serializer)) {
24
$this->serializer = new PterodactylSerializer();
25
}
26
27
// Automatically set the paginator on the response object if the
28
// data being provided implements a paginator.
29
if (is_null($this->paginator) && $this->data instanceof LengthAwarePaginator) {
30
$this->paginator = new IlluminatePaginatorAdapter($this->data);
31
}
32
33
// If the resource name is not set attempt to pull it off the transformer
34
// itself and set it automatically.
35
if (
36
is_null($this->resourceName)
37
&& $this->transformer instanceof TransformerAbstract
38
&& method_exists($this->transformer, 'getResourceName')
39
) {
40
$this->resourceName = $this->transformer->getResourceName();
41
}
42
43
return parent::createData();
44
}
45
}
46
47