Need help with mess?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

zakirullin
143 Stars 3 Forks MIT License 132 Commits 2 Opened issues

Description

Make your PHP arrays sweet'n'safe

Services available

!
?

Need anything else?

Contributors list

# 191,933
C
asm
lexer
PHP
114 commits
# 450,783
Shell
type-in...
securit...
PHP
2 commits
# 9,853
PHP
WordPre...
Laravel
php7
1 commit

Mess

Build Status Psalm coverage PHP from Packagist Latest Stable Version GitHub commits Software License

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:

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

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)

  • getListOfString()
  • getListOfInt()
  • getArrayOfStringToString()
  • getArrayOfStringToBool()
  • etc.

Installation

$ composer require zakirullin/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.

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?

Let's imagine a library that is configured this way: ```php $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:

php
$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 🚀

  • Deserialized data
  • Request
    body
    /
    query
  • API
    response
  • Config
  • etc.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.