1. Go to this page and download the library: Download phrity/util-accessor library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Phrity\Util\Accessor;
$subject = [
'string-val' => 'A string',
];
$accessor = new Accessor();
$accessor->get($subject, 'non-existing'); // => null
$accessor->get($subject, 'non-existing', 'My default'); // => "My default"
use Phrity\Util\Accessor;
use Phrity\Util\Transformer\Type;
$subject = [
'float-val' => 12.34,
'assoc-array-val' => [
'string-val' => 'Another string',
],
];
$accessor = new Accessor();
$accessor->get($subject, 'float-val', coerce: Type::STRING); // Return flaot as string
$accessor->get($subject, 'assoc-array-val', coerce: Type::OBJECT); // Return array as object
use Phrity\Util\Accessor;
use Phrity\Util\Transformer\{
FirstMatchResolver,
EnumConverter,
StringableConverter,
BasicTypeConverter,
};
$transformer = new FirstMatchResolver([
new EnumConverter(),
new StringableConverter(),
new BasicTypeConverter(),
]);
$accessor = new Accessor(transformer: $transformer);