OpenXE/classes/Modules/Api/Resource/Result/AbstractResult.php

100 lines
1.9 KiB
PHP
Raw Normal View History

2021-05-21 08:49:41 +02:00
<?php
namespace Xentral\Modules\Api\Resource\Result;
abstract class AbstractResult
{
const RESULT_TYPE_ITEM = 'item';
const RESULT_TYPE_COLLECTION = 'collection';
/** @var string $type */
protected $type;
/** @var array $data */
protected $data;
/** @var array $pagination */
protected $pagination;
/** @var bool $success Als Kennzeichen ob Anlegen oder Bearbeiten erfolgreich war */
protected $success;
/**
* @param array $collection
* @param array $pagination
*/
abstract public function __construct(array $collection, array $pagination = null);
/**
* Ergebnis als Array zurückgeben
*
* @return array
*/
public function getResult()
{
$result = [];
// Success-Flag ganz oben anzeigen
if ($this->success !== null) {
$result['success'] = $this->success;
}
$result['data'] = $this->getData();
// Paginierung als letztes anzeigen
if ($this->pagination !== null) {
$result['pagination'] = $this->pagination;
}
return $result;
}
/**
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* @return array
*/
public function getPagination()
{
return $this->pagination;
}
/**
* @return string [item|collection]
*/
public function getType()
{
return $this->type;
}
/**
* @param bool $success
*/
public function setSuccess($success)
{
$this->success = (bool)$success;
}
/**
* @return bool
*/
public function isItem()
{
return $this->type === self::RESULT_TYPE_ITEM;
}
/**
* @return bool
*/
public function isCollection()
{
return $this->type === self::RESULT_TYPE_COLLECTION;
}
}