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('
', ); }