Skip to content

Commit

Permalink
Working towards implementing #8
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jan 7, 2018
1 parent a746e23 commit 0272978
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 23 deletions.
86 changes: 72 additions & 14 deletions src/Bindable.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,83 @@ protected function setData(BaseElement $element, iterable $data):void {
$attr->name,$matches)) {
continue;
}
$bindType = $matches[1];

if($bindType === "class") {
$this->handleClassData(
$attr,
$element,
$data
);
}
else {
$this->handlePropertyData(
$attr,
$bindType,
$element,
$data
);
}
}
}

$dataKeyMatch = $this->getKeyFromAttribute($element, $attr);
$dataValue = $dataKeyMatch->getValue($data) ?? "";
protected function handlePropertyData(
Attr $attr,
string $bindType,
BaseElement $element,
iterable $data
):void {
$dataKeyMatch = $this->getKeyFromAttribute($element, $attr);
$dataValue = $dataKeyMatch->getValue($data) ?? "";

switch($matches[1]) {
case "html":
$element->innerHTML = $dataValue;
break;
switch($bindType) {
case "html":
$element->innerHTML = $dataValue;
break;

case "text":
$element->innerText = $dataValue;
break;
case "text":
$element->innerText = $dataValue;
break;

case "value":
$element->value = $dataValue;
break;
case "value":
$element->value = $dataValue;
break;

default:
throw new InvalidBindProperty($matches[1]);
default:
throw new InvalidBindProperty($matches[1]);
}
}

protected function handleClassData(
Attr $attr,
BaseElement $element,
iterable $data
):void {
$classList = explode(" ", $attr->value);
$this->setClassFromData($element, $data, ...$classList);
}

protected function setClassFromData(
BaseElement $element,
iterable $data,
string...$classList
):void {
foreach($classList as $class) {
if(!strstr($class, ":")) {
$class = "$class:$class";
}

list($keyMatch, $className) = explode(":", $class);

if(!isset($data[$keyMatch])) {
continue;
}

if($data[$keyMatch]) {
$element->classList->add($className);
}
else {
$element->classList->remove($className);
}
}
}
Expand Down
21 changes: 12 additions & 9 deletions test/unit/BindableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ public function testBindWithOptionalKey() {
public function testBindClass() {
$document = new HTMLDocument(Helper::HTML_TODO_LIST_BIND_CLASS);
$todoData = [
["title" => "Write tests", "complete" => true],
["title" => "Implement features", "complete" => false],
["title" => "Pass tests", "complete" => false],
["id" => 1, "title" => "Write tests", "complete" => true],
["id" => 2, "title" => "Implement features", "complete" => false],
["id" => 3, "title" => "Pass tests", "complete" => false],
];
$document->extractTemplates();
$todoListElement = $document->getElementById("todo-list");
Expand All @@ -250,9 +250,9 @@ public function testBindClass() {
public function testBindClassColon() {
$document = new HTMLDocument(Helper::HTML_TODO_LIST_BIND_CLASS_COLON);
$todoData = [
["title" => "Write tests", "dateTimeCompleted" => "2018-07-01 19:46:00"],
["title" => "Implement features", "dateTimeCompleted" => null],
["title" => "Pass tests", "dateTimeCompleted" => "2018-07-01 19:49:00"],
["id" => 1, "title" => "Write tests", "dateTimeCompleted" => "2018-07-01 19:46:00"],
["id" => 2, "title" => "Implement features", "dateTimeCompleted" => null],
["id" => 3, "title" => "Pass tests", "dateTimeCompleted" => "2018-07-01 19:49:00"],
];
$document->extractTemplates();
$todoListElement = $document->getElementById("todo-list");
Expand All @@ -275,16 +275,19 @@ public function testBindClassColonMultiple() {
$document = new HTMLDocument(Helper::HTML_TODO_LIST_BIND_CLASS_COLON);
$todoData = [
[
"id" => 1,
"title" => "Write tests",
"dateTimeCompleted" => "2018-07-01 19:46:00",
"dateTimeDeleted" => null,
],
[
"id" => 2,
"title" => "Implement features",
"dateTimeCompleted" => null,
"dateTimeDeleted" => "2018-07-01 19:54:00",
],
[
"id" => 3,
"title" => "Pass tests",
"dateTimeCompleted" => "2018-07-01 19:49:00",
"dateTimeDeleted" => null,
Expand All @@ -297,6 +300,8 @@ public function testBindClassColonMultiple() {
$items = $todoListElement->querySelectorAll("li");

foreach($todoData as $i => $todoDatum) {
self::assertTrue($items[$i]->classList->contains("existing-class"));

$completed = (bool)$todoDatum["dateTimeCompleted"];
self::assertEquals(
$completed,
Expand All @@ -305,11 +310,9 @@ public function testBindClassColonMultiple() {

$deleted = (bool)$todoDatum["dateTimeDeleted"];
self::assertEquals(
$completed,
$deleted,
$items[$i]->classList->contains("deleted")
);

self::assertTrue($items[$i]->classList->contains("existing-class"));
}
}
}

0 comments on commit 0272978

Please sign in to comment.