Completed
Push — master ( b96c3f...17a0ce )
by Florian
15s queued 11s
created

Chain::fill()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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