Convenient array-related routine & better type casting

0.8.5 2024-09-10 06:08 UTC

This package is auto-updated.

Last update: 2024-11-10 09:54:25 UTC


README

GitHub 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:

$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:

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

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