Skip to content

Commit

Permalink
yadm object builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ASKozienko committed Sep 19, 2017
1 parent 046dec4 commit 6403dbe
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/Node.php
@@ -1,6 +1,7 @@
<?php
namespace Formapro\Pvm;

use Formapro\Pvm\Yadm\CreateTrait;
use Makasim\Values\ObjectsTrait;
use Makasim\Values\ValuesTrait;

Expand All @@ -16,6 +17,8 @@ class Node
getObject as public;
}

use CreateTrait;

/**
* @var Process
*/
Expand Down
18 changes: 10 additions & 8 deletions src/Process.php
@@ -1,6 +1,7 @@
<?php
namespace Formapro\Pvm;

use Formapro\Pvm\Yadm\CreateTrait;
use Makasim\Values\ObjectsTrait;
use Makasim\Values\ValuesTrait;

Expand All @@ -12,6 +13,7 @@ class Process
}

use ObjectsTrait;
use CreateTrait;

/**
* @var Node[]
Expand Down Expand Up @@ -60,7 +62,7 @@ public function getNode($id)
}

/** @var Node $node */
if (null === $node = $this->getObject('nodes.'.$id, Node::class)) {
if (null === $node = $this->getObject('nodes.'.$id)) {
throw new \LogicException('Not found');
}

Expand All @@ -69,15 +71,15 @@ public function getNode($id)

public function getNodes()
{
return $this->getObjects('nodes', Node::class);
return $this->getObjects('nodes');
}

/**
* @return Transition[]
*/
public function getTransitions()
{
return $this->getObjects('transitions', Transition::class);
return $this->getObjects('transitions');
}

/**
Expand All @@ -92,7 +94,7 @@ public function getTransition($id)
}

/** @var Transition $transition */
if (null === $transition = $this->getObject('transitions.'.$id, Transition::class)) {
if (null === $transition = $this->getObject('transitions.'.$id)) {
throw new \LogicException('Not found');
}

Expand Down Expand Up @@ -187,7 +189,7 @@ public function registerNode(Node $node)
*/
public function createNode()
{
$node = new Node();
$node = Node::create();
$node->setProcess($this);

$this->setObject('nodes.'.$node->getId(), $node);
Expand All @@ -197,7 +199,7 @@ public function createNode()

public function createTransition(Node $from = null, Node $to = null, $name = null)
{
$transition = new Transition();
$transition = Transition::create();
$transition->setName($name);
$transition->setProcess($this);
$from && $transition->setFrom($from);
Expand Down Expand Up @@ -234,7 +236,7 @@ public function breakTransition(Transition $transition, Node $node, $newName = n
*/
public function createToken(Transition $transition)
{
$token = new Token();
$token = Token::create();
$token->setProcess($this);
$token->setTransition($transition);

Expand All @@ -246,7 +248,7 @@ public function createToken(Transition $transition)
public function getToken($id)
{
/** @var Token $token */
foreach ($this->getObjects('tokens', Token::class) as $token) {
foreach ($this->getObjects('tokens') as $token) {
if ($token->getId() === $id) {

return $token;
Expand Down
2 changes: 2 additions & 0 deletions src/Token.php
@@ -1,11 +1,13 @@
<?php
namespace Formapro\Pvm;

use Formapro\Pvm\Yadm\CreateTrait;
use Makasim\Values\ValuesTrait;

class Token
{
use ValuesTrait;
use CreateTrait;

/**
* @var Process
Expand Down
2 changes: 2 additions & 0 deletions src/Transition.php
@@ -1,6 +1,7 @@
<?php
namespace Formapro\Pvm;

use Formapro\Pvm\Yadm\CreateTrait;
use Makasim\Values\ValuesTrait;

class Transition
Expand All @@ -9,6 +10,7 @@ class Transition
setValue as public;
getValue as public;
}
use CreateTrait;

const STATE_OPENED = 'opened';
const STATE_PASSED = 'passed';
Expand Down
19 changes: 19 additions & 0 deletions src/Yadm/CreateTrait.php
@@ -0,0 +1,19 @@
<?php
namespace Formapro\Pvm\Yadm;

use function Makasim\Values\build_object;

trait CreateTrait
{
/**
* @param array $data
*
* @return self|object
*/
public static function create(array $data = [])
{
return build_object(null, array_replace([
'class' => static::class,
], $data));
}
}
42 changes: 42 additions & 0 deletions src/Yadm/ObjectBuilderHook.php
@@ -0,0 +1,42 @@
<?php
namespace Formapro\Pvm\Yadm;

use Formapro\Pvm\Node;
use Formapro\Pvm\Process;
use Formapro\Pvm\Token;
use Formapro\Pvm\Transition;
use function Makasim\Values\register_global_hook;

class ObjectBuilderHook
{
/**
* @var string[]
*/
private $classMap;

/**
* @param string[] $classMap
*/
public function __construct(array $classMap = [])
{
$this->classMap = array_replace([
Process::class => Process::class,
Node::class => Node::class,
Token::class => Token::class,
Transition::class => Transition::class,
], $classMap);
}

public function register()
{
register_global_hook('get_object_class', function(array $values) {
if (isset($values['class'])) {
if (false == array_key_exists($values['class'], $this->classMap)) {
throw new \LogicException(sprintf('An object has class set "%s" but there is no class for it', $values['class']));
}

return $this->classMap[$values['class']];
}
});
}
}

0 comments on commit 6403dbe

Please sign in to comment.