Chain   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 55
c 2
b 0
f 0
dl 0
loc 106
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fill() 0 3 1
A createFromString() 0 16 3
A create() 0 3 1
1
<?php
2
3
namespace Cocur\Chain;
4
5
use Cocur\Chain\Link\ChangeKeyCase;
6
use Cocur\Chain\Link\Combine;
7
use Cocur\Chain\Link\Count;
8
use Cocur\Chain\Link\CountValues;
9
use Cocur\Chain\Link\Diff;
10
use Cocur\Chain\Link\Every;
11
use Cocur\Chain\Link\Filter;
12
use Cocur\Chain\Link\First;
13
use Cocur\Chain\Link\Find;
14
use Cocur\Chain\Link\FlatMap;
15
use Cocur\Chain\Link\Flip;
16
use Cocur\Chain\Link\Includes;
17
use Cocur\Chain\Link\Intersect;
18
use Cocur\Chain\Link\IntersectAssoc;
19
use Cocur\Chain\Link\IntersectKey;
20
use Cocur\Chain\Link\Join;
21
use Cocur\Chain\Link\KeyExists;
22
use Cocur\Chain\Link\Keys;
23
use Cocur\Chain\Link\Last;
24
use Cocur\Chain\Link\Map;
25
use Cocur\Chain\Link\Merge;
26
use Cocur\Chain\Link\Pad;
27
use Cocur\Chain\Link\Pop;
28
use Cocur\Chain\Link\Product;
29
use Cocur\Chain\Link\Push;
30
use Cocur\Chain\Link\Rand;
31
use Cocur\Chain\Link\Reduce;
32
use Cocur\Chain\Link\Replace;
33
use Cocur\Chain\Link\Reverse;
34
use Cocur\Chain\Link\Search;
35
use Cocur\Chain\Link\Shift;
36
use Cocur\Chain\Link\Shuffle;
37
use Cocur\Chain\Link\Slice;
38
use Cocur\Chain\Link\Some;
39
use Cocur\Chain\Link\Sort;
40
use Cocur\Chain\Link\SortKeys;
41
use Cocur\Chain\Link\Splice;
42
use Cocur\Chain\Link\Sum;
43
use Cocur\Chain\Link\Unique;
44
use Cocur\Chain\Link\Unshift;
45
use Cocur\Chain\Link\Values;
46
use Countable;
47
48
/**
49
 * Chain.
50
 *
51
 * @author    Florian Eckerstorfer
52
 * @copyright 2015-2017 Florian Eckerstorfer
53
 */
54
class Chain extends AbstractChain implements Countable
55
{
56
    use ChangeKeyCase;
57
    use Combine;
58
    use Count;
59
    use CountValues;
60
    use Diff;
61
    use Every;
62
    use Filter;
63
    use Find;
64
    use First;
65
    use FlatMap;
66
    use Flip;
67
    use Includes;
68
    use Intersect;
69
    use IntersectAssoc;
70
    use IntersectKey;
71
    use Join;
72
    use KeyExists;
73
    use Keys;
74
    use Last;
75
    use Map;
76
    use Merge;
77
    use Pad;
78
    use Pop;
79
    use Product;
80
    use Push;
81
    use Rand;
82
    use Reduce;
83
    use Replace;
84
    use Reverse;
85
    use Search;
86
    use Shift;
87
    use Shuffle;
88
    use Slice;
89
    use Some;
90
    use Sort;
91
    use SortKeys;
92
    use Splice;
93
    use Sum;
94
    use Unique;
95
    use Unshift;
96
    use Values;
97
98
    /**
99
     * @param array $array
100
     */
101 2
    public function __construct(array $array = [])
102
    {
103 2
        $this->array = $array;
104 2
    }
105
106
    /**
107
     * @param array $array
108
     *
109
     * @return self
110
     */
111 1
    public static function create(array $array = []): self
112
    {
113 1
        return new static($array);
114
    }
115
116
    /**
117
     * @param string $delimiter If the option `regexp` is `true` this must be a regular expression
118
     * @param string $string
119
     * @param array  $options   if the option `regexp` is `true` the string is split by using `preg_split()`, otherwise
120
     *                          `explode()` is used
121
     *
122
     * @return self
123
     *
124
     * @throws \InvalidArgumentException if delimiter is an invalid regular exception
125
     */
126 3
    public static function createFromString(string $delimiter, string $string, array $options = []): self
127
    {
128 3
        $options = array_merge(['regexp' => false], $options);
129 3
        $chain   = new static();
130
131 3
        if ($options['regexp']) {
132 2
            $split = @preg_split($delimiter, $string);
133 2
            if (false === $split) {
134 1
                throw new \InvalidArgumentException('Invalid pattern "'.$delimiter.'"');
135
            }
136 1
            $chain->array = $split;
137
        } else {
138 1
            $chain->array = explode($delimiter, $string);
139
        }
140
141 2
        return $chain;
142
    }
143
144
    /**
145
     * Create a new Chain and fill with values.
146
     *
147
     * Creates a new Chain and fills the array with `num` entries of the value of `value` parameters, keys starting
148
     * at the `startIndex` parameter.
149
     *
150
     * @param int   $startIndex The first index of the array. If `startIndex` is negative, the first index of the
151
     *                          returned array will be `startIndex` and the following indices will start from zero.
152
     * @param int   $num        Number of elements to insert. Must be greater than or equal to zero.
153
     * @param mixed $value      value to use for filling
154
     *
155
     * @return self
156
     */
157 1
    public static function fill(int $startIndex, int $num, $value = null): self
158
    {
159 1
        return new self(array_fill($startIndex, $num, $value));
160
    }
161
}
162