Upgrade to 3.7.1

This commit is contained in:
Bastian Allgeier
2022-07-12 13:33:21 +02:00
parent 7931eb5e47
commit 1ad1eaf387
377 changed files with 63981 additions and 63824 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -19,160 +19,160 @@ use Kirby\Toolkit\Str;
*/
class Collection
{
/**
* @var \Kirby\Api\Api
*/
protected $api;
/**
* @var \Kirby\Api\Api
*/
protected $api;
/**
* @var mixed|null
*/
protected $data;
/**
* @var mixed|null
*/
protected $data;
/**
* @var mixed|null
*/
protected $model;
/**
* @var mixed|null
*/
protected $model;
/**
* @var mixed|null
*/
protected $select;
/**
* @var mixed|null
*/
protected $select;
/**
* @var mixed|null
*/
protected $view;
/**
* @var mixed|null
*/
protected $view;
/**
* Collection constructor
*
* @param \Kirby\Api\Api $api
* @param mixed|null $data
* @param array $schema
* @throws \Exception
*/
public function __construct(Api $api, $data, array $schema)
{
$this->api = $api;
$this->data = $data;
$this->model = $schema['model'] ?? null;
$this->view = $schema['view'] ?? null;
/**
* Collection constructor
*
* @param \Kirby\Api\Api $api
* @param mixed|null $data
* @param array $schema
* @throws \Exception
*/
public function __construct(Api $api, $data, array $schema)
{
$this->api = $api;
$this->data = $data;
$this->model = $schema['model'] ?? null;
$this->view = $schema['view'] ?? null;
if ($data === null) {
if (is_a($schema['default'] ?? null, 'Closure') === false) {
throw new Exception('Missing collection data');
}
if ($data === null) {
if (is_a($schema['default'] ?? null, 'Closure') === false) {
throw new Exception('Missing collection data');
}
$this->data = $schema['default']->call($this->api);
}
$this->data = $schema['default']->call($this->api);
}
if (
isset($schema['type']) === true &&
is_a($this->data, $schema['type']) === false
) {
throw new Exception('Invalid collection type');
}
}
if (
isset($schema['type']) === true &&
is_a($this->data, $schema['type']) === false
) {
throw new Exception('Invalid collection type');
}
}
/**
* @param string|array|null $keys
* @return $this
* @throws \Exception
*/
public function select($keys = null)
{
if ($keys === false) {
return $this;
}
/**
* @param string|array|null $keys
* @return $this
* @throws \Exception
*/
public function select($keys = null)
{
if ($keys === false) {
return $this;
}
if (is_string($keys)) {
$keys = Str::split($keys);
}
if (is_string($keys)) {
$keys = Str::split($keys);
}
if ($keys !== null && is_array($keys) === false) {
throw new Exception('Invalid select keys');
}
if ($keys !== null && is_array($keys) === false) {
throw new Exception('Invalid select keys');
}
$this->select = $keys;
return $this;
}
$this->select = $keys;
return $this;
}
/**
* @return array
* @throws \Kirby\Exception\NotFoundException
* @throws \Exception
*/
public function toArray(): array
{
$result = [];
/**
* @return array
* @throws \Kirby\Exception\NotFoundException
* @throws \Exception
*/
public function toArray(): array
{
$result = [];
foreach ($this->data as $item) {
$model = $this->api->model($this->model, $item);
foreach ($this->data as $item) {
$model = $this->api->model($this->model, $item);
if ($this->view !== null) {
$model = $model->view($this->view);
}
if ($this->view !== null) {
$model = $model->view($this->view);
}
if ($this->select !== null) {
$model = $model->select($this->select);
}
if ($this->select !== null) {
$model = $model->select($this->select);
}
$result[] = $model->toArray();
}
$result[] = $model->toArray();
}
return $result;
}
return $result;
}
/**
* @return array
* @throws \Kirby\Exception\NotFoundException
* @throws \Exception
*/
public function toResponse(): array
{
if ($query = $this->api->requestQuery('query')) {
$this->data = $this->data->query($query);
}
/**
* @return array
* @throws \Kirby\Exception\NotFoundException
* @throws \Exception
*/
public function toResponse(): array
{
if ($query = $this->api->requestQuery('query')) {
$this->data = $this->data->query($query);
}
if (!$this->data->pagination()) {
$this->data = $this->data->paginate([
'page' => $this->api->requestQuery('page', 1),
'limit' => $this->api->requestQuery('limit', 100)
]);
}
if (!$this->data->pagination()) {
$this->data = $this->data->paginate([
'page' => $this->api->requestQuery('page', 1),
'limit' => $this->api->requestQuery('limit', 100)
]);
}
$pagination = $this->data->pagination();
$pagination = $this->data->pagination();
if ($select = $this->api->requestQuery('select')) {
$this->select($select);
}
if ($select = $this->api->requestQuery('select')) {
$this->select($select);
}
if ($view = $this->api->requestQuery('view')) {
$this->view($view);
}
if ($view = $this->api->requestQuery('view')) {
$this->view($view);
}
return [
'code' => 200,
'data' => $this->toArray(),
'pagination' => [
'page' => $pagination->page(),
'total' => $pagination->total(),
'offset' => $pagination->offset(),
'limit' => $pagination->limit(),
],
'status' => 'ok',
'type' => 'collection'
];
}
return [
'code' => 200,
'data' => $this->toArray(),
'pagination' => [
'page' => $pagination->page(),
'total' => $pagination->total(),
'offset' => $pagination->offset(),
'limit' => $pagination->limit(),
],
'status' => 'ok',
'type' => 'collection'
];
}
/**
* @param string $view
* @return $this
*/
public function view(string $view)
{
$this->view = $view;
return $this;
}
/**
* @param string $view
* @return $this
*/
public function view(string $view)
{
$this->view = $view;
return $this;
}
}

