Completed
Push — master ( 5fca22...233410 )
by Emily
01:43
created

CallbackTransformation   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 2
dl 0
loc 30
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A call() 0 5 1
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Model\Collection\Transformation;
16
17
use OuterIterator;
18
use Spaark\CompositeUtils\Model\Collection\IterableIterator;
19
use Spaark\CompositeUtils\Model\Collection\OuterIteratorTrait;
20
21
/**
22
 * A CallbackTransformation is a Transformation which accepts some form
23
 * of callback during instanciation which is used during the
24
 * transformation
25
 *
26
 * Notable and common examples are the map and filter transformations.
27
 * Something like a splice transformation, for example, does NOT use a
28
 * callback.
29
 */
30
abstract class CallbackTransformation extends Transformation
31
{
32
    /**
33
     * @construct required
34
     */
35
    protected $transform;
36
37
    /**
38
     * Constructs the CallbackTransformation with the iterable original
39
     * data and a callback
40
     */
41 1
    public function __construct(iterable $iterator, callable $cb)
42
    {
43 1
        parent::__construct($iterator);
44
45 1
        $this->transform = $cb;
46 1
    }
47
48
    /**
49
     * Calls the callback with the current value and key, returning its
50
     * return value unmodified
51
     *
52
     * @return mixed
53
     */
54 1
    protected function call()
55
    {
56 1
        $cb = $this->transform;
57 1
        return $cb($this->iterator->current(), $this->iterator->key());
58
    }
59
}
60