PHP code example of lukaszmakuch / haringo

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

    

lukaszmakuch / haringo example snippets



use lukaszmakuch\Haringo\Builder\Impl\HaringoBuilderImpl;

$haringoBuilder = new HaringoBuilderImpl();


use lukaszmakuch\Haringo\Builder\HaringoBuilder;

/* @var $haringoBuilder HaringoBuilder */
$haringo = $haringoBuilder->build();


use lukaszmakuch\Haringo\ClassSourceResolver\Impl\ClassPathFromMapResolver\ClassPathSourceMap;
use lukaszmakuch\Haringo\ClassSource\Impl\ExactClassPath;

$map = new ClassPathSourceMap();


use lukaszmakuch\Haringo\Builder\HaringoBuilder;
use lukaszmakuch\Haringo\ClassSourceResolver\Impl\ClassPathFromMapResolver\ClassPathSourceMap;

/* @var $haringoBuilder HaringoBuilder */
/* @var $map ClassPathSourceMap */
$haringoBuilder->setClassSourceMap($map);


use lukaszmakuch\Haringo\MethodSelectorMatcher\Impl\MethodFromMap\MethodSelectorMap;

$map = new MethodSelectorMap();


use lukaszmakuch\Haringo\Builder\HaringoBuilder;
use lukaszmakuch\Haringo\MethodSelectorMatcher\Impl\MethodFromMap\MethodSelectorMap;

/* @var $haringoBuilder HaringoBuilder */
/* @var $map MethodSelectorMap */
$haringoBuilder->setMethodSelectorMap($map);


use lukaszmakuch\Haringo\ParamSelectorMatcher\Impl\ParamFromMapMatcher\ParamSelectorMap;

$map = new ParamSelectorMap();


use lukaszmakuch\Haringo\Builder\HaringoBuilder;
use lukaszmakuch\Haringo\ParamSelectorMatcher\Impl\ParamFromMapMatcher\ParamSelectorMap;

/* @var $haringoBuilder HaringoBuilder */
/* @var $map ParamSelectorMap */
$haringoBuilder->setParamSelectorMap($map);


use lukaszmakuch\Haringo\Builder\HaringoBuilder;
use lukaszmakuch\Haringo\Builder\Extension\ValueSourceExtension;

/* @var $haringoBuilder HaringoBuilder */
/* @var $extension ValueSourceExtension */
$haringoBuilder->addValueSourceExtension($extension);


namespace lukaszmakuch\Haringo;

class TestClass
{
    public $memberA;
    public $memberB;
    public $passedToConstructor;

    public function __construct($passedToConstructor = null)
    {
        $this->passedToConstructor = $passedToConstructor;
    }

    public function setMembers($newA = null, $newB = null)
    {
        $this->memberA = $newA;
        $this->memberB = $newB;
    }
}


use lukaszmakuch\Haringo\TestClass;

$obj = new TestClass(constructorParam);
$obj->setMembers("firstParamVal", "secondParamVal");


use lukaszmakuch\Haringo\BuildPlan\Impl\NewInstanceBuildPlan;
use lukaszmakuch\Haringo\ClassSource\Impl\ExactClassPath;
use lukaszmakuch\Haringo\MethodCall\MethodCall;
use lukaszmakuch\Haringo\MethodSelector\Impl\MethodByExactName;
use lukaszmakuch\Haringo\ParamSelector\Impl\ParamByExactName;
use lukaszmakuch\Haringo\ParamValue\AssignedParamValue;
use lukaszmakuch\Haringo\ValueSource\Impl\ScalarValue;
use lukaszmakuch\Haringo\TestClass;

$plan = (new NewInstanceBuildPlan())
    ->setClassSource(new ExactClassPath(TestClass::class))
    ->addMethodCall(
        (new MethodCall(new MethodByExactName("setMembers")))
            ->assignParamValue(new AssignedParamValue(
                new ParamByExactName("newB"),
                new ScalarValue("secondParamVal")
            ))
            ->assignParamValue(new AssignedParamValue(
                new ParamByExactName("newA"),
                new ScalarValue("firstParamVal")
            ))
    )
    ->addMethodCall(
        (new MethodCall(new MethodByExactName("__construct")))
            ->assignParamValue(new AssignedParamValue(
                new ParamByExactName("passedToConstructor"),
                new ScalarValue("constructorParam")
            ))
    );


