PHP code example of phrity / util-accessor
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/ */
phrity / util-accessor example snippets
use Phrity\Util\Accessor;
$subject = [
'string-val' => 'A string',
'assoc-array-val' => [
'string-val' => 'Another string',
],
'num-array-val' => [
'a',
],
'object-val' => (object)[
'string-val' => 'Yet another string',
],
];
$accessor = new Accessor();
$accessor->get($subject, 'string-val'); // => "A string"
$accessor->get($subject, 'assoc-array-val'); // => ['string-val' => "Another string"]
$accessor->get($subject, 'assoc-array-val/string-val'); // => "Another string"
$accessor->get($subject, 'num-array-val/0'); // => "a"
$accessor->get($subject, 'object-val/string-val'); // => "Yet another string"
$accessor->has($subject, 'assoc-array-val/string-val'); // => true
$accessor->has($subject, 'assoc-array-val/non-exising'); // => false
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;
$subject = [
'string-val' => 'A string',
];
$accessor = new Accessor();
$subject = $accessor->set($subject, 'string-val', 'Replaced value');
$subject = $accessor->set($subject, 'non-existing', 'Added value');
// $subject => ['string-val' => 'Replaced value', 'non-existing' => 'Added value']
use Phrity\Util\Accessor;
$subject = [
'object-val' => (object)[
'string-val' => 'A string',
],
];
$accessor = new Accessor('.');
$accessor->get($subject, 'object-val.string-val'); // => "A string"
$accessor->has($subject, 'object-val.string-val'); // => true
use Phrity\Util\PathAccessor;
$subject_1 = [
'object-val' => (object)[
'string-val' => 'A string',
],
];
$subject_2 = [
'object-val' => (object)[
'string-val' => 'Another string',
],
];
$accessor = new PathAccessor('object-val/string-val');
$accessor->get($subject_1); // => "A string"
$accessor->get($subject_2); // => "Another string"
$subject_1 = $accessor->set($subject_1, 'Replaced value');
$subject_2 = $accessor->set($subject_2, 'Replaced value');
use Phrity\Util\DataAccessor;
$subject = [
'object-val' => (object)[
'string-val' => 'A string',
'int-val' => 23,
],
];
$accessor = new DataAccessor(subject);
$accessor->get('object-val/string-val'); // => "A string"
$accessor->get('object-val/int-val'); // => 23
$accessor->set('object-val/string-val', 'Replaced string');
$accessor->set('object-val/int-val', 48);
use Phrity\Util\AccessorTrait;
class MyClass
{
use AccessorTrait;
public function doThings(): void
{
$my_data = ['array-val' => ['string-val' => 'A string']];
$exists = $this->accessorHas($my_data, ['array-val', 'string-val']);
$something = $this->accessorGet($my_data, ['array-val', 'string-val'], 'My default');
$my_path = $this->accessorParsePath('array-val#string-val', '#');
$exists = $this->accessorHas($my_data, $my_path);
$something = $this->accessorGet($my_data, $my_path, 'My default');
$modified = $this->accessorSet($my_data, $my_path, 'My new value');
}
}