update listing, collections and callbacks
This commit is contained in:
@@ -19,59 +19,85 @@ namespace Froxlor\UI;
|
||||
*/
|
||||
class Collection
|
||||
{
|
||||
private array $items;
|
||||
private array $userInfo;
|
||||
private array $params;
|
||||
private string $class;
|
||||
private array $has = [];
|
||||
private array $params;
|
||||
private array $userinfo;
|
||||
|
||||
public function __construct(string $class, array $userInfo, array $params = [])
|
||||
{
|
||||
$this->class = $class;
|
||||
$this->params = $params;
|
||||
$this->userInfo = $userInfo;
|
||||
|
||||
$this->items = $this->getListing($this->class, $this->params);
|
||||
$this->userinfo = $userInfo;
|
||||
}
|
||||
|
||||
private function getListing($class, $params)
|
||||
private function getListing($class, $params): array
|
||||
{
|
||||
return json_decode($class::getLocal($this->userInfo, $params)->listing(), true);
|
||||
return json_decode($class::getLocal($this->userinfo, $params)->listing(), true);
|
||||
}
|
||||
|
||||
public function count()
|
||||
public function count(): int
|
||||
{
|
||||
return json_decode($this->class::getLocal($this->userInfo, $this->params)->listingCount(), true);
|
||||
return json_decode($this->class::getLocal($this->userinfo, $this->params)->listingCount(), true)['data'];
|
||||
}
|
||||
|
||||
public function get()
|
||||
public function get(): array
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
$result = $this->getListing($this->class, $this->params);
|
||||
|
||||
public function getData()
|
||||
{
|
||||
return $this->get()['data'];
|
||||
}
|
||||
|
||||
public function getJson()
|
||||
{
|
||||
return json_encode($this->get());
|
||||
}
|
||||
|
||||
public function has($column, $class, $parentKey = 'id', $childKey = 'id', $params = []): Collection
|
||||
{
|
||||
// check if the api result contains any items (not the overall listingCount as we might be in a search-resultset)
|
||||
if (count($this->items) > 0) {
|
||||
$attributes = $this->getListing($class, $params);
|
||||
if (count($result)) {
|
||||
foreach ($this->has as $has) {
|
||||
$attributes = $this->getListing($has['class'], $has['params']);
|
||||
|
||||
foreach ($this->items['data']['list'] as $key => $item) {
|
||||
foreach ($attributes['data']['list'] as $list) {
|
||||
if ($item[$parentKey] == $list[$childKey]) {
|
||||
$this->items['data']['list'][$key][$column] = $list;
|
||||
foreach ($result['data']['list'] as $key => $item) {
|
||||
foreach ($attributes['data']['list'] as $list) {
|
||||
if ($item[$has['parentKey']] == $list[$has['childKey']]) {
|
||||
$result['data']['list'][$key][$has['column']] = $list;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getData(): array
|
||||
{
|
||||
return $this->get()['data'];
|
||||
}
|
||||
|
||||
public function getList(): array
|
||||
{
|
||||
return $this->getData()['list'];
|
||||
}
|
||||
|
||||
public function getJson(): string
|
||||
{
|
||||
return json_encode($this->get());
|
||||
}
|
||||
|
||||
public function has(string $column, string $class, string $parentKey = 'id', string $childKey = 'id', array $params = []): Collection
|
||||
{
|
||||
$this->has[] = [
|
||||
'column' => $column,
|
||||
'class' => $class,
|
||||
'parentKey' => $parentKey,
|
||||
'childKey' => $childKey,
|
||||
'params' => $params
|
||||
];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withPagination(array $columns): Collection
|
||||
{
|
||||
// TODO: handle 'sortable' => true in $columns
|
||||
|
||||
$pagination = new Pagination($this->userinfo, $columns, $this->count());
|
||||
$this->params = array_merge($this->params, $pagination->getApiCommandParams());
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user