-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
49 lines (37 loc) · 1.17 KB
/
Copy pathexample.php
File metadata and controls
49 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
require_once __DIR__ . '/vendor/autoload.php';
class DoubleAction extends \Actionator\Action
{
private float $number;
public function __construct(float $number)
{
$this->number = $number;
}
public function instruction()
{
return $this->number * 2;
}
}
class RoundFormat extends \Actionator\Format\FormatInterface
{
private float $result;
public function __construct(float $result)
{
$this->result = $result;
}
public function formattedResult()
{
return round($this->result);
}
}
$actionFactory = new \Actionator\Factory\ActionFactory();
$doubleAction = $actionFactory->make(DoubleAction::class, ['number' => 2.1]);
$doubleAction->done(); // returns false
$doubleAction->execute(); // if called more then one throws LogicException
$doubleAction->done(); // returns true
$result = $doubleAction->result(); // if called before execute throws LogicException
print $result; // 4.2
$result = $doubleAction->result(fn($result) => round($result)); // result with callback formatter
print $result; // 4
$result = $doubleAction->result(RoundFormat::class); // result with class formatter
print $result; // 4