RowProcessor::addProcessor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Format\Processor;
15
16
trait RowProcessor
17
{
18
    /**
19
     * @var callable[]
20
     */
21
    protected $processors = [];
22
23
    /**
24
     * Add a processor
25
     *
26
     * @param callable $processor
27
     *
28
     * @return static
29
     */
30 33
    public function addProcessor(callable $processor)
31
    {
32 33
        $this->processors[] = $processor;
33
34 33
        return $this;
35
    }
36
37
    /**
38
     * Go through all the processors
39
     *
40
     * @param array $row
41
     *
42
     * @return array
43
     */
44 20
    public function process(array $row)
45
    {
46 20
        foreach ($this->processors as $processor) {
47 20
            $row = call_user_func($processor, $row);
48
        }
49
50 20
        return $row;
51
    }
52
53
    /**
54
     * Remove a processor
55
     *
56
     * @param callable $processor
57
     *
58
     * @return static
59
     */
60 1
    public function removeProcessor(callable $processor)
61
    {
62 1
        $res = array_search($processor, $this->processors, true);
63 1
        if ($res !== false) {
64 1
            unset($this->processors[$res]);
65
        }
66
67 1
        return $this;
68
    }
69
70
    /**
71
     * Check if a processor has already been added
72
     *
73
     * @param callable $processor
74
     *
75
     * @return bool
76
     */
77 2
    public function hasProcessor(callable $processor)
78
    {
79 2
        return array_search($processor, $this->processors, true) !== false;
80
    }
81
}
82