forked from bartosz-maciaszek/chain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_1.php
85 lines (71 loc) · 1.58 KB
/
example_1.php
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
use BM\Chain\Chain;
use BM\Chain\ChainContextInterface;
use BM\Chain\ProcessorsQueue;
require '../vendor/autoload.php';
class Context implements ChainContextInterface
{
/**
* @var string
*/
private $string = '';
/**
* @return string
*/
public function getString(): string
{
return $this->string;
}
/**
* @param string $string
*/
public function setString(string $string)
{
$this->string = $string;
}
}
class Appender
{
/**
* @var string
*/
private $fragment;
/**
* Appender constructor.
* @param string $fragment
*/
public function __construct(string $fragment)
{
$this->fragment = $fragment;
}
/**
* @param Context $context
* @param callable $next
*/
public function __invoke(Context $context, callable $next)
{
$context->setString($context->getString() . $this->fragment);
$next($context);
}
}
$rot13 = function (Context $context, callable $next) {
$context->setString(str_rot13($context->getString()));
$next($context);
};
$exclamation = function (Context $context, callable $next) {
$context->setString($context->getString() . '!');
$next($context);
};
$queue = new ProcessorsQueue(
new Appender(', beautiful'),
new Appender(' world'),
$exclamation,
$exclamation,
$exclamation,
$rot13
);
$context = new Context();
$context->setString('Hello');
$chain = new Chain($queue);
$chain->execute($context);
var_dump($context->getString()); // Uryyb, ornhgvshy jbeyq!!!"