Overview

Namespaces

  • esperecyan
    • webidl
      • lib

Classes

  • BooleanType
  • DictionaryType
  • FloatType
  • IntegerType
  • NullableType
  • ObjectType
  • SequenceType
  • StringType
  • Type
  • UnionType

Traits

  • Error
  • Overview
  • Namespace
  • Class
  • Tree
 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;

/** @internal */
class NullableType
{
    use Utility;
    
    /**
     * 与えられた値を、指定された型、または NULL 型に変換して返します。
     * @link https://www.w3.org/TR/WebIDL-1/#idl-nullable-type WebIDL Level 1
     * @param mixed $value
     * @param string $type nullable 型の内部型 (T? の T)。
     * @param array $pseudoTypes callback interface 型、列挙型、callback 関数型、または dictionary 型の識別子をキーとした型情報の配列。
     * @throws \InvalidArgumentException 指定された型、または NULL 型のいずれにも合致しない値が与えられた場合。
     * @throws \DomainException 指定された型、または NULL 型のいずれにも合致しない値が与えられた場合。
     * @return array
     */
    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;
    }
}
esperecyan/webidl documentation API documentation generated by ApiGen