PHP code example of jgswift / kfiltr

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

    

jgswift / kfiltr example snippets



class MyFilter implements kfiltr\Interfaces\Filter {
    use kfilter\Filter;

    function execute() {
        return func_get_arg(0);
    }
}

$filter = new MyFilter();

var_dump($filter('foo')); // returns 'foo'


$filter = new MyFilter();
$filter->setDelegate(function() {
    return 'bar';
});

var_dump($filter('foo')); // returns 'bar'



class MyFactory {
    use qtil\Factory;
}

class MyMapper {
    use kfiltr\Mapper;

    function __construct(MyFactory $factory) {
        $this->setFactory($factory);
    }

    // do stuff to map object here.
    function map($input,$object) {
        return $object;
    }
}

class MyMiscClass {

}

$mapper = new MyMapper(new MyFactory());

$object = $mapper([],'MyMiscClass');

var_dump($object); // returns blank MyMiscClass object


// the same filter class from above
class MyFilter implements kfiltr\Interfaces\Filter {
    use kfilter\Filter;

    function execute() {
        return func_get_arg(0);
    }
}

class MyHook implements kfiltr\Interfaces\Filter, kfiltr\Interfaces\Hook {
    use kfiltr\Hook, kfiltr\Filter;

    // this will execute all filters in order and return an array containing all results
    function execute() {
        $filters = $this->getFilters();

        $results = [];
        if(!empty($filters)) {
            foreach($filters as $filter) {
                $results[] = call_user_func_array($filter,func_get_args());
            }
        }

        return $results;
    }
}

$filter = new MyFilter();

$hook = new MyHook();

$hook->addFilter($filter);

var_dump($hook('foo')); // returns [ 0 => 'foo' ]

namespace Creatures {
    class Animal { 
        function __construct($species) { /* ... */ }
    }

    class Human extends Animal {
        function __construct($ethnicity) { /* ... */ }
    }
}

class MyFilter {
    use kfiltr\Factory\Filter;
}

// specific class names keyed by an id
$mapping = [
    'animal' => 'Creatures\Animal',
    'human' => 'Creatures\Human'
];

$filter = new MyFilter(); // create filter
$filter->setMapping($mapping); // apply mapping

$animal = $filter(['cat'],'animal'); // create animal
$human = $filter(['polish'],'human');// create human

var_dump(get_class($animal));   // Creatures\Animal
var_dump(get_class($human));    // Creatures\Human

namespace Creatures {
    class Animal { 
        function __construct($species) { /* ... */ }
    }

    class Human extends Animal {
        function __construct($ethnicity) { /* ... */ }
    }
}

class MyFactory {
    use qtil\Factory;
}

class MyMapper {
    use kfiltr\Factory\Mapper;
}

$mapping = [
    'animal' => 'Creatures\Animal',
    'human' => 'Creatures\Human'
];

$mapper = new MyMapper();
$mapper->setFactory(new MyFactory);
$mapper->setMapping($mapping);

$caucasian = $mapper(['ethnicity'=>'caucasian'],'human');
$indian = $mapper(['ethnicity'=>'indian'],'human');
$elephant = $mapper(['species'=>'elephant'],'animal');

var_dump($caucasian->ethnicity);
var_dump($indian->ethnicity);
var_dump($elephant->species);
sh
php composer.phar