diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:38:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:38:04 +0000 |
commit | 1ff5c35de5dbd70a782875a91dd2232fd01b002b (patch) | |
tree | 77d9ce5e1bf78b3e6ef79f8f6e7861e2ced3c09b /vendor/ipl/stdlib/src/Str.php | |
parent | Initial commit. (diff) | |
download | icinga-php-library-1ff5c35de5dbd70a782875a91dd2232fd01b002b.tar.xz icinga-php-library-1ff5c35de5dbd70a782875a91dd2232fd01b002b.zip |
Adding upstream version 0.10.1.upstream/0.10.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/ipl/stdlib/src/Str.php')
-rw-r--r-- | vendor/ipl/stdlib/src/Str.php | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/vendor/ipl/stdlib/src/Str.php b/vendor/ipl/stdlib/src/Str.php new file mode 100644 index 0000000..d46989e --- /dev/null +++ b/vendor/ipl/stdlib/src/Str.php @@ -0,0 +1,93 @@ +<?php + +namespace ipl\Stdlib; + +/** + * Collection of string manipulation functions + */ +class Str +{ + /** + * Convert the given string to camel case + * + * The given string may be delimited by the following characters: '_' (underscore), '-' (dash), ' ' (space). + * + * @param string $subject + * + * @return string + */ + public static function camel($subject) + { + if ($subject === null) { + return ''; + } + + $normalized = str_replace(['-', '_'], ' ', $subject); + + return lcfirst(str_replace(' ', '', ucwords(strtolower($normalized)))); + } + + /** + * Check if the given string starts with the specified substring + * + * @param string $subject + * @param string $start + * @param bool $caseSensitive + * + * @return bool + */ + public static function startsWith($subject, string $start, bool $caseSensitive = true) + { + $subject = $subject ?? ''; + if (! $caseSensitive) { + return strncasecmp($subject, $start, strlen($start)) === 0; + } + + return substr($subject, 0, strlen($start)) === $start; + } + + /** + * Split string into an array padded to the size specified by limit + * + * This method is a perfect fit if you need default values for symmetric array destructuring. + * + * @param string $subject + * @param string $delimiter + * @param int $limit + * @param mixed $default + * + * @return array + */ + public static function symmetricSplit($subject, string $delimiter, int $limit, $default = null) + { + if ($subject === null) { + return []; + } + + return array_pad(explode($delimiter, $subject, $limit), $limit, $default); + } + + /** + * Split string into an array and trim spaces + * + * @param string $subject + * @param string $delimiter + * @param int $limit + * + * @return array + */ + public static function trimSplit($subject, string $delimiter = ',', int $limit = null) + { + if ($subject === null) { + return []; + } + + if ($limit !== null) { + $exploded = explode($delimiter, $subject, $limit); + } else { + $exploded = explode($delimiter, $subject); + } + + return array_map('trim', $exploded); + } +} |