Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
046dec4
commit 6403dbe
Showing
6 changed files
with
78 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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']]; | ||
} | ||
}); | ||
} | ||
} |