|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Yajra\DataTables\Html; |
| 4 | + |
| 5 | +use ArrayAccess; |
| 6 | +use Illuminate\Contracts\Support\Arrayable; |
| 7 | +use Illuminate\Contracts\Support\Jsonable; |
| 8 | +use JsonSerializable; |
| 9 | + |
| 10 | +/** |
| 11 | + * @template TKey of array-key |
| 12 | + * @template TValue |
| 13 | + * |
| 14 | + * @implements \Illuminate\Contracts\Support\Arrayable<TKey, TValue> |
| 15 | + * @implements \ArrayAccess<TKey, TValue> |
| 16 | + */ |
| 17 | +class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable |
| 18 | +{ |
| 19 | + /** |
| 20 | + * All of the attributes set on the fluent instance. |
| 21 | + * |
| 22 | + * @var array<TKey, TValue> |
| 23 | + */ |
| 24 | + protected $attributes = []; |
| 25 | + |
| 26 | + /** |
| 27 | + * Create a new fluent instance. |
| 28 | + * |
| 29 | + * @param iterable<TKey, TValue> $attributes |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function __construct($attributes = []) |
| 33 | + { |
| 34 | + foreach ($attributes as $key => $value) { |
| 35 | + $this->attributes[$key] = $value; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Get an attribute from the fluent instance using "dot" notation. |
| 41 | + * |
| 42 | + * @template TGetDefault |
| 43 | + * |
| 44 | + * @param TKey $key |
| 45 | + * @param TGetDefault|(\Closure(): TGetDefault) $default |
| 46 | + * @return TValue|TGetDefault |
| 47 | + */ |
| 48 | + public function get($key, $default = null) |
| 49 | + { |
| 50 | + return data_get($this->attributes, $key, $default); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Get an attribute from the fluent instance. |
| 55 | + * |
| 56 | + * @param string $key |
| 57 | + * @param mixed $default |
| 58 | + * @return mixed |
| 59 | + */ |
| 60 | + public function value($key, $default = null) |
| 61 | + { |
| 62 | + if (array_key_exists($key, $this->attributes)) { |
| 63 | + return $this->attributes[$key]; |
| 64 | + } |
| 65 | + |
| 66 | + return value($default); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get the value of the given key as a new Fluent instance. |
| 71 | + * |
| 72 | + * @param string $key |
| 73 | + * @param mixed $default |
| 74 | + * @return static |
| 75 | + */ |
| 76 | + public function scope($key, $default = null) |
| 77 | + { |
| 78 | + return new static( |
| 79 | + (array) $this->get($key, $default) |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get the attributes from the fluent instance. |
| 85 | + * |
| 86 | + * @return array<TKey, TValue> |
| 87 | + */ |
| 88 | + public function getAttributes() |
| 89 | + { |
| 90 | + return $this->attributes; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Convert the fluent instance to an array. |
| 95 | + * |
| 96 | + * @return array<TKey, TValue> |
| 97 | + */ |
| 98 | + public function toArray() |
| 99 | + { |
| 100 | + return $this->attributes; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Convert the fluent instance to a Collection. |
| 105 | + * |
| 106 | + * @param string|null $key |
| 107 | + * @return \Illuminate\Support\Collection |
| 108 | + */ |
| 109 | + public function collect($key = null) |
| 110 | + { |
| 111 | + return new Collection($this->get($key)); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Convert the object into something JSON serializable. |
| 116 | + * |
| 117 | + * @return array<TKey, TValue> |
| 118 | + */ |
| 119 | + public function jsonSerialize(): array |
| 120 | + { |
| 121 | + return $this->toArray(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Convert the fluent instance to JSON. |
| 126 | + * |
| 127 | + * @param int $options |
| 128 | + * @return string |
| 129 | + */ |
| 130 | + public function toJson($options = 0) |
| 131 | + { |
| 132 | + return json_encode($this->jsonSerialize(), $options); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Determine if the given offset exists. |
| 137 | + * |
| 138 | + * @param TKey $offset |
| 139 | + * @return bool |
| 140 | + */ |
| 141 | + public function offsetExists($offset): bool |
| 142 | + { |
| 143 | + return isset($this->attributes[$offset]); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Get the value for a given offset. |
| 148 | + * |
| 149 | + * @param TKey $offset |
| 150 | + * @return TValue|null |
| 151 | + */ |
| 152 | + public function offsetGet($offset): mixed |
| 153 | + { |
| 154 | + return $this->value($offset); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Set the value at the given offset. |
| 159 | + * |
| 160 | + * @param TKey $offset |
| 161 | + * @param TValue $value |
| 162 | + * @return void |
| 163 | + */ |
| 164 | + public function offsetSet($offset, $value): void |
| 165 | + { |
| 166 | + $this->attributes[$offset] = $value; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Unset the value at the given offset. |
| 171 | + * |
| 172 | + * @param TKey $offset |
| 173 | + * @return void |
| 174 | + */ |
| 175 | + public function offsetUnset($offset): void |
| 176 | + { |
| 177 | + unset($this->attributes[$offset]); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Handle dynamic calls to the fluent instance to set attributes. |
| 182 | + * |
| 183 | + * @param TKey $method |
| 184 | + * @param array{0: ?TValue} $parameters |
| 185 | + * @return $this |
| 186 | + */ |
| 187 | + public function __call($method, $parameters) |
| 188 | + { |
| 189 | + $this->attributes[$method] = count($parameters) > 0 ? reset($parameters) : true; |
| 190 | + |
| 191 | + return $this; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Dynamically retrieve the value of an attribute. |
| 196 | + * |
| 197 | + * @param TKey $key |
| 198 | + * @return TValue|null |
| 199 | + */ |
| 200 | + public function __get($key) |
| 201 | + { |
| 202 | + return $this->value($key); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Dynamically set the value of an attribute. |
| 207 | + * |
| 208 | + * @param TKey $key |
| 209 | + * @param TValue $value |
| 210 | + * @return void |
| 211 | + */ |
| 212 | + public function __set($key, $value) |
| 213 | + { |
| 214 | + $this->offsetSet($key, $value); |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Dynamically check if an attribute is set. |
| 219 | + * |
| 220 | + * @param TKey $key |
| 221 | + * @return bool |
| 222 | + */ |
| 223 | + public function __isset($key) |
| 224 | + { |
| 225 | + return $this->offsetExists($key); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Dynamically unset an attribute. |
| 230 | + * |
| 231 | + * @param TKey $key |
| 232 | + * @return void |
| 233 | + */ |
| 234 | + public function __unset($key) |
| 235 | + { |
| 236 | + $this->offsetUnset($key); |
| 237 | + } |
| 238 | +} |
0 commit comments