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
<?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;
use InvalidArgumentException;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Criteria\ConditionDecorator;
use Maslosoft\Mangan\Interfaces\SortInterface;
/**
* Sort
*/
class Sort implements SortInterface
{
/**
* Sort Ascending
* Alias left for BC
* @deprecated since version 4.0.7
*/
const SORT_ASC = self::SortAsc;
/**
* Sort Descending
* Alias left for BC
* @deprecated since version 4.0.7
*/
const SORT_DESC = self::SortDesc;
/**
* @Ignored
* @var AnnotatedInterface
*/
public $model = null;
/**
*
* @var int[]
*/
public $fields = [];
/**
* Condition decorator instance
* @var ConditionDecorator
*/
private $cd;
public function __construct($sort = [], AnnotatedInterface $model = null)
{
foreach ($sort as $field => $order)
{
$this->replace($field, $order);
}
$this->setModel($model);
}
/**
* Add sorting field order. If field is already declared it will be pushed to the end of sort list.
* @param string $field
* @param int $order
*/
public function add($field, $order)
{
$this->remove($field);
$this->replace($field, $order);
return $this;
}
/**
* Create or replace sorting field order. If field is already declared it will **not** be pushed to the end of sort list.
* @param string $field
* @param int $order
*/
public function replace($field, $order)
{
if (!in_array($order, [self::SortAsc, self::SortDesc]))
{
throw new InvalidArgumentException(sprintf('Invalid order `%s` value for field `%s` of model %s', $order, $field, get_class($this->model)));
}
$this->fields[$field] = $order;
return $this;
}
/**
* Remove sort field
* @param string $field
*/
public function remove($field)
{
if (isset($this->fields[$field]))
{
unset($this->fields[$field]);
}
return $this;
}
/**
* Returns true if sorting is applied
* @return bool
*/
public function isSorted()
{
return count($this->fields) > 0;
}
public function getSort()
{
$sort = [];
if (null === $this->cd)
{
$this->cd = new ConditionDecorator($this->model);
}
foreach ($this->fields as $fieldName => $order)
{
$decorated = $this->cd->decorate($fieldName);
$sort[key($decorated)] = $order;
}
return $sort;
}
public function setModel(AnnotatedInterface $model = null)
{
if (null !== $model)
{
$this->model = $model;
}
return $this;
}
}
API documentation generated by ApiGen