LaravelRUS /
SleepingOwlAdmin
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace SleepingOwl\Admin\Display\Column\Editable; |
||||
| 4 | |||||
| 5 | use Illuminate\Database\Eloquent\Model; |
||||
| 6 | use Illuminate\Http\Request; |
||||
| 7 | use Illuminate\Support\Arr; |
||||
| 8 | use SleepingOwl\Admin\Contracts\Display\ColumnEditableInterface; |
||||
| 9 | use SleepingOwl\Admin\Form\FormDefault; |
||||
| 10 | use SleepingOwl\Admin\Traits\SelectOptionsFromModel; |
||||
| 11 | |||||
| 12 | class Select extends EditableColumn implements ColumnEditableInterface |
||||
| 13 | { |
||||
| 14 | use SelectOptionsFromModel; |
||||
| 15 | |||||
| 16 | /** |
||||
| 17 | * @var string |
||||
| 18 | */ |
||||
| 19 | protected $view = 'column.editable.select'; |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * @var null |
||||
| 23 | */ |
||||
| 24 | protected $relationKey = null; |
||||
| 25 | /** |
||||
| 26 | * @var array |
||||
| 27 | */ |
||||
| 28 | protected $options = []; |
||||
| 29 | |||||
| 30 | /** |
||||
| 31 | * @var array |
||||
| 32 | */ |
||||
| 33 | protected $optionList = []; |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * @var array |
||||
| 37 | */ |
||||
| 38 | protected $exclude = []; |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * @var bool |
||||
| 42 | */ |
||||
| 43 | protected $sortable = true; |
||||
| 44 | |||||
| 45 | /** |
||||
| 46 | * @var null |
||||
| 47 | */ |
||||
| 48 | protected $defaultValue = null; |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * Select constructor. |
||||
| 52 | * @param $name |
||||
| 53 | * @param null $label |
||||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||||
| 54 | * @param array $options |
||||
| 55 | * @throws \SleepingOwl\Admin\Exceptions\Form\Element\SelectException |
||||
| 56 | */ |
||||
| 57 | public function __construct($name, $label = null, $options = []) |
||||
| 58 | { |
||||
| 59 | parent::__construct($name, $label); |
||||
| 60 | |||||
| 61 | if (is_array($options)) { |
||||
|
0 ignored issues
–
show
|
|||||
| 62 | $this->setOptions($options); |
||||
| 63 | } elseif (($options instanceof Model) || is_string($options)) { |
||||
| 64 | $this->setModelForOptions($options); |
||||
| 65 | } |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | public function getModifierValue() |
||||
| 69 | { |
||||
| 70 | if (is_callable($this->modifier)) { |
||||
| 71 | return call_user_func($this->modifier, $this); |
||||
| 72 | } |
||||
| 73 | |||||
| 74 | if (is_null($this->modifier)) { |
||||
| 75 | return $this->getOptionName($this->getModelValue()); |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | return $this->modifier; |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | /** |
||||
| 82 | * @param $relationKey |
||||
| 83 | * @return $this |
||||
| 84 | */ |
||||
| 85 | public function setRelationKey($relationKey) |
||||
| 86 | { |
||||
| 87 | $this->relationKey = $relationKey; |
||||
| 88 | |||||
| 89 | return $this; |
||||
| 90 | } |
||||
| 91 | |||||
| 92 | /** |
||||
| 93 | * @return null |
||||
| 94 | */ |
||||
| 95 | public function getRelationKey() |
||||
| 96 | { |
||||
| 97 | return $this->relationKey; |
||||
| 98 | } |
||||
| 99 | |||||
| 100 | /** |
||||
| 101 | * @param $defaultValue |
||||
| 102 | * @return $this |
||||
| 103 | */ |
||||
| 104 | public function setDefaultValue($defaultValue) |
||||
| 105 | { |
||||
| 106 | $this->defaultValue = $defaultValue; |
||||
| 107 | |||||
| 108 | return $this; |
||||
| 109 | } |
||||
| 110 | |||||
| 111 | /** |
||||
| 112 | * @return null |
||||
| 113 | */ |
||||
| 114 | public function getDefaultValue() |
||||
| 115 | { |
||||
| 116 | return $this->defaultValue; |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | /** |
||||
| 120 | * @param bool $sortable |
||||
| 121 | * |
||||
| 122 | * @return $this |
||||
| 123 | */ |
||||
| 124 | public function setSortable($sortable) |
||||
| 125 | { |
||||
| 126 | $this->sortable = (bool) $sortable; |
||||
| 127 | |||||
| 128 | return $this; |
||||
| 129 | } |
||||
| 130 | |||||
| 131 | /** |
||||
| 132 | * @return bool |
||||
| 133 | */ |
||||
| 134 | public function isSortable() |
||||
| 135 | { |
||||
| 136 | return $this->sortable; |
||||
| 137 | } |
||||
| 138 | |||||
| 139 | /** |
||||
| 140 | * @return array |
||||
| 141 | */ |
||||
| 142 | public function getOptions() |
||||
| 143 | { |
||||
| 144 | if (! is_null($this->getModelForOptions()) && ! is_null($this->getDisplay())) { |
||||
| 145 | $this->setOptions( |
||||
| 146 | $this->loadOptions() |
||||
| 147 | ); |
||||
| 148 | } |
||||
| 149 | |||||
| 150 | $options = Arr::except($this->options, $this->exclude); |
||||
| 151 | if ($this->isSortable()) { |
||||
| 152 | asort($options); |
||||
| 153 | } |
||||
| 154 | |||||
| 155 | return $options; |
||||
| 156 | } |
||||
| 157 | |||||
| 158 | /** |
||||
| 159 | * @return array |
||||
| 160 | */ |
||||
| 161 | public function mutateOptions() |
||||
| 162 | { |
||||
| 163 | $options = []; |
||||
| 164 | |||||
| 165 | $this->optionList = $this->getOptions(); |
||||
| 166 | |||||
| 167 | foreach ($this->optionList as $key => $value) { |
||||
| 168 | $options[] = ['value' => $key, 'text' => $value]; |
||||
| 169 | } |
||||
| 170 | |||||
| 171 | return $options; |
||||
| 172 | } |
||||
| 173 | |||||
| 174 | /** |
||||
| 175 | * @param $key |
||||
| 176 | * @return mixed|null |
||||
| 177 | */ |
||||
| 178 | public function getOptionName($value) |
||||
| 179 | { |
||||
| 180 | if (isset($value)) { |
||||
| 181 | if (isset($this->optionList[$value])) { |
||||
| 182 | return $this->optionList[$value]; |
||||
| 183 | } |
||||
| 184 | |||||
| 185 | return $value; |
||||
| 186 | } |
||||
| 187 | } |
||||
| 188 | |||||
| 189 | /** |
||||
| 190 | * @param array |
||||
| 191 | * |
||||
| 192 | * @return $this |
||||
| 193 | */ |
||||
| 194 | public function setOptions(array $options) |
||||
| 195 | { |
||||
| 196 | $this->options = $options; |
||||
| 197 | |||||
| 198 | return $this; |
||||
| 199 | } |
||||
| 200 | |||||
| 201 | /** |
||||
| 202 | * @param array $values |
||||
| 203 | * |
||||
| 204 | * @return $this |
||||
| 205 | */ |
||||
| 206 | public function setEnum(array $values) |
||||
| 207 | { |
||||
| 208 | return $this->setOptions(array_combine($values, $values)); |
||||
|
0 ignored issues
–
show
It seems like
array_combine($values, $values) can also be of type false; however, parameter $options of SleepingOwl\Admin\Displa...le\Select::setOptions() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 209 | } |
||||
| 210 | |||||
| 211 | /** |
||||
| 212 | * @return array |
||||
| 213 | */ |
||||
| 214 | public function toArray() |
||||
| 215 | { |
||||
| 216 | return array_merge(parent::toArray(), [ |
||||
| 217 | 'options' => $this->mutateOptions(), |
||||
| 218 | 'optionName' => $this->getOptionName($this->getModelValue()), |
||||
| 219 | 'text' => $this->getModifierValue(), |
||||
| 220 | ]); |
||||
| 221 | } |
||||
| 222 | |||||
| 223 | /** |
||||
| 224 | * @param \Illuminate\Http\Request $request |
||||
| 225 | * @throws \SleepingOwl\Admin\Exceptions\Form\Element\SelectException |
||||
| 226 | * @throws \SleepingOwl\Admin\Exceptions\Form\FormElementException |
||||
| 227 | * @throws \SleepingOwl\Admin\Exceptions\Form\FormException |
||||
| 228 | */ |
||||
| 229 | public function save(Request $request) |
||||
| 230 | { |
||||
| 231 | $model = $this->getModel(); |
||||
| 232 | |||||
| 233 | if (strpos($this->getName(), '.') !== false) { |
||||
| 234 | if ($this->getRelationKey()) { |
||||
|
0 ignored issues
–
show
Are you sure the usage of
$this->getRelationKey() targeting SleepingOwl\Admin\Displa...elect::getRelationKey() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||
| 235 | $this->setName($this->getRelationKey()); |
||||
|
0 ignored issues
–
show
$this->getRelationKey() of type void is incompatible with the type string expected by parameter $name of SleepingOwl\Admin\Displa...\NamedColumn::setName().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
Are you sure the usage of
$this->getRelationKey() targeting SleepingOwl\Admin\Displa...elect::getRelationKey() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||
| 236 | } else { |
||||
| 237 | //@TODO Make Relation Resolver |
||||
| 238 | $relationName = explode('.', $this->getName()); |
||||
|
0 ignored issues
–
show
|
|||||
| 239 | } |
||||
| 240 | } |
||||
| 241 | |||||
| 242 | $form = new FormDefault([ |
||||
| 243 | new \SleepingOwl\Admin\Form\Element\Select( |
||||
| 244 | $this->getName() |
||||
| 245 | ), |
||||
| 246 | ]); |
||||
| 247 | |||||
| 248 | $request->offsetSet($this->getName(), $request->input('value', $this->getDefaultValue())); |
||||
|
0 ignored issues
–
show
Are you sure the usage of
$this->getDefaultValue() targeting SleepingOwl\Admin\Displa...lect::getDefaultValue() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||
| 249 | |||||
| 250 | $form->setModelClass(get_class($model)); |
||||
| 251 | $form->initialize(); |
||||
| 252 | $form->setId($model->getKey()); |
||||
| 253 | |||||
| 254 | $form->saveForm($request); |
||||
| 255 | } |
||||
| 256 | } |
||||
| 257 |