web-fu/proxy

Library that allows to access array and object and proxy them

v1.0.0 2024-10-09 13:02 UTC

This package is auto-updated.

Last update: 2024-11-10 07:54:30 UTC


README

Latest Stable Version PHP Version Require Test status Static analysis status Code style status

A library that allows to create proxies for array and objects

This library allows to create proxies for arrays and objects.

This is a spin-off of the PHP Dot Notation library.

Installation

composer require web-fu/proxy

Usage

For arrays:

$element = [
    'foo' => 'bar',
];

$proxy = new ArrayProxy($element);

echo $proxy->has('foo'); //true
echo $proxy->isInitialised('foo'); //true
echo $proxy->get('foo'); //bar

$proxy->set('foo', 'baz');

echo $element['foo']; //baz

For objects:

$element = new class() {
    public string $property = 'test';
    
    public function method(): string
    {
        return 'foo';
    }
};

$proxy = new ClassProxy($element);

echo $proxy->has('property'); //true
echo $proxy->isInitialised('property'); //true
echo $proxy->get('property'); //test

echo $proxy->has('method()'); //true
echo $proxy->isInitialised('method()'); //true
echo $proxy->get('method()'); //foo

$proxy->set('property', 'baz');

echo $element->property; //baz

For both:

$element = [
    'foo' => 'bar',
];

$proxy = ProxyFactory::create($element);