nxmad /
Larapay
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | declare(strict_types=1); |
||||||
| 4 | |||||||
| 5 | namespace Nxmad\Larapay\Traits; |
||||||
| 6 | |||||||
| 7 | use Illuminate\Config\Repository; |
||||||
| 8 | |||||||
| 9 | trait HasMutations |
||||||
| 10 | { |
||||||
| 11 | /** |
||||||
| 12 | * The original query of request. |
||||||
| 13 | * |
||||||
| 14 | * @var Repository |
||||||
| 15 | */ |
||||||
| 16 | protected $query; |
||||||
| 17 | |||||||
| 18 | /** |
||||||
| 19 | * Magic get method. |
||||||
| 20 | * |
||||||
| 21 | * @param $name |
||||||
| 22 | * |
||||||
| 23 | * @return mixed |
||||||
| 24 | */ |
||||||
| 25 | public function __get($name) |
||||||
| 26 | { |
||||||
| 27 | return self::get(...func_get_args()); |
||||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
func_get_args() is expanded, but the parameter $field of Nxmad\Larapay\Traits\HasMutations::get() does not expect variable arguments.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 28 | } |
||||||
| 29 | |||||||
| 30 | /** |
||||||
| 31 | * Magic set method. |
||||||
| 32 | * |
||||||
| 33 | * @param $name |
||||||
| 34 | * @param $value |
||||||
| 35 | * |
||||||
| 36 | * @return self |
||||||
| 37 | */ |
||||||
| 38 | public function __set($name, $value) |
||||||
| 39 | { |
||||||
| 40 | return self::set(...func_get_args()); |
||||||
|
0 ignored issues
–
show
func_get_args() is expanded, but the parameter $field of Nxmad\Larapay\Traits\HasMutations::set() does not expect variable arguments.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The method
Nxmad\Larapay\Traits\HasMutations::set() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 41 | } |
||||||
| 42 | |||||||
| 43 | /** |
||||||
| 44 | * Get raw query. |
||||||
| 45 | * |
||||||
| 46 | * @return array |
||||||
| 47 | */ |
||||||
| 48 | public function getRaw() |
||||||
| 49 | { |
||||||
| 50 | return $this->query->all(); |
||||||
| 51 | } |
||||||
| 52 | |||||||
| 53 | /** |
||||||
| 54 | * Set raw query. |
||||||
| 55 | * |
||||||
| 56 | * @param $query |
||||||
| 57 | */ |
||||||
| 58 | public function setRaw($query) |
||||||
| 59 | { |
||||||
| 60 | $this->query = $query instanceof Repository ? $query : new Repository($query); |
||||||
| 61 | } |
||||||
| 62 | |||||||
| 63 | /** |
||||||
| 64 | * Determine if request has a field. |
||||||
| 65 | * |
||||||
| 66 | * @param string $field |
||||||
| 67 | * |
||||||
| 68 | * @return bool |
||||||
| 69 | */ |
||||||
| 70 | public function has(string $field): bool |
||||||
| 71 | { |
||||||
| 72 | return $this->get($field) !== null; |
||||||
| 73 | } |
||||||
| 74 | |||||||
| 75 | /** |
||||||
| 76 | * Get mutated request field using accessor. |
||||||
| 77 | * |
||||||
| 78 | * @param string $field |
||||||
| 79 | * @param mixed $default |
||||||
| 80 | * |
||||||
| 81 | * @return mixed |
||||||
| 82 | */ |
||||||
| 83 | public function get(string $field, $default = null) |
||||||
| 84 | { |
||||||
| 85 | $accessorName = 'get' . $this->camelize($field); |
||||||
|
0 ignored issues
–
show
It seems like
camelize() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 86 | |||||||
| 87 | if (method_exists($this, $accessorName)) { |
||||||
| 88 | return $this->{$accessorName}(); |
||||||
| 89 | } |
||||||
| 90 | |||||||
| 91 | if (isset($this->aliases[$field])) { |
||||||
|
0 ignored issues
–
show
The property
aliases does not exist on Nxmad\Larapay\Traits\HasMutations. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||||
| 92 | $field = $this->aliases[$field]; |
||||||
| 93 | } |
||||||
| 94 | |||||||
| 95 | return $this->query->get($field, $default); |
||||||
| 96 | } |
||||||
| 97 | |||||||
| 98 | /** |
||||||
| 99 | * Set request field. |
||||||
| 100 | * |
||||||
| 101 | * @param $field |
||||||
| 102 | * @param $value |
||||||
| 103 | * |
||||||
| 104 | * @return self |
||||||
| 105 | */ |
||||||
| 106 | public function set($field, $value = null) |
||||||
| 107 | { |
||||||
| 108 | if (is_array($field)) { |
||||||
| 109 | return $this->fill($field); |
||||||
| 110 | } |
||||||
| 111 | |||||||
| 112 | $accessorName = 'set' . $this->camelize($field); |
||||||
| 113 | |||||||
| 114 | if (method_exists($this, $accessorName)) { |
||||||
| 115 | $value = $this->{$accessorName}($value); |
||||||
| 116 | } |
||||||
| 117 | |||||||
| 118 | if (isset($this->aliases[$field])) { |
||||||
|
0 ignored issues
–
show
The property
aliases does not exist on Nxmad\Larapay\Traits\HasMutations. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||||
| 119 | $field = $this->aliases[$field]; |
||||||
| 120 | } |
||||||
| 121 | |||||||
| 122 | $this->query->set($field, $value); |
||||||
| 123 | |||||||
| 124 | return $this; |
||||||
| 125 | } |
||||||
| 126 | |||||||
| 127 | /** |
||||||
| 128 | * @param mixed ...$fields |
||||||
| 129 | */ |
||||||
| 130 | public function remove(...$fields) |
||||||
| 131 | { |
||||||
| 132 | foreach ($fields as $field) |
||||||
| 133 | { |
||||||
| 134 | if (is_array($field)) |
||||||
| 135 | { |
||||||
| 136 | $this->remove(...$field); |
||||||
| 137 | |||||||
| 138 | continue; |
||||||
| 139 | } |
||||||
| 140 | |||||||
| 141 | if (isset($this->aliases[$field])) { |
||||||
|
0 ignored issues
–
show
The property
aliases does not exist on Nxmad\Larapay\Traits\HasMutations. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||||
| 142 | $field = $this->aliases[$field]; |
||||||
| 143 | } |
||||||
| 144 | |||||||
| 145 | unset($this->query[$field]); |
||||||
| 146 | } |
||||||
| 147 | } |
||||||
| 148 | |||||||
| 149 | /** |
||||||
| 150 | * Fill request. |
||||||
| 151 | * |
||||||
| 152 | * @param array $parameters |
||||||
| 153 | * |
||||||
| 154 | * @return self |
||||||
| 155 | */ |
||||||
| 156 | public function fill(array $parameters) |
||||||
| 157 | { |
||||||
| 158 | foreach ($parameters as $key => $value) { |
||||||
| 159 | $this->set($key, $value); |
||||||
| 160 | } |
||||||
| 161 | |||||||
| 162 | return $this; |
||||||
| 163 | } |
||||||
| 164 | |||||||
| 165 | /** |
||||||
| 166 | * Get full mutated query. |
||||||
| 167 | * |
||||||
| 168 | * @param bool $object |
||||||
| 169 | * |
||||||
| 170 | * @return array|\stdClass |
||||||
| 171 | */ |
||||||
| 172 | public function all($object = true) |
||||||
| 173 | { |
||||||
| 174 | $arr = $this->toArray(); |
||||||
| 175 | |||||||
| 176 | return $object ? (object) $arr : $arr; |
||||||
| 177 | } |
||||||
| 178 | |||||||
| 179 | /** |
||||||
| 180 | * Request data to array. |
||||||
| 181 | * |
||||||
| 182 | * @return array |
||||||
| 183 | */ |
||||||
| 184 | public function toArray() |
||||||
| 185 | { |
||||||
| 186 | return $this->query->all(); |
||||||
| 187 | } |
||||||
| 188 | |||||||
| 189 | /** |
||||||
| 190 | * Flat request. |
||||||
| 191 | * |
||||||
| 192 | * @param null $query |
||||||
|
0 ignored issues
–
show
|
|||||||
| 193 | * @param string $prefix |
||||||
| 194 | * |
||||||
| 195 | * @return array |
||||||
| 196 | */ |
||||||
| 197 | public function flat($query = null, $prefix = '') |
||||||
| 198 | { |
||||||
| 199 | $result = []; |
||||||
| 200 | |||||||
| 201 | if (is_null($query)) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 202 | $query = $this->query->all(); |
||||||
| 203 | } |
||||||
| 204 | |||||||
| 205 | foreach ($query as $key => $value) { |
||||||
| 206 | if (is_array($value) && $this->isAssoc($value)) { |
||||||
| 207 | $result = array_merge($result, $this->flat($value, "{$prefix}{$key}.")); |
||||||
| 208 | } else { |
||||||
| 209 | $result[$prefix . $key] = $value; |
||||||
| 210 | } |
||||||
| 211 | } |
||||||
| 212 | |||||||
| 213 | return $result; |
||||||
| 214 | } |
||||||
| 215 | |||||||
| 216 | /** |
||||||
| 217 | * Determine if array is associative. |
||||||
| 218 | * |
||||||
| 219 | * @param array $array |
||||||
| 220 | * |
||||||
| 221 | * @return bool |
||||||
| 222 | */ |
||||||
| 223 | private function isAssoc(array $array) |
||||||
| 224 | { |
||||||
| 225 | if ([] === $array) { |
||||||
| 226 | return false; |
||||||
| 227 | } |
||||||
| 228 | |||||||
| 229 | return array_keys($array) !== range(0, count($array) - 1); |
||||||
| 230 | } |
||||||
| 231 | } |
||||||
| 232 |