This will thus not build the model when the statemachine asks for it, but rather the model is built when your application code creates it. This class is useful if your domain model actually subclasses the StateMachine class or uses object composition to use the statemachines' logic internally. In that case, you can construct the StateMachine in your models' constructor, including the Context with this class in it and with the domain model itself ($this) as an argument to the builder, so you can put our event handlers on the domain model and have them respond to the statemachine. eg: in the constructor of your domain model that uses object composition $builder = new ModelBuilder($this); $identifier = new Identifier($this->getId(), 'my-model-machine'); $context = new Context($identifier, $builder); $this->machine = new StateMachine($context); eg: in the constructor of your domain model that uses StateMachine as its' superclass $builder = new ModelBuilder($this); $identifier = new Identifier($this->getId(), 'my-model-machine'); $context = new Context($identifier, $builder); parent::__construct($context)
Автор: Rolf Vreijdenberger
Наследование: extends izzum\statemachine\EntityBuilder
 /**
  * @test
  */
 public function shouldUseModelBuilderCorrectly()
 {
     $identifier = new Identifier(-1, 'order');
     $entity = new \stdClass();
     $entity->id = 123;
     $builder = new ModelBuilder($entity);
     $this->assertSame($entity, $builder->getEntity($identifier));
     $this->assertSame($entity, $builder->getEntity($identifier, true));
     $this->assertNotSame($identifier, $entity);
     $this->assertNotSame($identifier, $builder->getEntity($identifier), 'default builder returns $identifer and modelbuilder does not');
     //now use a different model
     $builder = new ModelBuilder($identifier);
     $this->assertSame($identifier, $builder->getEntity($identifier), 'the argument being the same is the method signature, it has no influence on the object returned (in this case)');
     $this->assertSame($identifier, $builder->getEntity($identifier, true));
 }