Download the PHP package philiagus/parser without Composer
On this page you can find all versions of the php package philiagus/parser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package parser
philiagus/parser
PHP classes to assert, convert and parse data.
Is it tested?
Tested on the following PHP Version:
- PHP8.3
100% test covered. Test coverage generated on PHP8.3
Why do I need it?
Making sure your inputs are what they should be is one of the core principles of secure coding.
Obviously there are more, but we must tackle them one step at a time.
The basic idea of the parsers is, that the developer defines a structure through code and later throws a set of data against it. The parsers make sure, that the data is following the rules of defined in the structure.
How does it work?
A more in depth documentation and tutorials on how to write own parsers can be found here.
A simple example to assert that a provided value lies within defined boundaries:
The real fun begins, when you start stacking parsers into one another:
Let's say: You have an API which receives requests from a client in JSON format, and you need to find the relevant information.
If you execute this code the result will be Frank herbert was born 101 years ago
(at least on the date of this typing).
Would you change the input to {"name":123,"birthday":"1920-10-0f"}
, the result would be:
What if something is missing?
Fear not! All parsers implement Philiagus\Parser\Contract\Parser
, so you can easily write your own to fit your specific need. Check out Philiagus\Parser\Base\Parser
for a base class you can easily extend.
Some hints:
- If your parser validates a type (example: a parser the reads an XML, so non-strings are a no-go), the
Philiagus\Parser\Base\OverwritableTypeErrorMessage
-Trait might help - The
Philiagus\Parser\Base\Parser
already implements basic things such as chaining using->then($parser)
among other things, so you don't have to worry about that - If you need more control over the behaviour of the parser or just don't like the
ResultBuilder
, you only need to implement thePhiliagus\Parser\Contract\Parser
-Interface to be interoperable with other parsers. Just be aware that you MUST- create a
ParserBegin
subject when you enter the parser, wrapping the provided subject into another subject to ensure that the value chain is upheld - respect the
throwOnError()
info of the subject: If an error occurs andthrowOnError()
is active, you have to throw the Error (most times using$error->throw()
). Accordingly, ifthrowOnError()
is not active you have to add the Errors to the Result object.
- create a
All versions of parser with dependencies
ext-json Version *
ext-mbstring Version *