Download the PHP package overthink/array-item without Composer
On this page you can find all versions of the php package overthink/array-item. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download overthink/array-item
More information about overthink/array-item
Files in overthink/array-item
Package array-item
Short Description A fluent, typed accessor for reading and manipulating array data in Laravel apps.
License MIT
Homepage https://github.com/MarkoDevelop/array-item
Informations about the package array-item
Array Item
A fluent, typed accessor for reading and manipulating array data in Laravel apps. Wrap any array (API response, JSON column, config, ...) in an ArrayItem to get dot-notation access, typed getters (string, float, date, number, collection, JSON), and a small set of array manipulation helpers, without giving up ArrayAccess.
Requirements
- PHP
^8.3 - Laravel
^11.0 || ^12.0 || ^13.0
Need Laravel 10 / PHP 8.2? Use ^1.0 instead — it won't get new features, but it's still there.
Installation
You can install the package via composer:
You can publish the config file with:
Usage
Available methods
| Method | Description |
|---|---|
make(array\|ArrayItem $attributes = []) |
Create a new instance (static). |
default(array $attributes): array |
Overridable hook to seed default attributes on construction. |
get(string\|callable $key, mixed $default = null) |
Dot-notation get, or resolve a callable against the item. |
getOr(string\|callable $key, mixed $default = null) |
Like get(), but falls back to $default when the value is empty(). |
has(string $key): bool |
Dot-notation existence check. |
set(string\|callable $key, mixed $value = null, bool $merge = false) |
Dot-notation set; a callable replaces (or merges into) all attributes. |
merge(string\|callable $key, mixed $value = null) |
Shorthand for set(..., merge: true). |
only(string\|array\|Collection $keys) |
Keep only the given keys (supports ['from' => 'to'] remapping). |
remove(string\|array\|Collection $keys) |
Remove the given keys. |
string(string\|callable $key, string $default = '') |
Get the value as an Illuminate\Support\Stringable. |
float(string\|callable $key, ?string $default = null): float |
Parse the value as a float, handling "1.234,56"-style European numbers. |
number(string\|callable $key, ?string $default = null): Number |
Get the value wrapped in a Number helper (see below). |
numberFormat(string\|callable $key, ?int $decimals, ?string $decimalSeparator, ?string $thousandsSeparator, ?string $default): string |
number_format() over float(), using the static defaults below when omitted. |
collect(string\|callable $key, mixed $default = []): Collection |
Get the value as a Collection. |
json(string\|callable $key, mixed $default = null): mixed |
json_decode() the value. |
jsonItem(string\|callable $key, mixed $default = null): ArrayItem |
Decode the value and wrap it in a new ArrayItem. |
date(string\|callable $key, ?string $default = null): ?Carbon |
Parse the value as a Carbon date. |
dateFrom(string\|callable $key, string $from, ?string $default = null): ?Carbon |
Parse the value with an explicit input format. |
dateFormat(string\|callable $key, ?string $format = null, ?string $default = null): ?string |
Format a date value, defaulting to static::$dateFormat. |
timestamp(string\|callable $key, ?string $default = null): ?Carbon |
Parse the value as a unix timestamp. |
timestampFormat(string\|callable $key, ?string $format = null, ?string $default = null): ?string |
Format a timestamp value, defaulting to static::$dateFormat. |
convert(string\|callable $key, Convertable $converter, mixed $default = null) |
Pass the value through a custom Convertable implementation. |
getAttributes(): array / toArray(): array |
Get the raw underlying array. |
toCollection(): Collection |
Get the underlying array as a Collection. |
toJson($options = 0): string / __toString() |
JSON-encode the item. |
ArrayItem also implements ArrayAccess ($item['key']), Illuminate\Contracts\Support\Arrayable, Jsonable, JsonSerializable, and uses Laravel's Conditionable (when()/unless()) and Macroable traits.
Conditionable
Conditionally apply logic while staying in the fluent chain:
Macroable
Register your own methods on ArrayItem at boot time:
Static configuration, shared across all instances:
Number
ArrayItem::number() returns an Overthink\ArrayItem\Number instance, a thin wrapper around Illuminate\Support\Number:
convert() and Convertable
Custom conversion logic can be plugged in via the Convertable interface:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Marko Zagar
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of array-item with dependencies
spatie/laravel-package-tools Version ^1.14.0
illuminate/contracts Version ^10.48
illuminate/support Version ^10.48