public function testUnionType() { $fooInterface = Type::of("Hediet\\Types\\Test\\FooInterface"); $foo = Type::of("Hediet\\Types\\Test\\Foo"); $fooOrFooInterface = Type::ofUnion(array($fooInterface, $foo)); $this->assertTrue($fooOrFooInterface->equals($fooInterface)); $this->assertTrue(Type::ofUnion(array($fooInterface, Type::ofBoolean()))->isAssignableFrom($foo)); $this->assertTrue(Type::ofUnion(array($fooInterface, Type::ofBoolean()))->isAssignableFrom(Type::ofBoolean())); $this->assertFalse(Type::ofUnion(array($foo, Type::ofBoolean()))->isAssignableFrom($fooInterface)); $this->assertFalse(Type::ofUnion(array($foo, Type::ofBoolean()))->isAssignableFrom(Type::ofUnion(array($foo, Type::ofBoolean(), Type::ofNull())))); $this->assertTrue(Type::ofUnion(array($foo, Type::ofBoolean(), Type::ofNull()))->isAssignableFrom(Type::ofUnion(array(Type::ofBoolean(), $foo)))); }
/** * Gets a type that allows the given type to be null. * * @param \Hediet\Types\Type $type */ public static function ofNullable(Type $type) { return self::ofUnion(array($type, Type::ofNull())); }