2018-02-25 21:30:56 +00:00
|
|
|
<?php
|
|
|
|
|
2018-02-28 03:28:43 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client;
|
2018-02-25 21:30:56 +00:00
|
|
|
|
2018-02-28 03:28:43 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2021-08-07 20:06:45 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Transformer;
|
2018-02-28 03:28:43 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
2018-02-25 21:30:56 +00:00
|
|
|
|
|
|
|
abstract class ClientApiController extends ApplicationApiController
|
|
|
|
{
|
2020-07-07 04:25:00 +00:00
|
|
|
/**
|
|
|
|
* Returns only the includes which are valid for the given transformer.
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2021-08-07 20:06:45 +00:00
|
|
|
protected function getIncludesForTransformer(Transformer $transformer, array $merge = []): array
|
2020-07-07 04:25:00 +00:00
|
|
|
{
|
|
|
|
$filtered = array_filter($this->parseIncludes(), function ($datum) use ($transformer) {
|
|
|
|
return in_array($datum, $transformer->getAvailableIncludes());
|
|
|
|
});
|
|
|
|
|
|
|
|
return array_merge($filtered, $merge);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the parsed includes for this request.
|
2020-07-10 02:17:24 +00:00
|
|
|
*
|
|
|
|
* @return string[]
|
2020-07-07 04:25:00 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
protected function parseIncludes(): array
|
2020-07-07 04:25:00 +00:00
|
|
|
{
|
2020-07-10 02:17:24 +00:00
|
|
|
$includes = $this->request->query('include') ?? [];
|
2020-07-07 04:25:00 +00:00
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!is_string($includes)) {
|
2020-07-07 04:25:00 +00:00
|
|
|
return $includes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_map(function ($item) {
|
|
|
|
return trim($item);
|
|
|
|
}, explode(',', $includes));
|
|
|
|
}
|
|
|
|
|
2018-02-25 21:30:56 +00:00
|
|
|
/**
|
|
|
|
* Return an instance of an application transformer.
|
|
|
|
*
|
2021-08-07 20:06:45 +00:00
|
|
|
* @return \Pterodactyl\Transformers\Api\Transformer
|
2021-03-09 15:14:48 +00:00
|
|
|
*
|
2021-08-07 20:06:45 +00:00
|
|
|
* @deprecated
|
2018-02-25 21:30:56 +00:00
|
|
|
*/
|
2021-08-07 20:06:45 +00:00
|
|
|
public function getTransformer(string $class)
|
2018-02-25 21:30:56 +00:00
|
|
|
{
|
2021-08-07 20:06:45 +00:00
|
|
|
$transformer = new $class;
|
2018-02-25 21:30:56 +00:00
|
|
|
|
2021-08-07 20:06:45 +00:00
|
|
|
Assert::same(substr($class, 0, strlen(class_basename($class)) * -1), '\Pterodactyl\Transformers\Api\Client\\');
|
2018-02-25 21:30:56 +00:00
|
|
|
|
|
|
|
return $transformer;
|
|
|
|
}
|
|
|
|
}
|