1. Go to this page and download the library: Download xsolve-pl/model-factory 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/ */
xsolve-pl / model-factory example snippets
namespace Example;
use Xsolve\ModelFactory\ModelFactory\ModelFactory;
class FooModelFactory extends ModelFactory
{
/**
* {@inheritdoc}
*/
public function supportsObject($object)
{
return ($object instanceof Foo);
}
/**
* {@inheritdoc}
*/
protected function instantiateModel($object)
{
/* @var Foo $object */
return new FooModel($object);
}
}
namespace Example;
use Xsolve\ModelFactory\ModelFactory\ModelFactory;
class BazModelFactory extends ModelFactory
{
/**
* @var VolumetricWeightCalculator
*/
protected $volumetricWeightCalculator;
/**
* @param VolumetricWeightCalculator $volumetricWeightCalculator
*/
public function __construct(VolumetricWeightCalculator $volumetricWeightCalculator)
{
$this->volumetricWeightCalculator = $volumetricWeightCalculator;
}
/**
* @return VolumetricWeightCalculator
*/
public function getVolumetricWeightCalculator()
{
return $this->volumetricWeightCalculator;
}
/**
* {@inheritdoc}
*/
public function supportsObject($object)
{
return ($object instanceof Baz);
}
/**
* {@inheritdoc}
*/
protected function instantiateModel($object)
{
/* @var Baz $object */
return new BazModel($object);
}
}
namespace Example;
use Xsolve\ModelFactory\ModelFactory\ModelFactoryAwareModelInterface;
use Xsolve\ModelFactory\ModelFactory\ModelFactoryAwareModelTrait;
class BazModel implements ModelFactoryAwareModelInterface
{
use ModelFactoryAwareModelTrait;
/**
* @var Baz
*/
protected $baz;
/**
* @param Baz $baz
*/
public function __construct(Baz $baz)
{
$this->baz = $baz;
}
/**
* @return float
*/
public function getVolume()
{
return ($this->baz->getLength() * $this->baz->getWidth() * $this->baz->getHeight());
}
/**
* @return float
*/
public function getVolumetricWeight()
{
return $this
->getModelFactory()
->getVolumetricWeightCalculator()
->calculate($this->getVolume());
}
}
namespace Example;
use Xsolve\ModelFactory\ModelFactoryCollection\ModelFactoryCollection;
$fooModelFactory = new FooModelFactory();
$bazModelFactory = new BazModelFactory();
$modelFactoryCollection = new ModelFactoryCollection();
$modelFactoryCollection
->addModelFactory($fooModelFactory)
->addModelFactory($bazModelFactory);
$foo1 = $storage->getFoo(1);
$baz1 = $storage->getBaz(1);
$foo1Model = $modelFactoryCollection->createModel($foo1);
$baz1Model = $modelFactoryCollection->createModel($baz1);
namespace Example;
use Xsolve\ModelFactory\ModelFactoryCollection\ModelFactoryCollectionAwareModelInterface;
use Xsolve\ModelFactory\ModelFactoryCollection\ModelFactoryCollectionAwareModelTrait;
class FooModel implements ModelFactoryCollectionAwareModelInterface
{
use ModelFactoryCollectionAwareModelTrait;
/**
* @var Foo
*/
protected $foo;
/**
* @param Foo $foo
*/
public function __construct(Foo $foo)
{
$this->foo = $foo;
}
/**
* @return BazModel[]
*/
public function getBazs()
{
return $this
->getModelFactoryCollection()
->createModels($this->foo->getBazs());
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.