Maslosoft Mangan API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Traits\DataProvider;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Exceptions\ManganException;
use Maslosoft\Mangan\Interfaces\Criteria\LimitableInterface;
use Maslosoft\Mangan\Interfaces\Criteria\MergeableInterface;
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface;
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
use Maslosoft\Mangan\Interfaces\PaginationInterface;
use Maslosoft\Mangan\Interfaces\SortInterface;
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface;
use Maslosoft\Mangan\Meta\ManganMeta;
use Maslosoft\Mangan\Pagination;
/**
* ConfigureTrait
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
trait ConfigureTrait
{
protected function configure($modelClass, $config)
{
if (!empty($modelClass))
{
if (is_string($modelClass))
{
$this->setModel(new $modelClass);
}
elseif (is_object($modelClass))
{
$this->setModel($modelClass);
}
else
{
throw new ManganException('Invalid model type for ' . static::class);
}
}
$model = $this->getModel();
// Set criteria from model
$criteria = $this->getCriteria();
if (!empty($model) && $criteria instanceof MergeableInterface)
{
// NOTE: WithCriteria and CriteriaAware have just slightly different method names
if ($model instanceof WithCriteriaInterface)
{
$criteria->mergeWith($model->getDbCriteria());
}
elseif ($model instanceof CriteriaAwareInterface)
{
$criteria->mergeWith($model->getCriteria());
}
}
// Merge criteria from configuration
if (isset($config['criteria']))
{
$criteria->mergeWith($config['criteria']);
unset($config['criteria']);
}
// Merge limit from configuration
if (isset($config['limit']) && $config['limit'] > 0)
{
$criteria->setLimit($config['limit']);
unset($config['limit']);
}
// Merge sorting from configuration
if (isset($config['sort']))
{
// Apply default sorting if criteria does not have sort configured
$sort = $criteria->getSort();
if (isset($config['sort']['defaultOrder']) && empty($sort))
{
$criteria->setSort($config['sort']['defaultOrder']);
}
unset($config['sort']);
}
if (isset($config['pagination']))
{
$this->setPagination($config['pagination']);
unset($config['pagination']);
}
if (!empty($model) && !$criteria->getSelect())
{
$fields = array_keys(ManganMeta::create($model)->fields());
$selected = array_fill_keys($fields, true);
$criteria->setSelect($selected);
}
foreach ($config as $key => $value)
{
$this->$key = $value;
}
}
/**
* Configure limits, sorting for fetching data
* @return CriteriaInterface
*/
protected function configureFetch()
{
// Setup required objects
$sort = $this->getSort();
$criteria = $this->getCriteria();
$pagination = $this->getPagination();
// Apply limits if required
if ($pagination !== false && $criteria instanceof LimitableInterface)
{
$pagination->setCount($this->getTotalItemCount());
$pagination->apply($criteria);
}
// Apply sort if required
if ($sort->isSorted())
{
$criteria->setSort($sort);
}
return $criteria;
}
/**
* Returns the sort object.
* @return SortInterface the sorting object. If this is false, it means the sorting is disabled.
*/
abstract public function getSort();
/**
* Returns the pagination object.
* @param string $className the pagination object class name, use this param to override default pagination class.
* @return PaginationInterface|Pagination|false the pagination object. If this is false, it means the pagination is disabled.
*/
abstract public function getPagination($className = Pagination::class);
/**
* Set pagination
* @param boolean|array|PaginationInterface $pagination
* @return static
*/
abstract public function setPagination($pagination);
/**
* Returns the total number of data items.
* When {@link pagination} is set false, this returns the same value as {@link itemCount}.
* @return integer total number of possible data items.
*/
abstract public function getTotalItemCount();
/**
* @return CriteriaInterface
*/
abstract public function getCriteria();
/**
* @return AnnotatedInterface
*/
abstract public function getModel();
/**
* @param $model AnnotatedInterface
* @return static
*/
abstract public function setModel(AnnotatedInterface $model);
}
API documentation generated by ApiGen