Skip to content

Commit

Permalink
Improvements for next minor release (#94)
Browse files Browse the repository at this point in the history
* Fix return type for getElementById, closes #65

* Update PHPDoc to include inherited changes

* Rename function for removing template attributes

* Set value for certain input fields differently

* Bind list to document fragment for efficiency
for #77

* Remove unbindable check

* Allow returning any iterable, not just array - fixes #88

* Implement and test bind* function with get* kept for compatibility
Closes #89
  • Loading branch information
g105b committed Sep 18, 2019
1 parent 2ec8358 commit 4f38cd4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/BindDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface BindDataMapper {
* "age" => date("Y") - $this->dob->format("Y"),
* ]
*/
public function bindDataMap():array;
public function bindDataMap():iterable;
}
22 changes: 10 additions & 12 deletions src/Bindable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions test/unit/BindDataGetterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Gt\DomTemplate\Test;

use Gt\DomTemplate\BindDataGetter;
use Gt\DomTemplate\HTMLDocument;
use Gt\DomTemplate\Test\Helper\BindDataGetter\TodoItem;
use Gt\DomTemplate\Test\Helper\Helper;
use PHPUnit\Framework\TestCase;

class BindDataGetterTest extends TestCase {
public function testBindFunction() {
$id = rand(100, 1000);
$name = uniqid();
$sut = new TodoItem($id, $name);

$document = new HTMLDocument(Helper::HTML_TODO_LIST);
$document->extractTemplates();
$document->bindList([$sut]);

$li = $document->querySelector("li");
self::assertEquals($id, $li->querySelector("[name='id']")->value);
self::assertEquals($name, $li->querySelector("[name='title']")->value);
}
}
29 changes: 29 additions & 0 deletions test/unit/Helper/BindDataGetter/TodoItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Gt\DomTemplate\Test\Helper\BindDataGetter;

use Gt\DomTemplate\BindDataGetter;

class TodoItem implements BindDataGetter {
private $id;
private $title;

public function __construct(
int $id,
string $title
) {
$this->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();
}
}

0 comments on commit 4f38cd4

Please sign in to comment.