Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added Visitor::visitExtensionType() and implemented method on all visitors. #43

Merged
merged 2 commits into from

2 participants

@jmalloc
Collaborator

No description provided.

@ezzatron ezzatron merged commit 6f220ef into eloquent:master
@jmalloc jmalloc deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
44 src/Eloquent/Typhax/Comparator/TypeEquivalenceComparatorVisitor.php
@@ -16,6 +16,7 @@
use Eloquent\Typhax\Type\BooleanType;
use Eloquent\Typhax\Type\CallableType;
use Eloquent\Typhax\Type\CompositeType;
+use Eloquent\Typhax\Type\ExtensionType;
use Eloquent\Typhax\Type\FloatType;
use Eloquent\Typhax\Type\IntegerType;
use Eloquent\Typhax\Type\MixedType;
@@ -92,6 +93,49 @@ public function visitCallableType(CallableType $type)
}
/**
+ * @param ExtensionType $type
+ *
+ * @return mixed
+ */
+ public function visitExtensionType(ExtensionType $type)
+ {
+ $difference = $this->compareClass($type);
+ if (0 !== $difference) {
+ return $difference;
+ }
+
+ $difference = strcmp($this->type()->className(), $type->className());
+ if (0 !== $difference) {
+ return $difference;
+ }
+
+ $difference = $this->compareTypeList($type->types(), true, false);
+ if (0 !== $difference) {
+ return $difference;
+ }
+
+ $left = $this->type()->attributes();
+ $right = $type->attributes();
+
+ if ($left < $right) {
+ return -1;
+ } elseif ($left > $right) {
+ return 1;
+ }
+
+ $left = array_keys($left);
+ $right = array_keys($right);
+
+ if ($left < $right) {
+ return -1;
+ } elseif ($left > $right) {
+ return 1;
+ }
+
+ return 0;
+ }
+
+ /**
* @param FloatType $type
*
* @return integer
View
2  src/Eloquent/Typhax/Parser/Parser.php
@@ -275,7 +275,7 @@ protected function parseExtensionType(array &$tokens)
$this->consumeWhitespace($tokens);
if ($this->currentTokenIsType($tokens, Token::TOKEN_BRACE_OPEN)) {
- $attributes = $this->parseAttributes($tokens, ':' . $token->content());
+ $attributes = $this->parseAttributes($tokens, ':'.$token->content());
} else {
$attributes = array();
}
View
37 src/Eloquent/Typhax/Renderer/TypeRenderer.php
@@ -15,6 +15,7 @@
use Eloquent\Typhax\Type\ArrayType;
use Eloquent\Typhax\Type\BooleanType;
use Eloquent\Typhax\Type\CallableType;
+use Eloquent\Typhax\Type\ExtensionType;
use Eloquent\Typhax\Type\FloatType;
use Eloquent\Typhax\Type\IntegerType;
use Eloquent\Typhax\Type\MixedType;
@@ -78,6 +79,42 @@ public function visitCallableType(CallableType $type)
}
/**
+ * @param ExtensionType $type
+ *
+ * @return mixed
+ */
+ public function visitExtensionType(ExtensionType $type)
+ {
+ $string = ':'.$type->className()->toRelative()->string();
+
+ if (0 !== count($type->types())) {
+ $subTypes = array();
+ foreach ($type->types() as $subType) {
+ $subTypes[] = $subType->accept($this);
+ }
+
+ $string .= sprintf(
+ '<%s>',
+ implode(', ', $subTypes)
+ );
+ }
+
+ if (0 !== count($type->attributes())) {
+ $attributes = array();
+ foreach ($type->attributes() as $key => $value) {
+ $attributes[] = $key.': '.var_export($value, true);
+ }
+
+ $string .= sprintf(
+ ' {%s}',
+ implode(', ', $attributes)
+ );
+ }
+
+ return $string;
+ }
+
+ /**
* @param FloatType $type
*
* @return string
View
22 src/Eloquent/Typhax/Resolver/ObjectTypeClassNameResolver.php
@@ -16,6 +16,7 @@
use Eloquent\Typhax\Type\ArrayType;
use Eloquent\Typhax\Type\BooleanType;
use Eloquent\Typhax\Type\CallableType;
+use Eloquent\Typhax\Type\ExtensionType;
use Eloquent\Typhax\Type\FloatType;
use Eloquent\Typhax\Type\IntegerType;
use Eloquent\Typhax\Type\MixedType;
@@ -95,6 +96,27 @@ public function visitCallableType(CallableType $type)
}
/**
+ * @param ExtensionType $type
+ *
+ * @return mixed
+ */
+ public function visitExtensionType(ExtensionType $type)
+ {
+ $types = array();
+ foreach ($type->types() as $subType) {
+ $types[] = $subType->accept($this);
+ }
+
+ return new ExtensionType(
+ $this->classNameResolver()->resolve(
+ $type->className()
+ ),
+ $types,
+ $type->attributes()
+ );
+ }
+
+ /**
* @param FloatType $type
*
* @return Type
View
12 src/Eloquent/Typhax/Type/ExtensionType.php
@@ -18,13 +18,13 @@ class ExtensionType extends Host implements Type
{
/**
* @param ClassName $className
- * @param array<Type> $subTypes
+ * @param array<Type> $types
* @param array $attributes
*/
- public function __construct(ClassName $className, array $subTypes, array $attributes)
+ public function __construct(ClassName $className, array $types, array $attributes)
{
$this->className = $className;
- $this->subTypes = $subTypes;
+ $this->types = $types;
$this->attributes = $attributes;
}
@@ -39,9 +39,9 @@ public function className()
/**
* @return array<Type>
*/
- public function subTypes()
+ public function types()
{
- return $this->subTypes;
+ return $this->types;
}
/**
@@ -53,6 +53,6 @@ public function attributes()
}
private $className;
- private $subTypes;
+ private $types;
private $attributes;
}
View
7 src/Eloquent/Typhax/Type/Visitor.php
@@ -44,6 +44,13 @@ public function visitBooleanType(BooleanType $type);
public function visitCallableType(CallableType $type);
/**
+ * @param ExtensionType $type
+ *
+ * @return mixed
+ */
+ public function visitExtensionType(ExtensionType $type);
+
+ /**
* @param FloatType $type
*
* @return mixed
View
42 test/suite/Eloquent/Typhax/Comparator/TypeEquivalenceComparatorTest.php
@@ -11,10 +11,12 @@
namespace Eloquent\Typhax\Comparator;
+use Eloquent\Cosmos\ClassName;
use Eloquent\Typhax\Type\AndType;
use Eloquent\Typhax\Type\ArrayType;
use Eloquent\Typhax\Type\BooleanType;
use Eloquent\Typhax\Type\CallableType;
+use Eloquent\Typhax\Type\ExtensionType;
use Eloquent\Typhax\Type\FloatType;
use Eloquent\Typhax\Type\IntegerType;
use Eloquent\Typhax\Type\MixedType;
@@ -256,6 +258,46 @@ public function compareData()
$expected = -1;
$data['Attribute null and non-null non-equivalence 2'] = array($expected, $left, $right);
+ $left = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new IntegerType), array('foo' => 'bar'));
+ $right = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new IntegerType), array('foo' => 'bar'));
+ $expected = 0;
+ $data['Extension type equivalence'] = array($expected, $left, $right);
+
+ $left = new StreamType;
+ $right = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new IntegerType), array('foo' => 'bar'));
+ $expected = 1;
+ $data['Extension type non-equivalence (different type)'] = array($expected, $left, $right);
+
+ $left = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new IntegerType), array('foo' => 'bar'));
+ $right = new ExtensionType(ClassName::fromString('This\is\Not\Equivalent'), array(new IntegerType), array('foo' => 'bar'));
+ $expected = -1;
+ $data['Extension type non-equivalence (different class name)'] = array($expected, $left, $right);
+
+ $left = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new IntegerType), array('foo' => 'bar'));
+ $right = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new StringType), array('foo' => 'bar'));
+ $expected = -1;
+ $data['Extension type non-equivalence (different types)'] = array($expected, $left, $right);
+
+ $left = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new IntegerType), array('foo' => 'A'));
+ $right = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new IntegerType), array('foo' => 'B'));
+ $expected = -1;
+ $data['Extension type non-equivalence (< attribute values)'] = array($expected, $left, $right);
+
+ $left = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new IntegerType), array('foo' => 'B'));
+ $right = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new IntegerType), array('foo' => 'A'));
+ $expected = 1;
+ $data['Extension type non-equivalence (> attribute values)'] = array($expected, $left, $right);
+
+ $left = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new IntegerType), array('A' => 'bar'));
+ $right = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new IntegerType), array('B' => 'bar'));
+ $expected = -1;
+ $data['Extension type non-equivalence (< attribute keys)'] = array($expected, $left, $right);
+
+ $left = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new IntegerType), array('B' => 'bar'));
+ $right = new ExtensionType(ClassName::fromString('Foo\Bar'), array(new IntegerType), array('A' => 'bar'));
+ $expected = 1;
+ $data['Extension type non-equivalence (> attribute keys)'] = array($expected, $left, $right);
+
return $data;
}
View
33 test/suite/Eloquent/Typhax/Renderer/TypeRendererTest.php
@@ -16,6 +16,7 @@
use Eloquent\Typhax\Type\ArrayType;
use Eloquent\Typhax\Type\BooleanType;
use Eloquent\Typhax\Type\CallableType;
+use Eloquent\Typhax\Type\ExtensionType;
use Eloquent\Typhax\Type\FloatType;
use Eloquent\Typhax\Type\IntegerType;
use Eloquent\Typhax\Type\MixedType;
@@ -140,6 +141,38 @@ public function rendererData()
$expected = 'foo';
$data['Object type uses relative form when rendered'] = array($expected, $type);
+ $type = new ExtensionType(
+ ClassName::fromString('\ext'),
+ array(),
+ array()
+ );
+ $expected = ':ext';
+ $data['Extension type'] = array($expected, $type);
+
+ $type = new ExtensionType(
+ ClassName::fromString('\ext'),
+ array(new IntegerType, new ArrayType),
+ array()
+ );
+ $expected = ':ext<integer, array>';
+ $data['Extension type with types'] = array($expected, $type);
+
+ $type = new ExtensionType(
+ ClassName::fromString('\ext'),
+ array(),
+ array('foo' => 'bar', 'quux' => 'doom')
+ );
+ $expected = ":ext {foo: 'bar', quux: 'doom'}";
+ $data['Extension type with attributes'] = array($expected, $type);
+
+ $type = new ExtensionType(
+ ClassName::fromString('\ext'),
+ array(new IntegerType, new ArrayType),
+ array('foo' => 'bar', 'quux' => 'doom')
+ );
+ $expected = ":ext<integer, array> {foo: 'bar', quux: 'doom'}";
+ $data['Extension type with types and attributes'] = array($expected, $type);
+
return $data;
}
View
15 test/suite/Eloquent/Typhax/Resolver/ObjectTypeClassNameResolverTest.php
@@ -17,6 +17,7 @@
use Eloquent\Typhax\Type\ArrayType;
use Eloquent\Typhax\Type\BooleanType;
use Eloquent\Typhax\Type\CallableType;
+use Eloquent\Typhax\Type\ExtensionType;
use Eloquent\Typhax\Type\FloatType;
use Eloquent\Typhax\Type\IntegerType;
use Eloquent\Typhax\Type\MixedType;
@@ -57,6 +58,13 @@ public function testResolveObjectTypeName()
$type = new TraversableType(
new ObjectType(ClassName::fromString('Spam')),
new OrType(array(
+ new ExtensionType(
+ ClassName::fromString('Pip'),
+ array(
+ new ObjectType(ClassName::fromString('Kazaam'))
+ ),
+ array('key' => 'value')
+ ),
new ObjectType(ClassName::fromString('Spam')),
new ObjectType(ClassName::fromString('Pip')),
)),
@@ -72,6 +80,13 @@ public function testResolveObjectTypeName()
$expected = new TraversableType(
new ObjectType(ClassName::fromString('\Foo\Bar\Baz\Spam')),
new OrType(array(
+ new ExtensionType(
+ ClassName::fromString('\Qux\Doom\Splat'),
+ array(
+ new ObjectType(ClassName::fromString('\Foo\Bar\Baz\Kazaam'))
+ ),
+ array('key' => 'value')
+ ),
new ObjectType(ClassName::fromString('\Foo\Bar\Baz\Spam')),
new ObjectType(ClassName::fromString('\Qux\Doom\Splat')),
)),
View
6 test/suite/Eloquent/Typhax/Type/ExtensionTypeTest.php
@@ -19,13 +19,13 @@ class ExtensionTypeTest extends PHPUnit_Framework_TestCase
public function testExtensionType()
{
$className = ClassName::fromString('foo');
- $subTypes = array(new IntegerType);
+ $types = array(new IntegerType);
$attributes = array('foo' => 'bar');
- $type = new ExtensionType($className, $subTypes, $attributes);
+ $type = new ExtensionType($className, $types, $attributes);
$this->assertSame($className, $type->className());
- $this->assertSame($subTypes, $type->subTypes());
+ $this->assertSame($types, $type->types());
$this->assertSame($attributes, $type->attributes());
}
}
Something went wrong with that request. Please try again.