kahlan\util\Text::toString PHP Method

toString() public static method

Generate a string representation of arbitrary data.
public static toString ( string $value, array $options = [] ) : string
$value string The data to dump in string.
$options array Available options are: - `'quote'` : dump will quote string data if true (default `true`). - `'object'`: dump options for objects. - `'method'`: default method to call on string instance (default `__toString`). - `'array'` : dump options for arrays. - `'indent'`: level of indent (defaults to `1`). - `'char'`: indentation character. - `'multiplier'`: number of indentation character per indent (default `4`)
return string The dumped string.
    public static function toString($value, $options = [])
    {
        $defaults = ['quote' => '"', 'object' => ['method' => '__toString'], 'array' => ['indent' => 1, 'char' => ' ', 'multiplier' => 4]];
        $options += $defaults;
        $options['array'] += $defaults['array'];
        $options['object'] += $defaults['object'];
        if ($value instanceof Closure) {
            return '`Closure`';
        }
        if (is_array($value)) {
            return static::_arrayToString($value, $options);
        }
        if (is_object($value)) {
            return static::_objectToString($value, $options);
        }
        return static::dump($value, $options['quote']);
    }

Usage Example

Example #1
0
 /**
  * Callback called after a spec execution.
  *
  * @param object $log The log object of the whole spec.
  */
 public function specEnd($log = null)
 {
     $isOk = $log->passed() ? "ok" : "not ok";
     switch ($log->type()) {
         case 'skipped':
         case 'pending':
         case 'excluded':
             $prefix = "# {$log->type()} ";
             break;
         default:
             $prefix = '- ';
             break;
     }
     $message = $prefix . trim(implode(" ", $log->messages()));
     $this->_counter++;
     $this->write("{$isOk} {$this->_counter} {$message}\n");
     if ($exception = $log->exception()) {
         $this->write('# Exception: `' . get_class($exception) . '` Code(' . $exception->getCode() . '):' . "\n");
         $this->write('# Message: ' . $exception->getMessage() . "\n");
     } else {
         foreach ($log->children() as $log) {
             if ($log->passed()) {
                 continue;
             }
             $toString = function ($instance) {
                 return 'an instance of `' . get_class($instance) . '`';
             };
             foreach ($log->data() as $key => $value) {
                 $key = ucfirst($key);
                 $value = Text::toString($value, ['object' => ['method' => $toString]]);
                 $this->write("# {$key}: {$value}\n");
             }
         }
     }
 }
All Usage Examples Of kahlan\util\Text::toString