| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\Framework\Support; |
| 4 |
|
| 5 |
use ReflectionFunction; |
| 6 |
use InvalidArgumentException; |
| 7 |
|
| 8 |
class FluentArray extends Fluent |
| 9 |
{ |
| 10 |
protected $isFluent = false; |
| 11 |
|
| 12 |
public function __construct($items, $isFluent = false) |
| 13 |
{ |
| 14 |
parent::__construct($items); |
| 15 |
|
| 16 |
$this->isFluent = $isFluent; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Return the attributes |
| 21 |
* |
| 22 |
* @return mixed |
| 23 |
*/ |
| 24 |
public function value() |
| 25 |
{ |
| 26 |
return $this->toArray(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Get value(s) from attributes |
| 31 |
* |
| 32 |
* @param string $key |
| 33 |
* @param string $default |
| 34 |
* @return array |
| 35 |
*/ |
| 36 |
public function get($key = null, $default = null) |
| 37 |
{ |
| 38 |
if (!$key) { |
| 39 |
return $this->toArray(); |
| 40 |
} |
| 41 |
|
| 42 |
return $this->offsetGet($key) ?: parent::get($key, $default); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Dynamically set the value of an attribute. |
| 47 |
* |
| 48 |
* @param string $key |
| 49 |
* @param mixed $value |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function __set($key, $value) |
| 53 |
{ |
| 54 |
$this->offsetSet($key, $value); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Determine if the given offset exists. |
| 59 |
* |
| 60 |
* @param string $offset |
| 61 |
* @return bool |
| 62 |
*/ |
| 63 |
#[\ReturnTypeWillChange] |
| 64 |
public function offsetExists($offset) |
| 65 |
{ |
| 66 |
return Arr::has($this->attributes, $offset); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get the value for a given offset. |
| 71 |
* |
| 72 |
* @param string $offset |
| 73 |
* @return mixed |
| 74 |
*/ |
| 75 |
#[\ReturnTypeWillChange] |
| 76 |
public function offsetGet($offset) |
| 77 |
{ |
| 78 |
if ($offset === '*') return $this->attributes; |
| 79 |
|
| 80 |
return Arr::get($this->attributes, $offset); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Set the value at the given offset. |
| 85 |
* |
| 86 |
* @param string $offset |
| 87 |
* @param mixed $value |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
#[\ReturnTypeWillChange] |
| 91 |
public function offsetSet($offset, $value) |
| 92 |
{ |
| 93 |
Arr::set($this->attributes, $offset, $value); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Unset the value at the given offset. |
| 98 |
* |
| 99 |
* @param string $offset |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
#[\ReturnTypeWillChange] |
| 103 |
public function offsetUnset($offset) |
| 104 |
{ |
| 105 |
Arr::forget($this->attributes, $offset); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Handle dynamic calls to the fluent instance to set attributes. |
| 110 |
* |
| 111 |
* @param string $method |
| 112 |
* @param array $parameters |
| 113 |
* @return $this |
| 114 |
*/ |
| 115 |
public function __call($method, $params) |
| 116 |
{ |
| 117 |
$isUndef = true; |
| 118 |
|
| 119 |
if (method_exists(Arr::class, $method)) { |
| 120 |
$isUndef = false; |
| 121 |
$result = Arr::$method($this->attributes, ...$params); |
| 122 |
} elseif (function_exists($method)) { |
| 123 |
$isUndef = false; |
| 124 |
$result = $method(...$this->getParams($method, $params)); |
| 125 |
} elseif (function_exists($func = Str::snake($method))) { |
| 126 |
$isUndef = false; |
| 127 |
$result = $func(...$this->getParams($func, $params)); |
| 128 |
} elseif (function_exists($func = 'array_'.Str::snake($method))) { |
| 129 |
$isUndef = false; |
| 130 |
$result = $func(...$this->getParams($func, $params)); |
| 131 |
} |
| 132 |
|
| 133 |
if ($isUndef) { |
| 134 |
throw new InvalidArgumentException( |
| 135 |
"Undefined method ".__CLASS__."::{$method}" |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
if ($this->isFluent) { |
| 140 |
return is_scalar($result) ? $result : new static( |
| 141 |
$result, $this->isFluent |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
return $result; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Retuens the string representation of the array |
| 150 |
* |
| 151 |
* @return string [description] |
| 152 |
*/ |
| 153 |
public function __toString() |
| 154 |
{ |
| 155 |
return json_encode($this->attributes, JSON_PRETTY_PRINT); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Prepare the function parameters in the right order. |
| 160 |
* The attributes should be in the place of haystack, |
| 161 |
* if no haystack exists then put it at first place. |
| 162 |
* |
| 163 |
* @param String $func |
| 164 |
* @param array $params |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
public function getParams($func, $params) |
| 168 |
{ |
| 169 |
if (!$params) return [$this->attributes]; |
| 170 |
|
| 171 |
$reflection = new ReflectionFunction($func); |
| 172 |
|
| 173 |
$haystack = array_filter($reflection->getParameters(), function($i) { |
| 174 |
return $i->getName() === 'haystack'; |
| 175 |
}); |
| 176 |
|
| 177 |
if (!$haystack) { |
| 178 |
array_unshift($params, $this->attributes); |
| 179 |
return $params; |
| 180 |
} |
| 181 |
|
| 182 |
foreach ($reflection->getParameters() as $key => $parameter) { |
| 183 |
if ($parameter->getName() === 'haystack') { |
| 184 |
$params = Arr::insertAt($params, $key, $this->attributes); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
return $params; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Dump the attributes. |
| 193 |
* |
| 194 |
* @return array |
| 195 |
*/ |
| 196 |
public function dd() |
| 197 |
{ |
| 198 |
$attributes = $this->attributes; |
| 199 |
|
| 200 |
if ($keys = func_get_args()) { |
| 201 |
$attributes = $this->only(...$keys); |
| 202 |
} |
| 203 |
|
| 204 |
print_r($attributes); die; |
| 205 |
} |
| 206 |
} |
| 207 |
|