use lukaszmakuch\Haringo\TestClass;

class TestStaticFactory
{
    public static function getProduct($configValue)
    {
        return new TestClass($configValue);
    }
}



$obj = TestStaticFactory::getProduct("paramValue");



use lukaszmakuch\Haringo\BuildPlan\Impl\StaticFactoryProductBuildPlan;
use lukaszmakuch\Haringo\ClassSource\Impl\ExactClassPath;
use lukaszmakuch\Haringo\MethodCall\MethodCall;
use lukaszmakuch\Haringo\MethodSelector\Impl\MethodByExactName;
use lukaszmakuch\Haringo\ParamSelector\Impl\ParamByExactName;
use lukaszmakuch\Haringo\ParamValue\AssignedParamValue;
use lukaszmakuch\Haringo\TestClass;
use lukaszmakuch\Haringo\ValueSource\Impl\ScalarValue;

$plan = (new StaticFactoryProductBuildPlan())
    ->setFactoryClass(new ExactClassPath(TestStaticFactory::class))
    ->setFactoryMethodCall(
        (new MethodCall(new MethodByExactName("getProduct")))
            ->assignParamValue(new AssignedParamValue(
                new ParamByExactName("configValue"),
                new ScalarValue("paramValue")
            ))
    );


use lukaszmakuch\Haringo\TestClass;

class TestFactoryClass
{
    public function getProduct($configValue)
    {
        return new TestClass($configValue);
    }
}



$factory = new TestFactoryClass();
$obj = $factory->getProduct("paramValue");



use lukaszmakuch\Haringo\BuildPlan\Impl\FactoryObjectProductBuildPlan;
use lukaszmakuch\Haringo\BuildPlan\Impl\NewInstanceBuildPlan;
use lukaszmakuch\Haringo\ClassSource\Impl\ExactClassPath;
use lukaszmakuch\Haringo\MethodCall\MethodCall;
use lukaszmakuch\Haringo\MethodSelector\Impl\MethodByExactName;
use lukaszmakuch\Haringo\ParamSelector\Impl\ParamByExactName;
use lukaszmakuch\Haringo\ParamValue\AssignedParamValue;
use lukaszmakuch\Haringo\TestClass;
use lukaszmakuch\Haringo\ValueSource\Impl\BuildPlanResultValue;
use lukaszmakuch\Haringo\ValueSource\Impl\ScalarValue;

$plan = (new FactoryObjectProductBuildPlan())
    ->setFactoryObject(
        //build TestFactoryClass instance
        new BuildPlanResultValue((new NewInstanceBuildPlan())
            ->setClassSource(new ExactClassPath(TestFactoryClass::class)
        ))
    )
    ->setBuildMethodCall(
        (new MethodCall(new MethodByExactName("getProduct")))
            ->assignParamValue(new AssignedParamValue(
                new ParamByExactName("configValue"),
                new ScalarValue("paramValue")
            ))
    );


use lukaszmakuch\Haringo\TestClass;

class TestBuilder
{
    private $param;
    public function setConstructorParam($param) { $this->param = $param; }
    public function build()
    {
        return new TestClass($this->param);
    }
}



$builder = new TestBuilder();
$builder->setConstructorParam("paramValue");
$obj = $builder->build();


