PHPackages                             zakirullin/typed-accessor - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. zakirullin/typed-accessor

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

zakirullin/typed-accessor
=========================

Convenient array-related routine &amp; better type casting

0.8.6(6mo ago)21365[2 issues](https://github.com/zakirullin/mess/issues)MITPHPPHP ^7.2|^8.0CI failing

Since Jul 16Pushed 6mo ago6 watchersCompare

[ Source](https://github.com/zakirullin/mess)[ Packagist](https://packagist.org/packages/zakirullin/typed-accessor)[ Docs](https://github.com/zakirullin/mess)[ RSS](/packages/zakirullin-typed-accessor/feed)WikiDiscussions master Synced yesterday

READMEChangelog (8)Dependencies (6)Versions (24)Used By (0)

Mess
----

[](#mess)

[![GitHub Build Status](https://github.com/zakirullin/mess/actions/workflows/php.yml/badge.svg)](https://github.com/zakirullin/mess/actions/workflows/php.yml/badge.svg)[![Psalm coverage](https://camo.githubusercontent.com/c6ea5bb8d8977fd152eacc0a2e4906ff35718101fdcc3183a7cec579e3981f4d/68747470733a2f2f73686570686572642e6465762f6769746875622f7a616b6972756c6c696e2f6d6573732f636f7665726167652e7376673f)](https://camo.githubusercontent.com/c6ea5bb8d8977fd152eacc0a2e4906ff35718101fdcc3183a7cec579e3981f4d/68747470733a2f2f73686570686572642e6465762f6769746875622f7a616b6972756c6c696e2f6d6573732f636f7665726167652e7376673f)[![PHP from Packagist](https://camo.githubusercontent.com/769ae8d1233867b3d15df91143d7e93d7a82c278807dcc2f690686d9625b6740/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7a616b6972756c6c696e2f6d6573732e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/769ae8d1233867b3d15df91143d7e93d7a82c278807dcc2f690686d9625b6740/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7a616b6972756c6c696e2f6d6573732e7376673f7374796c653d666c61742d737175617265)[![Latest Stable Version](https://camo.githubusercontent.com/3da1b06909ec4f7d50f4ba7803e75df50a0742dc4b7089040c176feb213e4c28/68747470733a2f2f706f7365722e707567782e6f72672f7a616b6972756c6c696e2f6d6573732f762f737461626c652e737667)](https://packagist.org/packages/zakirullin/mess)[![GitHub commits](https://camo.githubusercontent.com/4f6506db1df2e0eb4c0076f0cc50c986efd356045b20b1ae125f205a1a56a3eb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6d6d6974732d73696e63652f7a616b6972756c6c696e2f6d6573732f302e312e302e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/4f6506db1df2e0eb4c0076f0cc50c986efd356045b20b1ae125f205a1a56a3eb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6d6d6974732d73696e63652f7a616b6972756c6c696e2f6d6573732f302e312e302e7376673f7374796c653d666c61742d737175617265)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

We face a few problems in our PHP projects
------------------------------------------

[](#we-face-a-few-problems-in-our-php-projects)

- Illogical type casting (`PHP`'s native implementation is way too "smart")
- Pointless casts like `array => float` are **allowed**
- Boilerplate code to work with arrays (check if `isset()`, throw an exception, cast the type, etc.)

Consider an example:

```
$userId = $queryParams['userId'] ?? null;
if ($userId === null) {
    throw ...
}
$userId = (int)$userId;
```

Way too verbose. Any ideas?
---------------------------

[](#way-too-verbose-any-ideas)

```
$userId = (new Mess($queryParams))['userId']->getAsInt();
```

You can mess with API responses/configs/whatever:

```
$mess = new Mess($response);
$book = new Book(
    $mess['title']->getString(),
    $mess['isBestseller']->getBool(),
    $mess['stats']['rating']->getInt(),
    $mess['tags']->getListOfString()
);
```

Generics support (Psalm compatible)
-----------------------------------

[](#generics-support-psalm-compatible)

- `getListOfString()`
- `getListOfInt()`
- `getArrayOfStringToString()`
- `getArrayOfStringToBool()`
- etc.

Installation
------------

[](#installation)

```
$ composer require zakirullin/mess
```

Dealing with mess
-----------------

[](#dealing-with-mess)

```
$queryParams = new Mess(['isDeleted' => 'true']);
$queryParams['isDeleted']->getBool(); // UnexpectedTypeException
$queryParams['isDeleted']->getAsBool(); // true

$value = new Mess('25');
$value->getInt(); // UnexpectedTypeException
$value->getAsInt(); // 25
$value->getString(); // '25'

$value = new Mess('25a');
$value->getInt(); // UnexpectedTypeException
$value->getAsInt(); // UncastableValueException

$config = new Mess(['param' => '1']);
$config['a']['b']->getInt(); // MissingKeyException: "MissingKeyException: a.b"
$config['a']['b']->findInt(); // null
$config['param']->getInt(); // UnexpectedTypeException
$config['param']->getAsInt(); // 1
$config['param']->findInt(); // UnexpectedTypeException
$config['param']->findAsInt(); // 1
```

As you might notice, type casting is performed while using `(find|get)As*` methods. Having trouble grasping `get*()`/`find*()`? Check out brilliant [Ocramius's slides](https://ocramius.github.io/doctrine-best-practices/#/94).

Type casting with Mess is rather predictable
--------------------------------------------

[](#type-casting-with-mess-is-rather-predictable)

```
'\d+' => int // OK
'buzz12' => int // UncastableValueException
bool => int // UncastableValueException
array => int // UncastableValueException
object => int // UncastableValueException
resource => int // UncastableValueException
```

Fairly simple, isn't it? Let us **fail fast**!

### Why one needs THAT naive type casting?

[](#why-one-needs-that-naive-type-casting)

Let's imagine a library that is configured this way:

```
$config = [
    'retries' => 5, // int
    'delay' => 20, // int
]

// Initialization
$retries = $config['retries'] ?? null;
if ($retries === null) {
    throw new MissingConfigKeyException(...);
}
...
$retries = (int)$retries;
$delay = (int)$delay;
```

Client-side code:

```
$config => [
    'retries' => [5, 10, 30], // (int)array => 1
    'delay' => true, // (int)bool => 1
]
```

No matter if that's a misuse, or a result of major update: The system will continue to work. And that's the worst thing about it. It will continue to work, though, not in a way it was supposed to work. `PHP` is trying to do its best to let it work **at least somehow**.

The library comes in handy in a variety of scenarios 🚀
------------------------------------------------------

[](#the-library-comes-in-handy-in-a-variety-of-scenarios-)

- Deserialized data
- Request `body`/`query`
- `API` response
- Config
- etc.

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance68

Regular maintenance activity

Popularity21

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 89.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~88 days

Recently: every ~403 days

Total

23

Last Release

189d ago

PHP version history (2 changes)0.1.0PHP ^7.2

0.8.0PHP ^7.2|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/44e326ab3bffd640b622d3cecba8f5fc99bc0ec657ae59f841e871dbdead08ac?d=identicon)[zakirullin](/maintainers/zakirullin)

---

Top Contributors

[![zakirullin](https://avatars.githubusercontent.com/u/3357383?v=4)](https://github.com/zakirullin "zakirullin (134 commits)")[![DavidGoodwin](https://avatars.githubusercontent.com/u/203929?v=4)](https://github.com/DavidGoodwin "DavidGoodwin (12 commits)")[![the-toster](https://avatars.githubusercontent.com/u/22966096?v=4)](https://github.com/the-toster "the-toster (2 commits)")[![danolshev](https://avatars.githubusercontent.com/u/17455176?v=4)](https://github.com/danolshev "danolshev (1 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (1 commits)")

---

Tags

phpjsontypearraylanguageassertcastmess

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/zakirullin-typed-accessor/health.svg)

```
[![Health](https://phpackages.com/badges/zakirullin-typed-accessor/health.svg)](https://phpackages.com/packages/zakirullin-typed-accessor)
```

###  Alternatives

[zakirullin/mess

Convenient array-related routine &amp; better type casting

21228.9k2](/packages/zakirullin-mess)[phpoption/phpoption

Option Type for PHP

2.7k541.2M159](/packages/phpoption-phpoption)[pinkary-project/type-guard

Type Guard module is part of the Pinkary Project, and allows you to \*\*narrow down the type\*\* of a variable to a more specific type.

198102.4k14](/packages/pinkary-project-type-guard)[hi-folks/data-block

Data class for managing nested arrays and JSON data.

1472.2k](/packages/hi-folks-data-block)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
