|
1
|
|
|
<?php namespace Gears\String\Methods; |
|
2
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
3
|
|
|
// __________ __ ________ __________ |
|
4
|
|
|
// \______ \ |__ ______ / _____/ ____ _____ ______\______ \ _______ ___ |
|
5
|
|
|
// | ___/ | \\____ \/ \ ____/ __ \\__ \\_ __ \ | _// _ \ \/ / |
|
6
|
|
|
// | | | Y \ |_> > \_\ \ ___/ / __ \| | \/ | ( <_> > < |
|
7
|
|
|
// |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \ |
|
8
|
|
|
// \/|__| \/ \/ \/ \/ \/ |
|
9
|
|
|
// ----------------------------------------------------------------------------- |
|
10
|
|
|
// Designed and Developed by Brad Jones <brad @="bjc.id.au" /> |
|
11
|
|
|
// ----------------------------------------------------------------------------- |
|
12
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
13
|
|
|
|
|
14
|
|
|
use voku\helper\UTF8; |
|
15
|
|
|
|
|
16
|
|
|
trait Pad |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Pads the string to a given length with $padStr. |
|
20
|
|
|
* |
|
21
|
|
|
* If length is less than or equal to the length of the string, no padding |
|
22
|
|
|
* takes places. The default string used for padding is a space, and the |
|
23
|
|
|
* default type (one of 'left', 'right', 'both') is 'right'. |
|
24
|
|
|
* |
|
25
|
|
|
* @param int $length Desired string length after padding. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $padStr String used to pad, defaults to space. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $padType One of 'left', 'right', 'both'. |
|
30
|
|
|
* |
|
31
|
|
|
* @return static String after being padded. |
|
32
|
|
|
* |
|
33
|
|
|
* @throws \InvalidArgumentException If $padType isn't one of 'right', |
|
34
|
|
|
* 'left' or 'both'. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function pad($length, $padStr = ' ', $padType = 'right') |
|
37
|
|
|
{ |
|
38
|
|
|
if (!in_array($padType, ['left', 'right', 'both'], true)) |
|
39
|
|
|
{ |
|
40
|
|
|
throw new \InvalidArgumentException |
|
41
|
|
|
( |
|
42
|
|
|
'Pad expects $padType '."to be one of 'left', 'right' or 'both'" |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
switch ($padType) |
|
47
|
|
|
{ |
|
48
|
|
|
case 'left': |
|
49
|
|
|
return $this->padLeft($length, $padStr); |
|
50
|
|
|
|
|
51
|
|
|
case 'right': |
|
52
|
|
|
return $this->padRight($length, $padStr); |
|
53
|
|
|
|
|
54
|
|
|
default: |
|
55
|
|
|
return $this->padBoth($length, $padStr); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Adds padding to the left of the string. |
|
61
|
|
|
* |
|
62
|
|
|
* @param int $length Desired string length after padding. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $padStr String used to pad, defaults to space. |
|
65
|
|
|
* |
|
66
|
|
|
* @return static String with left padding. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function padLeft($length, $padStr = ' ') |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->applyPadding($length - $this->getLength(), 0, $padStr); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Adds padding to the right of the string. |
|
75
|
|
|
* |
|
76
|
|
|
* @param int $length Desired string length after padding. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $padStr String used to pad, defaults to space. |
|
79
|
|
|
* |
|
80
|
|
|
* @return static String with right padding. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function padRight($length, $padStr = ' ') |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->applyPadding(0, $length - $this->getLength(), $padStr); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Adds padding to both sides of the string, equally. |
|
89
|
|
|
* |
|
90
|
|
|
* @param int $length Desired string length after padding. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $padStr String used to pad, defaults to space. |
|
93
|
|
|
* |
|
94
|
|
|
* @return static String with padding applied to both sides. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function padBoth($length, $padStr = ' ') |
|
97
|
|
|
{ |
|
98
|
|
|
$padding = $length - $this->getLength(); |
|
99
|
|
|
return $this->applyPadding(floor($padding / 2), ceil($padding / 2), $padStr); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Pad internal helper, adds padding to the left and right of the string. |
|
104
|
|
|
* |
|
105
|
|
|
* @param int $left Length of left padding. |
|
106
|
|
|
* |
|
107
|
|
|
* @param int $right Length of right padding. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $padStr String used to pad, default is a space. |
|
110
|
|
|
* |
|
111
|
|
|
* @return static String with padding applied. |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function applyPadding($left = 0, $right = 0, $padStr = ' ') |
|
114
|
|
|
{ |
|
115
|
|
|
$length = UTF8::strlen($padStr, $this->encoding); |
|
116
|
|
|
$strLength = $this->getLength(); |
|
117
|
|
|
$paddedLength = $strLength + $left + $right; |
|
118
|
|
|
|
|
119
|
|
|
if (!$length || $paddedLength <= $strLength) return $this; |
|
120
|
|
|
|
|
121
|
|
|
$leftPadding = UTF8::substr |
|
122
|
|
|
( |
|
123
|
|
|
UTF8::str_repeat($padStr, ceil($left / $length)), |
|
124
|
|
|
0, |
|
125
|
|
|
$left, |
|
126
|
|
|
$this->encoding |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
$rightPadding = UTF8::substr |
|
130
|
|
|
( |
|
131
|
|
|
UTF8::str_repeat($padStr, ceil($right / $length)), |
|
132
|
|
|
0, |
|
133
|
|
|
$right, |
|
134
|
|
|
$this->encoding |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
return $this->newSelf($leftPadding.$this->scalarString.$rightPadding); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|