izzum\statemachine\Transition::getCopy PHP Method

getCopy() public method

We need to instantiate it with a different from and to state since either one of those states can be the regex states. All other fields need to be copied. Override this method in a subclass to add other fields. By using 'new static' we are already instantiating a possible subclass.
public getCopy ( State $from, State $to ) : Transition
$from State
$to State
return Transition
    public function getCopy(State $from, State $to)
    {
        $copy = new static($from, $to, $this->getEvent(), $this->getRuleName(), $this->getCommandName(), $this->getGuardCallable(), $this->getTransitionCallable());
        $copy->setDescription($this->getDescription());
        return $copy;
    }

Usage Example

 /**
  * @test
  */
 public function shouldBeAbleToCopy()
 {
     $a = new State('a');
     $b = new State('b');
     $a_copy = new State('a');
     $b_copy = new State('b');
     $event = 'my-event';
     $rule = 'foo-rule';
     $command = 'foo-command';
     $description = 'foobar';
     $gc = function () {
         echo "guard callable";
         return true;
     };
     $tc = function () {
         echo "transition callable";
     };
     $t = new Transition($a, $b, $event, $rule, $command, $gc, $tc);
     $t->setDescription($description);
     $copy = $t->getCopy($a_copy, $b_copy);
     $this->assertNotSame($a, $a_copy);
     $this->assertNotSame($b, $b_copy);
     $this->assertNotSame($copy, $t);
     $this->assertEquals($description, $copy->getDescription());
     $this->assertEquals($rule, $copy->getRuleName());
     $this->assertEquals($command, $copy->getCommandName());
     $this->assertEquals($event, $copy->getEvent());
     $this->assertEquals($t->getName(), $copy->getName());
     $this->assertEquals($t->getGuardCallable(), $copy->getGuardCallable());
     $this->assertEquals($gc, $copy->getGuardCallable());
     $this->assertEquals($tc, $copy->getTransitionCallable());
 }
All Usage Examples Of izzum\statemachine\Transition::getCopy