* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class TypeUtil { /** * Walks through every nested element of the type and calls the visitor * callback for each type * * @param TypeInterface $type * @param \Closure $visitor */ public static function walk(TypeInterface $type, \Closure $visitor) { $visitor($type); if ($type instanceof StructType) { $properties = $type->getProperties(); if (is_iterable($properties)) { foreach ($properties as $property) { self::walk($property, $visitor); } } } elseif ($type instanceof MapType) { $additionalProperties = $type->getAdditionalProperties(); if ($additionalProperties instanceof TypeInterface) { self::walk($additionalProperties, $visitor); } } elseif ($type instanceof ArrayType) { $items = $type->getItems(); if ($items instanceof TypeInterface) { self::walk($items, $visitor); } } elseif ($type instanceof UnionType) { $oneOf = $type->getOneOf(); if (is_iterable($oneOf)) { foreach ($oneOf as $property) { self::walk($property, $visitor); } } } elseif ($type instanceof IntersectionType) { $allOf = $type->getAllOf(); if (is_iterable($allOf)) { foreach ($allOf as $property) { self::walk($property, $visitor); } } } } /** * Checks whether the type contains a specific type * * @param TypeInterface $type * @param string $class * @return bool */ public static function contains(TypeInterface $type, string $class, string $format = null): bool { $found = false; self::walk($type, function(TypeInterface $type) use ($class, $format, &$found) { if ($found === true) { return; } if (!$type instanceof $class) { return; } if ($format !== null && $type instanceof ScalarType) { $found = $type->getFormat() === $format; } else { $found = true; } }); return $found; } /** * Normalizes all reference types and removes the self namespace * * @param TypeInterface $type */ public static function normalize(TypeInterface $type): void { self::walk($type, function(TypeInterface $type){ if (!$type instanceof ReferenceType) { return; } [$ns, $name] = self::split($type->getRef()); if ($ns === DefinitionsInterface::SELF_NAMESPACE) { $type->setRef($name); } }); } /** * Splits a type name into the namespace and name * * @param string $ref * @return array */ public static function split(string $ref): array { if (strpos($ref, ':') !== false) { $parts = explode(':', $ref, 2); $ns = $parts[0] ?? ''; $name = $parts[1] ?? ''; } else { $ns = DefinitionsInterface::SELF_NAMESPACE; $name = $ref; } return [$ns, $name]; } /** * @param string $ref * @return string */ public static function getFullyQualifiedName(string $ref): string { [$ns, $name] = self::split($ref); return $ns . ':' . $name; } public static function getTypeName(TypeInterface $type): string { if ($type instanceof AnyType) { return 'any'; } elseif ($type instanceof ArrayType) { return 'array'; } elseif ($type instanceof BooleanType) { return 'boolean'; } elseif ($type instanceof GenericType) { return 'generic'; } elseif ($type instanceof IntegerType) { return 'integer'; } elseif ($type instanceof IntersectionType) { return 'intersection'; } elseif ($type instanceof MapType) { return 'map'; } elseif ($type instanceof NumberType) { return 'number'; } elseif ($type instanceof ReferenceType) { return 'reference'; } elseif ($type instanceof StringType) { return 'string'; } elseif ($type instanceof StructType) { return 'struct'; } elseif ($type instanceof UnionType) { return 'union'; } else { return 'unknown'; } } } __halt_compiler();----SIGNATURE:----J3VvQrYhfl5cmdWLXN9/HlJi+aPFu0H/IJ9RwNzD5HnBdONE5cYkBRP6zefM0gx9OxJL/WjCi7ObhE+NUTXyu9r5ZLTsuLGNyr0lJveyfVcYqU+zBdnZdAP3zxGth8e2Gt4mTpA818GnUjjLeD+COgDz981fWQVEuQoQFrKKipcop5jrs3++jq5EvWASJgGmc7gV/qsErtIxDHAWhSkyzslEmH988isay3M9BftWcuKcenrM1lsKWgMiD49DYxfuOMEYP4J9blDg4HI7SPXUtWKVv0IWjqxWHGEchnBSZpOpfVcqawbuzsuhPSdccplsQRvNBNYTczRAKC33KlktIqUt7td379nc21TzbsnhUWmHyX2CWnEGAC+NmFfKSS+GS3Zm+ZV50CtkhQ2JcZ1Qj5Unym3Fstu6mvqj/Le973Ul7yGpVHfvVlqam0it58zAu4K6RTAi2sn/fITLq7Lhe+s8luxjfBz1oiZqD8mf0Ldd5eufhdsEVMP+oXtIGsNfns5wCNv1jCBElx+zIzKa3n92FarTNwxMt5FJfzPxEhzN05uY0NhW80DR/TxJ0ThG+ESbUSNbQY/PdB0j02FxTkNJ1VSIdf0tGkwiu3RmMwf0jKlcIONOhF/YYsAHZpZblgMnOmJQa2TvCgHFj+yiPv1yBBKvohoL+UJsI5hnQL8=----ATTACHMENT:----MzIzMzQ1NTc1Njg2ODYyNyA4NTg2MDU3NzIyOTgxNTc2IDQ4MDA2NDgzODA5NzQ3NTA=