/** * Initializes a new instance of that class. * * @param string $message The message. * @param \Exception $innerException The inner exception. * @param int $code The code. */ public function __construct($message = null, \Exception $innerException = null, int $code = 0) { parent::__construct(ClrString::valueToString($message), $code, $innerException); }
/** * Initializes a new instance of that class. * * @param string $paramName The name of the underlying parameter. * @param string $message The message. * @param int $code The code. * @param \Exception $innerException The inner exception. */ public function __construct($paramName = null, $message = null, \Exception $innerException = null, int $code = 0) { $this->_paramName = ClrString::valueToString($paramName, false); parent::__construct($message, $innerException, $code); }
/** * Converts a value to another type. * * @param mixed $val The value to convert. * @param string|\ReflectionClass $conversionType The type the object should be converted to. * @param IFormatProvider|null $provider The optional format provider to use. * @param bool $handleConvertible Handle IConvertible objects or not. * * @return mixed The new object / value. * * @throws InvalidCastException Conversion failed. */ public static function convertTo($val, $conversionType, IFormatProvider $provider = null, $handleConvertible = true) { $conversionType = static::getRealValue($conversionType); $valueToConvert = static::getRealValue($val); if ($valueToConvert instanceof IConvertible && $handleConvertible) { return $valueToConvert->toType($conversionType, $provider); } $thisCls = new \ReflectionClass(static::class); $acm = $thisCls->getMethod('asCallable')->getClosure(null); $icm = $thisCls->getMethod('isCallable')->getClosure(null); $valueToCallable = function () use($acm, $icm, &$valueToConvert) { if (!$icm($valueToConvert)) { return function () use(&$valueToConvert) { return $valueToConvert; }; } return $acm($valueToConvert); }; if (\is_object($conversionType)) { if (!$conversionType instanceof \ReflectionClass) { $conversionType = new \ReflectionObject($conversionType); } } $typeName = \trim(ClrString::valueToString($conversionType)); if (!$conversionType instanceof \ReflectionClass) { if (\class_exists($typeName) || \interface_exists($typeName)) { $conversionType = new \ReflectionClass($typeName); } } if ($conversionType instanceof \ReflectionClass) { if (\is_object($valueToConvert) && $conversionType->isInstance($valueToConvert)) { return $valueToConvert; } // to closure? if ($conversionType->getName() === \Closure::class) { return function () use($valueToCallable) { return \call_user_func_array($valueToCallable(), \func_get_args()); }; } // to IValueWrapper? if ($conversionType->isInterface() && $conversionType->getName() === IValueWrapper::class) { return new Comparer($valueToConvert); } // to ILazy? if ($conversionType->isInterface() && $conversionType->getName() === ILazy::class) { return new Lazy($valueToCallable()); } // ILazy based object? if ($conversionType->isInstantiable() && $conversionType->implementsInterface(ILazy::class)) { return $conversionType->newInstance($valueToCallable()); } // IValueWrapper based object? if ($conversionType->isInstantiable() && $conversionType->implementsInterface(IValueWrapper::class)) { return $conversionType->newInstance($valueToConvert); } if (null === $valueToConvert) { return null; } } else { switch ($typeName) { case 'callable': case 'lazy': case 'function': return 'lazy' === $typeName ? new Lazy($valueToCallable()) : $valueToCallable(); case 'real': return (double) $valueToConvert; case 'unset': return null; } if (false !== \settype($valueToConvert, $typeName)) { return $valueToConvert; } } throw new InvalidCastException($conversionType, $val); }