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

Commit bda5586

Browse files
committed
Merge pull request #148 from fredemmott/num-arraykey
Add support for num and arraykey attributes
2 parents ac2c533 + dd058df commit bda5586

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/core/ComposableElement.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,17 @@ final protected function validateAttributeValue<T>(
522522
if (enum_exists($class) && $class::isValid($val)) {
523523
break;
524524
}
525+
// Things that are a valid array key without any coercion
526+
if ($class === 'HH\arraykey') {
527+
if (is_int($val) || is_string($val)) {
528+
break;
529+
}
530+
}
531+
if ($class === 'HH\num') {
532+
if (is_int($val) || is_float($val)) {
533+
break;
534+
}
535+
}
525536
throw new XHPInvalidAttributeException(
526537
$this, $class, $attr, $val
527538
);

tests/AttributesTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class :test:attribute-types extends :x:element {
1212
enum {'foo', 'bar'} myenum,
1313
float myfloat,
1414
Vector<string> myvector,
15-
Map<string, string> mymap;
15+
Map<string, string> mymap,
16+
arraykey myarraykey,
17+
num mynum;
1618

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

74+
public function testValidArrayKeys(): void {
75+
$x = <test:attribute-types myarraykey="foo" />;
76+
$this->assertSame('<div></div>', $x->toString());
77+
$x = <test:attribute-types myarraykey={123} />;
78+
$this->assertSame('<div></div>', $x->toString());
79+
}
80+
81+
/**
82+
* @expectedException XHPInvalidAttributeException
83+
*/
84+
public function testInvalidArrayKeys(): void {
85+
$x = <test:attribute-types myarraykey={1.23} />;
86+
$x->toString();
87+
}
88+
89+
public function testValidNum(): void {
90+
$x = <test:attribute-types mynum={123} />;
91+
$this->assertSame('<div></div>', $x->toString());
92+
$x = <test:attribute-types mynum={1.23} />;
93+
$this->assertSame('<div></div>', $x->toString());
94+
}
95+
96+
/**
97+
* @expectedException XHPInvalidAttributeException
98+
*/
99+
public function testInvalidNum(): void {
100+
$x = <test:attribute-types mynum="123" />;
101+
$x->toString();
102+
}
103+
72104
public function testNoAttributes(): void {
73105
$this->assertEquals('<div></div>', <test:attribute-types />);
74106
}

0 commit comments

Comments
 (0)