duncan3dc / serial
Consistent serialization helpers for multiple backends
Installs: 294 802
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 2
Open Issues: 0
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- symfony/yaml: ^3.4 || ^4.2
Requires (Dev)
- maglnet/composer-require-checker: ^3.3
- phpstan/phpstan: ^0.12.93
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2024-11-25 00:08:27 UTC
README
A collection of PHP serialization helpers with a consistent interface for each.
](https://github.com/duncan3dc/serial/actions?query=branch%3Amaster+workflow%3Abuildcheck)
Available Classes
- Json (using the native json_* functions)
- Yaml (using the Symfony Yaml component)
- Php (using the native serialize/unserialize functions)
Interface
All serialization classes implement the interface duncan3dc\Serial\SerialInterface
Examples
Convert array data to string format
use duncan3dc\Serial\Json; $data = BusinessLogic::getDataAsArray(); $json = Json::encode($data);
Convert string formatted data to an array
use duncan3dc\Serial\Yaml; $yaml = Api::getYamlResponse($request); $response = Yaml::decode($yaml);
Convient methods to serialize and store data on disk
use duncan3dc\Serial\Json; $filename = "/tmp/file.json"; $data = BusinessLogic::getDataAsArray(); Json::encodeToFile($filename, $data);
Retrieve previously stored data from disk
use duncan3dc\Serial\Json; $filename = "/tmp/file.json"; $data = Json::decodeFromFile($filename);
ArrayObject
The decode()
and decodeFromFile()
methods return a custom ArrayObject.
If you need a plain array you can get one like so:
$array = Json::decode($jsonString)->asArray();
There are also helper methods to convert to any of the available serialization formats.
$data = Json::decode($jsonString); $yaml = $data->asYaml(); $json = $data->asJson(); $php = $data->asPhp();