Exemple #1
0
/**
 * Wraps provided native string in a managed string instance.
 *
 * On providing managed string instance it's cloned.
 *
 * If parameter $useIfNotAString is containing string and $string is not a
 * string, then the former is used to replace the latter. This is available
 * for conveniently validating parameters expected to be string.
 *
 * @param string $string native or managed string to wrap/clone
 * @param string $encoding encoding of provided string, ignored on cloning
 * @param string $useIfNotAString if $string is not a string this string is used instead
 * @return \de\toxa\txf\string managed string instance
 */
function _S($string, $encoding = null, $useIfNotAString = null)
{
    return string::wrap($string, $encoding, $useIfNotAString);
}
Exemple #2
0
 /**
  * Describes provided value.
  *
  * This method is returning string containing provided value extended by its
  * type and length/size. If value is exceeding length of 30 characters it's
  * being shortened.
  *
  * @param mixed $value value to describe
  * @param boolean $includeType true to have type included in description
  * @param boolean $fullSize true to disable shortening of longer values
  * @param void $_internal
  * @return string description of provided value
  */
 public static function describe($value, $includeType = true, $fullSize = false, $_internal = null)
 {
     $type = gettype($value);
     switch ($type) {
         case 'string':
             $type = 'string[' . strlen($value) . ']';
             $value = '"' . ($fullSize ? $value : string::wrap($value)->limit(40)) . '"';
             break;
         case 'boolean':
             $type = 'boolean';
             $value = $value ? 'true' : 'false';
             break;
         case 'unknown':
             $type = 'unknown';
             $value = '?';
             break;
         case 'array':
             $type = 'array[' . count($value) . ']';
             if ($_internal > 10) {
                 $value = '*MAX-DEPTH*';
             } else {
                 $isRegular = static::isRegularArray($value);
                 $out = '';
                 foreach ($value as $key => $item) {
                     $out .= $out !== '' ? ', ' : '[';
                     if (!$isRegular) {
                         $out .= (is_integer($key) ? $key : '"' . $key . '"') . ' => ';
                     }
                     $out .= static::describe($item, $includeType, $fullSize, $_internal + 1);
                 }
                 $value = $out . ']';
             }
             break;
         case 'object':
             $type = get_class($value);
             // can't use is_callable() here due to occasional use of __call() in inspected objects
             $inspector = new \ReflectionObject($value);
             if ($inspector->hasMethod('__describe')) {
                 list($type, $value) = $value->__describe();
             } else {
                 if ($inspector->hasMethod('__toString')) {
                     $value = strval($value);
                 } else {
                     $value = '[no-string]';
                 }
             }
             if (!$fullSize) {
                 $value = string::wrap($value)->limit(40);
             }
             break;
     }
     if ($includeType) {
         return "({$type}) {$value}";
     }
     return strval($value);
 }