@@ -82,7 +82,7 @@ |
||
82 | 82 | /** |
83 | 83 | * getLastCommand returns the string last used by a previous call to `run()`. |
84 | 84 | * |
85 | - * @return string|null |
|
85 | + * @return string |
|
86 | 86 | */ |
87 | 87 | public function getLastCommand() { |
88 | 88 | return $this->lastCommand; |
@@ -14,174 +14,174 @@ |
||
14 | 14 | * @see \nochso\Omni\OS::hasBinary Check if the binary/command is available before you run it. |
15 | 15 | */ |
16 | 16 | class Exec { |
17 | - /** |
|
18 | - * @var string[] |
|
19 | - */ |
|
20 | - private $prefixes; |
|
21 | - /** |
|
22 | - * @var string[] |
|
23 | - */ |
|
24 | - private $output; |
|
25 | - /** |
|
26 | - * @var int |
|
27 | - */ |
|
28 | - private $status; |
|
29 | - /** |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - private $lastCommand; |
|
17 | + /** |
|
18 | + * @var string[] |
|
19 | + */ |
|
20 | + private $prefixes; |
|
21 | + /** |
|
22 | + * @var string[] |
|
23 | + */ |
|
24 | + private $output; |
|
25 | + /** |
|
26 | + * @var int |
|
27 | + */ |
|
28 | + private $status; |
|
29 | + /** |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + private $lastCommand; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Create a new callable `Exec` object. |
|
36 | - * |
|
37 | - * @param string[] $prefixes,... Optional arguments will always be added to the beginning of the command. |
|
38 | - * |
|
39 | - * @return \nochso\Omni\Exec |
|
40 | - */ |
|
41 | - public static function create(...$prefixes) { |
|
42 | - $exec = new self(); |
|
43 | - $exec->prefixes = $prefixes; |
|
44 | - return $exec; |
|
45 | - } |
|
34 | + /** |
|
35 | + * Create a new callable `Exec` object. |
|
36 | + * |
|
37 | + * @param string[] $prefixes,... Optional arguments will always be added to the beginning of the command. |
|
38 | + * |
|
39 | + * @return \nochso\Omni\Exec |
|
40 | + */ |
|
41 | + public static function create(...$prefixes) { |
|
42 | + $exec = new self(); |
|
43 | + $exec->prefixes = $prefixes; |
|
44 | + return $exec; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Run a command with auto-escaped arguments. |
|
49 | - * |
|
50 | - * @param string[] $arguments,... Optional arguments will be added after the prefixes. |
|
51 | - * |
|
52 | - * @return $this |
|
53 | - */ |
|
54 | - public function run(...$arguments) { |
|
55 | - $this->lastCommand = $this->getCommand(...$arguments); |
|
56 | - exec($this->lastCommand, $output, $status); |
|
57 | - $this->output = $output; |
|
58 | - $this->status = $status; |
|
59 | - return $this; |
|
60 | - } |
|
47 | + /** |
|
48 | + * Run a command with auto-escaped arguments. |
|
49 | + * |
|
50 | + * @param string[] $arguments,... Optional arguments will be added after the prefixes. |
|
51 | + * |
|
52 | + * @return $this |
|
53 | + */ |
|
54 | + public function run(...$arguments) { |
|
55 | + $this->lastCommand = $this->getCommand(...$arguments); |
|
56 | + exec($this->lastCommand, $output, $status); |
|
57 | + $this->output = $output; |
|
58 | + $this->status = $status; |
|
59 | + return $this; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * getCommand returns the string to be used by `\exec()`. |
|
64 | - * |
|
65 | - * @param string[] $arguments,... |
|
66 | - * |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - public function getCommand(...$arguments) { |
|
70 | - $command = []; |
|
71 | - $allArguments = array_merge($this->prefixes, $arguments); |
|
72 | - if (count($allArguments) === 0) { |
|
73 | - $allArguments[] = ''; |
|
74 | - } |
|
75 | - foreach ($allArguments as $argument) { |
|
76 | - $command[] = $this->escapeArgument($argument); |
|
77 | - } |
|
78 | - $commandString = implode(' ', $command); |
|
79 | - return $commandString; |
|
80 | - } |
|
62 | + /** |
|
63 | + * getCommand returns the string to be used by `\exec()`. |
|
64 | + * |
|
65 | + * @param string[] $arguments,... |
|
66 | + * |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + public function getCommand(...$arguments) { |
|
70 | + $command = []; |
|
71 | + $allArguments = array_merge($this->prefixes, $arguments); |
|
72 | + if (count($allArguments) === 0) { |
|
73 | + $allArguments[] = ''; |
|
74 | + } |
|
75 | + foreach ($allArguments as $argument) { |
|
76 | + $command[] = $this->escapeArgument($argument); |
|
77 | + } |
|
78 | + $commandString = implode(' ', $command); |
|
79 | + return $commandString; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * getLastCommand returns the string last used by a previous call to `run()`. |
|
84 | - * |
|
85 | - * @return string|null |
|
86 | - */ |
|
87 | - public function getLastCommand() { |
|
88 | - return $this->lastCommand; |
|
89 | - } |
|
82 | + /** |
|
83 | + * getLastCommand returns the string last used by a previous call to `run()`. |
|
84 | + * |
|
85 | + * @return string|null |
|
86 | + */ |
|
87 | + public function getLastCommand() { |
|
88 | + return $this->lastCommand; |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * getOutput of last execution. |
|
93 | - * |
|
94 | - * @return string[] |
|
95 | - */ |
|
96 | - public function getOutput() { |
|
97 | - return $this->output; |
|
98 | - } |
|
91 | + /** |
|
92 | + * getOutput of last execution. |
|
93 | + * |
|
94 | + * @return string[] |
|
95 | + */ |
|
96 | + public function getOutput() { |
|
97 | + return $this->output; |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * getStatus code of last execution. |
|
102 | - * |
|
103 | - * @return int |
|
104 | - */ |
|
105 | - public function getStatus() { |
|
106 | - return $this->status; |
|
107 | - } |
|
100 | + /** |
|
101 | + * getStatus code of last execution. |
|
102 | + * |
|
103 | + * @return int |
|
104 | + */ |
|
105 | + public function getStatus() { |
|
106 | + return $this->status; |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * __invoke allows using this object as a callable by calling `run()`. |
|
111 | - * |
|
112 | - * e.g. `$runner('argument');` |
|
113 | - * |
|
114 | - * @param array $arguments,... |
|
115 | - * |
|
116 | - * @return \nochso\Omni\Exec |
|
117 | - */ |
|
118 | - public function __invoke(...$arguments) { |
|
119 | - return $this->run(...$arguments); |
|
120 | - } |
|
109 | + /** |
|
110 | + * __invoke allows using this object as a callable by calling `run()`. |
|
111 | + * |
|
112 | + * e.g. `$runner('argument');` |
|
113 | + * |
|
114 | + * @param array $arguments,... |
|
115 | + * |
|
116 | + * @return \nochso\Omni\Exec |
|
117 | + */ |
|
118 | + public function __invoke(...$arguments) { |
|
119 | + return $this->run(...$arguments); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * @param string $argument |
|
124 | - * |
|
125 | - * @return string |
|
126 | - */ |
|
127 | - private function escapeArgument($argument) { |
|
128 | - // Always escape an empty argument so it doesn't get lost. |
|
129 | - if ($argument === '') { |
|
130 | - return escapeshellarg($argument); |
|
131 | - } |
|
132 | - if (!OS::isWindows()) { |
|
133 | - return $this->escapeLinuxArgument($argument); |
|
134 | - } |
|
135 | - return $this->escapeWindowsArgument($argument); |
|
136 | - } |
|
122 | + /** |
|
123 | + * @param string $argument |
|
124 | + * |
|
125 | + * @return string |
|
126 | + */ |
|
127 | + private function escapeArgument($argument) { |
|
128 | + // Always escape an empty argument so it doesn't get lost. |
|
129 | + if ($argument === '') { |
|
130 | + return escapeshellarg($argument); |
|
131 | + } |
|
132 | + if (!OS::isWindows()) { |
|
133 | + return $this->escapeLinuxArgument($argument); |
|
134 | + } |
|
135 | + return $this->escapeWindowsArgument($argument); |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * @param string $argument |
|
140 | - * |
|
141 | - * @return string |
|
142 | - */ |
|
143 | - private function escapeLinuxArgument($argument) { |
|
144 | - $escapedArgument = escapeshellarg($argument); |
|
145 | - // Is escaping really needed? |
|
146 | - if ($argument !== '--' && mb_substr($escapedArgument, 1, -1) === $argument && preg_match('/^[a-z0-9-]+$/i', $argument) === 1) { |
|
147 | - return $argument; |
|
148 | - } |
|
149 | - return $escapedArgument; |
|
150 | - } |
|
138 | + /** |
|
139 | + * @param string $argument |
|
140 | + * |
|
141 | + * @return string |
|
142 | + */ |
|
143 | + private function escapeLinuxArgument($argument) { |
|
144 | + $escapedArgument = escapeshellarg($argument); |
|
145 | + // Is escaping really needed? |
|
146 | + if ($argument !== '--' && mb_substr($escapedArgument, 1, -1) === $argument && preg_match('/^[a-z0-9-]+$/i', $argument) === 1) { |
|
147 | + return $argument; |
|
148 | + } |
|
149 | + return $escapedArgument; |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * @param string $argument |
|
154 | - * |
|
155 | - * @return string |
|
156 | - * |
|
157 | - * @link https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/ |
|
158 | - */ |
|
159 | - private function escapeWindowsArgument($argument) { |
|
160 | - // Check if there's anything to escape |
|
161 | - if (strpbrk($argument, " \t\n\v\"\\") === false) { |
|
162 | - return $argument; |
|
163 | - } |
|
164 | - $escapedArgument = '"'; |
|
165 | - $strlen = mb_strlen($argument); |
|
166 | - for ($i = 0; $i < $strlen; $i++) { |
|
167 | - $backslashes = 0; |
|
168 | - while ($i < $strlen && mb_substr($argument, $i, 1) === '\\') { |
|
169 | - $i++; |
|
170 | - $backslashes++; |
|
171 | - } |
|
172 | - if ($i === $strlen) { |
|
173 | - // Escape all backslashes, but let the terminating double quote be interpreted as a meta character |
|
174 | - $escapedArgument .= str_repeat('\\', $backslashes * 2); |
|
175 | - } elseif (mb_substr($argument, $i, 1) === '"') { |
|
176 | - // Escape all backslashes and the following quotation mark |
|
177 | - $escapedArgument .= str_repeat('\\', $backslashes * 2 + 1); |
|
178 | - $escapedArgument .= '"'; |
|
179 | - } else { |
|
180 | - $escapedArgument .= str_repeat('\\', $backslashes); |
|
181 | - $escapedArgument .= mb_substr($argument, $i, 1); |
|
182 | - } |
|
183 | - } |
|
184 | - $escapedArgument .= '"'; |
|
185 | - return $escapedArgument; |
|
186 | - } |
|
152 | + /** |
|
153 | + * @param string $argument |
|
154 | + * |
|
155 | + * @return string |
|
156 | + * |
|
157 | + * @link https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/ |
|
158 | + */ |
|
159 | + private function escapeWindowsArgument($argument) { |
|
160 | + // Check if there's anything to escape |
|
161 | + if (strpbrk($argument, " \t\n\v\"\\") === false) { |
|
162 | + return $argument; |
|
163 | + } |
|
164 | + $escapedArgument = '"'; |
|
165 | + $strlen = mb_strlen($argument); |
|
166 | + for ($i = 0; $i < $strlen; $i++) { |
|
167 | + $backslashes = 0; |
|
168 | + while ($i < $strlen && mb_substr($argument, $i, 1) === '\\') { |
|
169 | + $i++; |
|
170 | + $backslashes++; |
|
171 | + } |
|
172 | + if ($i === $strlen) { |
|
173 | + // Escape all backslashes, but let the terminating double quote be interpreted as a meta character |
|
174 | + $escapedArgument .= str_repeat('\\', $backslashes * 2); |
|
175 | + } elseif (mb_substr($argument, $i, 1) === '"') { |
|
176 | + // Escape all backslashes and the following quotation mark |
|
177 | + $escapedArgument .= str_repeat('\\', $backslashes * 2 + 1); |
|
178 | + $escapedArgument .= '"'; |
|
179 | + } else { |
|
180 | + $escapedArgument .= str_repeat('\\', $backslashes); |
|
181 | + $escapedArgument .= mb_substr($argument, $i, 1); |
|
182 | + } |
|
183 | + } |
|
184 | + $escapedArgument .= '"'; |
|
185 | + return $escapedArgument; |
|
186 | + } |
|
187 | 187 | } |
@@ -26,122 +26,122 @@ |
||
26 | 26 | * as it uses different constructor parameters. |
27 | 27 | */ |
28 | 28 | final class VcsVersionInfo { |
29 | - /** |
|
30 | - * @var \nochso\Omni\VersionInfo |
|
31 | - */ |
|
32 | - private $version; |
|
33 | - /** |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - private $repositoryRoot; |
|
29 | + /** |
|
30 | + * @var \nochso\Omni\VersionInfo |
|
31 | + */ |
|
32 | + private $version; |
|
33 | + /** |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + private $repositoryRoot; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @param string $name Package or application name. |
|
40 | - * @param string $fallBackVersion Optional version to fall back on if no repository info was found. |
|
41 | - * @param string $repositoryRoot Path the VCS repository root (e.g. folder that contains ".git", ".hg", etc.) |
|
42 | - * @param string $infoFormat Optional format to use for `getInfo`. Defaults to `VersionInfo::INFO_FORMAT_DEFAULT` |
|
43 | - * |
|
44 | - * @throws \RuntimeException When no fallback was given and tag could not be extracted from a VCS repo. |
|
45 | - */ |
|
46 | - public function __construct( |
|
47 | - $name, |
|
48 | - $fallBackVersion = null, |
|
49 | - $repositoryRoot = '.', |
|
50 | - $infoFormat = \nochso\Omni\VersionInfo::INFO_FORMAT_SHORT |
|
51 | - ) { |
|
52 | - $this->repositoryRoot = $repositoryRoot; |
|
53 | - $tag = $this->extractTag(); |
|
54 | - if ($tag === null) { |
|
55 | - $tag = $fallBackVersion; |
|
56 | - } |
|
57 | - if ($tag === null) { |
|
58 | - throw new \RuntimeException( |
|
59 | - 'Unable to detect version from VCS repository and no fallback version was specified.' |
|
60 | - ); |
|
61 | - } |
|
62 | - $this->version = new VersionInfo($name, $tag, $infoFormat); |
|
63 | - } |
|
38 | + /** |
|
39 | + * @param string $name Package or application name. |
|
40 | + * @param string $fallBackVersion Optional version to fall back on if no repository info was found. |
|
41 | + * @param string $repositoryRoot Path the VCS repository root (e.g. folder that contains ".git", ".hg", etc.) |
|
42 | + * @param string $infoFormat Optional format to use for `getInfo`. Defaults to `VersionInfo::INFO_FORMAT_DEFAULT` |
|
43 | + * |
|
44 | + * @throws \RuntimeException When no fallback was given and tag could not be extracted from a VCS repo. |
|
45 | + */ |
|
46 | + public function __construct( |
|
47 | + $name, |
|
48 | + $fallBackVersion = null, |
|
49 | + $repositoryRoot = '.', |
|
50 | + $infoFormat = \nochso\Omni\VersionInfo::INFO_FORMAT_SHORT |
|
51 | + ) { |
|
52 | + $this->repositoryRoot = $repositoryRoot; |
|
53 | + $tag = $this->extractTag(); |
|
54 | + if ($tag === null) { |
|
55 | + $tag = $fallBackVersion; |
|
56 | + } |
|
57 | + if ($tag === null) { |
|
58 | + throw new \RuntimeException( |
|
59 | + 'Unable to detect version from VCS repository and no fallback version was specified.' |
|
60 | + ); |
|
61 | + } |
|
62 | + $this->version = new VersionInfo($name, $tag, $infoFormat); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - public function getInfo() { |
|
69 | - return $this->version->getInfo(); |
|
70 | - } |
|
65 | + /** |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + public function getInfo() { |
|
69 | + return $this->version->getInfo(); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - public function getVersion() { |
|
76 | - return $this->version->getVersion(); |
|
77 | - } |
|
72 | + /** |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + public function getVersion() { |
|
76 | + return $this->version->getVersion(); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return string |
|
81 | - */ |
|
82 | - public function getName() { |
|
83 | - return $this->version->getName(); |
|
84 | - } |
|
79 | + /** |
|
80 | + * @return string |
|
81 | + */ |
|
82 | + public function getName() { |
|
83 | + return $this->version->getName(); |
|
84 | + } |
|
85 | 85 | |
86 | - private function extractTag() { |
|
87 | - $tag = $this->readGit(); |
|
88 | - if ($tag === null) { |
|
89 | - $tag = $this->readMercurial(); |
|
90 | - } |
|
91 | - return $tag; |
|
92 | - } |
|
86 | + private function extractTag() { |
|
87 | + $tag = $this->readGit(); |
|
88 | + if ($tag === null) { |
|
89 | + $tag = $this->readMercurial(); |
|
90 | + } |
|
91 | + return $tag; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @return null|string |
|
96 | - */ |
|
97 | - private function readGit() { |
|
98 | - $gitDir = Path::combine($this->repositoryRoot, '.git'); |
|
99 | - if (!is_dir($gitDir) || !OS::hasBinary('git')) { |
|
100 | - return null; |
|
101 | - } |
|
102 | - $git = Exec::create('git', '--git-dir=' . $gitDir, '--work-tree=' . $this->repositoryRoot); |
|
103 | - $git->run('describe', '--tags', '--always', '--dirty'); |
|
104 | - return Dot::get($git->getOutput(), 0); |
|
105 | - } |
|
94 | + /** |
|
95 | + * @return null|string |
|
96 | + */ |
|
97 | + private function readGit() { |
|
98 | + $gitDir = Path::combine($this->repositoryRoot, '.git'); |
|
99 | + if (!is_dir($gitDir) || !OS::hasBinary('git')) { |
|
100 | + return null; |
|
101 | + } |
|
102 | + $git = Exec::create('git', '--git-dir=' . $gitDir, '--work-tree=' . $this->repositoryRoot); |
|
103 | + $git->run('describe', '--tags', '--always', '--dirty'); |
|
104 | + return Dot::get($git->getOutput(), 0); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * @return null|string |
|
109 | - */ |
|
110 | - private function readMercurial() { |
|
111 | - $hgDir = Path::combine($this->repositoryRoot, '.hg'); |
|
112 | - if (!is_dir($hgDir) || !OS::hasBinary('hg')) { |
|
113 | - return null; |
|
114 | - } |
|
115 | - $hg = Exec::create('hg', '--repository', $this->repositoryRoot); |
|
116 | - // Removes everything but the tag if distance is zero. |
|
117 | - $hg->run( |
|
118 | - 'log', |
|
119 | - '-r', |
|
120 | - '.', |
|
121 | - '--template', |
|
122 | - '{latesttag}{sub(\'^-0-m.*\', \'\', \'-{latesttagdistance}-m{node|short}\')}' |
|
123 | - ); |
|
124 | - $tag = Dot::get($hg->getOutput(), 0); |
|
125 | - // Actual null if no lines were returned or `hg log` returned actual "null". |
|
126 | - // Either way, need to fall back to the revision id. |
|
127 | - if ($tag === null || $tag === 'null' || Strings::startsWith($tag, 'null-')) { |
|
128 | - $hg->run('id', '-i'); |
|
129 | - $tag = Dot::get($hg->getOutput(), 0); |
|
130 | - // Remove 'dirty' plus from revision id |
|
131 | - $tag = rtrim($tag, '+'); |
|
132 | - } |
|
133 | - $summary = $hg->run('summary')->getOutput(); |
|
134 | - $isDirty = 0 === count( |
|
135 | - array_filter( |
|
136 | - $summary, |
|
137 | - function ($line) { |
|
138 | - return preg_match('/^commit: .*\(clean\)$/', $line) === 1; |
|
139 | - } |
|
140 | - ) |
|
141 | - ); |
|
142 | - if ($isDirty) { |
|
143 | - $tag .= '-dirty'; |
|
144 | - } |
|
145 | - return $tag; |
|
146 | - } |
|
107 | + /** |
|
108 | + * @return null|string |
|
109 | + */ |
|
110 | + private function readMercurial() { |
|
111 | + $hgDir = Path::combine($this->repositoryRoot, '.hg'); |
|
112 | + if (!is_dir($hgDir) || !OS::hasBinary('hg')) { |
|
113 | + return null; |
|
114 | + } |
|
115 | + $hg = Exec::create('hg', '--repository', $this->repositoryRoot); |
|
116 | + // Removes everything but the tag if distance is zero. |
|
117 | + $hg->run( |
|
118 | + 'log', |
|
119 | + '-r', |
|
120 | + '.', |
|
121 | + '--template', |
|
122 | + '{latesttag}{sub(\'^-0-m.*\', \'\', \'-{latesttagdistance}-m{node|short}\')}' |
|
123 | + ); |
|
124 | + $tag = Dot::get($hg->getOutput(), 0); |
|
125 | + // Actual null if no lines were returned or `hg log` returned actual "null". |
|
126 | + // Either way, need to fall back to the revision id. |
|
127 | + if ($tag === null || $tag === 'null' || Strings::startsWith($tag, 'null-')) { |
|
128 | + $hg->run('id', '-i'); |
|
129 | + $tag = Dot::get($hg->getOutput(), 0); |
|
130 | + // Remove 'dirty' plus from revision id |
|
131 | + $tag = rtrim($tag, '+'); |
|
132 | + } |
|
133 | + $summary = $hg->run('summary')->getOutput(); |
|
134 | + $isDirty = 0 === count( |
|
135 | + array_filter( |
|
136 | + $summary, |
|
137 | + function ($line) { |
|
138 | + return preg_match('/^commit: .*\(clean\)$/', $line) === 1; |
|
139 | + } |
|
140 | + ) |
|
141 | + ); |
|
142 | + if ($isDirty) { |
|
143 | + $tag .= '-dirty'; |
|
144 | + } |
|
145 | + return $tag; |
|
146 | + } |
|
147 | 147 | } |
@@ -99,7 +99,7 @@ |
||
99 | 99 | if (!is_dir($gitDir) || !OS::hasBinary('git')) { |
100 | 100 | return null; |
101 | 101 | } |
102 | - $git = Exec::create('git', '--git-dir=' . $gitDir, '--work-tree=' . $this->repositoryRoot); |
|
102 | + $git = Exec::create('git', '--git-dir='.$gitDir, '--work-tree='.$this->repositoryRoot); |
|
103 | 103 | $git->run('describe', '--tags', '--always', '--dirty'); |
104 | 104 | return Dot::get($git->getOutput(), 0); |
105 | 105 | } |
@@ -10,34 +10,34 @@ |
||
10 | 10 | * - easier debugging |
11 | 11 | */ |
12 | 12 | final class Type { |
13 | - /** |
|
14 | - * Summarize the type of any variable. |
|
15 | - * |
|
16 | - * @param mixed $value |
|
17 | - * |
|
18 | - * @return string Result of `get_class` for objects or `gettype` for anything else. |
|
19 | - * |
|
20 | - * @see gettype |
|
21 | - * @see get_class |
|
22 | - */ |
|
23 | - public static function summarize($value) { |
|
24 | - if (!is_object($value)) { |
|
25 | - return gettype($value); |
|
26 | - } |
|
27 | - return get_class($value); |
|
28 | - } |
|
13 | + /** |
|
14 | + * Summarize the type of any variable. |
|
15 | + * |
|
16 | + * @param mixed $value |
|
17 | + * |
|
18 | + * @return string Result of `get_class` for objects or `gettype` for anything else. |
|
19 | + * |
|
20 | + * @see gettype |
|
21 | + * @see get_class |
|
22 | + */ |
|
23 | + public static function summarize($value) { |
|
24 | + if (!is_object($value)) { |
|
25 | + return gettype($value); |
|
26 | + } |
|
27 | + return get_class($value); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * getClassName returns the class name without namespaces. |
|
32 | - * |
|
33 | - * @param object|string $object Object instance of a fully qualified name. |
|
34 | - * |
|
35 | - * @return string |
|
36 | - */ |
|
37 | - public static function getClassName($object) { |
|
38 | - if (is_object($object)) { |
|
39 | - $object = get_class($object); |
|
40 | - } |
|
41 | - return ltrim(substr($object, strrpos($object, '\\')), '\\'); |
|
42 | - } |
|
30 | + /** |
|
31 | + * getClassName returns the class name without namespaces. |
|
32 | + * |
|
33 | + * @param object|string $object Object instance of a fully qualified name. |
|
34 | + * |
|
35 | + * @return string |
|
36 | + */ |
|
37 | + public static function getClassName($object) { |
|
38 | + if (is_object($object)) { |
|
39 | + $object = get_class($object); |
|
40 | + } |
|
41 | + return ltrim(substr($object, strrpos($object, '\\')), '\\'); |
|
42 | + } |
|
43 | 43 | } |
@@ -24,85 +24,85 @@ |
||
24 | 24 | * ``` |
25 | 25 | */ |
26 | 26 | class PrettyPSR extends Config { |
27 | - /** |
|
28 | - * @param string|array $dirs |
|
29 | - * |
|
30 | - * @return static |
|
31 | - */ |
|
32 | - public static function createIn($dirs) { |
|
33 | - $config = new static(); |
|
34 | - $config->finder(DefaultFinder::createIn($dirs)); |
|
35 | - return $config; |
|
36 | - } |
|
27 | + /** |
|
28 | + * @param string|array $dirs |
|
29 | + * |
|
30 | + * @return static |
|
31 | + */ |
|
32 | + public static function createIn($dirs) { |
|
33 | + $config = new static(); |
|
34 | + $config->finder(DefaultFinder::createIn($dirs)); |
|
35 | + return $config; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @param string $name |
|
40 | - * @param string $description |
|
41 | - */ |
|
42 | - public function __construct($name = self::class, $description = '') { |
|
43 | - parent::__construct($name, $description); |
|
44 | - $this->level = FixerInterface::PSR2_LEVEL; |
|
45 | - $this->fixers = $this->getDefaultFixers(); |
|
46 | - $this->setUsingCache(true); |
|
47 | - } |
|
38 | + /** |
|
39 | + * @param string $name |
|
40 | + * @param string $description |
|
41 | + */ |
|
42 | + public function __construct($name = self::class, $description = '') { |
|
43 | + parent::__construct($name, $description); |
|
44 | + $this->level = FixerInterface::PSR2_LEVEL; |
|
45 | + $this->fixers = $this->getDefaultFixers(); |
|
46 | + $this->setUsingCache(true); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @return string[] |
|
51 | - */ |
|
52 | - public function getDefaultFixers() { |
|
53 | - return [ |
|
54 | - '-psr0', |
|
55 | - '-return', |
|
56 | - '-concat_without_spaces', |
|
57 | - '-empty_return', |
|
58 | - 'extra_empty_lines', |
|
59 | - 'concat_with_spaces', |
|
60 | - 'array_element_no_space_before_comma', |
|
61 | - 'array_element_white_space_after_comma', |
|
62 | - 'double_arrow_multiline_whitespaces', |
|
63 | - 'duplicate_semicolon', |
|
64 | - 'function_typehint_space', |
|
65 | - 'include', |
|
66 | - 'list_commas', |
|
67 | - 'multiline_array_trailing_comma', |
|
68 | - 'namespace_no_leading_whitespace', |
|
69 | - 'newline_after_open_tag', |
|
70 | - 'new_with_braces', |
|
71 | - 'no_blank_lines_after_class_opening', |
|
72 | - 'no_blank_lines_before_namespace', |
|
73 | - 'no_empty_lines_after_phpdocs', |
|
74 | - 'object_operator', |
|
75 | - 'operators_spaces', |
|
76 | - 'spaces_cast', |
|
77 | - 'ordered_use', |
|
78 | - 'phpdoc_indent', |
|
79 | - 'phpdoc_inline_tag', |
|
80 | - 'phpdoc_no_access', |
|
81 | - 'phpdoc_no_package', |
|
82 | - 'phpdoc_order', |
|
83 | - 'phpdoc_params', |
|
84 | - 'phpdoc_scalar', |
|
85 | - 'phpdoc_separation', |
|
86 | - 'phpdoc_short_description', |
|
87 | - 'phpdoc_to_comment', |
|
88 | - 'phpdoc_trim', |
|
89 | - 'phpdoc_types', |
|
90 | - 'phpdoc_type_to_var', |
|
91 | - 'print_to_echo', |
|
92 | - 'remove_lines_between_uses', |
|
93 | - 'self_accessor', |
|
94 | - 'short_array_syntax', |
|
95 | - 'single_array_no_trailing_comma', |
|
96 | - 'single_quote', |
|
97 | - 'spaces_before_semicolon', |
|
98 | - 'ternary_spaces', |
|
99 | - 'trim_array_spaces', |
|
100 | - 'unalign_double_arrow', |
|
101 | - 'unalign_equals', |
|
102 | - 'unary_operators_spaces', |
|
103 | - 'unneeded_control_parentheses', |
|
104 | - 'unused_use', |
|
105 | - 'whitespacy_lines', |
|
106 | - ]; |
|
107 | - } |
|
49 | + /** |
|
50 | + * @return string[] |
|
51 | + */ |
|
52 | + public function getDefaultFixers() { |
|
53 | + return [ |
|
54 | + '-psr0', |
|
55 | + '-return', |
|
56 | + '-concat_without_spaces', |
|
57 | + '-empty_return', |
|
58 | + 'extra_empty_lines', |
|
59 | + 'concat_with_spaces', |
|
60 | + 'array_element_no_space_before_comma', |
|
61 | + 'array_element_white_space_after_comma', |
|
62 | + 'double_arrow_multiline_whitespaces', |
|
63 | + 'duplicate_semicolon', |
|
64 | + 'function_typehint_space', |
|
65 | + 'include', |
|
66 | + 'list_commas', |
|
67 | + 'multiline_array_trailing_comma', |
|
68 | + 'namespace_no_leading_whitespace', |
|
69 | + 'newline_after_open_tag', |
|
70 | + 'new_with_braces', |
|
71 | + 'no_blank_lines_after_class_opening', |
|
72 | + 'no_blank_lines_before_namespace', |
|
73 | + 'no_empty_lines_after_phpdocs', |
|
74 | + 'object_operator', |
|
75 | + 'operators_spaces', |
|
76 | + 'spaces_cast', |
|
77 | + 'ordered_use', |
|
78 | + 'phpdoc_indent', |
|
79 | + 'phpdoc_inline_tag', |
|
80 | + 'phpdoc_no_access', |
|
81 | + 'phpdoc_no_package', |
|
82 | + 'phpdoc_order', |
|
83 | + 'phpdoc_params', |
|
84 | + 'phpdoc_scalar', |
|
85 | + 'phpdoc_separation', |
|
86 | + 'phpdoc_short_description', |
|
87 | + 'phpdoc_to_comment', |
|
88 | + 'phpdoc_trim', |
|
89 | + 'phpdoc_types', |
|
90 | + 'phpdoc_type_to_var', |
|
91 | + 'print_to_echo', |
|
92 | + 'remove_lines_between_uses', |
|
93 | + 'self_accessor', |
|
94 | + 'short_array_syntax', |
|
95 | + 'single_array_no_trailing_comma', |
|
96 | + 'single_quote', |
|
97 | + 'spaces_before_semicolon', |
|
98 | + 'ternary_spaces', |
|
99 | + 'trim_array_spaces', |
|
100 | + 'unalign_double_arrow', |
|
101 | + 'unalign_equals', |
|
102 | + 'unary_operators_spaces', |
|
103 | + 'unneeded_control_parentheses', |
|
104 | + 'unused_use', |
|
105 | + 'whitespacy_lines', |
|
106 | + ]; |
|
107 | + } |
|
108 | 108 | } |
@@ -8,74 +8,74 @@ |
||
8 | 8 | * DefaultFinder respects ignore files for Git, Mercurial and Darcs. |
9 | 9 | */ |
10 | 10 | class DefaultFinder extends Finder { |
11 | - /** |
|
12 | - * @param array|string $dirs |
|
13 | - * |
|
14 | - * @return \nochso\Omni\PhpCsFixer\DefaultFinder |
|
15 | - */ |
|
16 | - public static function createIn($dirs) { |
|
17 | - $finder = new self(); |
|
18 | - if (!is_array($dirs)) { |
|
19 | - $dirs = [$dirs]; |
|
20 | - } |
|
21 | - $finder->in($dirs); |
|
22 | - $excludes = []; |
|
23 | - foreach ($finder->getVcsIgnoreFiles($dirs) as $ignore) { |
|
24 | - $excludes = array_merge($excludes, $finder->readExcludeLines($ignore)); |
|
25 | - } |
|
26 | - $finder->exclude($excludes); |
|
27 | - return $finder; |
|
28 | - } |
|
11 | + /** |
|
12 | + * @param array|string $dirs |
|
13 | + * |
|
14 | + * @return \nochso\Omni\PhpCsFixer\DefaultFinder |
|
15 | + */ |
|
16 | + public static function createIn($dirs) { |
|
17 | + $finder = new self(); |
|
18 | + if (!is_array($dirs)) { |
|
19 | + $dirs = [$dirs]; |
|
20 | + } |
|
21 | + $finder->in($dirs); |
|
22 | + $excludes = []; |
|
23 | + foreach ($finder->getVcsIgnoreFiles($dirs) as $ignore) { |
|
24 | + $excludes = array_merge($excludes, $finder->readExcludeLines($ignore)); |
|
25 | + } |
|
26 | + $finder->exclude($excludes); |
|
27 | + return $finder; |
|
28 | + } |
|
29 | 29 | |
30 | - public function __construct() { |
|
31 | - parent::__construct(); |
|
32 | - foreach ($this->getNames() as $name) { |
|
33 | - $this->name($name); |
|
34 | - } |
|
35 | - $this->files()->ignoreDotFiles(true)->ignoreVCS(true); |
|
36 | - } |
|
30 | + public function __construct() { |
|
31 | + parent::__construct(); |
|
32 | + foreach ($this->getNames() as $name) { |
|
33 | + $this->name($name); |
|
34 | + } |
|
35 | + $this->files()->ignoreDotFiles(true)->ignoreVCS(true); |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @return array |
|
40 | - */ |
|
41 | - protected function getNames() { |
|
42 | - return ['*.php', '*.twig', '#.*.ya?ml#i', '*.xml']; |
|
43 | - } |
|
38 | + /** |
|
39 | + * @return array |
|
40 | + */ |
|
41 | + protected function getNames() { |
|
42 | + return ['*.php', '*.twig', '#.*.ya?ml#i', '*.xml']; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param array $dirs |
|
47 | - * |
|
48 | - * @return array |
|
49 | - */ |
|
50 | - protected function getVcsIgnoreFiles(array $dirs) { |
|
51 | - $files = ['.gitignore', '.hgignore', '_darcs/prefs/boring']; |
|
52 | - $filepaths = []; |
|
53 | - foreach ($dirs as $dir) { |
|
54 | - foreach ($files as $file) { |
|
55 | - $filepaths[] = Path::combine($dir, $file); |
|
56 | - } |
|
57 | - } |
|
58 | - return $filepaths; |
|
59 | - } |
|
45 | + /** |
|
46 | + * @param array $dirs |
|
47 | + * |
|
48 | + * @return array |
|
49 | + */ |
|
50 | + protected function getVcsIgnoreFiles(array $dirs) { |
|
51 | + $files = ['.gitignore', '.hgignore', '_darcs/prefs/boring']; |
|
52 | + $filepaths = []; |
|
53 | + foreach ($dirs as $dir) { |
|
54 | + foreach ($files as $file) { |
|
55 | + $filepaths[] = Path::combine($dir, $file); |
|
56 | + } |
|
57 | + } |
|
58 | + return $filepaths; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @param $ignoreFile |
|
63 | - * |
|
64 | - * @return array |
|
65 | - */ |
|
66 | - private function readExcludeLines($ignoreFile) { |
|
67 | - if (!file_exists($ignoreFile)) { |
|
68 | - return []; |
|
69 | - } |
|
70 | - $lines = file($ignoreFile); |
|
71 | - if ($lines === false) { |
|
72 | - // Not sure how to test, so ignore it instead of inlining it above |
|
73 | - return []; |
|
74 | - // @codeCoverageIgnore |
|
61 | + /** |
|
62 | + * @param $ignoreFile |
|
63 | + * |
|
64 | + * @return array |
|
65 | + */ |
|
66 | + private function readExcludeLines($ignoreFile) { |
|
67 | + if (!file_exists($ignoreFile)) { |
|
68 | + return []; |
|
69 | + } |
|
70 | + $lines = file($ignoreFile); |
|
71 | + if ($lines === false) { |
|
72 | + // Not sure how to test, so ignore it instead of inlining it above |
|
73 | + return []; |
|
74 | + // @codeCoverageIgnore |
|
75 | 75 | |
76 | - } |
|
77 | - return array_map(function ($line) { |
|
78 | - return rtrim($line, "\r\n\\/"); |
|
79 | - }, $lines); |
|
80 | - } |
|
76 | + } |
|
77 | + return array_map(function ($line) { |
|
78 | + return rtrim($line, "\r\n\\/"); |
|
79 | + }, $lines); |
|
80 | + } |
|
81 | 81 | } |
@@ -5,174 +5,174 @@ |
||
5 | 5 | * Dot allows easy access to multi-dimensional arrays using dot notation. |
6 | 6 | */ |
7 | 7 | final class Dot { |
8 | - /** |
|
9 | - * Get the value of the element at the given dot key path. |
|
10 | - * |
|
11 | - * @param array $array Multi-dimensional array to access. |
|
12 | - * @param string $path Dot-notation key path. e.g. `parent.child` |
|
13 | - * @param null|mixed $default Default value to return |
|
14 | - * |
|
15 | - * @return mixed |
|
16 | - */ |
|
17 | - public static function get(array $array, $path, $default = null) { |
|
18 | - $getter = function ($arrayCarry, $key) use ($default) { |
|
19 | - if (!is_array($arrayCarry) || !isset($arrayCarry[$key])) { |
|
20 | - return $default; |
|
21 | - } |
|
22 | - return $arrayCarry[$key]; |
|
23 | - }; |
|
24 | - $keys = self::extractKeys($path); |
|
25 | - return array_reduce($keys, $getter, $array); |
|
26 | - } |
|
8 | + /** |
|
9 | + * Get the value of the element at the given dot key path. |
|
10 | + * |
|
11 | + * @param array $array Multi-dimensional array to access. |
|
12 | + * @param string $path Dot-notation key path. e.g. `parent.child` |
|
13 | + * @param null|mixed $default Default value to return |
|
14 | + * |
|
15 | + * @return mixed |
|
16 | + */ |
|
17 | + public static function get(array $array, $path, $default = null) { |
|
18 | + $getter = function ($arrayCarry, $key) use ($default) { |
|
19 | + if (!is_array($arrayCarry) || !isset($arrayCarry[$key])) { |
|
20 | + return $default; |
|
21 | + } |
|
22 | + return $arrayCarry[$key]; |
|
23 | + }; |
|
24 | + $keys = self::extractKeys($path); |
|
25 | + return array_reduce($keys, $getter, $array); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * Has returns true if an element exists at the given dot key path. |
|
30 | - * |
|
31 | - * @param array $array Multi-dimensionsal array to search. |
|
32 | - * @param string $path Dot-notation key path to search. |
|
33 | - * |
|
34 | - * @return bool |
|
35 | - */ |
|
36 | - public static function has(array $array, $path) { |
|
37 | - $unique = new \stdClass(); |
|
38 | - $value = self::get($array, $path, $unique); |
|
39 | - return $value !== $unique; |
|
40 | - } |
|
28 | + /** |
|
29 | + * Has returns true if an element exists at the given dot key path. |
|
30 | + * |
|
31 | + * @param array $array Multi-dimensionsal array to search. |
|
32 | + * @param string $path Dot-notation key path to search. |
|
33 | + * |
|
34 | + * @return bool |
|
35 | + */ |
|
36 | + public static function has(array $array, $path) { |
|
37 | + $unique = new \stdClass(); |
|
38 | + $value = self::get($array, $path, $unique); |
|
39 | + return $value !== $unique; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Set a value at a certain path by creating missing elements and overwriting non-array values. |
|
44 | - * |
|
45 | - * If any of the visited elements is not an array, it will be replaced with an array. |
|
46 | - * |
|
47 | - * This will overwrite existing non-array values. |
|
48 | - * |
|
49 | - * @param array $array |
|
50 | - * @param string $path |
|
51 | - * @param mixed $value |
|
52 | - */ |
|
53 | - public static function set(array &$array, $path, $value) { |
|
54 | - self::setHelper($array, $path, $value, false); |
|
55 | - } |
|
42 | + /** |
|
43 | + * Set a value at a certain path by creating missing elements and overwriting non-array values. |
|
44 | + * |
|
45 | + * If any of the visited elements is not an array, it will be replaced with an array. |
|
46 | + * |
|
47 | + * This will overwrite existing non-array values. |
|
48 | + * |
|
49 | + * @param array $array |
|
50 | + * @param string $path |
|
51 | + * @param mixed $value |
|
52 | + */ |
|
53 | + public static function set(array &$array, $path, $value) { |
|
54 | + self::setHelper($array, $path, $value, false); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * trySet sets a value at a certain path, expecting arrays or missing elements along the way. |
|
59 | - * |
|
60 | - * If any of the visited elements is not an array, a \RuntimeException is thrown. |
|
61 | - * |
|
62 | - * Use this if you want to avoid overwriting existing non-array values. |
|
63 | - * |
|
64 | - * @param array $array |
|
65 | - * @param string $path |
|
66 | - * @param mixed $value |
|
67 | - * |
|
68 | - * @throws \RuntimeException |
|
69 | - */ |
|
70 | - public static function trySet(array &$array, $path, $value) { |
|
71 | - self::setHelper($array, $path, $value, true); |
|
72 | - } |
|
57 | + /** |
|
58 | + * trySet sets a value at a certain path, expecting arrays or missing elements along the way. |
|
59 | + * |
|
60 | + * If any of the visited elements is not an array, a \RuntimeException is thrown. |
|
61 | + * |
|
62 | + * Use this if you want to avoid overwriting existing non-array values. |
|
63 | + * |
|
64 | + * @param array $array |
|
65 | + * @param string $path |
|
66 | + * @param mixed $value |
|
67 | + * |
|
68 | + * @throws \RuntimeException |
|
69 | + */ |
|
70 | + public static function trySet(array &$array, $path, $value) { |
|
71 | + self::setHelper($array, $path, $value, true); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Remove an element if it exists. |
|
76 | - * |
|
77 | - * @param array $array |
|
78 | - * @param string $path |
|
79 | - */ |
|
80 | - public static function remove(array &$array, $path) { |
|
81 | - $keys = self::extractKeys($path); |
|
82 | - $keysWithoutLast = array_slice($keys, 0, -1); |
|
83 | - $keyCount = count($keysWithoutLast); |
|
84 | - $lastKey = $keys[$keyCount]; |
|
85 | - $node =& $array; |
|
86 | - // Abort when key is missing earlier than expected |
|
87 | - for ($i = 0; $i < $keyCount && is_array($node) && isset($node[$keys[$i]]); ++$i) { |
|
88 | - $node =& $node[$keys[$i]]; |
|
89 | - } |
|
90 | - if ($i < $keyCount) { |
|
91 | - return; |
|
92 | - } |
|
93 | - if (is_array($node) && isset($node[$lastKey])) { |
|
94 | - unset($node[$lastKey]); |
|
95 | - } |
|
96 | - } |
|
74 | + /** |
|
75 | + * Remove an element if it exists. |
|
76 | + * |
|
77 | + * @param array $array |
|
78 | + * @param string $path |
|
79 | + */ |
|
80 | + public static function remove(array &$array, $path) { |
|
81 | + $keys = self::extractKeys($path); |
|
82 | + $keysWithoutLast = array_slice($keys, 0, -1); |
|
83 | + $keyCount = count($keysWithoutLast); |
|
84 | + $lastKey = $keys[$keyCount]; |
|
85 | + $node =& $array; |
|
86 | + // Abort when key is missing earlier than expected |
|
87 | + for ($i = 0; $i < $keyCount && is_array($node) && isset($node[$keys[$i]]); ++$i) { |
|
88 | + $node =& $node[$keys[$i]]; |
|
89 | + } |
|
90 | + if ($i < $keyCount) { |
|
91 | + return; |
|
92 | + } |
|
93 | + if (is_array($node) && isset($node[$lastKey])) { |
|
94 | + unset($node[$lastKey]); |
|
95 | + } |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * Flatten the array into a single dimension array with dot paths as keys. |
|
100 | - * |
|
101 | - * @param array $array |
|
102 | - * @param null|string $parent |
|
103 | - * |
|
104 | - * @return array |
|
105 | - */ |
|
106 | - public static function flatten(array $array, $parent = null) { |
|
107 | - $flat = []; |
|
108 | - foreach ($array as $key => $value) { |
|
109 | - $keypath = self::escapeKey($key); |
|
110 | - if ($parent !== null) { |
|
111 | - $keypath = $parent . '.' . $keypath; |
|
112 | - } |
|
113 | - if (is_array($value)) { |
|
114 | - $flat = array_merge(self::flatten($value, $keypath), $flat); |
|
115 | - } else { |
|
116 | - $flat[$keypath] = $value; |
|
117 | - } |
|
118 | - } |
|
119 | - return $flat; |
|
120 | - } |
|
98 | + /** |
|
99 | + * Flatten the array into a single dimension array with dot paths as keys. |
|
100 | + * |
|
101 | + * @param array $array |
|
102 | + * @param null|string $parent |
|
103 | + * |
|
104 | + * @return array |
|
105 | + */ |
|
106 | + public static function flatten(array $array, $parent = null) { |
|
107 | + $flat = []; |
|
108 | + foreach ($array as $key => $value) { |
|
109 | + $keypath = self::escapeKey($key); |
|
110 | + if ($parent !== null) { |
|
111 | + $keypath = $parent . '.' . $keypath; |
|
112 | + } |
|
113 | + if (is_array($value)) { |
|
114 | + $flat = array_merge(self::flatten($value, $keypath), $flat); |
|
115 | + } else { |
|
116 | + $flat[$keypath] = $value; |
|
117 | + } |
|
118 | + } |
|
119 | + return $flat; |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * escapeKey escapes an individual part of a key. |
|
124 | - * |
|
125 | - * @param string $key |
|
126 | - * |
|
127 | - * @return string |
|
128 | - */ |
|
129 | - private static function escapeKey($key) { |
|
130 | - $re = '/([\\.\\\\])/'; |
|
131 | - $subst = '\\\$1'; |
|
132 | - return preg_replace($re, $subst, $key); |
|
133 | - } |
|
122 | + /** |
|
123 | + * escapeKey escapes an individual part of a key. |
|
124 | + * |
|
125 | + * @param string $key |
|
126 | + * |
|
127 | + * @return string |
|
128 | + */ |
|
129 | + private static function escapeKey($key) { |
|
130 | + $re = '/([\\.\\\\])/'; |
|
131 | + $subst = '\\\$1'; |
|
132 | + return preg_replace($re, $subst, $key); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * @param string $path |
|
137 | - * |
|
138 | - * @return array |
|
139 | - */ |
|
140 | - private static function extractKeys($path) { |
|
141 | - $keys = []; |
|
142 | - if (!preg_match_all('/(?:\\\\.|[^\\.\\\\]++)+/', $path, $matches)) { |
|
143 | - return [$path]; |
|
144 | - } |
|
145 | - foreach ($matches[0] as $match) { |
|
146 | - $keys[] = str_replace(['\.', '\\\\'], ['.', '\\'], $match); |
|
147 | - } |
|
148 | - return $keys; |
|
149 | - } |
|
135 | + /** |
|
136 | + * @param string $path |
|
137 | + * |
|
138 | + * @return array |
|
139 | + */ |
|
140 | + private static function extractKeys($path) { |
|
141 | + $keys = []; |
|
142 | + if (!preg_match_all('/(?:\\\\.|[^\\.\\\\]++)+/', $path, $matches)) { |
|
143 | + return [$path]; |
|
144 | + } |
|
145 | + foreach ($matches[0] as $match) { |
|
146 | + $keys[] = str_replace(['\.', '\\\\'], ['.', '\\'], $match); |
|
147 | + } |
|
148 | + return $keys; |
|
149 | + } |
|
150 | 150 | |
151 | - private static function setHelper(array &$array, $path, $value, $strict = true) { |
|
152 | - $keys = self::extractKeys($path); |
|
153 | - $lastKey = $keys[count($keys) - 1]; |
|
154 | - $keysWithoutLast = array_slice($keys, 0, -1); |
|
155 | - $node =& $array; |
|
156 | - foreach ($keysWithoutLast as $key) { |
|
157 | - self::prepareNode($node, $key, $path, $strict); |
|
158 | - $node =& $node[$key]; |
|
159 | - } |
|
160 | - $node[$lastKey] = $value; |
|
161 | - } |
|
151 | + private static function setHelper(array &$array, $path, $value, $strict = true) { |
|
152 | + $keys = self::extractKeys($path); |
|
153 | + $lastKey = $keys[count($keys) - 1]; |
|
154 | + $keysWithoutLast = array_slice($keys, 0, -1); |
|
155 | + $node =& $array; |
|
156 | + foreach ($keysWithoutLast as $key) { |
|
157 | + self::prepareNode($node, $key, $path, $strict); |
|
158 | + $node =& $node[$key]; |
|
159 | + } |
|
160 | + $node[$lastKey] = $value; |
|
161 | + } |
|
162 | 162 | |
163 | - private static function prepareNode(array &$node, $key, $path, $strict) { |
|
164 | - if (!isset($node[$key]) || !$strict && !is_array($node[$key])) { |
|
165 | - $node[$key] = []; |
|
166 | - } |
|
167 | - if ($strict && !is_array($node[$key])) { |
|
168 | - throw new \RuntimeException( |
|
169 | - sprintf( |
|
170 | - "Can not set value at path '%s' because the element at key '%s' is not an array. Found value of type '%s' instead.", |
|
171 | - $path, |
|
172 | - $key, |
|
173 | - gettype($node[$key]) |
|
174 | - ) |
|
175 | - ); |
|
176 | - } |
|
177 | - } |
|
163 | + private static function prepareNode(array &$node, $key, $path, $strict) { |
|
164 | + if (!isset($node[$key]) || !$strict && !is_array($node[$key])) { |
|
165 | + $node[$key] = []; |
|
166 | + } |
|
167 | + if ($strict && !is_array($node[$key])) { |
|
168 | + throw new \RuntimeException( |
|
169 | + sprintf( |
|
170 | + "Can not set value at path '%s' because the element at key '%s' is not an array. Found value of type '%s' instead.", |
|
171 | + $path, |
|
172 | + $key, |
|
173 | + gettype($node[$key]) |
|
174 | + ) |
|
175 | + ); |
|
176 | + } |
|
177 | + } |
|
178 | 178 | } |
@@ -82,10 +82,10 @@ discard block |
||
82 | 82 | $keysWithoutLast = array_slice($keys, 0, -1); |
83 | 83 | $keyCount = count($keysWithoutLast); |
84 | 84 | $lastKey = $keys[$keyCount]; |
85 | - $node =& $array; |
|
85 | + $node = & $array; |
|
86 | 86 | // Abort when key is missing earlier than expected |
87 | 87 | for ($i = 0; $i < $keyCount && is_array($node) && isset($node[$keys[$i]]); ++$i) { |
88 | - $node =& $node[$keys[$i]]; |
|
88 | + $node = & $node[$keys[$i]]; |
|
89 | 89 | } |
90 | 90 | if ($i < $keyCount) { |
91 | 91 | return; |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | foreach ($array as $key => $value) { |
109 | 109 | $keypath = self::escapeKey($key); |
110 | 110 | if ($parent !== null) { |
111 | - $keypath = $parent . '.' . $keypath; |
|
111 | + $keypath = $parent.'.'.$keypath; |
|
112 | 112 | } |
113 | 113 | if (is_array($value)) { |
114 | 114 | $flat = array_merge(self::flatten($value, $keypath), $flat); |
@@ -152,10 +152,10 @@ discard block |
||
152 | 152 | $keys = self::extractKeys($path); |
153 | 153 | $lastKey = $keys[count($keys) - 1]; |
154 | 154 | $keysWithoutLast = array_slice($keys, 0, -1); |
155 | - $node =& $array; |
|
155 | + $node = & $array; |
|
156 | 156 | foreach ($keysWithoutLast as $key) { |
157 | 157 | self::prepareNode($node, $key, $path, $strict); |
158 | - $node =& $node[$key]; |
|
158 | + $node = & $node[$key]; |
|
159 | 159 | } |
160 | 160 | $node[$lastKey] = $value; |
161 | 161 | } |
@@ -28,163 +28,163 @@ |
||
28 | 28 | * @see \nochso\Omni\Dot |
29 | 29 | */ |
30 | 30 | class DotArray implements \ArrayAccess, \IteratorAggregate{ |
31 | - private $data; |
|
31 | + private $data; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @param array $array Any (nested) array. |
|
35 | - */ |
|
36 | - public function __construct(array $array = []) { |
|
37 | - $this->data = $array; |
|
38 | - } |
|
33 | + /** |
|
34 | + * @param array $array Any (nested) array. |
|
35 | + */ |
|
36 | + public function __construct(array $array = []) { |
|
37 | + $this->data = $array; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * getArray returns the complete array. |
|
42 | - * |
|
43 | - * @return array |
|
44 | - */ |
|
45 | - public function getArray() { |
|
46 | - return $this->data; |
|
47 | - } |
|
40 | + /** |
|
41 | + * getArray returns the complete array. |
|
42 | + * |
|
43 | + * @return array |
|
44 | + */ |
|
45 | + public function getArray() { |
|
46 | + return $this->data; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Get the value of the element at the given dot key path. |
|
51 | - * |
|
52 | - * @param string $path Dot-notation key path. e.g. `parent.child` |
|
53 | - * @param null|mixed $default Default value to return |
|
54 | - * |
|
55 | - * @return mixed |
|
56 | - */ |
|
57 | - public function get($path, $default = null) { |
|
58 | - return Dot::get($this->data, $path, $default); |
|
59 | - } |
|
49 | + /** |
|
50 | + * Get the value of the element at the given dot key path. |
|
51 | + * |
|
52 | + * @param string $path Dot-notation key path. e.g. `parent.child` |
|
53 | + * @param null|mixed $default Default value to return |
|
54 | + * |
|
55 | + * @return mixed |
|
56 | + */ |
|
57 | + public function get($path, $default = null) { |
|
58 | + return Dot::get($this->data, $path, $default); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Has returns true if an element exists at the given dot key path. |
|
63 | - * |
|
64 | - * @param string $path Dot-notation key path to search. |
|
65 | - * |
|
66 | - * @return bool |
|
67 | - */ |
|
68 | - public function has($path) { |
|
69 | - return Dot::has($this->data, $path); |
|
70 | - } |
|
61 | + /** |
|
62 | + * Has returns true if an element exists at the given dot key path. |
|
63 | + * |
|
64 | + * @param string $path Dot-notation key path to search. |
|
65 | + * |
|
66 | + * @return bool |
|
67 | + */ |
|
68 | + public function has($path) { |
|
69 | + return Dot::has($this->data, $path); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Set a value at a certain path by creating missing elements and overwriting non-array values. |
|
74 | - * |
|
75 | - * If any of the visited elements is not an array, it will be replaced with an array. |
|
76 | - * |
|
77 | - * This will overwrite existing non-array values. |
|
78 | - * |
|
79 | - * @param string $path |
|
80 | - * @param mixed $value |
|
81 | - */ |
|
82 | - public function set($path, $value) { |
|
83 | - Dot::set($this->data, $path, $value); |
|
84 | - } |
|
72 | + /** |
|
73 | + * Set a value at a certain path by creating missing elements and overwriting non-array values. |
|
74 | + * |
|
75 | + * If any of the visited elements is not an array, it will be replaced with an array. |
|
76 | + * |
|
77 | + * This will overwrite existing non-array values. |
|
78 | + * |
|
79 | + * @param string $path |
|
80 | + * @param mixed $value |
|
81 | + */ |
|
82 | + public function set($path, $value) { |
|
83 | + Dot::set($this->data, $path, $value); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * trySet sets a value at a certain path, expecting arrays or missing elements along the way. |
|
88 | - * |
|
89 | - * If any of the visited elements is not an array, a \RuntimeException is thrown. |
|
90 | - * |
|
91 | - * Use this if you want to avoid overwriting existing non-array values. |
|
92 | - * |
|
93 | - * @param string $path |
|
94 | - * @param mixed $value |
|
95 | - * |
|
96 | - * @throws \RuntimeException |
|
97 | - */ |
|
98 | - public function trySet($path, $value) { |
|
99 | - Dot::trySet($this->data, $path, $value); |
|
100 | - } |
|
86 | + /** |
|
87 | + * trySet sets a value at a certain path, expecting arrays or missing elements along the way. |
|
88 | + * |
|
89 | + * If any of the visited elements is not an array, a \RuntimeException is thrown. |
|
90 | + * |
|
91 | + * Use this if you want to avoid overwriting existing non-array values. |
|
92 | + * |
|
93 | + * @param string $path |
|
94 | + * @param mixed $value |
|
95 | + * |
|
96 | + * @throws \RuntimeException |
|
97 | + */ |
|
98 | + public function trySet($path, $value) { |
|
99 | + Dot::trySet($this->data, $path, $value); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Remove an element if it exists. |
|
104 | - * |
|
105 | - * @param string $path |
|
106 | - */ |
|
107 | - public function remove($path) { |
|
108 | - Dot::remove($this->data, $path); |
|
109 | - } |
|
102 | + /** |
|
103 | + * Remove an element if it exists. |
|
104 | + * |
|
105 | + * @param string $path |
|
106 | + */ |
|
107 | + public function remove($path) { |
|
108 | + Dot::remove($this->data, $path); |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Flatten the array into a single dimension array with escaped dot paths as keys. |
|
113 | - * |
|
114 | - * Dots within specific keys are escaped. |
|
115 | - * |
|
116 | - * @return array |
|
117 | - */ |
|
118 | - public function flatten() { |
|
119 | - return Dot::flatten($this->data); |
|
120 | - } |
|
111 | + /** |
|
112 | + * Flatten the array into a single dimension array with escaped dot paths as keys. |
|
113 | + * |
|
114 | + * Dots within specific keys are escaped. |
|
115 | + * |
|
116 | + * @return array |
|
117 | + */ |
|
118 | + public function flatten() { |
|
119 | + return Dot::flatten($this->data); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * getIterator allows you to iterate over a flattened array using `foreach`. |
|
124 | - * |
|
125 | - * Keys are escaped and thus safe to use. |
|
126 | - * |
|
127 | - * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
128 | - * |
|
129 | - * @return \Traversable An instance of an object implementing <b>Iterator</b> or <b>Traversable</b> |
|
130 | - */ |
|
131 | - public function getIterator() { |
|
132 | - return new \ArrayIterator($this->flatten()); |
|
133 | - } |
|
122 | + /** |
|
123 | + * getIterator allows you to iterate over a flattened array using `foreach`. |
|
124 | + * |
|
125 | + * Keys are escaped and thus safe to use. |
|
126 | + * |
|
127 | + * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
128 | + * |
|
129 | + * @return \Traversable An instance of an object implementing <b>Iterator</b> or <b>Traversable</b> |
|
130 | + */ |
|
131 | + public function getIterator() { |
|
132 | + return new \ArrayIterator($this->flatten()); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * offsetExists allows using `isset($da['a.b'])`. |
|
137 | - * |
|
138 | - * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
139 | - * |
|
140 | - * @param mixed $offset An offset to check for. |
|
141 | - * |
|
142 | - * @return bool true on success or false on failure. |
|
143 | - */ |
|
144 | - public function offsetExists($offset) { |
|
145 | - return $this->has($offset); |
|
146 | - } |
|
135 | + /** |
|
136 | + * offsetExists allows using `isset($da['a.b'])`. |
|
137 | + * |
|
138 | + * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
139 | + * |
|
140 | + * @param mixed $offset An offset to check for. |
|
141 | + * |
|
142 | + * @return bool true on success or false on failure. |
|
143 | + */ |
|
144 | + public function offsetExists($offset) { |
|
145 | + return $this->has($offset); |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * offsetGet allows array access, e.g. `$da['a.b']`. |
|
150 | - * |
|
151 | - * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
152 | - * |
|
153 | - * @param mixed $offset The offset to retrieve. |
|
154 | - * |
|
155 | - * @return mixed Can return all value types. |
|
156 | - */ |
|
157 | - public function offsetGet($offset) { |
|
158 | - return $this->get($offset); |
|
159 | - } |
|
148 | + /** |
|
149 | + * offsetGet allows array access, e.g. `$da['a.b']`. |
|
150 | + * |
|
151 | + * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
152 | + * |
|
153 | + * @param mixed $offset The offset to retrieve. |
|
154 | + * |
|
155 | + * @return mixed Can return all value types. |
|
156 | + */ |
|
157 | + public function offsetGet($offset) { |
|
158 | + return $this->get($offset); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * offsetSet allows writing to arrays `$da['a.b'] = 'foo'`. |
|
163 | - * |
|
164 | - * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
165 | - * |
|
166 | - * @param mixed $offset The offset to assign the value to. |
|
167 | - * @param mixed $value The value to set. |
|
168 | - * |
|
169 | - * @return void |
|
170 | - */ |
|
171 | - public function offsetSet($offset, $value) { |
|
172 | - if ($offset === null) { |
|
173 | - throw new \InvalidArgumentException(sprintf('Appending to %s is not supported.', self::class)); |
|
174 | - } |
|
175 | - $this->set($offset, $value); |
|
176 | - } |
|
161 | + /** |
|
162 | + * offsetSet allows writing to arrays `$da['a.b'] = 'foo'`. |
|
163 | + * |
|
164 | + * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
165 | + * |
|
166 | + * @param mixed $offset The offset to assign the value to. |
|
167 | + * @param mixed $value The value to set. |
|
168 | + * |
|
169 | + * @return void |
|
170 | + */ |
|
171 | + public function offsetSet($offset, $value) { |
|
172 | + if ($offset === null) { |
|
173 | + throw new \InvalidArgumentException(sprintf('Appending to %s is not supported.', self::class)); |
|
174 | + } |
|
175 | + $this->set($offset, $value); |
|
176 | + } |
|
177 | 177 | |
178 | - /** |
|
179 | - * offsetUnset allows using `unset($da['a.b'])`. |
|
180 | - * |
|
181 | - * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
182 | - * |
|
183 | - * @param mixed $offset The offset to unset. |
|
184 | - * |
|
185 | - * @return void |
|
186 | - */ |
|
187 | - public function offsetUnset($offset) { |
|
188 | - $this->remove($offset); |
|
189 | - } |
|
178 | + /** |
|
179 | + * offsetUnset allows using `unset($da['a.b'])`. |
|
180 | + * |
|
181 | + * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
182 | + * |
|
183 | + * @param mixed $offset The offset to unset. |
|
184 | + * |
|
185 | + * @return void |
|
186 | + */ |
|
187 | + public function offsetUnset($offset) { |
|
188 | + $this->remove($offset); |
|
189 | + } |
|
190 | 190 | } |
@@ -7,241 +7,241 @@ |
||
7 | 7 | * `mb_*` methods are used where sensible, so make sure to pass UTF-8 strings. |
8 | 8 | */ |
9 | 9 | final class Strings { |
10 | - private static $controlCharMap = [ |
|
11 | - "\n" => '\n', |
|
12 | - "\r" => '\r', |
|
13 | - "\t" => '\t', |
|
14 | - "\v" => '\v', |
|
15 | - "\e" => '\e', |
|
16 | - "\f" => '\f', |
|
17 | - ]; |
|
10 | + private static $controlCharMap = [ |
|
11 | + "\n" => '\n', |
|
12 | + "\r" => '\r', |
|
13 | + "\t" => '\t', |
|
14 | + "\v" => '\v', |
|
15 | + "\e" => '\e', |
|
16 | + "\f" => '\f', |
|
17 | + ]; |
|
18 | 18 | |
19 | - const CONTROL_CHAR_PATTERN = '/[\x00-\x1F\x7F]/'; |
|
19 | + const CONTROL_CHAR_PATTERN = '/[\x00-\x1F\x7F]/'; |
|
20 | 20 | |
21 | - /** |
|
22 | - * startsWith returns true if the input begins with a prefix. |
|
23 | - * |
|
24 | - * @param string $input |
|
25 | - * @param string $prefix |
|
26 | - * |
|
27 | - * @return bool |
|
28 | - */ |
|
29 | - public static function startsWith($input, $prefix) { |
|
30 | - return substr($input, 0, strlen($prefix)) === $prefix; |
|
31 | - } |
|
21 | + /** |
|
22 | + * startsWith returns true if the input begins with a prefix. |
|
23 | + * |
|
24 | + * @param string $input |
|
25 | + * @param string $prefix |
|
26 | + * |
|
27 | + * @return bool |
|
28 | + */ |
|
29 | + public static function startsWith($input, $prefix) { |
|
30 | + return substr($input, 0, strlen($prefix)) === $prefix; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * endsWith returns true if the input ends with a suffix. |
|
35 | - * |
|
36 | - * @param string $input |
|
37 | - * @param string $suffix |
|
38 | - * |
|
39 | - * @return bool |
|
40 | - */ |
|
41 | - public static function endsWith($input, $suffix) { |
|
42 | - return strlen($suffix) === 0 || substr($input, -strlen($suffix)) === $suffix; |
|
43 | - } |
|
33 | + /** |
|
34 | + * endsWith returns true if the input ends with a suffix. |
|
35 | + * |
|
36 | + * @param string $input |
|
37 | + * @param string $suffix |
|
38 | + * |
|
39 | + * @return bool |
|
40 | + */ |
|
41 | + public static function endsWith($input, $suffix) { |
|
42 | + return strlen($suffix) === 0 || substr($input, -strlen($suffix)) === $suffix; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * getMostFrequentNeedle by counting occurences of each needle in haystack. |
|
47 | - * |
|
48 | - * @param string $haystack Haystack to be searched in. |
|
49 | - * @param array $needles Needles to be counted. |
|
50 | - * |
|
51 | - * @return string|null The most occuring needle. If counts are tied, the first tied needle is returned. If no |
|
52 | - * needles were found, `null` is returned. |
|
53 | - */ |
|
54 | - public static function getMostFrequentNeedle($haystack, array $needles) { |
|
55 | - $maxCount = 0; |
|
56 | - $maxNeedle = null; |
|
57 | - foreach ($needles as $needle) { |
|
58 | - $newCount = mb_substr_count($haystack, $needle); |
|
59 | - if ($newCount > $maxCount) { |
|
60 | - $maxCount = $newCount; |
|
61 | - $maxNeedle = $needle; |
|
62 | - } |
|
63 | - } |
|
64 | - return $maxNeedle; |
|
65 | - } |
|
45 | + /** |
|
46 | + * getMostFrequentNeedle by counting occurences of each needle in haystack. |
|
47 | + * |
|
48 | + * @param string $haystack Haystack to be searched in. |
|
49 | + * @param array $needles Needles to be counted. |
|
50 | + * |
|
51 | + * @return string|null The most occuring needle. If counts are tied, the first tied needle is returned. If no |
|
52 | + * needles were found, `null` is returned. |
|
53 | + */ |
|
54 | + public static function getMostFrequentNeedle($haystack, array $needles) { |
|
55 | + $maxCount = 0; |
|
56 | + $maxNeedle = null; |
|
57 | + foreach ($needles as $needle) { |
|
58 | + $newCount = mb_substr_count($haystack, $needle); |
|
59 | + if ($newCount > $maxCount) { |
|
60 | + $maxCount = $newCount; |
|
61 | + $maxNeedle = $needle; |
|
62 | + } |
|
63 | + } |
|
64 | + return $maxNeedle; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * escapeControlChars by replacing line feeds, tabs, etc. to their escaped representation. |
|
69 | - * |
|
70 | - * e.g. an actual line feed will return '\n' |
|
71 | - * |
|
72 | - * @param string $input |
|
73 | - * |
|
74 | - * @return string |
|
75 | - */ |
|
76 | - public static function escapeControlChars($input) { |
|
77 | - $escaper = function ($chars) { |
|
78 | - $char = $chars[0]; |
|
79 | - if (isset(self::$controlCharMap[$char])) { |
|
80 | - return self::$controlCharMap[$char]; |
|
81 | - } |
|
82 | - return sprintf('\x%02X', ord($char)); |
|
83 | - }; |
|
84 | - $output = str_replace('\\', '\\\\', $input); |
|
85 | - return preg_replace_callback(self::CONTROL_CHAR_PATTERN, $escaper, $output); |
|
86 | - } |
|
67 | + /** |
|
68 | + * escapeControlChars by replacing line feeds, tabs, etc. to their escaped representation. |
|
69 | + * |
|
70 | + * e.g. an actual line feed will return '\n' |
|
71 | + * |
|
72 | + * @param string $input |
|
73 | + * |
|
74 | + * @return string |
|
75 | + */ |
|
76 | + public static function escapeControlChars($input) { |
|
77 | + $escaper = function ($chars) { |
|
78 | + $char = $chars[0]; |
|
79 | + if (isset(self::$controlCharMap[$char])) { |
|
80 | + return self::$controlCharMap[$char]; |
|
81 | + } |
|
82 | + return sprintf('\x%02X', ord($char)); |
|
83 | + }; |
|
84 | + $output = str_replace('\\', '\\\\', $input); |
|
85 | + return preg_replace_callback(self::CONTROL_CHAR_PATTERN, $escaper, $output); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * padMultibyte strings to a certain length with another string. |
|
90 | - * |
|
91 | - * @param string $input The input string to be padded. |
|
92 | - * @param int $padLength If the pad is length smaller than the input length, no padding takes place. |
|
93 | - * @param string $padding Optional, defaults to a space character. Can be more than one character. The padding |
|
94 | - * may be truncated if the required number of padding characters can't be evenly |
|
95 | - * divided. |
|
96 | - * @param int $paddingType Optional, defaults to STR_PAD_RIGHT. Must be one of STR_PAD_LEFT, STR_PAD_RIGHT or |
|
97 | - * STR_PAD_BOTH. |
|
98 | - * |
|
99 | - * @return string The padded string. |
|
100 | - */ |
|
101 | - public static function padMultibyte($input, $padLength, $padding = ' ', $paddingType = STR_PAD_RIGHT) { |
|
102 | - if ($paddingType !== STR_PAD_LEFT && $paddingType !== STR_PAD_RIGHT && $paddingType !== STR_PAD_BOTH) { |
|
103 | - throw new \InvalidArgumentException('Padding type must be one of STR_PAD_LEFT, STR_PAD_RIGHT or STR_PAD_BOTH.'); |
|
104 | - } |
|
105 | - $paddingLength = mb_strlen($padding); |
|
106 | - if ($paddingLength === 0) { |
|
107 | - throw new \InvalidArgumentException('Padding string must not be empty.'); |
|
108 | - } |
|
109 | - $inputLength = mb_strlen($input); |
|
110 | - if ($inputLength > $padLength) { |
|
111 | - return $input; |
|
112 | - } |
|
113 | - $freeLength = $padLength - $inputLength; |
|
114 | - if ($paddingType === STR_PAD_BOTH) { |
|
115 | - // Original str_pad prefers trailing padding |
|
116 | - $leftPadLength = $padLength - ceil($freeLength / 2); |
|
117 | - // Reuse the below left/right implementation |
|
118 | - return self::padMultibyte( |
|
119 | - self::padMultibyte($input, $leftPadLength, $padding, STR_PAD_LEFT), |
|
120 | - $padLength, |
|
121 | - $padding, |
|
122 | - STR_PAD_RIGHT |
|
123 | - ); |
|
124 | - } |
|
125 | - $foo = str_repeat($padding, $freeLength / $paddingLength); |
|
126 | - $partialPadLength = $freeLength % $paddingLength; |
|
127 | - if ($partialPadLength > 0) { |
|
128 | - $foo .= mb_substr($padding, 0, $partialPadLength); |
|
129 | - } |
|
130 | - if ($paddingType === STR_PAD_LEFT) { |
|
131 | - return $foo . $input; |
|
132 | - } |
|
133 | - return $input . $foo; |
|
134 | - } |
|
88 | + /** |
|
89 | + * padMultibyte strings to a certain length with another string. |
|
90 | + * |
|
91 | + * @param string $input The input string to be padded. |
|
92 | + * @param int $padLength If the pad is length smaller than the input length, no padding takes place. |
|
93 | + * @param string $padding Optional, defaults to a space character. Can be more than one character. The padding |
|
94 | + * may be truncated if the required number of padding characters can't be evenly |
|
95 | + * divided. |
|
96 | + * @param int $paddingType Optional, defaults to STR_PAD_RIGHT. Must be one of STR_PAD_LEFT, STR_PAD_RIGHT or |
|
97 | + * STR_PAD_BOTH. |
|
98 | + * |
|
99 | + * @return string The padded string. |
|
100 | + */ |
|
101 | + public static function padMultibyte($input, $padLength, $padding = ' ', $paddingType = STR_PAD_RIGHT) { |
|
102 | + if ($paddingType !== STR_PAD_LEFT && $paddingType !== STR_PAD_RIGHT && $paddingType !== STR_PAD_BOTH) { |
|
103 | + throw new \InvalidArgumentException('Padding type must be one of STR_PAD_LEFT, STR_PAD_RIGHT or STR_PAD_BOTH.'); |
|
104 | + } |
|
105 | + $paddingLength = mb_strlen($padding); |
|
106 | + if ($paddingLength === 0) { |
|
107 | + throw new \InvalidArgumentException('Padding string must not be empty.'); |
|
108 | + } |
|
109 | + $inputLength = mb_strlen($input); |
|
110 | + if ($inputLength > $padLength) { |
|
111 | + return $input; |
|
112 | + } |
|
113 | + $freeLength = $padLength - $inputLength; |
|
114 | + if ($paddingType === STR_PAD_BOTH) { |
|
115 | + // Original str_pad prefers trailing padding |
|
116 | + $leftPadLength = $padLength - ceil($freeLength / 2); |
|
117 | + // Reuse the below left/right implementation |
|
118 | + return self::padMultibyte( |
|
119 | + self::padMultibyte($input, $leftPadLength, $padding, STR_PAD_LEFT), |
|
120 | + $padLength, |
|
121 | + $padding, |
|
122 | + STR_PAD_RIGHT |
|
123 | + ); |
|
124 | + } |
|
125 | + $foo = str_repeat($padding, $freeLength / $paddingLength); |
|
126 | + $partialPadLength = $freeLength % $paddingLength; |
|
127 | + if ($partialPadLength > 0) { |
|
128 | + $foo .= mb_substr($padding, 0, $partialPadLength); |
|
129 | + } |
|
130 | + if ($paddingType === STR_PAD_LEFT) { |
|
131 | + return $foo . $input; |
|
132 | + } |
|
133 | + return $input . $foo; |
|
134 | + } |
|
135 | 135 | |
136 | - /** |
|
137 | - * getCommonPrefix of two strings. |
|
138 | - * |
|
139 | - * @param string $first |
|
140 | - * @param string $second |
|
141 | - * |
|
142 | - * @return string All common characters from the beginning of both strings. |
|
143 | - */ |
|
144 | - public static function getCommonPrefix($first, $second) { |
|
145 | - if ($first === $second) { |
|
146 | - return $first; |
|
147 | - } |
|
148 | - $length = min(mb_strlen($first), mb_strlen($second)); |
|
149 | - for ($i = 0; $i < $length; $i++) { |
|
150 | - if ($first[$i] !== $second[$i]) { |
|
151 | - return mb_substr($first, 0, $i); |
|
152 | - } |
|
153 | - } |
|
154 | - return mb_substr($first, 0, $length); |
|
155 | - } |
|
136 | + /** |
|
137 | + * getCommonPrefix of two strings. |
|
138 | + * |
|
139 | + * @param string $first |
|
140 | + * @param string $second |
|
141 | + * |
|
142 | + * @return string All common characters from the beginning of both strings. |
|
143 | + */ |
|
144 | + public static function getCommonPrefix($first, $second) { |
|
145 | + if ($first === $second) { |
|
146 | + return $first; |
|
147 | + } |
|
148 | + $length = min(mb_strlen($first), mb_strlen($second)); |
|
149 | + for ($i = 0; $i < $length; $i++) { |
|
150 | + if ($first[$i] !== $second[$i]) { |
|
151 | + return mb_substr($first, 0, $i); |
|
152 | + } |
|
153 | + } |
|
154 | + return mb_substr($first, 0, $length); |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * getCommonSuffix of two strings. |
|
159 | - * |
|
160 | - * @param string $first |
|
161 | - * @param string $second |
|
162 | - * |
|
163 | - * @return string All common characters from the end of both strings. |
|
164 | - */ |
|
165 | - public static function getCommonSuffix($first, $second) { |
|
166 | - $reversedCommon = self::getCommonPrefix(self::reverse($first), self::reverse($second)); |
|
167 | - return self::reverse($reversedCommon); |
|
168 | - } |
|
157 | + /** |
|
158 | + * getCommonSuffix of two strings. |
|
159 | + * |
|
160 | + * @param string $first |
|
161 | + * @param string $second |
|
162 | + * |
|
163 | + * @return string All common characters from the end of both strings. |
|
164 | + */ |
|
165 | + public static function getCommonSuffix($first, $second) { |
|
166 | + $reversedCommon = self::getCommonPrefix(self::reverse($first), self::reverse($second)); |
|
167 | + return self::reverse($reversedCommon); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * Reverse a string. |
|
172 | - * |
|
173 | - * @param string $input |
|
174 | - * |
|
175 | - * @return string The reversed string. |
|
176 | - */ |
|
177 | - public static function reverse($input) { |
|
178 | - $length = mb_strlen($input); |
|
179 | - $reversed = ''; |
|
180 | - for ($i = $length - 1; $i !== -1; --$i) { |
|
181 | - $reversed .= mb_substr($input, $i, 1); |
|
182 | - } |
|
183 | - return $reversed; |
|
184 | - } |
|
170 | + /** |
|
171 | + * Reverse a string. |
|
172 | + * |
|
173 | + * @param string $input |
|
174 | + * |
|
175 | + * @return string The reversed string. |
|
176 | + */ |
|
177 | + public static function reverse($input) { |
|
178 | + $length = mb_strlen($input); |
|
179 | + $reversed = ''; |
|
180 | + for ($i = $length - 1; $i !== -1; --$i) { |
|
181 | + $reversed .= mb_substr($input, $i, 1); |
|
182 | + } |
|
183 | + return $reversed; |
|
184 | + } |
|
185 | 185 | |
186 | - /** |
|
187 | - * groupByCommonPrefix returns an array with a common key and a list of differing suffixes. |
|
188 | - * |
|
189 | - * e.g. passing an array `['sameHERE', 'sameTHERE']` would return |
|
190 | - * ``` |
|
191 | - * 'same' => [ |
|
192 | - * 'HERE', |
|
193 | - * 'THERE', |
|
194 | - * ] |
|
195 | - * ``` |
|
196 | - * |
|
197 | - * This can be used to group several file paths by a common base. |
|
198 | - * |
|
199 | - * @param string[] $strings |
|
200 | - * |
|
201 | - * @return string[][] |
|
202 | - */ |
|
203 | - public static function groupByCommonPrefix($strings) { |
|
204 | - sort($strings); |
|
205 | - $common = null; |
|
206 | - foreach ($strings as $folder) { |
|
207 | - if ($common === null) { |
|
208 | - $common = $folder; |
|
209 | - } else { |
|
210 | - $common = self::getCommonPrefix($common, $folder); |
|
211 | - } |
|
212 | - } |
|
213 | - $trimmedFolders = []; |
|
214 | - $commonLength = mb_strlen($common); |
|
215 | - foreach ($strings as $folder) { |
|
216 | - $trimmedFolders[$common][] = mb_substr($folder, $commonLength); |
|
217 | - } |
|
218 | - return $trimmedFolders; |
|
219 | - } |
|
186 | + /** |
|
187 | + * groupByCommonPrefix returns an array with a common key and a list of differing suffixes. |
|
188 | + * |
|
189 | + * e.g. passing an array `['sameHERE', 'sameTHERE']` would return |
|
190 | + * ``` |
|
191 | + * 'same' => [ |
|
192 | + * 'HERE', |
|
193 | + * 'THERE', |
|
194 | + * ] |
|
195 | + * ``` |
|
196 | + * |
|
197 | + * This can be used to group several file paths by a common base. |
|
198 | + * |
|
199 | + * @param string[] $strings |
|
200 | + * |
|
201 | + * @return string[][] |
|
202 | + */ |
|
203 | + public static function groupByCommonPrefix($strings) { |
|
204 | + sort($strings); |
|
205 | + $common = null; |
|
206 | + foreach ($strings as $folder) { |
|
207 | + if ($common === null) { |
|
208 | + $common = $folder; |
|
209 | + } else { |
|
210 | + $common = self::getCommonPrefix($common, $folder); |
|
211 | + } |
|
212 | + } |
|
213 | + $trimmedFolders = []; |
|
214 | + $commonLength = mb_strlen($common); |
|
215 | + foreach ($strings as $folder) { |
|
216 | + $trimmedFolders[$common][] = mb_substr($folder, $commonLength); |
|
217 | + } |
|
218 | + return $trimmedFolders; |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * groupByCommonSuffix returns an array with a common key and a list of differing suffixes. |
|
223 | - * |
|
224 | - * e.g. passing an array `['sameHERE', 'sameTHERE']` would return |
|
225 | - * ``` |
|
226 | - * 'HERE' => [ |
|
227 | - * 'same', |
|
228 | - * 'sameT', |
|
229 | - * ] |
|
230 | - * ``` |
|
231 | - * |
|
232 | - * @param string[] $strings |
|
233 | - * |
|
234 | - * @return string[][] |
|
235 | - */ |
|
236 | - public static function groupByCommonSuffix($strings) { |
|
237 | - foreach ($strings as $key => $string) { |
|
238 | - $strings[$key] = self::reverse($string); |
|
239 | - } |
|
240 | - $reversedGroups = self::groupByCommonPrefix($strings); |
|
241 | - $groups = []; |
|
242 | - foreach ($reversedGroups as $revKey => $revStrings) { |
|
243 | - $groups[self::reverse($revKey)] = array_map([self::class, 'reverse'], $revStrings); |
|
244 | - } |
|
245 | - return $groups; |
|
246 | - } |
|
221 | + /** |
|
222 | + * groupByCommonSuffix returns an array with a common key and a list of differing suffixes. |
|
223 | + * |
|
224 | + * e.g. passing an array `['sameHERE', 'sameTHERE']` would return |
|
225 | + * ``` |
|
226 | + * 'HERE' => [ |
|
227 | + * 'same', |
|
228 | + * 'sameT', |
|
229 | + * ] |
|
230 | + * ``` |
|
231 | + * |
|
232 | + * @param string[] $strings |
|
233 | + * |
|
234 | + * @return string[][] |
|
235 | + */ |
|
236 | + public static function groupByCommonSuffix($strings) { |
|
237 | + foreach ($strings as $key => $string) { |
|
238 | + $strings[$key] = self::reverse($string); |
|
239 | + } |
|
240 | + $reversedGroups = self::groupByCommonPrefix($strings); |
|
241 | + $groups = []; |
|
242 | + foreach ($reversedGroups as $revKey => $revStrings) { |
|
243 | + $groups[self::reverse($revKey)] = array_map([self::class, 'reverse'], $revStrings); |
|
244 | + } |
|
245 | + return $groups; |
|
246 | + } |
|
247 | 247 | } |
@@ -128,9 +128,9 @@ |
||
128 | 128 | $foo .= mb_substr($padding, 0, $partialPadLength); |
129 | 129 | } |
130 | 130 | if ($paddingType === STR_PAD_LEFT) { |
131 | - return $foo . $input; |
|
131 | + return $foo.$input; |
|
132 | 132 | } |
133 | - return $input . $foo; |
|
133 | + return $input.$foo; |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | /** |
@@ -5,154 +5,154 @@ |
||
5 | 5 | * Multiline string class for working with lines of text. |
6 | 6 | */ |
7 | 7 | final class Multiline extends ArrayCollection { |
8 | - /** |
|
9 | - * @var \nochso\Omni\EOL |
|
10 | - */ |
|
11 | - private $eol; |
|
8 | + /** |
|
9 | + * @var \nochso\Omni\EOL |
|
10 | + */ |
|
11 | + private $eol; |
|
12 | 12 | |
13 | - /** |
|
14 | - * Create a new Multiline object from a string. |
|
15 | - * |
|
16 | - * First the input string is split into lines by the detected end-of-line |
|
17 | - * character. Afterwards any extra EOL chars will be trimmed. |
|
18 | - * |
|
19 | - * @see \nochso\Omni\EOL |
|
20 | - * |
|
21 | - * @param string $input A string to split into a Multiline object |
|
22 | - * @param string $defaultEol Default end-of-line type to split the input by. This is a fallback in case it could |
|
23 | - * not be detected from the input string. Optional, defaults to `EOL::EOL_LF` i.e. "\n". |
|
24 | - * See the `EOL::EOL_*` class constants. |
|
25 | - * |
|
26 | - * @return \nochso\Omni\Multiline |
|
27 | - */ |
|
28 | - public static function create($input, $defaultEol = \nochso\Omni\EOL::EOL_LF) { |
|
29 | - $eol = EOL::detectDefault($input, $defaultEol); |
|
30 | - $lines = explode($eol, $input); |
|
31 | - $multiline = new self($lines); |
|
32 | - // Remove left-over line feeds |
|
33 | - $multiline->apply(function ($line) { |
|
34 | - return trim($line, "\r\n"); |
|
35 | - }); |
|
36 | - $multiline->setEol($eol); |
|
37 | - return $multiline; |
|
38 | - } |
|
13 | + /** |
|
14 | + * Create a new Multiline object from a string. |
|
15 | + * |
|
16 | + * First the input string is split into lines by the detected end-of-line |
|
17 | + * character. Afterwards any extra EOL chars will be trimmed. |
|
18 | + * |
|
19 | + * @see \nochso\Omni\EOL |
|
20 | + * |
|
21 | + * @param string $input A string to split into a Multiline object |
|
22 | + * @param string $defaultEol Default end-of-line type to split the input by. This is a fallback in case it could |
|
23 | + * not be detected from the input string. Optional, defaults to `EOL::EOL_LF` i.e. "\n". |
|
24 | + * See the `EOL::EOL_*` class constants. |
|
25 | + * |
|
26 | + * @return \nochso\Omni\Multiline |
|
27 | + */ |
|
28 | + public static function create($input, $defaultEol = \nochso\Omni\EOL::EOL_LF) { |
|
29 | + $eol = EOL::detectDefault($input, $defaultEol); |
|
30 | + $lines = explode($eol, $input); |
|
31 | + $multiline = new self($lines); |
|
32 | + // Remove left-over line feeds |
|
33 | + $multiline->apply(function ($line) { |
|
34 | + return trim($line, "\r\n"); |
|
35 | + }); |
|
36 | + $multiline->setEol($eol); |
|
37 | + return $multiline; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * __toString returns a single string using the current EOL style. |
|
42 | - * |
|
43 | - * @return string |
|
44 | - */ |
|
45 | - public function __toString() { |
|
46 | - return implode((string) $this->eol, $this->list); |
|
47 | - } |
|
40 | + /** |
|
41 | + * __toString returns a single string using the current EOL style. |
|
42 | + * |
|
43 | + * @return string |
|
44 | + */ |
|
45 | + public function __toString() { |
|
46 | + return implode((string) $this->eol, $this->list); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Get EOL style ending. |
|
51 | - * |
|
52 | - * @return \nochso\Omni\EOL |
|
53 | - */ |
|
54 | - public function getEol() { |
|
55 | - return $this->eol; |
|
56 | - } |
|
49 | + /** |
|
50 | + * Get EOL style ending. |
|
51 | + * |
|
52 | + * @return \nochso\Omni\EOL |
|
53 | + */ |
|
54 | + public function getEol() { |
|
55 | + return $this->eol; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * getMaxLength of all lines. |
|
60 | - * |
|
61 | - * @return int |
|
62 | - */ |
|
63 | - public function getMaxLength() { |
|
64 | - $length = 0; |
|
65 | - foreach ($this->list as $line) { |
|
66 | - $length = max($length, mb_strlen($line)); |
|
67 | - } |
|
68 | - return $length; |
|
69 | - } |
|
58 | + /** |
|
59 | + * getMaxLength of all lines. |
|
60 | + * |
|
61 | + * @return int |
|
62 | + */ |
|
63 | + public function getMaxLength() { |
|
64 | + $length = 0; |
|
65 | + foreach ($this->list as $line) { |
|
66 | + $length = max($length, mb_strlen($line)); |
|
67 | + } |
|
68 | + return $length; |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Set EOL used by this Multiline string. |
|
73 | - * |
|
74 | - * @param \nochso\Omni\EOL|string $eol Either an `EOL` object or a string ("\r\n" or "\n") |
|
75 | - * |
|
76 | - * @return $this |
|
77 | - */ |
|
78 | - public function setEol($eol) { |
|
79 | - if (!$eol instanceof EOL) { |
|
80 | - $eol = new EOL($eol); |
|
81 | - } |
|
82 | - $this->eol = $eol; |
|
83 | - return $this; |
|
84 | - } |
|
71 | + /** |
|
72 | + * Set EOL used by this Multiline string. |
|
73 | + * |
|
74 | + * @param \nochso\Omni\EOL|string $eol Either an `EOL` object or a string ("\r\n" or "\n") |
|
75 | + * |
|
76 | + * @return $this |
|
77 | + */ |
|
78 | + public function setEol($eol) { |
|
79 | + if (!$eol instanceof EOL) { |
|
80 | + $eol = new EOL($eol); |
|
81 | + } |
|
82 | + $this->eol = $eol; |
|
83 | + return $this; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Append text to a certain line. |
|
88 | - * |
|
89 | - * @param string $text |
|
90 | - * @param null|int $index Optional, defaults to the last line. Other |
|
91 | - * |
|
92 | - * @return $this |
|
93 | - */ |
|
94 | - public function append($text, $index = null) { |
|
95 | - if ($index === null) { |
|
96 | - $index = count($this) - 1; |
|
97 | - } |
|
98 | - $this->list[$index] .= $text; |
|
99 | - return $this; |
|
100 | - } |
|
86 | + /** |
|
87 | + * Append text to a certain line. |
|
88 | + * |
|
89 | + * @param string $text |
|
90 | + * @param null|int $index Optional, defaults to the last line. Other |
|
91 | + * |
|
92 | + * @return $this |
|
93 | + */ |
|
94 | + public function append($text, $index = null) { |
|
95 | + if ($index === null) { |
|
96 | + $index = count($this) - 1; |
|
97 | + } |
|
98 | + $this->list[$index] .= $text; |
|
99 | + return $this; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Prefix all lines with a string. |
|
104 | - * |
|
105 | - * @param string $prefix The prefix to add to the start of the string. |
|
106 | - * |
|
107 | - * @return string |
|
108 | - */ |
|
109 | - public function prefix($prefix) { |
|
110 | - $prefixer = function ($line) use ($prefix) { |
|
111 | - return $prefix . $line; |
|
112 | - }; |
|
113 | - return $this->apply($prefixer); |
|
114 | - } |
|
102 | + /** |
|
103 | + * Prefix all lines with a string. |
|
104 | + * |
|
105 | + * @param string $prefix The prefix to add to the start of the string. |
|
106 | + * |
|
107 | + * @return string |
|
108 | + */ |
|
109 | + public function prefix($prefix) { |
|
110 | + $prefixer = function ($line) use ($prefix) { |
|
111 | + return $prefix . $line; |
|
112 | + }; |
|
113 | + return $this->apply($prefixer); |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Pad all lines to the same length using `str_pad`. |
|
118 | - * |
|
119 | - * @param int $length If length is larger than the maximum line length, all lines will be padded up to the |
|
120 | - * given length. If length is null, the maximum of all line lengths is used. Optional, |
|
121 | - * defaults to null. |
|
122 | - * @param string $padding Optional, defaults to a space character. Can be more than one character. The padding |
|
123 | - * may be truncated if the required number of padding characters can't be evenly |
|
124 | - * divided. |
|
125 | - * @param int $paddingType Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. |
|
126 | - * Defaults to STR_PAD_RIGHT. |
|
127 | - * |
|
128 | - * @return string |
|
129 | - */ |
|
130 | - public function pad($length = null, $padding = ' ', $paddingType = STR_PAD_RIGHT) { |
|
131 | - if ($length === null) { |
|
132 | - $length = $this->getMaxLength(); |
|
133 | - } |
|
134 | - $padder = function ($line) use ($length, $padding, $paddingType) { |
|
135 | - return Strings::padMultibyte($line, $length, $padding, $paddingType); |
|
136 | - }; |
|
137 | - return $this->apply($padder); |
|
138 | - } |
|
116 | + /** |
|
117 | + * Pad all lines to the same length using `str_pad`. |
|
118 | + * |
|
119 | + * @param int $length If length is larger than the maximum line length, all lines will be padded up to the |
|
120 | + * given length. If length is null, the maximum of all line lengths is used. Optional, |
|
121 | + * defaults to null. |
|
122 | + * @param string $padding Optional, defaults to a space character. Can be more than one character. The padding |
|
123 | + * may be truncated if the required number of padding characters can't be evenly |
|
124 | + * divided. |
|
125 | + * @param int $paddingType Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. |
|
126 | + * Defaults to STR_PAD_RIGHT. |
|
127 | + * |
|
128 | + * @return string |
|
129 | + */ |
|
130 | + public function pad($length = null, $padding = ' ', $paddingType = STR_PAD_RIGHT) { |
|
131 | + if ($length === null) { |
|
132 | + $length = $this->getMaxLength(); |
|
133 | + } |
|
134 | + $padder = function ($line) use ($length, $padding, $paddingType) { |
|
135 | + return Strings::padMultibyte($line, $length, $padding, $paddingType); |
|
136 | + }; |
|
137 | + return $this->apply($padder); |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * getLineIndexByCharacterPosition returns the line index containing a certain position. |
|
142 | - * |
|
143 | - * @param int $characterPosition Position of a character as if Multiline was a raw string. |
|
144 | - * |
|
145 | - * @return int|null The array index of the line containing the character position. |
|
146 | - */ |
|
147 | - public function getLineIndexByCharacterPosition($characterPosition) { |
|
148 | - $position = 0; |
|
149 | - foreach ($this->list as $key => $line) { |
|
150 | - $length = mb_strlen($line . $this->getEol()); |
|
151 | - if ($characterPosition >= $position && $characterPosition <= $position + $length) { |
|
152 | - return $key; |
|
153 | - } |
|
154 | - $position += $length; |
|
155 | - } |
|
156 | - return null; |
|
157 | - } |
|
140 | + /** |
|
141 | + * getLineIndexByCharacterPosition returns the line index containing a certain position. |
|
142 | + * |
|
143 | + * @param int $characterPosition Position of a character as if Multiline was a raw string. |
|
144 | + * |
|
145 | + * @return int|null The array index of the line containing the character position. |
|
146 | + */ |
|
147 | + public function getLineIndexByCharacterPosition($characterPosition) { |
|
148 | + $position = 0; |
|
149 | + foreach ($this->list as $key => $line) { |
|
150 | + $length = mb_strlen($line . $this->getEol()); |
|
151 | + if ($characterPosition >= $position && $characterPosition <= $position + $length) { |
|
152 | + return $key; |
|
153 | + } |
|
154 | + $position += $length; |
|
155 | + } |
|
156 | + return null; |
|
157 | + } |
|
158 | 158 | } |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | * @return string |
44 | 44 | */ |
45 | 45 | public function __toString() { |
46 | - return implode((string) $this->eol, $this->list); |
|
46 | + return implode((string)$this->eol, $this->list); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | */ |
109 | 109 | public function prefix($prefix) { |
110 | 110 | $prefixer = function ($line) use ($prefix) { |
111 | - return $prefix . $line; |
|
111 | + return $prefix.$line; |
|
112 | 112 | }; |
113 | 113 | return $this->apply($prefixer); |
114 | 114 | } |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | public function getLineIndexByCharacterPosition($characterPosition) { |
148 | 148 | $position = 0; |
149 | 149 | foreach ($this->list as $key => $line) { |
150 | - $length = mb_strlen($line . $this->getEol()); |
|
150 | + $length = mb_strlen($line.$this->getEol()); |
|
151 | 151 | if ($characterPosition >= $position && $characterPosition <= $position + $length) { |
152 | 152 | return $key; |
153 | 153 | } |