Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Add support for num and arraykey attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
fredemmott committed Jul 14, 2015
1 parent ac2c533 commit dd058df
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/core/ComposableElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,17 @@ final protected function validateAttributeValue<T>(
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
);
Expand Down
34 changes: 33 additions & 1 deletion tests/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class :test:attribute-types extends :x:element {
enum {'foo', 'bar'} myenum,
float myfloat,
Vector<string> myvector,
Map<string, string> mymap;
Map<string, string> mymap,
arraykey myarraykey,
num mynum;

protected function render(): XHPRoot {
return <div />;
Expand Down Expand Up @@ -69,6 +71,36 @@ public function testValidTypes(): void {
$this->assertEquals('<div></div>', $x->toString());
}

public function testValidArrayKeys(): void {
$x = <test:attribute-types myarraykey="foo" />;
$this->assertSame('<div></div>', $x->toString());
$x = <test:attribute-types myarraykey={123} />;
$this->assertSame('<div></div>', $x->toString());
}

/**
* @expectedException XHPInvalidAttributeException
*/
public function testInvalidArrayKeys(): void {
$x = <test:attribute-types myarraykey={1.23} />;
$x->toString();
}

public function testValidNum(): void {
$x = <test:attribute-types mynum={123} />;
$this->assertSame('<div></div>', $x->toString());
$x = <test:attribute-types mynum={1.23} />;
$this->assertSame('<div></div>', $x->toString());
}

/**
* @expectedException XHPInvalidAttributeException
*/
public function testInvalidNum(): void {
$x = <test:attribute-types mynum="123" />;
$x->toString();
}

public function testNoAttributes(): void {
$this->assertEquals('<div></div>', <test:attribute-types />);
}
Expand Down

0 comments on commit dd058df

Please sign in to comment.