@@ -332,29 +332,29 @@ |
||
| 332 | 332 | * |
| 333 | 333 | * @return void |
| 334 | 334 | */ |
| 335 | - public function if($name, callable $callback): void |
|
| 335 | + public function if ($name, callable $callback): void |
|
| 336 | 336 | { |
| 337 | 337 | $this->conditions[$name] = $callback; |
| 338 | 338 | |
| 339 | - $this->directive($name, function ($expression) use ($name) { |
|
| 339 | + $this->directive($name, function($expression) use ($name) { |
|
| 340 | 340 | return $expression !== '' |
| 341 | 341 | ? "<?php if (\Syscodes\Components\Support\Facades\Plaze::checkpoint('{$name}', {$expression})): ?>" |
| 342 | 342 | : "<?php if (\Syscodes\Components\Support\Facades\Plaze::checkpoint('{$name}')): ?>"; |
| 343 | 343 | }); |
| 344 | 344 | |
| 345 | - $this->directive('unless'.$name, function ($expression) use ($name) { |
|
| 345 | + $this->directive('unless'.$name, function($expression) use ($name) { |
|
| 346 | 346 | return $expression !== '' |
| 347 | 347 | ? "<?php if (! \Syscodes\Components\Support\Facades\Plaze::checkpoint('{$name}', {$expression})): ?>" |
| 348 | 348 | : "<?php if (! \Syscodes\Components\Support\Facades\Plaze::checkpoint('{$name}')): ?>"; |
| 349 | 349 | }); |
| 350 | 350 | |
| 351 | - $this->directive('else'.$name, function ($expression) use ($name) { |
|
| 351 | + $this->directive('else'.$name, function($expression) use ($name) { |
|
| 352 | 352 | return $expression !== '' |
| 353 | 353 | ? "<?php elseif (\Syscodes\Components\Support\Facades\Plaze::checkpoint('{$name}', {$expression})): ?>" |
| 354 | 354 | : "<?php elseif (\Syscodes\Components\Support\Facades\Plaze::checkpoint('{$name}')): ?>"; |
| 355 | 355 | }); |
| 356 | 356 | |
| 357 | - $this->directive('end'.$name, function () { |
|
| 357 | + $this->directive('end'.$name, function() { |
|
| 358 | 358 | return '<?php endif; ?>'; |
| 359 | 359 | }); |
| 360 | 360 | } |
@@ -92,7 +92,7 @@ |
||
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | return $this->files->lastModified($path) >= |
| 95 | - $this->files->lastModified($compiled); |
|
| 95 | + $this->files->lastModified($compiled); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
@@ -43,510 +43,510 @@ |
||
| 43 | 43 | */ |
| 44 | 44 | class View implements ArrayAccess, Webable, ViewContract |
| 45 | 45 | { |
| 46 | - use Macroable { |
|
| 47 | - __call as macroCall; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Array of local variables. |
|
| 52 | - * |
|
| 53 | - * @var array $data |
|
| 54 | - */ |
|
| 55 | - protected $data = []; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The engine implementation. |
|
| 59 | - * |
|
| 60 | - * @var \Syscodes\Components\Contracts\View\Engine $engine |
|
| 61 | - */ |
|
| 62 | - protected $engine; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * The view factory instance. |
|
| 66 | - * |
|
| 67 | - * @var \Syscodes\Components\View\factory $factory |
|
| 68 | - */ |
|
| 69 | - protected $factory; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * The path to the view file. |
|
| 73 | - * |
|
| 74 | - * @var string $path |
|
| 75 | - */ |
|
| 76 | - protected $path; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Get the name of the view. |
|
| 80 | - * |
|
| 81 | - * @var string $view |
|
| 82 | - */ |
|
| 83 | - protected $view; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Constructor: Create a new view instance. |
|
| 87 | - * |
|
| 88 | - * @param \Syscodes\Components\View\factory $factory |
|
| 89 | - * @param \Syscodes\Components\Contracts\View\Engine $engine |
|
| 90 | - * @param string $view |
|
| 91 | - * @param string $path |
|
| 92 | - * @param array $data |
|
| 93 | - * |
|
| 94 | - * @return void |
|
| 95 | - * |
|
| 96 | - * @throws \InvalidArgumentException |
|
| 97 | - */ |
|
| 98 | - public function __construct( |
|
| 99 | - Factory $factory, |
|
| 100 | - Engine $engine, |
|
| 101 | - $view, |
|
| 102 | - $path, |
|
| 103 | - $data = [] |
|
| 104 | - ) { |
|
| 105 | - $this->factory = $factory; |
|
| 106 | - $this->engine = $engine; |
|
| 107 | - $this->view = $view; |
|
| 108 | - $this->path = $path; |
|
| 109 | - $this->data = $data instanceof Arrayable ? $data->toArray() : (array) $data; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Get the string contents of the view. |
|
| 114 | - * |
|
| 115 | - * @example View::render(); |
|
| 116 | - * |
|
| 117 | - * @param \Callable|null $callback |
|
| 118 | - * |
|
| 119 | - * @return array|string |
|
| 120 | - * |
|
| 121 | - * @throws \Throwable |
|
| 122 | - */ |
|
| 123 | - public function render(Callable $callback = null) |
|
| 124 | - { |
|
| 125 | - try { |
|
| 126 | - $contents = $this->renderContents(); |
|
| 127 | - |
|
| 128 | - $response = isset($callback) ? $callback($this, $contents) : null; |
|
| 129 | - |
|
| 130 | - $this->factory->flushStateIfDoneRendering(); |
|
| 131 | - |
|
| 132 | - return ! is_null($response) ? $response : $contents; |
|
| 133 | - } catch(Throwable $e) { |
|
| 134 | - $this->factory->flushState(); |
|
| 135 | - |
|
| 136 | - throw $e; |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Get the contents of the view instance. |
|
| 142 | - * |
|
| 143 | - * @return void |
|
| 144 | - */ |
|
| 145 | - protected function renderContents() |
|
| 146 | - { |
|
| 147 | - $this->factory->increment(); |
|
| 148 | - |
|
| 149 | - $contents = $this->getContents(); |
|
| 150 | - |
|
| 151 | - $this->factory->decrement(); |
|
| 152 | - |
|
| 153 | - return $contents; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Get the evaluated contents of the view. |
|
| 158 | - * |
|
| 159 | - * @return string |
|
| 160 | - */ |
|
| 161 | - protected function getContents(): string |
|
| 162 | - { |
|
| 163 | - return $this->engine->get($this->path, $this->getArrayData()); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * The view data will be extracted. |
|
| 168 | - * |
|
| 169 | - * @return array |
|
| 170 | - */ |
|
| 171 | - public function getArrayData(): array |
|
| 172 | - { |
|
| 173 | - $data = array_merge($this->factory->getShared(), $this->data); |
|
| 46 | + use Macroable { |
|
| 47 | + __call as macroCall; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Array of local variables. |
|
| 52 | + * |
|
| 53 | + * @var array $data |
|
| 54 | + */ |
|
| 55 | + protected $data = []; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The engine implementation. |
|
| 59 | + * |
|
| 60 | + * @var \Syscodes\Components\Contracts\View\Engine $engine |
|
| 61 | + */ |
|
| 62 | + protected $engine; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * The view factory instance. |
|
| 66 | + * |
|
| 67 | + * @var \Syscodes\Components\View\factory $factory |
|
| 68 | + */ |
|
| 69 | + protected $factory; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * The path to the view file. |
|
| 73 | + * |
|
| 74 | + * @var string $path |
|
| 75 | + */ |
|
| 76 | + protected $path; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Get the name of the view. |
|
| 80 | + * |
|
| 81 | + * @var string $view |
|
| 82 | + */ |
|
| 83 | + protected $view; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Constructor: Create a new view instance. |
|
| 87 | + * |
|
| 88 | + * @param \Syscodes\Components\View\factory $factory |
|
| 89 | + * @param \Syscodes\Components\Contracts\View\Engine $engine |
|
| 90 | + * @param string $view |
|
| 91 | + * @param string $path |
|
| 92 | + * @param array $data |
|
| 93 | + * |
|
| 94 | + * @return void |
|
| 95 | + * |
|
| 96 | + * @throws \InvalidArgumentException |
|
| 97 | + */ |
|
| 98 | + public function __construct( |
|
| 99 | + Factory $factory, |
|
| 100 | + Engine $engine, |
|
| 101 | + $view, |
|
| 102 | + $path, |
|
| 103 | + $data = [] |
|
| 104 | + ) { |
|
| 105 | + $this->factory = $factory; |
|
| 106 | + $this->engine = $engine; |
|
| 107 | + $this->view = $view; |
|
| 108 | + $this->path = $path; |
|
| 109 | + $this->data = $data instanceof Arrayable ? $data->toArray() : (array) $data; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Get the string contents of the view. |
|
| 114 | + * |
|
| 115 | + * @example View::render(); |
|
| 116 | + * |
|
| 117 | + * @param \Callable|null $callback |
|
| 118 | + * |
|
| 119 | + * @return array|string |
|
| 120 | + * |
|
| 121 | + * @throws \Throwable |
|
| 122 | + */ |
|
| 123 | + public function render(Callable $callback = null) |
|
| 124 | + { |
|
| 125 | + try { |
|
| 126 | + $contents = $this->renderContents(); |
|
| 127 | + |
|
| 128 | + $response = isset($callback) ? $callback($this, $contents) : null; |
|
| 129 | + |
|
| 130 | + $this->factory->flushStateIfDoneRendering(); |
|
| 131 | + |
|
| 132 | + return ! is_null($response) ? $response : $contents; |
|
| 133 | + } catch(Throwable $e) { |
|
| 134 | + $this->factory->flushState(); |
|
| 135 | + |
|
| 136 | + throw $e; |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Get the contents of the view instance. |
|
| 142 | + * |
|
| 143 | + * @return void |
|
| 144 | + */ |
|
| 145 | + protected function renderContents() |
|
| 146 | + { |
|
| 147 | + $this->factory->increment(); |
|
| 148 | + |
|
| 149 | + $contents = $this->getContents(); |
|
| 150 | + |
|
| 151 | + $this->factory->decrement(); |
|
| 152 | + |
|
| 153 | + return $contents; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Get the evaluated contents of the view. |
|
| 158 | + * |
|
| 159 | + * @return string |
|
| 160 | + */ |
|
| 161 | + protected function getContents(): string |
|
| 162 | + { |
|
| 163 | + return $this->engine->get($this->path, $this->getArrayData()); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * The view data will be extracted. |
|
| 168 | + * |
|
| 169 | + * @return array |
|
| 170 | + */ |
|
| 171 | + public function getArrayData(): array |
|
| 172 | + { |
|
| 173 | + $data = array_merge($this->factory->getShared(), $this->data); |
|
| 174 | 174 | |
| 175 | - return array_map(fn ($value) => ($value instanceof Renderable) ? $value->render() : $value, $data); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Get the sections of the rendered view. |
|
| 180 | - * |
|
| 181 | - * @return array |
|
| 182 | - * |
|
| 183 | - * @throws \Throwable |
|
| 184 | - */ |
|
| 185 | - public function renderSections() |
|
| 186 | - { |
|
| 187 | - return $this->render(fn () => $this->factory->getSections()); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Add a piece of data to the view. |
|
| 192 | - * |
|
| 193 | - * @example $view->assign($content, $data); |
|
| 194 | - * |
|
| 195 | - * @param string|array $key |
|
| 196 | - * @param mixed $value |
|
| 197 | - * |
|
| 198 | - * @return static |
|
| 199 | - */ |
|
| 200 | - public function assign($key, $value = null): static |
|
| 201 | - { |
|
| 202 | - if (is_array($key)) { |
|
| 203 | - $this->data = array_merge($this->data, $key); |
|
| 204 | - } else { |
|
| 205 | - $this->data[$key] = $value; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - return $this; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Assigns a value by reference. The benefit of binding is that values can be altered |
|
| 213 | - * without re-setting them. It is also possible to bind variables before they have values. |
|
| 214 | - * Assigned values will be available as a variable within the view file: |
|
| 215 | - * |
|
| 216 | - * @example $view->bind('ref', $bar); |
|
| 217 | - * |
|
| 218 | - * @param string $key Variable name |
|
| 219 | - * @param mixed $value Referenced variable |
|
| 220 | - * |
|
| 221 | - * @return static |
|
| 222 | - */ |
|
| 223 | - public function bind($key, & $value): static |
|
| 224 | - { |
|
| 225 | - $this->data[$key] =& $value; |
|
| 226 | - |
|
| 227 | - return $this; |
|
| 228 | - } |
|
| 175 | + return array_map(fn ($value) => ($value instanceof Renderable) ? $value->render() : $value, $data); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Get the sections of the rendered view. |
|
| 180 | + * |
|
| 181 | + * @return array |
|
| 182 | + * |
|
| 183 | + * @throws \Throwable |
|
| 184 | + */ |
|
| 185 | + public function renderSections() |
|
| 186 | + { |
|
| 187 | + return $this->render(fn () => $this->factory->getSections()); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Add a piece of data to the view. |
|
| 192 | + * |
|
| 193 | + * @example $view->assign($content, $data); |
|
| 194 | + * |
|
| 195 | + * @param string|array $key |
|
| 196 | + * @param mixed $value |
|
| 197 | + * |
|
| 198 | + * @return static |
|
| 199 | + */ |
|
| 200 | + public function assign($key, $value = null): static |
|
| 201 | + { |
|
| 202 | + if (is_array($key)) { |
|
| 203 | + $this->data = array_merge($this->data, $key); |
|
| 204 | + } else { |
|
| 205 | + $this->data[$key] = $value; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + return $this; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Assigns a value by reference. The benefit of binding is that values can be altered |
|
| 213 | + * without re-setting them. It is also possible to bind variables before they have values. |
|
| 214 | + * Assigned values will be available as a variable within the view file: |
|
| 215 | + * |
|
| 216 | + * @example $view->bind('ref', $bar); |
|
| 217 | + * |
|
| 218 | + * @param string $key Variable name |
|
| 219 | + * @param mixed $value Referenced variable |
|
| 220 | + * |
|
| 221 | + * @return static |
|
| 222 | + */ |
|
| 223 | + public function bind($key, & $value): static |
|
| 224 | + { |
|
| 225 | + $this->data[$key] =& $value; |
|
| 226 | + |
|
| 227 | + return $this; |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - /** |
|
| 231 | - * Add validation errors to the view. |
|
| 232 | - * |
|
| 233 | - * @param \Syscodes\Components\Contracts\Support\MessageProvider|array $provider |
|
| 234 | - * |
|
| 235 | - * @return static |
|
| 236 | - */ |
|
| 237 | - public function withErrors($provider): static |
|
| 238 | - { |
|
| 239 | - $this->with('errors', $this->formatErrors($provider)); |
|
| 230 | + /** |
|
| 231 | + * Add validation errors to the view. |
|
| 232 | + * |
|
| 233 | + * @param \Syscodes\Components\Contracts\Support\MessageProvider|array $provider |
|
| 234 | + * |
|
| 235 | + * @return static |
|
| 236 | + */ |
|
| 237 | + public function withErrors($provider): static |
|
| 238 | + { |
|
| 239 | + $this->with('errors', $this->formatErrors($provider)); |
|
| 240 | 240 | |
| 241 | - return $this; |
|
| 242 | - } |
|
| 241 | + return $this; |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - /** |
|
| 245 | - * Format the given message provider into a MessageBag. |
|
| 246 | - * |
|
| 247 | - * @param \Syscodes\Components\Contracts\Support\MessageProvider|array $provider |
|
| 248 | - * |
|
| 249 | - * @return \Syscodes\Components\Support\MessageBag |
|
| 250 | - */ |
|
| 251 | - protected function formatErrors($provider) |
|
| 252 | - { |
|
| 253 | - return $provider instanceof MessageProvider |
|
| 254 | - ? $provider->getMessageBag() : new MessageBag((array) $provider); |
|
| 244 | + /** |
|
| 245 | + * Format the given message provider into a MessageBag. |
|
| 246 | + * |
|
| 247 | + * @param \Syscodes\Components\Contracts\Support\MessageProvider|array $provider |
|
| 248 | + * |
|
| 249 | + * @return \Syscodes\Components\Support\MessageBag |
|
| 250 | + */ |
|
| 251 | + protected function formatErrors($provider) |
|
| 252 | + { |
|
| 253 | + return $provider instanceof MessageProvider |
|
| 254 | + ? $provider->getMessageBag() : new MessageBag((array) $provider); |
|
| 255 | 255 | |
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Get the array of view data. |
|
| 260 | - * |
|
| 261 | - * @return array |
|
| 262 | - */ |
|
| 263 | - public function getData(): array |
|
| 264 | - { |
|
| 265 | - return $this->data; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Get the name of the view. |
|
| 270 | - * |
|
| 271 | - * @return string |
|
| 272 | - */ |
|
| 273 | - public function getView(): string |
|
| 274 | - { |
|
| 275 | - return $this->view; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Get the path to the view file. |
|
| 280 | - * |
|
| 281 | - * @return string |
|
| 282 | - */ |
|
| 283 | - public function getPath(): string |
|
| 284 | - { |
|
| 285 | - return $this->path; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Set the path to the view file. |
|
| 290 | - * |
|
| 291 | - * @param string $path |
|
| 292 | - * |
|
| 293 | - * @return void |
|
| 294 | - */ |
|
| 295 | - public function setPath($path): void |
|
| 296 | - { |
|
| 297 | - $this->path = $path; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Get the view factory instance. |
|
| 302 | - * |
|
| 303 | - * @return \Syscodes\Components\View\factory |
|
| 304 | - */ |
|
| 305 | - public function getFactory() |
|
| 306 | - { |
|
| 307 | - return $this->factory; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Get the view's rendering engine. |
|
| 312 | - * |
|
| 313 | - * @return \Syscodes\Components\Contracts\View\Engine |
|
| 314 | - */ |
|
| 315 | - public function getEngine() |
|
| 316 | - { |
|
| 317 | - return $this->engine; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Searches for the given variable and returns its value. |
|
| 322 | - * Local variables will be returned before global variables. |
|
| 323 | - * |
|
| 324 | - * @example $value = $view->get('foo', 'bar'); |
|
| 325 | - * |
|
| 326 | - * If the key is not given or null, the entire data array is returned. |
|
| 327 | - * |
|
| 328 | - * @param string $key The variable name |
|
| 329 | - * @param mixed $default The default value to return |
|
| 330 | - * |
|
| 331 | - * @return mixed |
|
| 332 | - * |
|
| 333 | - * @throws \InvalidArgumentException |
|
| 334 | - */ |
|
| 335 | - public function &get($key, $default = null) |
|
| 336 | - { |
|
| 337 | - if (strpos($key, '.') === false) { |
|
| 338 | - if (array_key_exists($key, $this->data)) { |
|
| 339 | - return $this->data[$key]; |
|
| 340 | - } else { |
|
| 341 | - throw new InvalidArgumentException(__('view.variableNotSet')); |
|
| 342 | - } |
|
| 343 | - } else { |
|
| 344 | - return value($default); |
|
| 345 | - } |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Assigns a variable by name. Assigned values will be available as a |
|
| 350 | - * variable within the view file: |
|
| 351 | - * |
|
| 352 | - * This value can be accessed as $var within the view |
|
| 353 | - * @example $view->set(array('food' => 'bread', 'beverage' => 'water')); |
|
| 354 | - * |
|
| 355 | - * @param string|array $key Variable name |
|
| 356 | - * @param mixed $value Value |
|
| 357 | - * |
|
| 358 | - * @return static |
|
| 359 | - */ |
|
| 360 | - public function set($key, $value = null): static |
|
| 361 | - { |
|
| 362 | - if (is_array($key) || $key instanceof Traversable) { |
|
| 363 | - foreach ($key as $name => $value) { |
|
| 364 | - $this->assign($name, $value); |
|
| 365 | - } |
|
| 366 | - } else { |
|
| 367 | - if (strpos($key, '.') === false) { |
|
| 368 | - $this->data[$key] = $value; |
|
| 369 | - } else { |
|
| 370 | - Arr::set($this->data, $key, $value); |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - return $this; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /* |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Get the array of view data. |
|
| 260 | + * |
|
| 261 | + * @return array |
|
| 262 | + */ |
|
| 263 | + public function getData(): array |
|
| 264 | + { |
|
| 265 | + return $this->data; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Get the name of the view. |
|
| 270 | + * |
|
| 271 | + * @return string |
|
| 272 | + */ |
|
| 273 | + public function getView(): string |
|
| 274 | + { |
|
| 275 | + return $this->view; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Get the path to the view file. |
|
| 280 | + * |
|
| 281 | + * @return string |
|
| 282 | + */ |
|
| 283 | + public function getPath(): string |
|
| 284 | + { |
|
| 285 | + return $this->path; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Set the path to the view file. |
|
| 290 | + * |
|
| 291 | + * @param string $path |
|
| 292 | + * |
|
| 293 | + * @return void |
|
| 294 | + */ |
|
| 295 | + public function setPath($path): void |
|
| 296 | + { |
|
| 297 | + $this->path = $path; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Get the view factory instance. |
|
| 302 | + * |
|
| 303 | + * @return \Syscodes\Components\View\factory |
|
| 304 | + */ |
|
| 305 | + public function getFactory() |
|
| 306 | + { |
|
| 307 | + return $this->factory; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Get the view's rendering engine. |
|
| 312 | + * |
|
| 313 | + * @return \Syscodes\Components\Contracts\View\Engine |
|
| 314 | + */ |
|
| 315 | + public function getEngine() |
|
| 316 | + { |
|
| 317 | + return $this->engine; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Searches for the given variable and returns its value. |
|
| 322 | + * Local variables will be returned before global variables. |
|
| 323 | + * |
|
| 324 | + * @example $value = $view->get('foo', 'bar'); |
|
| 325 | + * |
|
| 326 | + * If the key is not given or null, the entire data array is returned. |
|
| 327 | + * |
|
| 328 | + * @param string $key The variable name |
|
| 329 | + * @param mixed $default The default value to return |
|
| 330 | + * |
|
| 331 | + * @return mixed |
|
| 332 | + * |
|
| 333 | + * @throws \InvalidArgumentException |
|
| 334 | + */ |
|
| 335 | + public function &get($key, $default = null) |
|
| 336 | + { |
|
| 337 | + if (strpos($key, '.') === false) { |
|
| 338 | + if (array_key_exists($key, $this->data)) { |
|
| 339 | + return $this->data[$key]; |
|
| 340 | + } else { |
|
| 341 | + throw new InvalidArgumentException(__('view.variableNotSet')); |
|
| 342 | + } |
|
| 343 | + } else { |
|
| 344 | + return value($default); |
|
| 345 | + } |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Assigns a variable by name. Assigned values will be available as a |
|
| 350 | + * variable within the view file: |
|
| 351 | + * |
|
| 352 | + * This value can be accessed as $var within the view |
|
| 353 | + * @example $view->set(array('food' => 'bread', 'beverage' => 'water')); |
|
| 354 | + * |
|
| 355 | + * @param string|array $key Variable name |
|
| 356 | + * @param mixed $value Value |
|
| 357 | + * |
|
| 358 | + * @return static |
|
| 359 | + */ |
|
| 360 | + public function set($key, $value = null): static |
|
| 361 | + { |
|
| 362 | + if (is_array($key) || $key instanceof Traversable) { |
|
| 363 | + foreach ($key as $name => $value) { |
|
| 364 | + $this->assign($name, $value); |
|
| 365 | + } |
|
| 366 | + } else { |
|
| 367 | + if (strpos($key, '.') === false) { |
|
| 368 | + $this->data[$key] = $value; |
|
| 369 | + } else { |
|
| 370 | + Arr::set($this->data, $key, $value); |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + return $this; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /* |
|
| 378 | 378 | |----------------------------------------------------------------- |
| 379 | 379 | | ArrayAccess Methods |
| 380 | 380 | |----------------------------------------------------------------- |
| 381 | 381 | */ |
| 382 | 382 | |
| 383 | - /** |
|
| 384 | - * Whether or not an offset exists. |
|
| 385 | - * |
|
| 386 | - * @param string $offset |
|
| 387 | - * |
|
| 388 | - * @return bool |
|
| 389 | - */ |
|
| 390 | - public function offsetExists($offset): bool |
|
| 391 | - { |
|
| 392 | - return array_key_exists($offset, $this->data); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Returns the value at specified offset. |
|
| 397 | - * |
|
| 398 | - * @param string $offset |
|
| 399 | - * |
|
| 400 | - * @return mixed |
|
| 401 | - */ |
|
| 402 | - public function offsetGet($offset) |
|
| 403 | - { |
|
| 404 | - return $this->data[$offset]; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Assigns a value to the specified offset |
|
| 409 | - * |
|
| 410 | - * @param string $offset |
|
| 411 | - * @param mixed $value |
|
| 412 | - * |
|
| 413 | - * @return void |
|
| 414 | - */ |
|
| 415 | - public function offsetSet($offset, $value): void |
|
| 416 | - { |
|
| 417 | - $this->assign($offset, $value); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * Unsets an offset. |
|
| 422 | - * |
|
| 423 | - * @param string $offset |
|
| 424 | - * |
|
| 425 | - * @return void |
|
| 426 | - */ |
|
| 427 | - public function offsetUnset($offset): void |
|
| 428 | - { |
|
| 429 | - unset($this->data[$offset]); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Magic method. |
|
| 434 | - * |
|
| 435 | - * Searches for the given variable and returns its value. |
|
| 436 | - * Local variables will be returned before global variables. |
|
| 437 | - * |
|
| 438 | - * @example $value = $view->var; |
|
| 439 | - * |
|
| 440 | - * @param string $key Variable name |
|
| 441 | - * |
|
| 442 | - * @return mixed |
|
| 443 | - * |
|
| 444 | - * @throws \Syscodes\Components\LenevorException |
|
| 445 | - */ |
|
| 446 | - public function &__get($key) |
|
| 447 | - { |
|
| 448 | - return $this->get($key); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Magic method. |
|
| 453 | - * |
|
| 454 | - * Calls [$this->set] with the same parameters. |
|
| 455 | - * |
|
| 456 | - * @example $view->var = 'something'; |
|
| 457 | - * |
|
| 458 | - * @param string $key Variable name |
|
| 459 | - * @param mixed $value Value |
|
| 460 | - * |
|
| 461 | - * @return void |
|
| 462 | - */ |
|
| 463 | - public function __set($key, $value) |
|
| 464 | - { |
|
| 465 | - $this->set($key, $value); |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * Magic method. |
|
| 470 | - * |
|
| 471 | - * Determines if a variable is set. |
|
| 472 | - * |
|
| 473 | - * @example isset($view->foo); |
|
| 474 | - * |
|
| 475 | - * Variables are not considered to be set. |
|
| 476 | - * |
|
| 477 | - * @param string $key variable name |
|
| 478 | - * |
|
| 479 | - * @return boolean |
|
| 480 | - */ |
|
| 481 | - public function __isset($key) |
|
| 482 | - { |
|
| 483 | - return isset($this->data[$key]); |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * Magic method. |
|
| 488 | - * |
|
| 489 | - * Unsets a given variable. |
|
| 490 | - * |
|
| 491 | - * @example unset($view->var); |
|
| 492 | - * |
|
| 493 | - * @param string $key Variable name |
|
| 494 | - * |
|
| 495 | - * @return void |
|
| 496 | - */ |
|
| 497 | - public function __unset($key) |
|
| 498 | - { |
|
| 499 | - unset($this->data[$key]); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * Magic Method for handling dynamic functions. |
|
| 504 | - * |
|
| 505 | - * @param string $method |
|
| 506 | - * @param array $parameters |
|
| 507 | - * |
|
| 508 | - * @return mixed |
|
| 509 | - * |
|
| 510 | - * @throws \BadMethodCallException |
|
| 511 | - */ |
|
| 512 | - public function __call($method, $parameters) |
|
| 513 | - { |
|
| 514 | - if (static::hasMacro($method)) { |
|
| 515 | - return $this->macroCall($method, $parameters); |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - if (Str::startsWith($method, 'assign')) { |
|
| 519 | - $name = Str::camelcase(substr($method, 4)); |
|
| 520 | - |
|
| 521 | - return $this->assign($name, $parameters[0]); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - throw new BadMethodCallException(sprintf( |
|
| 525 | - 'Method %s::%s() does not exist', static::class, $method) |
|
| 526 | - ); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * Get content as a string of HTML. |
|
| 531 | - * |
|
| 532 | - * @return string |
|
| 533 | - */ |
|
| 534 | - public function toHtml(): string |
|
| 535 | - { |
|
| 536 | - return $this->render(); |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - /** |
|
| 540 | - * Magic method. |
|
| 541 | - * |
|
| 542 | - * Returns the output of [static::render]. |
|
| 543 | - * |
|
| 544 | - * @return string |
|
| 545 | - * |
|
| 546 | - * @throws \Throwable |
|
| 547 | - */ |
|
| 548 | - public function __toString(): string |
|
| 549 | - { |
|
| 550 | - return $this->render(); |
|
| 551 | - } |
|
| 383 | + /** |
|
| 384 | + * Whether or not an offset exists. |
|
| 385 | + * |
|
| 386 | + * @param string $offset |
|
| 387 | + * |
|
| 388 | + * @return bool |
|
| 389 | + */ |
|
| 390 | + public function offsetExists($offset): bool |
|
| 391 | + { |
|
| 392 | + return array_key_exists($offset, $this->data); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Returns the value at specified offset. |
|
| 397 | + * |
|
| 398 | + * @param string $offset |
|
| 399 | + * |
|
| 400 | + * @return mixed |
|
| 401 | + */ |
|
| 402 | + public function offsetGet($offset) |
|
| 403 | + { |
|
| 404 | + return $this->data[$offset]; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Assigns a value to the specified offset |
|
| 409 | + * |
|
| 410 | + * @param string $offset |
|
| 411 | + * @param mixed $value |
|
| 412 | + * |
|
| 413 | + * @return void |
|
| 414 | + */ |
|
| 415 | + public function offsetSet($offset, $value): void |
|
| 416 | + { |
|
| 417 | + $this->assign($offset, $value); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * Unsets an offset. |
|
| 422 | + * |
|
| 423 | + * @param string $offset |
|
| 424 | + * |
|
| 425 | + * @return void |
|
| 426 | + */ |
|
| 427 | + public function offsetUnset($offset): void |
|
| 428 | + { |
|
| 429 | + unset($this->data[$offset]); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Magic method. |
|
| 434 | + * |
|
| 435 | + * Searches for the given variable and returns its value. |
|
| 436 | + * Local variables will be returned before global variables. |
|
| 437 | + * |
|
| 438 | + * @example $value = $view->var; |
|
| 439 | + * |
|
| 440 | + * @param string $key Variable name |
|
| 441 | + * |
|
| 442 | + * @return mixed |
|
| 443 | + * |
|
| 444 | + * @throws \Syscodes\Components\LenevorException |
|
| 445 | + */ |
|
| 446 | + public function &__get($key) |
|
| 447 | + { |
|
| 448 | + return $this->get($key); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Magic method. |
|
| 453 | + * |
|
| 454 | + * Calls [$this->set] with the same parameters. |
|
| 455 | + * |
|
| 456 | + * @example $view->var = 'something'; |
|
| 457 | + * |
|
| 458 | + * @param string $key Variable name |
|
| 459 | + * @param mixed $value Value |
|
| 460 | + * |
|
| 461 | + * @return void |
|
| 462 | + */ |
|
| 463 | + public function __set($key, $value) |
|
| 464 | + { |
|
| 465 | + $this->set($key, $value); |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * Magic method. |
|
| 470 | + * |
|
| 471 | + * Determines if a variable is set. |
|
| 472 | + * |
|
| 473 | + * @example isset($view->foo); |
|
| 474 | + * |
|
| 475 | + * Variables are not considered to be set. |
|
| 476 | + * |
|
| 477 | + * @param string $key variable name |
|
| 478 | + * |
|
| 479 | + * @return boolean |
|
| 480 | + */ |
|
| 481 | + public function __isset($key) |
|
| 482 | + { |
|
| 483 | + return isset($this->data[$key]); |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * Magic method. |
|
| 488 | + * |
|
| 489 | + * Unsets a given variable. |
|
| 490 | + * |
|
| 491 | + * @example unset($view->var); |
|
| 492 | + * |
|
| 493 | + * @param string $key Variable name |
|
| 494 | + * |
|
| 495 | + * @return void |
|
| 496 | + */ |
|
| 497 | + public function __unset($key) |
|
| 498 | + { |
|
| 499 | + unset($this->data[$key]); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * Magic Method for handling dynamic functions. |
|
| 504 | + * |
|
| 505 | + * @param string $method |
|
| 506 | + * @param array $parameters |
|
| 507 | + * |
|
| 508 | + * @return mixed |
|
| 509 | + * |
|
| 510 | + * @throws \BadMethodCallException |
|
| 511 | + */ |
|
| 512 | + public function __call($method, $parameters) |
|
| 513 | + { |
|
| 514 | + if (static::hasMacro($method)) { |
|
| 515 | + return $this->macroCall($method, $parameters); |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + if (Str::startsWith($method, 'assign')) { |
|
| 519 | + $name = Str::camelcase(substr($method, 4)); |
|
| 520 | + |
|
| 521 | + return $this->assign($name, $parameters[0]); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + throw new BadMethodCallException(sprintf( |
|
| 525 | + 'Method %s::%s() does not exist', static::class, $method) |
|
| 526 | + ); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * Get content as a string of HTML. |
|
| 531 | + * |
|
| 532 | + * @return string |
|
| 533 | + */ |
|
| 534 | + public function toHtml(): string |
|
| 535 | + { |
|
| 536 | + return $this->render(); |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + /** |
|
| 540 | + * Magic method. |
|
| 541 | + * |
|
| 542 | + * Returns the output of [static::render]. |
|
| 543 | + * |
|
| 544 | + * @return string |
|
| 545 | + * |
|
| 546 | + * @throws \Throwable |
|
| 547 | + */ |
|
| 548 | + public function __toString(): string |
|
| 549 | + { |
|
| 550 | + return $this->render(); |
|
| 551 | + } |
|
| 552 | 552 | } |
| 553 | 553 | \ No newline at end of file |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | $this->factory->flushStateIfDoneRendering(); |
| 131 | 131 | |
| 132 | 132 | return ! is_null($response) ? $response : $contents; |
| 133 | - } catch(Throwable $e) { |
|
| 133 | + } catch (Throwable $e) { |
|
| 134 | 134 | $this->factory->flushState(); |
| 135 | 135 | |
| 136 | 136 | throw $e; |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | */ |
| 223 | 223 | public function bind($key, & $value): static |
| 224 | 224 | { |
| 225 | - $this->data[$key] =& $value; |
|
| 225 | + $this->data[$key] = & $value; |
|
| 226 | 226 | |
| 227 | 227 | return $this; |
| 228 | 228 | } |
@@ -57,7 +57,7 @@ |
||
| 57 | 57 | * |
| 58 | 58 | * @return int|string |
| 59 | 59 | */ |
| 60 | - protected function algorithm(): int|string |
|
| 60 | + protected function algorithm(): int | string |
|
| 61 | 61 | { |
| 62 | 62 | return PASSWORD_ARGON2ID; |
| 63 | 63 | } |
@@ -129,7 +129,7 @@ |
||
| 129 | 129 | 'memory_cost' => $this->memory($options), |
| 130 | 130 | 'time_cost' => $this->time($options), |
| 131 | 131 | 'threads' => $this->threads($options), |
| 132 | - ]); |
|
| 132 | + ]); |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | /** |
@@ -179,7 +179,7 @@ |
||
| 179 | 179 | * |
| 180 | 180 | * @return int|string |
| 181 | 181 | */ |
| 182 | - protected function algorithm(): int|string |
|
| 182 | + protected function algorithm(): int | string |
|
| 183 | 183 | { |
| 184 | 184 | return PASSWORD_ARGON2I; |
| 185 | 185 | } |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | * |
| 115 | 115 | * @return $this |
| 116 | 116 | */ |
| 117 | - public function date(string|array $dates): static |
|
| 117 | + public function date(string | array $dates): static |
|
| 118 | 118 | { |
| 119 | 119 | foreach ((array) $dates as $date) { |
| 120 | 120 | $this->dates[] = new DateComparator($date); |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @throws DirectoryNotFoundException if one of the directories does not exist |
| 152 | 152 | */ |
| 153 | - public function in(string|array $dirs): static |
|
| 153 | + public function in(string | array $dirs): static |
|
| 154 | 154 | { |
| 155 | 155 | $resolvedDirs = []; |
| 156 | 156 | |
@@ -692,7 +692,7 @@ discard block |
||
| 692 | 692 | return array_search($value, $this->items, $strict); |
| 693 | 693 | } |
| 694 | 694 | |
| 695 | - foreach($this->items as $key => $item) { |
|
| 695 | + foreach ($this->items as $key => $item) { |
|
| 696 | 696 | if ($value($item, $key)) { |
| 697 | 697 | return $key; |
| 698 | 698 | } |
@@ -745,9 +745,9 @@ discard block |
||
| 745 | 745 | * |
| 746 | 746 | * @return static |
| 747 | 747 | */ |
| 748 | - public function sort(callable|int $callback = null): static |
|
| 748 | + public function sort(callable | int $callback = null): static |
|
| 749 | 749 | { |
| 750 | - $items = $this->items; |
|
| 750 | + $items = $this->items; |
|
| 751 | 751 | |
| 752 | 752 | $callback && is_callable($callback) |
| 753 | 753 | ? uasort($items, $callback) |
@@ -796,7 +796,7 @@ discard block |
||
| 796 | 796 | * |
| 797 | 797 | * @return static |
| 798 | 798 | */ |
| 799 | - public function sortKeysDesc(int $options = SORT_REGULAR): static |
|
| 799 | + public function sortKeysDesc(int $options = SORT_REGULAR): static |
|
| 800 | 800 | { |
| 801 | 801 | return $this->sortKeys($options, true); |
| 802 | 802 | } |
@@ -29,42 +29,42 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | class Arr |
| 31 | 31 | { |
| 32 | - /** |
|
| 33 | - * Determine whether the value is accessible in a array. |
|
| 34 | - * |
|
| 35 | - * @param mixed $value The default value |
|
| 36 | - * |
|
| 37 | - * @return mixed |
|
| 38 | - * |
|
| 39 | - * @uses instanceof ArrayAccess |
|
| 40 | - */ |
|
| 41 | - public static function accessible(mixed $value): mixed |
|
| 42 | - { |
|
| 43 | - return is_array($value) || $value instanceof ArrayAccess; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Add an element to an array using "dot" notation if it doesn't exist. |
|
| 48 | - * |
|
| 49 | - * @param array $array The search array |
|
| 50 | - * @param string $key The key exist |
|
| 51 | - * @param mixed $value The default value |
|
| 52 | - * |
|
| 53 | - * @return array |
|
| 54 | - */ |
|
| 55 | - public static function add(array $array, string $key, mixed $value): array |
|
| 56 | - { |
|
| 57 | - if (is_null(static::get($array, $key))) { |
|
| 58 | - static::set($array, $key, $value); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - return $array; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 32 | + /** |
|
| 33 | + * Determine whether the value is accessible in a array. |
|
| 34 | + * |
|
| 35 | + * @param mixed $value The default value |
|
| 36 | + * |
|
| 37 | + * @return mixed |
|
| 38 | + * |
|
| 39 | + * @uses instanceof ArrayAccess |
|
| 40 | + */ |
|
| 41 | + public static function accessible(mixed $value): mixed |
|
| 42 | + { |
|
| 43 | + return is_array($value) || $value instanceof ArrayAccess; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Add an element to an array using "dot" notation if it doesn't exist. |
|
| 48 | + * |
|
| 49 | + * @param array $array The search array |
|
| 50 | + * @param string $key The key exist |
|
| 51 | + * @param mixed $value The default value |
|
| 52 | + * |
|
| 53 | + * @return array |
|
| 54 | + */ |
|
| 55 | + public static function add(array $array, string $key, mixed $value): array |
|
| 56 | + { |
|
| 57 | + if (is_null(static::get($array, $key))) { |
|
| 58 | + static::set($array, $key, $value); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + return $array; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | 65 | * Collapse the collection items into a single array. |
| 66 | - * |
|
| 67 | - * @param array $array |
|
| 66 | + * |
|
| 67 | + * @param array $array |
|
| 68 | 68 | * |
| 69 | 69 | * @return array |
| 70 | 70 | */ |
@@ -73,440 +73,440 @@ discard block |
||
| 73 | 73 | $results = []; |
| 74 | 74 | |
| 75 | 75 | foreach ($array as $values) { |
| 76 | - if ($values instanceof Collection) { |
|
| 77 | - $values = $values->all(); |
|
| 78 | - } elseif ( ! is_array($values)) { |
|
| 79 | - continue; |
|
| 80 | - } |
|
| 76 | + if ($values instanceof Collection) { |
|
| 77 | + $values = $values->all(); |
|
| 78 | + } elseif ( ! is_array($values)) { |
|
| 79 | + continue; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - $results[] = $values; |
|
| 82 | + $results[] = $values; |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | return array_merge([], ...$results); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Divide an array into two arrays. One with keys and the other with values. |
|
| 90 | - * |
|
| 91 | - * @param array $array |
|
| 92 | - * |
|
| 93 | - * @return array |
|
| 94 | - */ |
|
| 95 | - public static function divide(array $array): array |
|
| 96 | - { |
|
| 97 | - return [array_keys($array), array_values($array)]; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Get all of the given array except for a specified array of items. |
|
| 102 | - * |
|
| 103 | - * @param array $array |
|
| 104 | - * @param string|array $keys |
|
| 105 | - * |
|
| 106 | - * @return array |
|
| 107 | - */ |
|
| 108 | - public static function except(array $array, string|array $keys): array |
|
| 109 | - { |
|
| 110 | - static::erase($array, $keys); |
|
| 111 | - |
|
| 112 | - return $array; |
|
| 113 | - } |
|
| 88 | + /** |
|
| 89 | + * Divide an array into two arrays. One with keys and the other with values. |
|
| 90 | + * |
|
| 91 | + * @param array $array |
|
| 92 | + * |
|
| 93 | + * @return array |
|
| 94 | + */ |
|
| 95 | + public static function divide(array $array): array |
|
| 96 | + { |
|
| 97 | + return [array_keys($array), array_values($array)]; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Get all of the given array except for a specified array of items. |
|
| 102 | + * |
|
| 103 | + * @param array $array |
|
| 104 | + * @param string|array $keys |
|
| 105 | + * |
|
| 106 | + * @return array |
|
| 107 | + */ |
|
| 108 | + public static function except(array $array, string|array $keys): array |
|
| 109 | + { |
|
| 110 | + static::erase($array, $keys); |
|
| 111 | + |
|
| 112 | + return $array; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Determine if the given key exists in the provided array. |
|
| 117 | - * |
|
| 118 | - * @param ArrayAccess|array $array The search array |
|
| 119 | - * @param string|int $key The key exist |
|
| 120 | - * |
|
| 121 | - * @return bool |
|
| 122 | - */ |
|
| 123 | - public static function exists($array, $key): bool |
|
| 124 | - { |
|
| 125 | - if ($array instanceof ArrayAccess) { |
|
| 126 | - return $array->offsetExists($key); |
|
| 127 | - } |
|
| 115 | + /** |
|
| 116 | + * Determine if the given key exists in the provided array. |
|
| 117 | + * |
|
| 118 | + * @param ArrayAccess|array $array The search array |
|
| 119 | + * @param string|int $key The key exist |
|
| 120 | + * |
|
| 121 | + * @return bool |
|
| 122 | + */ |
|
| 123 | + public static function exists($array, $key): bool |
|
| 124 | + { |
|
| 125 | + if ($array instanceof ArrayAccess) { |
|
| 126 | + return $array->offsetExists($key); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - return array_key_exists($key, $array); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Unsets dot-notated key from an array. |
|
| 134 | - * |
|
| 135 | - * @param array $array The search array |
|
| 136 | - * @param mixed $keys The dot-notated key or array of keys |
|
| 137 | - * |
|
| 138 | - * @return mixed |
|
| 139 | - */ |
|
| 140 | - public static function erase(array &$array, mixed $keys) |
|
| 141 | - { |
|
| 142 | - $original = &$array; |
|
| 143 | - |
|
| 144 | - $keys = (array) $keys; |
|
| 145 | - |
|
| 146 | - if (count($keys) === 0) { |
|
| 147 | - return; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - foreach ($keys as $key) { |
|
| 151 | - if (static::exists($array, $key)) { |
|
| 152 | - unset($array[$key]); |
|
| 153 | - |
|
| 154 | - continue; |
|
| 155 | - } |
|
| 129 | + return array_key_exists($key, $array); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Unsets dot-notated key from an array. |
|
| 134 | + * |
|
| 135 | + * @param array $array The search array |
|
| 136 | + * @param mixed $keys The dot-notated key or array of keys |
|
| 137 | + * |
|
| 138 | + * @return mixed |
|
| 139 | + */ |
|
| 140 | + public static function erase(array &$array, mixed $keys) |
|
| 141 | + { |
|
| 142 | + $original = &$array; |
|
| 143 | + |
|
| 144 | + $keys = (array) $keys; |
|
| 145 | + |
|
| 146 | + if (count($keys) === 0) { |
|
| 147 | + return; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + foreach ($keys as $key) { |
|
| 151 | + if (static::exists($array, $key)) { |
|
| 152 | + unset($array[$key]); |
|
| 153 | + |
|
| 154 | + continue; |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - $parts = explode('.', $key); |
|
| 157 | + $parts = explode('.', $key); |
|
| 158 | 158 | |
| 159 | - // Clean up after each pass |
|
| 160 | - $array = &$original; |
|
| 159 | + // Clean up after each pass |
|
| 160 | + $array = &$original; |
|
| 161 | 161 | |
| 162 | - // traverse the array into the second last key |
|
| 163 | - while (count($parts) > 1) { |
|
| 164 | - $part = array_shift($parts); |
|
| 162 | + // traverse the array into the second last key |
|
| 163 | + while (count($parts) > 1) { |
|
| 164 | + $part = array_shift($parts); |
|
| 165 | 165 | |
| 166 | - if (isset($array[$part]) && is_array($array[$part])) { |
|
| 167 | - $array = &$array[$key]; |
|
| 168 | - } else { |
|
| 169 | - continue 2; |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - unset($array[array_shift($parts)]); |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Flatten a multi-dimensional array into a single level. |
|
| 179 | - * |
|
| 180 | - * @param array $array |
|
| 181 | - * |
|
| 182 | - * @return array |
|
| 183 | - */ |
|
| 184 | - public static function flatten(array $array): array |
|
| 185 | - { |
|
| 186 | - $result = []; |
|
| 187 | - |
|
| 188 | - array_walk_recursive($array, function ($value) use (&$result) { |
|
| 189 | - $result[] = $value; |
|
| 190 | - }); |
|
| 191 | - |
|
| 192 | - return $result; |
|
| 193 | - } |
|
| 166 | + if (isset($array[$part]) && is_array($array[$part])) { |
|
| 167 | + $array = &$array[$key]; |
|
| 168 | + } else { |
|
| 169 | + continue 2; |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + unset($array[array_shift($parts)]); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Flatten a multi-dimensional array into a single level. |
|
| 179 | + * |
|
| 180 | + * @param array $array |
|
| 181 | + * |
|
| 182 | + * @return array |
|
| 183 | + */ |
|
| 184 | + public static function flatten(array $array): array |
|
| 185 | + { |
|
| 186 | + $result = []; |
|
| 187 | + |
|
| 188 | + array_walk_recursive($array, function ($value) use (&$result) { |
|
| 189 | + $result[] = $value; |
|
| 190 | + }); |
|
| 191 | + |
|
| 192 | + return $result; |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Fetch a flattened array of a nested array element. |
|
| 197 | - * |
|
| 198 | - * @param array $array |
|
| 199 | - * @param string $key |
|
| 200 | - * |
|
| 201 | - * @return array |
|
| 202 | - */ |
|
| 203 | - public static function fetch(array $array, string $key): array |
|
| 204 | - { |
|
| 205 | - $segments = explode('.', $key); |
|
| 195 | + /** |
|
| 196 | + * Fetch a flattened array of a nested array element. |
|
| 197 | + * |
|
| 198 | + * @param array $array |
|
| 199 | + * @param string $key |
|
| 200 | + * |
|
| 201 | + * @return array |
|
| 202 | + */ |
|
| 203 | + public static function fetch(array $array, string $key): array |
|
| 204 | + { |
|
| 205 | + $segments = explode('.', $key); |
|
| 206 | 206 | |
| 207 | - foreach ($segments as $segment) { |
|
| 208 | - $results = array(); |
|
| 207 | + foreach ($segments as $segment) { |
|
| 208 | + $results = array(); |
|
| 209 | 209 | |
| 210 | - foreach ($array as $value) { |
|
| 211 | - if (array_key_exists($segment, $value = (array) $value)) { |
|
| 212 | - $results[] = $value[$segment]; |
|
| 213 | - } |
|
| 214 | - } |
|
| 210 | + foreach ($array as $value) { |
|
| 211 | + if (array_key_exists($segment, $value = (array) $value)) { |
|
| 212 | + $results[] = $value[$segment]; |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | 215 | |
| 216 | - $array = array_values($results); |
|
| 217 | - } |
|
| 216 | + $array = array_values($results); |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | - return array_values($results); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Return the first element in an array passing a given truth test. |
|
| 224 | - * |
|
| 225 | - * @param array $array |
|
| 226 | - * @param \callable|null $callback |
|
| 227 | - * @param mixed $default |
|
| 228 | - * |
|
| 229 | - * @return mixed |
|
| 230 | - */ |
|
| 231 | - public static function first(array $array, callable $callback = null, mixed $default = null) |
|
| 232 | - { |
|
| 233 | - if (is_null($callback)) { |
|
| 234 | - if (empty($array)) { |
|
| 235 | - return value($default); |
|
| 236 | - } |
|
| 219 | + return array_values($results); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Return the first element in an array passing a given truth test. |
|
| 224 | + * |
|
| 225 | + * @param array $array |
|
| 226 | + * @param \callable|null $callback |
|
| 227 | + * @param mixed $default |
|
| 228 | + * |
|
| 229 | + * @return mixed |
|
| 230 | + */ |
|
| 231 | + public static function first(array $array, callable $callback = null, mixed $default = null) |
|
| 232 | + { |
|
| 233 | + if (is_null($callback)) { |
|
| 234 | + if (empty($array)) { |
|
| 235 | + return value($default); |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - foreach ($array as $item) { |
|
| 239 | - return $item; |
|
| 240 | - } |
|
| 241 | - } |
|
| 238 | + foreach ($array as $item) { |
|
| 239 | + return $item; |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - foreach ($array as $key => $value) { |
|
| 244 | - if ($callback($value, $key)) return $value; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - return value($default); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Get an item from an array using "dot" notation. |
|
| 252 | - * |
|
| 253 | - * @param \ArrayAccess|array $array The search array |
|
| 254 | - * @param string|array|null $key The dot-notated key or array of keys or null |
|
| 255 | - * @param mixed $default The default value |
|
| 256 | - * |
|
| 257 | - * @return mixed |
|
| 258 | - */ |
|
| 259 | - public static function get($array, string|array $key = null, mixed $default = null) |
|
| 260 | - { |
|
| 261 | - if ( ! static::accessible($array)) { |
|
| 262 | - return value($default); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - if (is_null($key)) { |
|
| 266 | - return $array; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - if (static::exists($array, $key)) { |
|
| 270 | - return $array[$key]; |
|
| 271 | - } |
|
| 243 | + foreach ($array as $key => $value) { |
|
| 244 | + if ($callback($value, $key)) return $value; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + return value($default); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Get an item from an array using "dot" notation. |
|
| 252 | + * |
|
| 253 | + * @param \ArrayAccess|array $array The search array |
|
| 254 | + * @param string|array|null $key The dot-notated key or array of keys or null |
|
| 255 | + * @param mixed $default The default value |
|
| 256 | + * |
|
| 257 | + * @return mixed |
|
| 258 | + */ |
|
| 259 | + public static function get($array, string|array $key = null, mixed $default = null) |
|
| 260 | + { |
|
| 261 | + if ( ! static::accessible($array)) { |
|
| 262 | + return value($default); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + if (is_null($key)) { |
|
| 266 | + return $array; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + if (static::exists($array, $key)) { |
|
| 270 | + return $array[$key]; |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | - if (strpos($key, '.') === false) { |
|
| 274 | - return $array[$key] ?? value($default); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - $segments = explode('.', $key); |
|
| 278 | - |
|
| 279 | - foreach ($segments as $segment) { |
|
| 280 | - if (static::accessible($array) && static::exists($array, $segment)) { |
|
| 281 | - $array = $array[$segment]; |
|
| 282 | - } else { |
|
| 283 | - return value($default); |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - return $array; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Return the last element in an array passing a given truth test. |
|
| 292 | - * |
|
| 293 | - * @param array $array |
|
| 294 | - * @param \callable|null $callback |
|
| 295 | - * @param mixed $default |
|
| 296 | - * |
|
| 297 | - * @return mixed |
|
| 298 | - */ |
|
| 299 | - public static function last(array $array, callable $callback = null, mixed $default = null) |
|
| 300 | - { |
|
| 301 | - if (is_null($callback)) { |
|
| 302 | - return empty($array) ? value($default) : lastItem($array); |
|
| 303 | - } |
|
| 273 | + if (strpos($key, '.') === false) { |
|
| 274 | + return $array[$key] ?? value($default); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + $segments = explode('.', $key); |
|
| 278 | + |
|
| 279 | + foreach ($segments as $segment) { |
|
| 280 | + if (static::accessible($array) && static::exists($array, $segment)) { |
|
| 281 | + $array = $array[$segment]; |
|
| 282 | + } else { |
|
| 283 | + return value($default); |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + return $array; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Return the last element in an array passing a given truth test. |
|
| 292 | + * |
|
| 293 | + * @param array $array |
|
| 294 | + * @param \callable|null $callback |
|
| 295 | + * @param mixed $default |
|
| 296 | + * |
|
| 297 | + * @return mixed |
|
| 298 | + */ |
|
| 299 | + public static function last(array $array, callable $callback = null, mixed $default = null) |
|
| 300 | + { |
|
| 301 | + if (is_null($callback)) { |
|
| 302 | + return empty($array) ? value($default) : lastItem($array); |
|
| 303 | + } |
|
| 304 | 304 | |
| 305 | - return static::first(array_reverse($array), $callback, $default); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Check if an item exists in an array using "dot" notation. |
|
| 310 | - * |
|
| 311 | - * @param array $array |
|
| 312 | - * @param string $key |
|
| 313 | - * |
|
| 314 | - * @return bool |
|
| 315 | - */ |
|
| 316 | - public static function has(array $array, string $key): bool |
|
| 317 | - { |
|
| 318 | - if (empty($array) || is_null($key)) return false; |
|
| 305 | + return static::first(array_reverse($array), $callback, $default); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Check if an item exists in an array using "dot" notation. |
|
| 310 | + * |
|
| 311 | + * @param array $array |
|
| 312 | + * @param string $key |
|
| 313 | + * |
|
| 314 | + * @return bool |
|
| 315 | + */ |
|
| 316 | + public static function has(array $array, string $key): bool |
|
| 317 | + { |
|
| 318 | + if (empty($array) || is_null($key)) return false; |
|
| 319 | 319 | |
| 320 | - if (static::exists($array, $key)) return true; |
|
| 320 | + if (static::exists($array, $key)) return true; |
|
| 321 | 321 | |
| 322 | - $segments = explode('.', $key); |
|
| 322 | + $segments = explode('.', $key); |
|
| 323 | 323 | |
| 324 | - foreach ($segments as $segment) { |
|
| 325 | - if ( ! is_array($array) || ! static::exists($array, $segment)) { |
|
| 326 | - return false; |
|
| 327 | - } |
|
| 324 | + foreach ($segments as $segment) { |
|
| 325 | + if ( ! is_array($array) || ! static::exists($array, $segment)) { |
|
| 326 | + return false; |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | - $array = $array[$segment]; |
|
| 330 | - } |
|
| 329 | + $array = $array[$segment]; |
|
| 330 | + } |
|
| 331 | 331 | |
| 332 | - return true; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Gets max width of an array. |
|
| 337 | - * |
|
| 338 | - * @param array $data |
|
| 339 | - * @param bool $exclude |
|
| 340 | - * |
|
| 341 | - * @return int |
|
| 342 | - */ |
|
| 343 | - public static function getMaxWidth(array $data, bool $exclude = true): int |
|
| 344 | - { |
|
| 345 | - $maxWidth = 0; |
|
| 332 | + return true; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Gets max width of an array. |
|
| 337 | + * |
|
| 338 | + * @param array $data |
|
| 339 | + * @param bool $exclude |
|
| 340 | + * |
|
| 341 | + * @return int |
|
| 342 | + */ |
|
| 343 | + public static function getMaxWidth(array $data, bool $exclude = true): int |
|
| 344 | + { |
|
| 345 | + $maxWidth = 0; |
|
| 346 | 346 | |
| 347 | - foreach ($data as $key => $value) { |
|
| 348 | - // key is not a integer |
|
| 349 | - if ( ! $exclude || ! is_numeric($key)) { |
|
| 350 | - $width = mb_strlen((string) $key, 'UTF-8'); |
|
| 351 | - $maxWidth = $width > $maxWidth ? $width : $maxWidth; |
|
| 352 | - } |
|
| 353 | - } |
|
| 347 | + foreach ($data as $key => $value) { |
|
| 348 | + // key is not a integer |
|
| 349 | + if ( ! $exclude || ! is_numeric($key)) { |
|
| 350 | + $width = mb_strlen((string) $key, 'UTF-8'); |
|
| 351 | + $maxWidth = $width > $maxWidth ? $width : $maxWidth; |
|
| 352 | + } |
|
| 353 | + } |
|
| 354 | 354 | |
| 355 | - return $maxWidth; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Get a subset of the items from the given array. |
|
| 360 | - * |
|
| 361 | - * @param array $array |
|
| 362 | - * @param array|string $keys |
|
| 363 | - * |
|
| 364 | - * @return array |
|
| 365 | - */ |
|
| 366 | - public static function only(array $array, array|string $keys): array |
|
| 367 | - { |
|
| 368 | - return array_intersect_key($array, array_flip($array), $keys); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Sets a value in an array using "dot" notation. |
|
| 373 | - * |
|
| 374 | - * @param array $array The search array |
|
| 375 | - * @param string $key The dot-notated key or array of keys |
|
| 376 | - * @param mixed $value The default value |
|
| 377 | - * |
|
| 378 | - * @return mixed |
|
| 379 | - */ |
|
| 380 | - public static function set(array &$array, string $key, mixed $value = null): mixed |
|
| 381 | - { |
|
| 382 | - if (is_null($key)) { |
|
| 383 | - return $array = $value; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - $keys = explode('.', $key); |
|
| 387 | - |
|
| 388 | - while (count($keys) > 1) { |
|
| 389 | - $key = array_shift($keys); |
|
| 390 | - |
|
| 391 | - if ( ! static::exists($array, $key)) { |
|
| 392 | - $array[$key] = []; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - $array = &$array[$key]; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - $array[array_shift($keys)] = $value; |
|
| 399 | - |
|
| 400 | - return $array; |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Push an item onto the beginning of an array. |
|
| 405 | - * |
|
| 406 | - * @param mixed $array |
|
| 407 | - * @param mixed $value |
|
| 408 | - * @param mixed key |
|
| 409 | - * |
|
| 410 | - * @return array |
|
| 411 | - */ |
|
| 412 | - public static function prepend(mixed $array, mixed $value, mixed $key = null): array |
|
| 413 | - { |
|
| 414 | - if (func_num_args() == 2) { |
|
| 415 | - array_unshift($array, $value); |
|
| 416 | - } else { |
|
| 417 | - $array = [$key => $value] + $array; |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - return $array; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Get a value from the array, and remove it. |
|
| 425 | - * |
|
| 426 | - * @param array $array |
|
| 427 | - * @param string $key |
|
| 428 | - * @param mixed $default |
|
| 429 | - * |
|
| 430 | - * @return mixed |
|
| 431 | - */ |
|
| 432 | - public static function pull(array &$array, string $key, mixed $default = null): mixed |
|
| 433 | - { |
|
| 434 | - $value = static::get($array, $key, $default); |
|
| 435 | - |
|
| 436 | - static::erase($array, $key); |
|
| 437 | - |
|
| 438 | - return $value; |
|
| 439 | - } |
|
| 355 | + return $maxWidth; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Get a subset of the items from the given array. |
|
| 360 | + * |
|
| 361 | + * @param array $array |
|
| 362 | + * @param array|string $keys |
|
| 363 | + * |
|
| 364 | + * @return array |
|
| 365 | + */ |
|
| 366 | + public static function only(array $array, array|string $keys): array |
|
| 367 | + { |
|
| 368 | + return array_intersect_key($array, array_flip($array), $keys); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Sets a value in an array using "dot" notation. |
|
| 373 | + * |
|
| 374 | + * @param array $array The search array |
|
| 375 | + * @param string $key The dot-notated key or array of keys |
|
| 376 | + * @param mixed $value The default value |
|
| 377 | + * |
|
| 378 | + * @return mixed |
|
| 379 | + */ |
|
| 380 | + public static function set(array &$array, string $key, mixed $value = null): mixed |
|
| 381 | + { |
|
| 382 | + if (is_null($key)) { |
|
| 383 | + return $array = $value; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + $keys = explode('.', $key); |
|
| 387 | + |
|
| 388 | + while (count($keys) > 1) { |
|
| 389 | + $key = array_shift($keys); |
|
| 390 | + |
|
| 391 | + if ( ! static::exists($array, $key)) { |
|
| 392 | + $array[$key] = []; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + $array = &$array[$key]; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + $array[array_shift($keys)] = $value; |
|
| 399 | + |
|
| 400 | + return $array; |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Push an item onto the beginning of an array. |
|
| 405 | + * |
|
| 406 | + * @param mixed $array |
|
| 407 | + * @param mixed $value |
|
| 408 | + * @param mixed key |
|
| 409 | + * |
|
| 410 | + * @return array |
|
| 411 | + */ |
|
| 412 | + public static function prepend(mixed $array, mixed $value, mixed $key = null): array |
|
| 413 | + { |
|
| 414 | + if (func_num_args() == 2) { |
|
| 415 | + array_unshift($array, $value); |
|
| 416 | + } else { |
|
| 417 | + $array = [$key => $value] + $array; |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + return $array; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Get a value from the array, and remove it. |
|
| 425 | + * |
|
| 426 | + * @param array $array |
|
| 427 | + * @param string $key |
|
| 428 | + * @param mixed $default |
|
| 429 | + * |
|
| 430 | + * @return mixed |
|
| 431 | + */ |
|
| 432 | + public static function pull(array &$array, string $key, mixed $default = null): mixed |
|
| 433 | + { |
|
| 434 | + $value = static::get($array, $key, $default); |
|
| 435 | + |
|
| 436 | + static::erase($array, $key); |
|
| 437 | + |
|
| 438 | + return $value; |
|
| 439 | + } |
|
| 440 | 440 | |
| 441 | - /** |
|
| 442 | - * Pluck an array of values from an array. |
|
| 443 | - * |
|
| 444 | - * @param \iterable $array |
|
| 445 | - * @param string|array|int|null $value |
|
| 446 | - * @param string|array|null $key |
|
| 447 | - * |
|
| 448 | - * @return array |
|
| 449 | - */ |
|
| 450 | - public static function pluck($array, $value, $key = null): array |
|
| 451 | - { |
|
| 452 | - $results = []; |
|
| 453 | - |
|
| 454 | - foreach ($array as $item) { |
|
| 455 | - $itemValue = is_object($item) ? $item->{$value} : $item[$value]; |
|
| 441 | + /** |
|
| 442 | + * Pluck an array of values from an array. |
|
| 443 | + * |
|
| 444 | + * @param \iterable $array |
|
| 445 | + * @param string|array|int|null $value |
|
| 446 | + * @param string|array|null $key |
|
| 447 | + * |
|
| 448 | + * @return array |
|
| 449 | + */ |
|
| 450 | + public static function pluck($array, $value, $key = null): array |
|
| 451 | + { |
|
| 452 | + $results = []; |
|
| 453 | + |
|
| 454 | + foreach ($array as $item) { |
|
| 455 | + $itemValue = is_object($item) ? $item->{$value} : $item[$value]; |
|
| 456 | 456 | |
| 457 | - // If the key is "null", we will just append the value to the array and keep |
|
| 458 | - // looping. Otherwise we will key the array using the value of the key we |
|
| 459 | - // received from the developer. Then we'll return the final array form. |
|
| 460 | - if (is_null($key)) { |
|
| 461 | - $results[] = $itemValue; |
|
| 462 | - } else { |
|
| 463 | - $itemKey = is_object($item) ? $item->{$key} : $item[$key]; |
|
| 457 | + // If the key is "null", we will just append the value to the array and keep |
|
| 458 | + // looping. Otherwise we will key the array using the value of the key we |
|
| 459 | + // received from the developer. Then we'll return the final array form. |
|
| 460 | + if (is_null($key)) { |
|
| 461 | + $results[] = $itemValue; |
|
| 462 | + } else { |
|
| 463 | + $itemKey = is_object($item) ? $item->{$key} : $item[$key]; |
|
| 464 | 464 | |
| 465 | - $results[$itemKey] = $itemValue; |
|
| 466 | - } |
|
| 467 | - } |
|
| 465 | + $results[$itemKey] = $itemValue; |
|
| 466 | + } |
|
| 467 | + } |
|
| 468 | 468 | |
| 469 | - return $results; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Convert the array into a query string. |
|
| 474 | - * |
|
| 475 | - * @param array $array |
|
| 476 | - * |
|
| 477 | - * @return bool |
|
| 478 | - */ |
|
| 479 | - public static function query(array $array): bool |
|
| 480 | - { |
|
| 481 | - return http_build_query($array, '', '&', PHP_QUERY_RFC3986); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - /** |
|
| 485 | - * Filter the array using the given callback. |
|
| 486 | - * |
|
| 487 | - * @param array $array |
|
| 488 | - * @param \Callable $callback |
|
| 489 | - * |
|
| 490 | - * @return array |
|
| 491 | - */ |
|
| 492 | - public static function where(array $array, Callable $callback): array |
|
| 493 | - { |
|
| 494 | - return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH); |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * If the given value is not an array and not null, wrap it in one. |
|
| 499 | - * |
|
| 500 | - * @param mixed $value |
|
| 501 | - * |
|
| 502 | - * @return array |
|
| 503 | - */ |
|
| 504 | - public static function wrap(mixed $value): array |
|
| 505 | - { |
|
| 506 | - if (is_null($value)) { |
|
| 507 | - return []; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - return is_array($value) ? $value : [$value]; |
|
| 511 | - } |
|
| 469 | + return $results; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Convert the array into a query string. |
|
| 474 | + * |
|
| 475 | + * @param array $array |
|
| 476 | + * |
|
| 477 | + * @return bool |
|
| 478 | + */ |
|
| 479 | + public static function query(array $array): bool |
|
| 480 | + { |
|
| 481 | + return http_build_query($array, '', '&', PHP_QUERY_RFC3986); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + /** |
|
| 485 | + * Filter the array using the given callback. |
|
| 486 | + * |
|
| 487 | + * @param array $array |
|
| 488 | + * @param \Callable $callback |
|
| 489 | + * |
|
| 490 | + * @return array |
|
| 491 | + */ |
|
| 492 | + public static function where(array $array, Callable $callback): array |
|
| 493 | + { |
|
| 494 | + return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH); |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * If the given value is not an array and not null, wrap it in one. |
|
| 499 | + * |
|
| 500 | + * @param mixed $value |
|
| 501 | + * |
|
| 502 | + * @return array |
|
| 503 | + */ |
|
| 504 | + public static function wrap(mixed $value): array |
|
| 505 | + { |
|
| 506 | + if (is_null($value)) { |
|
| 507 | + return []; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + return is_array($value) ? $value : [$value]; |
|
| 511 | + } |
|
| 512 | 512 | } |
| 513 | 513 | \ No newline at end of file |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | * |
| 106 | 106 | * @return array |
| 107 | 107 | */ |
| 108 | - public static function except(array $array, string|array $keys): array |
|
| 108 | + public static function except(array $array, string | array $keys): array |
|
| 109 | 109 | { |
| 110 | 110 | static::erase($array, $keys); |
| 111 | 111 | |
@@ -185,7 +185,7 @@ discard block |
||
| 185 | 185 | { |
| 186 | 186 | $result = []; |
| 187 | 187 | |
| 188 | - array_walk_recursive($array, function ($value) use (&$result) { |
|
| 188 | + array_walk_recursive($array, function($value) use (&$result) { |
|
| 189 | 189 | $result[] = $value; |
| 190 | 190 | }); |
| 191 | 191 | |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | * |
| 257 | 257 | * @return mixed |
| 258 | 258 | */ |
| 259 | - public static function get($array, string|array $key = null, mixed $default = null) |
|
| 259 | + public static function get($array, string | array $key = null, mixed $default = null) |
|
| 260 | 260 | { |
| 261 | 261 | if ( ! static::accessible($array)) { |
| 262 | 262 | return value($default); |
@@ -363,7 +363,7 @@ discard block |
||
| 363 | 363 | * |
| 364 | 364 | * @return array |
| 365 | 365 | */ |
| 366 | - public static function only(array $array, array|string $keys): array |
|
| 366 | + public static function only(array $array, array | string $keys): array |
|
| 367 | 367 | { |
| 368 | 368 | return array_intersect_key($array, array_flip($array), $keys); |
| 369 | 369 | } |
@@ -241,7 +241,9 @@ discard block |
||
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | foreach ($array as $key => $value) { |
| 244 | - if ($callback($value, $key)) return $value; |
|
| 244 | + if ($callback($value, $key)) { |
|
| 245 | + return $value; |
|
| 246 | + } |
|
| 245 | 247 | } |
| 246 | 248 | |
| 247 | 249 | return value($default); |
@@ -315,9 +317,13 @@ discard block |
||
| 315 | 317 | */ |
| 316 | 318 | public static function has(array $array, string $key): bool |
| 317 | 319 | { |
| 318 | - if (empty($array) || is_null($key)) return false; |
|
| 320 | + if (empty($array) || is_null($key)) { |
|
| 321 | + return false; |
|
| 322 | + } |
|
| 319 | 323 | |
| 320 | - if (static::exists($array, $key)) return true; |
|
| 324 | + if (static::exists($array, $key)) { |
|
| 325 | + return true; |
|
| 326 | + } |
|
| 321 | 327 | |
| 322 | 328 | $segments = explode('.', $key); |
| 323 | 329 | |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * |
| 49 | 49 | * @return mixed |
| 50 | 50 | */ |
| 51 | - function data_get($target, string|array $key, mixed $default = null) |
|
| 51 | + function data_get($target, string | array $key, mixed $default = null) |
|
| 52 | 52 | { |
| 53 | 53 | if (is_null($key)) return $target; |
| 54 | 54 | |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | if ($segment === '*') { |
| 59 | 59 | if ($target instanceof Collection) { |
| 60 | 60 | $target = $target->all(); |
| 61 | - } elseif (! is_array($target)) { |
|
| 61 | + } elseif ( ! is_array($target)) { |
|
| 62 | 62 | return value($default); |
| 63 | 63 | } |
| 64 | 64 | |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | } |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | -if( ! function_exists('data_set')) { |
|
| 83 | +if ( ! function_exists('data_set')) { |
|
| 84 | 84 | /** |
| 85 | 85 | * Set an item on an array or object using dot notation. |
| 86 | 86 | * |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * |
| 92 | 92 | * @return mixed |
| 93 | 93 | */ |
| 94 | - function data_set(&$target, string|array $key, mixed $value, bool $overwrite = true) |
|
| 94 | + function data_set(&$target, string | array $key, mixed $value, bool $overwrite = true) |
|
| 95 | 95 | { |
| 96 | 96 | $segments = is_array($key) ? $key : explode('.', $key); |
| 97 | 97 | |
@@ -50,7 +50,9 @@ |
||
| 50 | 50 | */ |
| 51 | 51 | function data_get($target, string|array $key, mixed $default = null) |
| 52 | 52 | { |
| 53 | - if (is_null($key)) return $target; |
|
| 53 | + if (is_null($key)) { |
|
| 54 | + return $target; |
|
| 55 | + } |
|
| 54 | 56 | |
| 55 | 57 | $key = is_array($key) ? $key : explode('.', $key); |
| 56 | 58 | |