File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -24,11 +24,13 @@ public function __construct(
2424 }
2525
2626 public function __get (string $ name ):?string {
27+ $ name = $ this ->correctCamelCase ($ name );
2728 $ keyValuePairs = call_user_func ($ this ->getterCallback );
2829 return $ keyValuePairs [$ name ] ?? null ;
2930 }
3031
3132 public function __set (string $ name , string $ value ):void {
33+ $ name = $ this ->correctCamelCase ($ name );
3234 $ keyValuePairs = call_user_func ($ this ->getterCallback );
3335 $ keyValuePairs [$ name ] = $ value ;
3436 call_user_func ($ this ->setterCallback , $ keyValuePairs );
@@ -46,4 +48,18 @@ public function count():int {
4648 $ keyValuePairs = call_user_func ($ this ->getterCallback );
4749 return count ($ keyValuePairs );
4850 }
51+
52+ private function correctCamelCase (string $ name ):string {
53+ preg_match_all (
54+ '/((?:^|[A-Z])[a-z]+)/ ' ,
55+ $ name ,
56+ $ matches
57+ );
58+
59+ $ wordArray = [];
60+ foreach ($ matches [0 ] as $ word ) {
61+ array_push ($ wordArray , lcfirst ($ word ));
62+ }
63+ return implode ("- " , $ wordArray );
64+ }
4965}
Original file line number Diff line number Diff line change @@ -38,6 +38,19 @@ public function testCreateDatasetWithDataAttribute():void {
3838 self ::assertEquals ("php " , $ domStringMap ->language );
3939 }
4040
41+ public function testSetCorrectsCamelCase ():void {
42+ $ document = new HTMLDocument ();
43+ $ element = $ document ->createElement ("example-element " );
44+ $ domStringMap = DOMStringMapFactory::createDataset ($ element );
45+ self ::assertCount (0 , $ element ->attributes );
46+ $ domStringMap ->thisIsCamelCase = "test " ;
47+ self ::assertCount (1 , $ element ->attributes );
48+ $ attribute = $ element ->attributes [0 ];
49+ self ::assertSame ("data-this-is-camel-case " , $ attribute ->name );
50+ self ::assertSame ("test " , $ attribute ->value );
51+ self ::assertSame ("test " , $ element ->dataset ->thisIsCamelCase );
52+ }
53+
4154 public function testCreateDatasetMutate ():void {
4255 $ attributeArray = [
4356 "id " => "example " ,
You can’t perform that action at this time.
0 commit comments