Download the PHP package k2gl/array-reader without Composer

On this page you can find all versions of the php package k2gl/array-reader. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package array-reader

k2gl/array-reader

CI Latest Stable Version Total Downloads PHPStan Level License

Read typed values out of an untyped array — query strings, form input, CSV rows, decoded JSON, config, environment — without the isset(...) && is_string(...) ? ... : null dance, and without dropping to mixed in the eyes of PHPStan / Psalm.

Without it you write the same guard for every field and still end up with mixed:

Why install it: one tiny, zero-dependency class family that turns messy input arrays into typed values, lets you choose how forgiving the conversion is, fails loudly on missing data, and keeps static analysis green.

Install

Requires PHP 8.1+. No runtime dependencies.

Pick a reader

There are three readers. They expose the same methods and differ only in how they handle a value whose type doesn't match what you asked for. Here is one input read by each:

ArrayReader — safe casting (use this by default)

Converts the obvious string/number/bool representations and rejects anything ambiguous or lossy. Ideal for data that arrives as strings: $_GET, $_POST, CSV rows, environment variables.

StrictArrayReader — exact type only

Accepts a value only if it is already the requested type (the lone convenience: an int may be read as float). Ideal for data you already trust to be well-typed, e.g. a decoded JSON document.

LooseArrayReader — PHP's native cast

Casts any scalar with PHP's own rules and never rejects a scalar — so malformed input passes through silently. Reach for it only when you explicitly want that behaviour.

Reading values

For every scalar type each reader offers two accessors:

Arrays, lists and nesting

array(), list() and nested() never cast — in every reader they only validate shape:

Read a list of nested objects ({"items": [{...}, {...}]}) as a list of readers with nestedList() / nestedListOr():

Typed scalar lists

ints(), strings(), floats() and bools() read a list and produce every element through the same cast pipeline as the scalar accessors, so the reader's mode applies element by element. They turn ids[]=1&ids[]=2, CSV columns or JSON arrays into a list<int> / list<string> / … without a manual array_map().

For lists of enums or dates see the sections below; for any other element type listOf() maps each element of a list through a caster of your own — the "array of objects to DTOs" payload:

Enums

enum() / enumOr() read a backed enum: the enum's backing scalar is produced through the same cast pipeline (so the reader's cast mode applies), then resolved with BackedEnum::tryFrom(). A value that is absent, cannot be produced as the backing type, or is not one of the enum's cases throws (strict) or returns the default (enumOr):

The cast mode applies to the backing scalar: with ArrayReader (safe) a numeric string "2" resolves an int-backed enum, StrictArrayReader requires the exact backing type, and LooseArrayReader coerces any scalar. Only backed enums are supported.

Dates

dateTime() / dateTimeOr() read a date/time string into a DateTimeImmutable. The value is read as a string through the cast pipeline, then parsed. Without a format any DateTimeImmutable-parsable string is accepted (ISO-8601, relative, @timestamp); pass a format and the input must match it exactly — surplus characters or parse warnings are rejected.

Nested keys (dot notation)

Every key-based accessor (has(), the scalar getters, enum(), dateTime(), nested(), the list accessors, …) accepts a dot path into nested arrays. A literal key always wins — a key that contains a dot keeps resolving to itself — and the path is only walked when no literal key matches, so this is fully backward compatible.

Lazy defaults and required keys

stringOrElse() / intOrElse() / floatOrElse() / boolOrElse() work like the *Or accessors, but the default comes from a callback that runs only when the value cannot be produced — handy when computing the default is expensive.

require() asserts that a set of keys (dot paths allowed) is present, failing once with all the missing keys instead of one at a time, and returns the reader for chaining.

Helpers and JSON

Error handling

Every exception implements K2gl\ArrayReader\Exception\ArrayReaderException, so you can catch the whole family at once:

Safe casting reference (ArrayReader)

What ArrayReader accepts beyond the exact type — anything else throws (strict) or returns the default (*Or):

Accessor Also accepts
string int/float → string, bool"1"/"0", Stringable
int integer numeric string ("5", "-3"), bool1/0 — rejects floats, "5.5", "abc"
float int, numeric string ("1.5", "2")
bool "1"/"true"/"on"/"yes" → true, "0"/"false"/"off"/"no"/"" → false, int 0/1

Zero dependencies

array-reader has no runtime dependencies — its only require is php itself. Installing it pulls nothing else into your dependency tree: no transitive packages to audit, no version conflicts to resolve, and it is safe to drop into any application or library, including ones that must stay dependency-light.

License

MIT © Nick Harin. See LICENSE.


All versions of array-reader with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package k2gl/array-reader contains the following files

Loading the files please wait ...