From dd058df64e1d776c4251f2b9a10cb6e29c91d98b Mon Sep 17 00:00:00 2001 From: Fred Emmott Date: Tue, 14 Jul 2015 09:37:55 -0700 Subject: [PATCH] Add support for num and arraykey attributes --- src/core/ComposableElement.php | 11 +++++++++++ tests/AttributesTest.php | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/core/ComposableElement.php b/src/core/ComposableElement.php index ca3c4fee..073e6511 100644 --- a/src/core/ComposableElement.php +++ b/src/core/ComposableElement.php @@ -522,6 +522,17 @@ final protected function validateAttributeValue( if (enum_exists($class) && $class::isValid($val)) { break; } + // Things that are a valid array key without any coercion + if ($class === 'HH\arraykey') { + if (is_int($val) || is_string($val)) { + break; + } + } + if ($class === 'HH\num') { + if (is_int($val) || is_float($val)) { + break; + } + } throw new XHPInvalidAttributeException( $this, $class, $attr, $val ); diff --git a/tests/AttributesTest.php b/tests/AttributesTest.php index 1bdba0d1..ddf493d3 100644 --- a/tests/AttributesTest.php +++ b/tests/AttributesTest.php @@ -12,7 +12,9 @@ class :test:attribute-types extends :x:element { enum {'foo', 'bar'} myenum, float myfloat, Vector myvector, - Map mymap; + Map mymap, + arraykey myarraykey, + num mynum; protected function render(): XHPRoot { return
; @@ -69,6 +71,36 @@ public function testValidTypes(): void { $this->assertEquals('
', $x->toString()); } + public function testValidArrayKeys(): void { + $x = ; + $this->assertSame('
', $x->toString()); + $x = ; + $this->assertSame('
', $x->toString()); + } + + /** + * @expectedException XHPInvalidAttributeException + */ + public function testInvalidArrayKeys(): void { + $x = ; + $x->toString(); + } + + public function testValidNum(): void { + $x = ; + $this->assertSame('
', $x->toString()); + $x = ; + $this->assertSame('
', $x->toString()); + } + + /** + * @expectedException XHPInvalidAttributeException + */ + public function testInvalidNum(): void { + $x = ; + $x->toString(); + } + public function testNoAttributes(): void { $this->assertEquals('
', ); }