PHP code example of bingo-soft / util

1. Go to this page and download the library: Download bingo-soft/util 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/ */

    

bingo-soft / util example snippets


/* Original class */

interface BusinessServiceInterface
{
}

class BusinessServiceImpl
{
    public function doSomething(int $id, string $name)
    {
        return "$id - $name";
    }
}

/* Handler */

use Util\Proxy\MethodHandlerInterface;

class DoSomethingMethodHandler implements MethodHandlerInterface
{
    public function invoke($proxy, \ReflectionMethod $thisMethod, \ReflectionMethod $proceed, array $args)
    {
        return 'prepend - ' . $proceed->invoke($proxy, ...$args);
    }
}

/* Custom proxy factory*/

use Util\Proxy\{
    MethodHandlerInterface,
    ProxyFactory
};

class MyProxyFactory
{
    public static function createProxy(string $type, MethodHandlerInterface $method, array $args = [])
    {
        $enhancer = new ProxyFactory();
        $enhancer->setSuperclass($type);
        $enhancer->setInterfaces([ BusinessServiceInterface::class ]);
        return $enhancer->create($args);
    }
}

/* Creation and test of proxy class*/

$method = new DoSomethingMethodHandler();
$proxy = MyProxyFactory::createProxy(BusinessServiceImpl::class, $method);
$proxy->setHandler($method);
echo $proxy->doSomething(1, "curitis"); // will print "prepend - 1 - curitis"



/* Set nested property */

use Tests\Domain\Misc\RichType;
use Util\Reflection\SystemMetaObject;

$rich = new RichType();
$meta = SystemMetaObject::forObject($rich);
$meta->setValue("richType.richField", "foo");

echo $meta->getValue("richType.richField"); // new RichType( richType => new RichType ( richField => "foo" )) 

/* Create meta object from array */

$map = [];
$metaMap = SystemMetaObject::forObject($map);
$metaMap->setValue("id", "100");
$metaMap->setValue("name.first", "Clinton");
print_r($map); // [ "id" => 100, "name" => [ "first" => "Clinton" ]]