dox Executable Documentation

Facilitate registration

Register entity representer

$this->assertSame($this->registrar->getRepresenter(), $this->registry->getEntityRepresenter(\DateTime::class));

Register method action

$someAction = $this->registrar->addMethodAction('SomeActionHandler', 'someAction');
$otherAction = $this->registrar->addMethodAction('SomeActionHandler', 'otherAction');
$this->assertSame($someAction, $this->registry->getActionRepresenter($someAction->getClass()));
$actions = $this->registrar->getRepresenter()->getActions(new \DateTime());
$this->assertCount(2, $actions);
$this->assertEquals($someAction->getClass(), $actions[0]->getClass());
$this->assertEquals($otherAction->getClass(), $actions[1]->getClass());

Control visible actions with predicate

$ifInFuture = function (\DateTime $d) {
    return $d > new \DateTime();
};
$ifInPast = function (\DateTime $d) {
    return $d < new \DateTime();
};
$someAction = $this->registrar->addMethodAction('SomeActionHandler', 'someAction', $ifInFuture);
$otherAction = $this->registrar->addMethodAction('SomeActionHandler', 'otherAction', $ifInPast);
$actions = $this->registrar->getRepresenter()->getActions(new \DateTime('1 hour ago'));
$this->assertCount(1, $actions);
$this->assertEquals($otherAction->getClass(), $actions[0]->getClass());
$actions = $this->registrar->getRepresenter()->getActions(new \DateTime('1 hour'));
$this->assertCount(1, $actions);
$this->assertEquals($someAction->getClass(), $actions[0]->getClass());

Generate arguments

$this->registrar->addMethodAction('SomeActionHandler', 'someAction', null, function (\DateTime $d) {
    return array('time' => $d->format('Y-m-d'));
});
$actions = $this->registrar->getRepresenter()->getActions(new \DateTime('2001-02-03'));
$this->assertEquals(array('time' => '2001-02-03'), $actions[0]->getArguments()->toArray());

Default arguments from property

Given the class 'EntityWithIdentifierProperty' with the body 'public $id = 73;'
$this->registrar->addMethodAction('SomeActionHandler', 'someAction');

* @noinspection PhpUndefinedClassInspection

$actions = $this->registrar->getRepresenter()->getActions(new \EntityWithIdentifierProperty());
$this->assertEquals(array('id' => 73), $actions[0]->getArguments()->toArray());

Default arguments from getter

Given the class 'EntityWithIdentifierGetter' with the body 'public function getId() { return 42; }'
$this->registrar->addMethodAction('SomeActionHandler', 'someAction');

* @noinspection PhpUndefinedClassInspection

$actions = $this->registrar->getRepresenter()->getActions(new \EntityWithIdentifierGetter());
$this->assertEquals(array('id' => 42), $actions[0]->getArguments()->toArray());