diff --git a/src/BindDataMapper.php b/src/BindDataMapper.php index f42fddc..f4177c7 100644 --- a/src/BindDataMapper.php +++ b/src/BindDataMapper.php @@ -11,5 +11,5 @@ interface BindDataMapper { * "age" => date("Y") - $this->dob->format("Y"), * ] */ - public function bindDataMap():array; + public function bindDataMap():iterable; } \ No newline at end of file diff --git a/src/Bindable.php b/src/Bindable.php index 1731a56..3681dea 100644 --- a/src/Bindable.php +++ b/src/Bindable.php @@ -62,22 +62,20 @@ public function bindData( } elseif($kvp instanceof BindDataGetter) { $iterable = []; + $prefixes = ["bind", "get"]; - foreach(get_class_methods($kvp) as $method) { - if(strpos($method, "get") !== 0) { - continue; - } + foreach($prefixes as $prefix) { + foreach(get_class_methods($kvp) as $method) { + if(strpos($method, $prefix) !== 0) { + continue; + } - $key = lcfirst(substr($method, 3)); - $value = $kvp->$method(); - $iterable[$key] = $value; + $key = lcfirst(substr($method, strlen($prefix))); + $value = $kvp->$method(); + $iterable[$key] = $value; + } } } - else { - throw new UnbindableObjectException( - get_class($kvp) - ); - } } foreach($iterable as $key => $value) { diff --git a/test/unit/BindDataGetterTest.php b/test/unit/BindDataGetterTest.php new file mode 100644 index 0000000..e51dfaf --- /dev/null +++ b/test/unit/BindDataGetterTest.php @@ -0,0 +1,24 @@ +extractTemplates(); + $document->bindList([$sut]); + + $li = $document->querySelector("li"); + self::assertEquals($id, $li->querySelector("[name='id']")->value); + self::assertEquals($name, $li->querySelector("[name='title']")->value); + } +} \ No newline at end of file diff --git a/test/unit/Helper/BindDataGetter/TodoItem.php b/test/unit/Helper/BindDataGetter/TodoItem.php new file mode 100644 index 0000000..7919ab1 --- /dev/null +++ b/test/unit/Helper/BindDataGetter/TodoItem.php @@ -0,0 +1,29 @@ +id = $id; + $this->title = $title; + } + + public function getTitle():string { + return $this->title; + } + + + public function bindId():int { + return $this->id; + } + public function bindTitle():string { + return $this->getTitle(); + } +} \ No newline at end of file