molovo / object
Installs: 502
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 2
Language:HTML
Requires
- php: >=7.1
Requires (Dev)
- codeception/codeception: ^2.1
- codeception/verify: ^0.3.0
- filp/whoops: 1.*
- satooshi/php-coveralls: ^1.0
README
A lightweight object wrapper, ideal for storing nested config etc.
Install
composer require molovo/object
Use
<?php use Molovo\Object\DataObject; // Pass the array of values directly to the constructor $object = new DataObject([ 'some' => [ 'awesome' => [ 'nested' => 'values' ] ] ]); // Getting values echo $object->some->awesome->nested; // returns 'values' // Setting values $object->some->awesome = 'code'; // Getting nested values echo $object->valueForPath('some.awesome'); // returns 'code' // Setting nested values $object->setValueForPath('some.awesome.new.values.are', 'awesome'); // Get a pointer to a value (even if it doesn't exist) $pointer = &$object->getPointer('some'); $pointer = &$pointer->getPointer('value'); $pointer = 'rules'; // Return the values as an array $object->toArray(); // returns: // [ // 'some' => [ // 'awesome' => [ // 'new' => [ // 'values' => [ // 'are' => 'awesome' // ] // ] // ], // 'value' => 'rules' // ] // ] // Merge objects together $merged = $object->merge(new DataObject(['new' => 'values']);
Immutable Objects
<?php use Molovo\Object\ImmutableObject; $object = new ImmutableObject([ 'some' => [ 'awesome' => [ 'nested' => 'values' ] ] ]); $object->some = 'thing else'; // throws Molovo\Object\Exception\ImmutabilityViolationException $object->some->awesome->nested = 'changed'; // throws Molovo\Object\Exception\ImmutabilityViolationException