2018-01-19 21:47:06 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Extensions\Spatie\Fractalistic;
|
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
use League\Fractal\Scope;
|
2018-01-25 22:34:53 -06:00
|
|
|
use Spatie\Fractal\Fractal as SpatieFractal;
|
2022-12-14 19:13:00 -07:00
|
|
|
use Pterodactyl\Transformers\Api\Transformer;
|
2018-01-19 21:47:06 -06:00
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
2018-01-27 12:38:56 -06:00
|
|
|
use Pterodactyl\Extensions\League\Fractal\Serializers\PterodactylSerializer;
|
2018-01-19 21:47:06 -06:00
|
|
|
|
|
|
|
class Fractal extends SpatieFractal
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create fractal data.
|
|
|
|
*
|
|
|
|
* @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation
|
|
|
|
* @throws \Spatie\Fractalistic\Exceptions\NoTransformerSpecified
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function createData(): Scope
|
2018-01-19 21:47:06 -06:00
|
|
|
{
|
|
|
|
// Set the serializer by default.
|
|
|
|
if (is_null($this->serializer)) {
|
2021-01-23 12:33:34 -08:00
|
|
|
$this->serializer = new PterodactylSerializer();
|
2018-01-19 21:47:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Automatically set the paginator on the response object if the
|
|
|
|
// data being provided implements a paginator.
|
|
|
|
if (is_null($this->paginator) && $this->data instanceof LengthAwarePaginator) {
|
|
|
|
$this->paginator = new IlluminatePaginatorAdapter($this->data);
|
|
|
|
}
|
|
|
|
|
2018-01-25 21:26:06 -06:00
|
|
|
// If the resource name is not set attempt to pull it off the transformer
|
|
|
|
// itself and set it automatically.
|
2022-12-14 19:13:00 -07:00
|
|
|
$class = is_string($this->transformer) ? new $this->transformer() : $this->transformer;
|
|
|
|
if (is_null($this->resourceName) && $class instanceof Transformer) {
|
2022-12-14 19:15:19 -07:00
|
|
|
$this->resourceName = $class->getResourceName();
|
2018-01-21 14:37:57 -06:00
|
|
|
}
|
|
|
|
|
2018-01-19 21:47:06 -06:00
|
|
|
return parent::createData();
|
|
|
|
}
|
|
|
|
}
|