1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace fphammerle\helpers;
- class StringHelper
- {
- /**
- * @return string
- */
- public static function prepend($prefix, $text)
- {
- if(is_array($text)) {
- $result = [];
- foreach($text as $key => $value) {
- $result[$key] = self::prepend($prefix, $value);
- }
- return $result;
- } else {
- return ($text === null) ? null : ($prefix . $text);
- }
- }
- /**
- * @return string
- */
- public static function append($text, $postfix)
- {
- if(is_array($text)) {
- $result = [];
- foreach($text as $key => $value) {
- $result[$key] = self::append($value, $postfix);
- }
- return $result;
- } else {
- return ($text === null) ? null : ($text . $postfix);
- }
- }
- /**
- * @return string
- */
- public static function embed($prefix, $text, $postfix)
- {
- return self::prepend($prefix, self::append($text, $postfix));
- }
- /**
- * @return string
- */
- public static function embrace($brace, $text)
- {
- return self::embed($brace, $text, $brace);
- }
- /**
- * @return string|null
- */
- public static function implode($glue, array $pieces = null)
- {
- if($pieces === null) {
- return null;
- } else {
- $pieces = array_filter(
- $pieces,
- function($piece) { return $piece !== null; }
- );
- if(sizeof($pieces) == 0) {
- return null;
- } else {
- return implode($glue, $pieces);
- }
- }
- }
- /**
- * @throws InvalidArgumentException empty needle
- * @param array $needles
- * @param string $haystack
- * @return bool
- */
- public static function containsAny(array $needles, $haystack)
- {
- foreach($needles as $needle) {
- if(empty($needle)) {
- throw new \InvalidArgumentException('empty needle');
- } elseif(strpos($haystack, $needle) !== false) {
- return true;
- }
- }
- return false;
- }
- }
|