Esempio n. 1
0
 protected function stringifyArray(array $array)
 {
     if (a\isList($array)) {
         return 'array(' . implode(', ', array_map(function ($v) {
             return is_scalar($v) ? var_export($v, true) : \nspl\getType($v);
         }, $array)) . ')';
     }
     return f\I(var_export($this->array, true), f\partial('str_replace', "\n", ''), f\partial('str_replace', 'array (  ', 'array('), f\partial('str_replace', '  ', ' '), f\partial('str_replace', ',)', ')'));
 }
Esempio n. 2
0
File: args.php Progetto: ihor/Nspl
/**
 * @param mixed $arg
 * @param string $hadTo
 * @param int $atPosition
 * @param string|\Throwable $exception
 * @param bool $has
 * @throws \Throwable
 * @throws string
 */
function throwExpectsException($arg, $hadTo, $atPosition = null, $exception = '\\InvalidArgumentException', $has = false)
{
    list($function, $position, $file, $line) = getErrorSource($arg);
    if (!is_object($exception) || !$exception instanceof \Exception) {
        // @todo instanceof \Throwable since PHP 7
        $position = $atPosition !== null ? $atPosition : $position + 1;
        /** @var \InvalidArgumentException $_exception */
        $exception = new $exception(sprintf('Argument %s passed to %s() %s %s%s', $position, $function, $has ? 'has' : 'must', $hadTo, is_scalar($arg) ? ', ' . \nspl\getType($arg) . ' ' . var_export($arg, true) . ' given' : ', ' . \nspl\getType($arg) . (is_array($arg) ? ' ' . json_encode($arg) : '') . ' given'));
    }
    $setter = function ($property, $arg) {
        $this->{$property} = $arg;
    };
    $exceptionSetter = $setter->bindTo($exception, $exception);
    $exceptionSetter('file', $file);
    $exceptionSetter('line', $line);
    $baseExceptionSetter = $setter->bindTo($exception, 'Exception');
    $baseExceptionSetter('trace', a\drop($exception->getTrace(), 2));
    throw $exception;
}