1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167:
<?php
namespace Webmozart\Console\Util;
use Webmozart\Console\Api\Args\Format\InvalidValueException;
use Webmozart\Console\Api\Formatter\Formatter;
class StringUtil
{
public static function parseString($value, $nullable = true)
{
if ($nullable && (null === $value || 'null' === $value)) {
return null;
}
if (null === $value) {
return 'null';
}
if (true === $value) {
return 'true';
}
if (false === $value) {
return 'false';
}
return (string) $value;
}
public static function parseBoolean($value, $nullable = true)
{
if ($nullable && (null === $value || 'null' === $value)) {
return null;
}
if (is_bool($value)) {
return $value;
}
if (is_string($value) || is_int($value)) {
switch ((string) $value) {
case '':
case 'false':
case '0':
case 'no':
case 'off':
return false;
case 'true':
case '1':
case 'yes':
case 'on':
return true;
}
}
throw new InvalidValueException(sprintf(
'The value "%s" cannot be parsed as boolean.',
$value
));
}
public static function parseInteger($value, $nullable = true)
{
if ($nullable && (null === $value || 'null' === $value)) {
return null;
}
if (is_numeric($value) || is_bool($value)) {
return (int) $value;
}
throw new InvalidValueException(sprintf(
'The value "%s" cannot be parsed as integer.',
$value
));
}
public static function parseFloat($value, $nullable = true)
{
if ($nullable && (null === $value || 'null' === $value)) {
return null;
}
if (is_numeric($value) || is_bool($value)) {
return (float) $value;
}
throw new InvalidValueException(sprintf(
'The value "%s" cannot be parsed as float.',
$value
));
}
public static function getLength($string, Formatter $formatter = null)
{
if ($formatter) {
$string = $formatter->removeFormat($string);
}
if (!function_exists('mb_strwidth')) {
return strlen($string);
}
if (false === $encoding = mb_detect_encoding($string)) {
return strlen($string);
}
return mb_strwidth($string, $encoding);
}
public static function getMaxWordLength($string, Formatter $formatter = null)
{
if ($formatter) {
$string = $formatter->removeFormat($string);
}
$maxLength = 0;
$words = preg_split('/\s+/', $string);
foreach ($words as $word) {
$maxLength = max($maxLength, self::getLength($word));
}
return $maxLength;
}
public static function getMaxLineLength($string, Formatter $formatter = null)
{
if ($formatter) {
$string = $formatter->removeFormat($string);
}
$maxLength = 0;
$lines = explode("\n", $string);
foreach ($lines as $word) {
$maxLength = max($maxLength, self::getLength($word));
}
return $maxLength;
}
private function __construct()
{
}
}