use lukaszmakuch\Haringo\BuildPlan\Impl\BuilderObjectProductBuildPlan;
use lukaszmakuch\Haringo\BuildPlan\Impl\NewInstanceBuildPlan;
use lukaszmakuch\Haringo\ClassSource\Impl\ExactClassPath;
use lukaszmakuch\Haringo\MethodCall\MethodCall;
use lukaszmakuch\Haringo\MethodSelector\Impl\MethodByExactName;
use lukaszmakuch\Haringo\ParamSelector\Impl\ParamByExactName;
use lukaszmakuch\Haringo\ParamValue\AssignedParamValue;
use lukaszmakuch\Haringo\TestClass;
use lukaszmakuch\Haringo\ValueSource\Impl\BuildPlanResultValue;
use lukaszmakuch\Haringo\ValueSource\Impl\ScalarValue;

$plan = (new BuilderObjectProductBuildPlan())
    ->setBuilderSource(
        //build TestBuilder object
        new BuildPlanResultValue((new NewInstanceBuildPlan())
            ->setClassSource(new ExactClassPath(TestBuilder::class)
        ))
    )
    ->addSettingMethodCall(
        (new MethodCall(new MethodByExactName("setConstructorParam")))
            ->assignParamValue(new AssignedParamValue(
                new ParamByExactName("param"),
                new ScalarValue("paramValue")
            ))
    )
    ->setBuildMethodCall(
        (new MethodCall(new MethodByExactName("build")))
    );


use lukaszmakuch\Haringo\ClassSource\Impl\ExactClassPath;

//source of the \DateTime class
$classSrc = new ExactClassPath(\DateTime::class);


use lukaszmakuch\Haringo\ClassSourceResolver\Impl\ClassPathFromMapResolver\ClassPathSourceMap;
use lukaszmakuch\Haringo\ClassSource\Impl\ExactClassPath;

/* @var $map ClassPathSourceMap */

//add the \DateTime class source under the "date_time" key
$map->addSource(
    //key within the map
    "date_time",
    //actual class path source
    new ExactClassPath(\DateTime::class)
);


use lukaszmakuch\Haringo\ClassSource\Impl\ClassPathFromMap;

//class source stored under the "date_time" key
$classSrc = new ClassPathFromMap("date_time");


use lukaszmakuch\Haringo\MethodSelector\Impl\ConstructorSelector;

//constructor of any class
$methodSelector = ConstructorSelector::getInstance();


use lukaszmakuch\Haringo\MethodSelector\Impl\MethodByExactName;

//method with name "myMethodName"
$methodSelector = new MethodByExactName("myMethodName");


use lukaszmakuch\Haringo\MethodSelectorMatcher\Impl\MethodFromMap\MethodSelectorMap;
use lukaszmakuch\Haringo\MethodSelectorMatcher\Impl\MethodFromMap\FullMethodIdentifier;
use lukaszmakuch\Haringo\MethodSelector\Impl\MethodByExactName;
use lukaszmakuch\Haringo\ClassSource\Impl\ExactClassPath;

/* @var $map MethodSelectorMap */

//add the \DateTime::modify method selector under the "date_time.modify" key
$map->addSelector(
    //key within the map
    "date_time.modify",
    //full method identifier
    new FullMethodIdentifier(
        //source of the class which contains this method
        new ExactClassPath(\DateTime::class),
        //actual method selector
        new MethodByExactName("modify")
    )
);


use lukaszmakuch\Haringo\MethodSelector\Impl\MethodFromMap;

//method selector stored under the "date_time.modify" key
$methodSelector = new MethodFromMap("date_time.modify");


use lukaszmakuch\Haringo\ParamSelector\Impl\ParamByPosition;

//second parameter of some method
$paramSelector = new ParamByPosition(1);


use lukaszmakuch\Haringo\ParamSelector\Impl\ParamByExactName;

//parameter called "name"
$paramSelector = new ParamByExactName("name");


use lukaszmakuch\Haringo\ParamSelectorMatcher\Impl\ParamFromMapMatcher\ParamSelectorMap;
use lukaszmakuch\Haringo\ParamSelectorMatcher\Impl\ParamFromMapMatcher\FullParamIdentifier;
use lukaszmakuch\Haringo\MethodSelectorMatcher\Impl\MethodFromMap\FullMethodIdentifier;
use lukaszmakuch\Haringo\ParamSelector\Impl\ParamByPosition;

