Matcher::matchAndRun()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
eloc 1
nc 2
nop 2
1
<?php
2
3
namespace Ghc\Rosetta\Matchers;
4
5
use Ghc\Rosetta\Configurable;
6
use Ghc\Rosetta\Pipeline;
7
use Ghc\Rosetta\Pipes\Pipeable;
8
use Illuminate\Support\Arr;
9
10
abstract class Matcher implements Pipeable
11
{
12
    use Configurable;
13
14
    /**
15
     * @var array
16
     */
17
    protected $data;
18
19
    /**
20
     * Matcher constructor.
21
     *
22
     * @param $data
23
     * @param $config
24
     */
25
    public function __construct($data = null, $config = [])
26
    {
27
        $this->setData($data);
28
        $this->setConfig($config);
29
    }
30
31
    /**
32
     * @return mixed
33
     */
34
    public function getData()
35
    {
36
        return $this->data;
37
    }
38
39
    /**
40
     * @param mixed $data
41
     *
42
     * @return self
43
     */
44
    public function setData($data)
45
    {
46
        $this->data = $data;
47
48
        return $this;
49
    }
50
51
    /**
52
     * @return bool
53
     */
54
    abstract public function match();
55
56
    /**
57
     * @param \Closure $true
58
     * @param \Closure $false
59
     *
60
     * @return mixed
61
     */
62
    public function matchAndRun($true, $false)
63
    {
64
        return $this->match() ? $true() : $false();
65
    }
66
67
    public function pipe($options = [])
68
    {
69
        return function ($inputData) use ($options) {
70
            $true = Arr::get($options, 'true', new Pipeline());
71
            $true = $true instanceof \Closure ? $true() : $true;
72
            $false = Arr::get($options, 'false', new Pipeline());
73
            $false = $false instanceof \Closure ? $false() : $false;
74
75
            $this->setData($inputData);
76
77
            return $this->matchAndRun(
78
                function () use ($true, $inputData) {
79
                    /* @var Pipeline $true */
80
                    return $true->flow($inputData);
81
                },
82
                function () use ($false, $inputData) {
83
                    /* @var Pipeline $false */
84
                    return $false->flow($inputData);
85
                }
86
            );
87
        };
88
    }
89
}
90