Mess
Make your PHP arrays sweet'n'safe
Install / Use
/learn @zakirullin/MessREADME
Mess
We face a few problems in our PHP projects
- Illogical type casting (
PHP's native implementation is way too "smart") - Pointless casts like
array => floatare 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 APIresponse- Config
- etc.
Related Skills
node-connect
350.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
350.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
350.8kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
