molovo/object

There is no license information available for the latest version (v3.0.0) of this package.

Maintainers

Details

github.com/molovo/object

Source

Issues

Installs: 501

Dependents: 4

Suggesters: 0

Security: 0

Stars: 1

Watchers: 2

Forks: 0

Open Issues: 2

Language:HTML

v3.0.0 2018-06-19 14:14 UTC

This package is auto-updated.

Last update: 2024-10-29 05:04:38 UTC


README

Build Status Coverage Status

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