Completed
Push — master ( dde30f...3b3524 )
by Cristiano
02:10
created

ArrayConversionsPart::split()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the Phootwork package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @license MIT License
8
 * @copyright Thomas Gossmann
9
 */
10
namespace phootwork\lang\parts;
11
12
use phootwork\lang\ArrayObject;
13
use phootwork\lang\Text;
14
15
/**
16
 * Text methods for array/ArrayObject conversions
17
 *
18
 * @author Thomas Gossmann
19
 * @author Cristiano Cinotti
20
 */
21
trait ArrayConversionsPart {
22
	abstract protected function getString(): string;
23
24
	/**
25
	 * Verify that $value is positive, otherwise throws an exception.
26
	 *
27
	 * @param int    $value
28
	 * @param string $name
29
	 *
30
	 * @throws \InvalidArgumentException
31
	 */
32
	abstract protected function verifyPositive(int $value, string $name): void;
33
34
	/**
35
	 * Splits the string by string
36
	 *
37
	 * @param string $delimiter The boundary string.
38
	 * @param int $limit
39
	 * 		If limit is set and positive, the returned array will contain a maximum of
40
	 * 		limit elements with the last element containing the rest of string.
41
	 *
42
	 * 		If the limit parameter is negative, all components except the last
43
	 * 		-limit are returned.
44
	 *
45
	 * 		If the limit parameter is zero, then this is treated as 1.
46
	 *
47
	 * @throws \InvalidArgumentException If the delimiter is an empty string.
48
	 *
49
	 * @return ArrayObject
50
	 * 		Returns an array of strings created by splitting the string parameter on boundaries
51
	 * 		formed by the delimiter.
52
	 *
53
	 *        If delimiter contains a value that is not contained in string and a negative limit is used,
54
	 *        then an empty array will be returned, otherwise an array containing string will be returned.
55
	 *
56
	 */
57 21
	public function split(string $delimiter, int $limit = PHP_INT_MAX): ArrayObject {
58 21
		if ('' === $delimiter) {
59 1
			throw new \InvalidArgumentException("The delimiter can't be an empty string");
60
		}
61
62 20
		return new ArrayObject(explode($delimiter, $this->getString(), $limit));
63
	}
64
65
	/**
66
	 * Join array elements with a string
67
	 *
68
	 * @param array $pieces The array of strings to join.
69
	 * @param string $glue Defaults to an empty string.
70
	 * @param string $encoding the desired encoding
71
	 *
72
	 * @return Text
73
	 * 		Returns a string containing a string representation of all the array elements in the
74
	 * 		same order, with the glue string between each element.
75
	 */
76 1
	public static function join(array $pieces, string $glue = '', ?string $encoding = null): Text {
77 1
		return new Text(implode($glue, $pieces), $encoding);
78
	}
79
80
	/**
81
	 * Convert the string to an array
82
	 *
83
	 * @param int $splitLength Maximum length of the chunk.
84
	 *
85
	 * @throws \InvalidArgumentException If splitLength is less than 1.
86
	 *
87
	 * @return ArrayObject
88
	 * 		If the optional splitLength parameter is specified, the returned array will be
89
	 * 		broken down into chunks with each being splitLength in length, otherwise each chunk
90
	 * 		will be one character in length.
91
	 *      If the split_length length exceeds the length of string, the entire string is returned
92
	 *      as the first (and only) array element.
93
	 */
94 3
	public function chunk(int $splitLength = 1): ArrayObject {
95 3
		$this->verifyPositive($splitLength, 'The chunk length');
96
97 1
		return new ArrayObject(str_split($this->getString(), $splitLength));
98
	}
99
}
100