| Total Complexity | 16 |
| Total Lines | 145 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | abstract class FilterBase implements FilterInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | protected $name; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $alias; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string|\Closure|null |
||
| 23 | */ |
||
| 24 | protected $title; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var mixed |
||
| 28 | */ |
||
| 29 | protected $value; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param string $name |
||
| 33 | * @param string|\Closure|null $title |
||
| 34 | */ |
||
| 35 | public function __construct($name, $title = null) |
||
| 36 | { |
||
| 37 | $this->setName($name); |
||
| 38 | |||
| 39 | if (! is_null($title)) { |
||
| 40 | $this->setTitle($title); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Initialize filter. |
||
| 46 | */ |
||
| 47 | public function initialize() |
||
| 48 | { |
||
| 49 | if (is_null($value = $this->getValue())) { |
||
| 50 | $value = request()->input($this->getAlias()); |
||
| 51 | } |
||
| 52 | |||
| 53 | $this->setValue($value); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | public function getName() |
||
| 60 | { |
||
| 61 | return $this->name; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $name |
||
| 66 | * |
||
| 67 | * @return $this |
||
| 68 | */ |
||
| 69 | public function setName($name) |
||
| 70 | { |
||
| 71 | $this->name = $name; |
||
| 72 | |||
| 73 | return $this; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | public function getAlias() |
||
| 80 | { |
||
| 81 | if (! $this->alias) { |
||
| 82 | return $this->getName(); |
||
| 83 | } |
||
| 84 | |||
| 85 | return $this->alias; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $alias |
||
| 90 | * |
||
| 91 | * @return $this |
||
| 92 | */ |
||
| 93 | public function setAlias($alias) |
||
| 94 | { |
||
| 95 | $this->alias = $alias; |
||
| 96 | |||
| 97 | return $this; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function getTitle() |
||
| 104 | { |
||
| 105 | if (is_null($this->title)) { |
||
| 106 | return Str::studly($this->getAlias()); |
||
| 107 | } |
||
| 108 | |||
| 109 | if (is_callable($this->title)) { |
||
| 110 | return call_user_func($this->title, $this->getValue()); |
||
| 111 | } |
||
| 112 | |||
| 113 | return strtr($this->title, [':value' => $this->getValue()]); |
||
|
|
|||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param Closure|string $title |
||
| 118 | * |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | public function setTitle($title) |
||
| 122 | { |
||
| 123 | $this->title = $title; |
||
| 124 | |||
| 125 | return $this; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return mixed |
||
| 130 | */ |
||
| 131 | public function getValue() |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param mixed $value |
||
| 138 | * |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | public function setValue($value) |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | public function isActive() |
||
| 154 | } |
||
| 155 | } |
||
| 156 |