/* @var $map ParamSelectorMap */

//add the first parameter of the \DateTime::modify method
//under the "date_time.modify.first_param" key
$map->addSelector(
    //key of the mapped selector
    "date_time.modify.first_param",
    //full identifier of the parameter
    new FullParamIdentifier(
        //full identifier of the method
        new FullMethodIdentifier(
            //class source
            new ExactClassPath(\DateTime::class),
            //method selector
            new MethodByExactName("modify")
        ),
        //actual parameter selector
        new ParamByPosition(0)
    )
);


use lukaszmakuch\Haringo\ParamSelector\Impl\ParamFromMap;

//parameter selector stored under the "date_time.modify.first_param" key
$paramSelector = new ParamFromMap("date_time.modify.first_param");


use lukaszmakuch\Haringo\ValueSource\Impl\ScalarValue;

//string "Hello Haringo!"
$valueSource = new ScalarValue("Hello Haringo!");

//integer 42
$valueSource = new ScalarValue(42);

//float 36.6
$valueSource = new ScalarValue(36.6);

//boolean true
$valueSource = new ScalarValue(true);


use lukaszmakuch\Haringo\ValueSource\Impl\ArrayValue;
use lukaszmakuch\Haringo\ValueSource\Impl\ScalarValue;

//[123 => "one two three", "anotherKey" => true]
$valueSource = new ArrayValue();
$valueSource->addValue(123, new ScalarValue("one two three"));
$valueSource->addValue("anotherKey", new ScalarValue(true));


use lukaszmakuch\Haringo\BuildPlan\Impl\NewInstanceBuildPlan;
use lukaszmakuch\Haringo\ClassSource\Impl\ExactClassPath;
use lukaszmakuch\Haringo\ValueSource\Impl\BuildPlanResultValue;

//create a build plan of a new \DateTime
$plan = new NewInstanceBuildPlan();
$plan->setClassSource(new ExactClassPath(\DateTime::class));

//create a value source based on this plan
$valueSource = new BuildPlanResultValue($plan);


use lukaszmakuch\Haringo\Haringo;
use lukaszmakuch\Haringo\BuildPlan\BuildPlan;
use lukaszmakuch\Haringo\Exception\UnableToBuild;

/* @var $haringo Haringo */
/* @var $buildPlan BuildPlan */
try {
    $buitObject = $haringo->buildObjectBasedOn($buildPlan);
} catch (UnableToBuild $e) {
    //it was impossible to build an object based on the given build plan
}


use lukaszmakuch\Haringo\Haringo;
use lukaszmakuch\Haringo\BuildPlan\BuildPlan;
use lukaszmakuch\Haringo\Exception\UnableToDeserialize;
use lukaszmakuch\Haringo\Exception\UnableToSerialize;

/* @var $haringo Haringo */
/* @var $buildPlan BuildPlan */
try {
    $serializedBuildPlan = $haringo->serializeBuildPlan(buildPlan);
    $deserializedBuildPlan = $haringo->deserializeBuildPlan(serializedBuildPlan);
} catch (UnableToSerialize $e) {
    //it was impossible to serialize this build plan
} catch (UnableToDeserialize $e) {
    //it was impossible to deserialize this build plan
}


use lukaszmakuch\Haringo\Haringo;
use lukaszmakuch\Haringo\Exception\BuildPlanNotFound;
use lukaszmakuch\Haringo\Exception\UnableToBuild;
use lukaszmakuch\Haringo\BuildPlan\BuildPlan;

/* @var $haringo Haringo */
/* @var $buildPlan BuildPlan */
try {
    $buitObject = $haringo->buildObjectBasedOn($buildPlan);
    $fetchedBuildPlan = $haringo->getBuildPlanUsedToBuild(buitObject);
} catch (UnableToBuild $e) {
    //it was impossible to build an object based on the given build plan
} catch (BuildPlanNotFound $e) {
    //it was impossible to fetch the build plan used to build the given object
}