PHP code example of laasti / lazydata

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

    

laasti / lazydata example snippets



$data = [
    'title' => 'render_title',
    'with_arguments' => ['=my_callable', [/* args here */]],
    'with_class' => ['=my_class', 'my_function'], //or '=my_class::my_function',
    'with_object' => [$object, 'my_function'],
    'meta' => function() {
        return [
            'description' => 'My description'
        ]
    }
];

$viewdata = new Laasti\Lazydata\Data($data);
$viewdata->set('username', function() {return 'George';});

//You can use dot notation within the lazy loaded data
$viewdata->get('meta.description'); //Returns 'My description'


    $data = [
        'native_example' => 'strtoupper:test', //I know, it's a stupid example :P
        'closure_example' => 'closure:Test',
    ];
    $resolver = new \Laasti\Lazydata\Resolvers\FilterResolver;
    $resolver->setFilter('closure', function($value) {
        return md5($value.'MYSALT');
    });
    $viewdata = new Laasti\Lazydata\Data($data, $resolver);

    $viewdata->get('native_example'); //Returns 'TEST'
    $viewdata->get('closure_example'); //Returns '56e29f03228697ad59822c71eb4d7750'



//We need to setup the ContainerResolver that comes with the package
$container = new League\Container\Container;
$container->add('Laasti\Lazydata\Resolvers\ResolverInterface', 'Laasti\Lazydata\Resolvers\ContainerResolver')->withArgument($container);
$container->add('Laasti\Lazydata\Data')->withArguments([[], 'Laasti\Lazydata\Resolvers\ResolverInterface']);

$viewdata = $container->get('Laasti\Lazydata\Data);;

$container->add('container_key', 'some value');

$viewdata->set('viewdata_key', '=container_key');
$viewdata->get('viewdata_key'); //Returns 'some value'

//Returns the value from SomeClass->myMethod();, SomeClass is resolved with the container
$viewdata->set('viewdata_callable_key', '=SomeClass::myMethod');
$viewdata->get('viewdata_callable_key');

//Returns the value from SomeClass->myMethod('George'); SomeClass is resolved with the container
$viewdata->set('viewdata_callable_args_key', ['=SomeClass::myMethod', ['George']]);
$viewdata->get('viewdata_callable_args_key');