1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* NextFlow (http://github.com/nextflow) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/nextflow/nextflow-php for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2014-2016 NextFlow (http://github.com/nextflow) |
7
|
|
|
* @license https://raw.github.com/nextflow/nextflow-php/master/LICENSE MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace NextFlow\Regex\Action; |
11
|
|
|
|
12
|
|
|
use NextFlow\Core\Action\AbstractAction; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A replace action that replaces a string with another string based on a regular expression. |
16
|
|
|
*/ |
17
|
|
|
final class ReplaceAction extends AbstractAction |
18
|
|
|
{ |
19
|
|
|
/** The output socket. */ |
20
|
|
|
const SOCKET_OUTPUT = 'out'; |
21
|
|
|
|
22
|
|
|
/** The pattern variable socket. */ |
23
|
|
|
const SOCKET_PATTERN = 'pattern'; |
24
|
|
|
|
25
|
|
|
/** The replacement variable socket. */ |
26
|
|
|
const SOCKET_REPLACEMENT = 'replacement'; |
27
|
|
|
|
28
|
|
|
/** The subject variable socket. */ |
29
|
|
|
const SOCKET_SUBJECT = 'subject'; |
30
|
|
|
|
31
|
|
|
/** The result variable socket. */ |
32
|
|
|
const SOCKET_RESULT = 'result'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initializes a new instance of this class. |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function __construct() |
38
|
|
|
{ |
39
|
|
|
parent::__construct(); |
40
|
|
|
|
41
|
|
|
$this->createSocket(self::SOCKET_OUTPUT); |
42
|
|
|
$this->createSocket(self::SOCKET_PATTERN); |
43
|
|
|
$this->createSocket(self::SOCKET_REPLACEMENT); |
44
|
|
|
$this->createSocket(self::SOCKET_SUBJECT); |
45
|
|
|
$this->createSocket(self::SOCKET_RESULT); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Executes the node's logic. |
50
|
|
|
*/ |
51
|
|
|
public function execute() |
52
|
|
|
{ |
53
|
|
|
$pattern = $this->getSocket(self::SOCKET_PATTERN)->getNode(0); |
54
|
|
|
if ($pattern === null || $pattern->getValue() === null) { |
55
|
|
|
throw new \InvalidArgumentException('No pattern provided.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$replacement = $this->getSocket(self::SOCKET_REPLACEMENT)->getNode(0); |
59
|
|
|
if ($replacement === null || $replacement->getValue() === null) { |
60
|
|
|
throw new \InvalidArgumentException('No replacement provided.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$subject = $this->getSocket(self::SOCKET_SUBJECT)->getNode(0); |
64
|
|
|
if ($subject === null || $subject->getValue() === null) { |
65
|
|
|
throw new \InvalidArgumentException('No subject provided.'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$resultVar = $this->getSocket(self::SOCKET_RESULT)->getNode(0); |
69
|
|
|
if ($resultVar === null) { |
70
|
|
|
throw new \InvalidArgumentException('No result variable provided.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$value = preg_replace($pattern->getValue(), $replacement->getValue(), $subject->getValue()); |
74
|
|
|
|
75
|
|
|
$resultVar->setValue($value); |
76
|
|
|
|
77
|
|
|
$this->activate(self::SOCKET_OUTPUT); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|