예제 #1
0
 /**
  * 与えられた値を、指定された型のいずれか一つに変換して返します。
  * @link https://heycam.github.io/webidl/#idl-union Web IDL (Second Edition)
  * @link http://www.w3.org/TR/WebIDL/#idl-union Web IDL
  * @link https://heycam.github.io/webidl/#es-union Web IDL (Second Edition)
  * @param mixed $value
  * @param string $unitTypeString 共用体型。先頭、末尾の丸括弧も含む文字列。
  * @param array $pseudoTypes callback interface 型、列挙型、callback 関数型、または dictionary 型の識別子をキーとした型情報の配列。
  * @throws \InvalidArgumentException 指定された型のいずれにも一致しない値が与えられた場合。
  * @throws \DomainException 指定された型のいずれにも一致しない値が与えられた場合。
  * @return mixed
  */
 public static function toUnion($value, $unitTypeString, $pseudoTypes = [])
 {
     $flattenedTypesAndNullableNums = self::getFlattenedTypesAndNullableNums($unitTypeString);
     if ($flattenedTypesAndNullableNums['numberOfNullableMemberTypes'] === 1 && is_null($value)) {
         return null;
     }
     foreach ($flattenedTypesAndNullableNums['flattenedMemberTypes'] as $type) {
         $genericTypes[$type] = self::getGenericType($type, $pseudoTypes);
     }
     if (is_object($value) || $value instanceof \__PHP_Incomplete_Class) {
         foreach (array_keys($genericTypes, 'interface') as $interfaceType) {
             try {
                 return Type::to($interfaceType, $value, $pseudoTypes);
             } catch (\LogicException $exception) {
                 if ($exception instanceof \InvalidArgumentException) {
                     $lastInvalidArgumentException = $exception;
                 } elseif ($exception instanceof \DomainException) {
                     $lastDomainException = $exception;
                 } else {
                     throw $exception;
                 }
             }
         }
         if (isset($genericTypes['object'])) {
             return $value;
         }
     }
     if (RegExpType::isRegExpCastable($value) && isset($genericTypes['RegExp'])) {
         try {
             return RegExpType::toRegExp($value);
         } catch (\LogicException $exception) {
             if ($exception instanceof \DomainException) {
                 $lastDomainException = $exception;
             } else {
                 throw $exception;
             }
         }
     }
     if (is_callable($value) && array_search('callback function', $genericTypes)) {
         return $value;
     }
     try {
         if ((is_array($value) || is_object($value) || $value instanceof \__PHP_Incomplete_Class || is_null($value)) && ($identifier = array_search('dictionary', $genericTypes)) !== false) {
             return DictionaryType::toDictionary($value, $identifier, $pseudoTypes);
         }
         if (is_array($value) || is_object($value) || $value instanceof \__PHP_Incomplete_Class || is_null($value)) {
             $type = array_search('array', $genericTypes) ?: array_search('sequence', $genericTypes) ?: array_search('FrozenArray', $genericTypes);
             if ($type) {
                 return Type::to($type, $value, $pseudoTypes);
             }
             foreach (array_keys($genericTypes, 'interface') as $interfaceType) {
                 if (isset($pseudoTypes[$interfaceType]) && ($pseudoTypes[$interfaceType] === 'callback interface' || $pseudoTypes[$interfaceType] === 'single operation callback interface')) {
                     return Type::to($interfaceType, $value, $pseudoTypes);
                 }
             }
         }
         if (is_bool($value) && isset($genericTypes['boolean'])) {
             return $value;
         }
         if ((is_int($value) || is_float($value)) && ($type = array_search('numeric', $genericTypes))) {
             return Type::to($type, $value);
         }
         if ($type = array_search('string', $genericTypes) ?: array_search('numeric', $genericTypes)) {
             return Type::to($type, $value, $pseudoTypes);
         }
         if (isset($genericTypes['boolean'])) {
             return BooleanType::toBoolean($value);
         }
     } catch (\LogicException $exception) {
         if ($exception instanceof \InvalidArgumentException) {
             $lastInvalidArgumentException = $exception;
         } elseif ($exception instanceof \DomainException) {
             $lastDomainException = $exception;
         } else {
             throw $exception;
         }
     }
     $errorMessage = ErrorMessageCreator::create($value, $unitTypeString);
     if (isset($lastDomainException)) {
         throw new \DomainException($errorMessage, 0, $lastDomainException);
     } elseif (isset($lastInvalidArgumentException)) {
         throw new \InvalidArgumentException($errorMessage, 0, $lastInvalidArgumentException);
     } else {
         throw new \InvalidArgumentException($errorMessage);
     }
 }