View File

@@ -21,228 +21,228 @@ use Kirby\Toolkit\Str;
*/
class Model
{
/**
* @var \Kirby\Api\Api
*/
protected $api;
/**
* @var \Kirby\Api\Api
*/
protected $api;
/**
* @var mixed|null
*/
protected $data;
/**
* @var mixed|null
*/
protected $data;
/**
* @var array|mixed
*/
protected $fields;
/**
* @var array|mixed
*/
protected $fields;
/**
* @var mixed|null
*/
protected $select;
/**
* @var mixed|null
*/
protected $select;
/**
* @var array|mixed
*/
protected $views;
/**
* @var array|mixed
*/
protected $views;
/**
* Model constructor
*
* @param \Kirby\Api\Api $api
* @param mixed $data
* @param array $schema
* @throws \Exception
*/
public function __construct(Api $api, $data, array $schema)
{
$this->api = $api;
$this->data = $data;
$this->fields = $schema['fields'] ?? [];
$this->select = $schema['select'] ?? null;
$this->views = $schema['views'] ?? [];
/**
* Model constructor
*
* @param \Kirby\Api\Api $api
* @param mixed $data
* @param array $schema
* @throws \Exception
*/
public function __construct(Api $api, $data, array $schema)
{
$this->api = $api;
$this->data = $data;
$this->fields = $schema['fields'] ?? [];
$this->select = $schema['select'] ?? null;
$this->views = $schema['views'] ?? [];
if ($this->select === null && array_key_exists('default', $this->views)) {
$this->view('default');
}
if ($this->select === null && array_key_exists('default', $this->views)) {
$this->view('default');
}
if ($data === null) {
if (is_a($schema['default'] ?? null, 'Closure') === false) {
throw new Exception('Missing model data');
}
if ($data === null) {
if (is_a($schema['default'] ?? null, 'Closure') === false) {
throw new Exception('Missing model data');
}
$this->data = $schema['default']->call($this->api);
}
$this->data = $schema['default']->call($this->api);
}
if (
isset($schema['type']) === true &&
is_a($this->data, $schema['type']) === false
) {
throw new Exception(sprintf('Invalid model type "%s" expected: "%s"', get_class($this->data), $schema['type']));
}
}
if (
isset($schema['type']) === true &&
is_a($this->data, $schema['type']) === false
) {
throw new Exception(sprintf('Invalid model type "%s" expected: "%s"', get_class($this->data), $schema['type']));
}
}
/**
* @param null $keys
* @return $this
* @throws \Exception
*/
public function select($keys = null)
{
if ($keys === false) {
return $this;
}
/**
* @param null $keys
* @return $this
* @throws \Exception
*/
public function select($keys = null)
{
if ($keys === false) {
return $this;
}
if (is_string($keys)) {
$keys = Str::split($keys);
}
if (is_string($keys)) {
$keys = Str::split($keys);
}
if ($keys !== null && is_array($keys) === false) {
throw new Exception('Invalid select keys');
}
if ($keys !== null && is_array($keys) === false) {
throw new Exception('Invalid select keys');
}
$this->select = $keys;
return $this;
}
$this->select = $keys;
return $this;
}
/**
* @return array
* @throws \Exception
*/
public function selection(): array
{
$select = $this->select;
/**
* @return array
* @throws \Exception
*/
public function selection(): array
{
$select = $this->select;
if ($select === null) {
$select = array_keys($this->fields);
}
if ($select === null) {
$select = array_keys($this->fields);
}
$selection = [];
$selection = [];
foreach ($select as $key => $value) {
if (is_int($key) === true) {
$selection[$value] = [
'view' => null,
'select' => null
];
continue;
}
foreach ($select as $key => $value) {
if (is_int($key) === true) {
$selection[$value] = [
'view' => null,
'select' => null
];
continue;
}
if (is_string($value) === true) {
if ($value === 'any') {
throw new Exception('Invalid sub view: "any"');
}
if (is_string($value) === true) {
if ($value === 'any') {
throw new Exception('Invalid sub view: "any"');
}
$selection[$key] = [
'view' => $value,
'select' => null
];
$selection[$key] = [
'view' => $value,
'select' => null
];
continue;
}
continue;
}
if (is_array($value) === true) {
$selection[$key] = [
'view' => null,
'select' => $value
];
}
}
if (is_array($value) === true) {
$selection[$key] = [
'view' => null,
'select' => $value
];
}
}
return $selection;
}
return $selection;
}
/**
* @return array
* @throws \Kirby\Exception\NotFoundException
* @throws \Exception
*/
public function toArray(): array
{
$select = $this->selection();
$result = [];
/**
* @return array
* @throws \Kirby\Exception\NotFoundException
* @throws \Exception
*/
public function toArray(): array
{
$select = $this->selection();
$result = [];
foreach ($this->fields as $key => $resolver) {
if (array_key_exists($key, $select) === false || is_a($resolver, 'Closure') === false) {
continue;
}
foreach ($this->fields as $key => $resolver) {
if (array_key_exists($key, $select) === false || is_a($resolver, 'Closure') === false) {
continue;
}
$value = $resolver->call($this->api, $this->data);
$value = $resolver->call($this->api, $this->data);
if (is_object($value)) {
$value = $this->api->resolve($value);
}
if (is_object($value)) {
$value = $this->api->resolve($value);
}
if (
is_a($value, 'Kirby\Api\Collection') === true ||
is_a($value, 'Kirby\Api\Model') === true
) {
$selection = $select[$key];
if (
is_a($value, 'Kirby\Api\Collection') === true ||
is_a($value, 'Kirby\Api\Model') === true
) {
$selection = $select[$key];
if ($subview = $selection['view']) {
$value->view($subview);
}
if ($subview = $selection['view']) {
$value->view($subview);
}
if ($subselect = $selection['select']) {
$value->select($subselect);
}
if ($subselect = $selection['select']) {
$value->select($subselect);
}
$value = $value->toArray();
}
$value = $value->toArray();
}
$result[$key] = $value;
}
$result[$key] = $value;
}
ksort($result);
ksort($result);
return $result;
}
return $result;
}
/**
* @return array
* @throws \Kirby\Exception\NotFoundException
* @throws \Exception
*/
public function toResponse(): array
{
$model = $this;
/**
* @return array
* @throws \Kirby\Exception\NotFoundException
* @throws \Exception
*/
public function toResponse(): array
{
$model = $this;
if ($select = $this->api->requestQuery('select')) {
$model = $model->select($select);
}
if ($select = $this->api->requestQuery('select')) {
$model = $model->select($select);
}
if ($view = $this->api->requestQuery('view')) {
$model = $model->view($view);
}
if ($view = $this->api->requestQuery('view')) {
$model = $model->view($view);
}
return [
'code' => 200,
'data' => $model->toArray(),
'status' => 'ok',
'type' => 'model'
];
}
return [
'code' => 200,
'data' => $model->toArray(),
'status' => 'ok',
'type' => 'model'
];
}
/**
* @param string $name
* @return $this
* @throws \Exception
*/
public function view(string $name)
{
if ($name === 'any') {
return $this->select(null);
}
/**
* @param string $name
* @return $this
* @throws \Exception
*/
public function view(string $name)
{
if ($name === 'any') {
return $this->select(null);
}
if (isset($this->views[$name]) === false) {
$name = 'default';
if (isset($this->views[$name]) === false) {
$name = 'default';
// try to fall back to the default view at least
if (isset($this->views[$name]) === false) {
throw new Exception(sprintf('The view "%s" does not exist', $name));
}
}
// try to fall back to the default view at least
if (isset($this->views[$name]) === false) {
throw new Exception(sprintf('The view "%s" does not exist', $name));
}
}
return $this->select($this->views[$name]);
}
return $this->select($this->views[$name]);
}
}