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:
<?php
namespace esperecyan\webidl\lib;
class NullableType
{
use Utility;
public static function toNullable($value, $type, $pseudoTypes = [])
{
if (is_null($value)) {
$nullable = null;
} else {
try {
$nullable = Type::to($type, $value, $pseudoTypes);
} catch (\LogicException $exception) {
$errorMessage = ErrorMessageCreator::create(null, sprintf('%s (%s or null)', $type . '?', $type), '');
if ($exception instanceof \InvalidArgumentException) {
throw new \InvalidArgumentException($errorMessage, 0, $exception);
} elseif ($exception instanceof \DomainException) {
throw new \DomainException($errorMessage, 0, $exception);
} else {
throw $exception;
}
}
}
return $nullable;
}
}