-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* build: upgrade dom requirement and loosen version range * docs: update examples * feature: trim whitespace when there are only template children closes #363 * maintenance: phpstorm analysis improvements * tweak: remove data-element attribute * feature: remove element from bind key modifier closes #481 * test: more tests * tweak: keep up to date with docs * tweak: simplify iterable objects * test: add test to missing coverage line
- Loading branch information
Showing
5 changed files
with
236 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
use Gt\Dom\HTMLDocument; | ||
use Gt\DomTemplate\Bind; | ||
use Gt\DomTemplate\BindableCache; | ||
use Gt\DomTemplate\Binder; | ||
use Gt\DomTemplate\BindGetter; | ||
use Gt\DomTemplate\DocumentBinder; | ||
use Gt\DomTemplate\ElementBinder; | ||
use Gt\DomTemplate\HTMLAttributeBinder; | ||
use Gt\DomTemplate\HTMLAttributeCollection; | ||
use Gt\DomTemplate\ListBinder; | ||
use Gt\DomTemplate\ListElementCollection; | ||
use Gt\DomTemplate\PlaceholderBinder; | ||
use Gt\DomTemplate\TableBinder; | ||
|
||
require __DIR__ . "/../../vendor/autoload.php"; | ||
|
||
// EXAMPLE CODE: https://github.com/PhpGt/DomTemplate/wiki/Binding-objects#iterator-and-iteratoraggregate-objects | ||
|
||
$html = <<<HTML | ||
<!DOCTYPE html> | ||
<h1>Events for <span data-bind:text="dateString">1st Jan 2000</span>:</h1> | ||
<ol> | ||
<li data-list> | ||
<time data-bind:text="eventStartTimeText">00:00</time> | ||
<p data-bind:text="title">Title of event</p> | ||
</li> | ||
</ol> | ||
HTML; | ||
|
||
function example(Binder $binder, CalendarDay $calendarDay):void { | ||
$binder->bindData($calendarDay); | ||
} | ||
|
||
class CalendarDay implements IteratorAggregate { | ||
private DateTime $dateTime; | ||
|
||
public function __construct( | ||
public int $year, | ||
public int $month, | ||
public int $day, | ||
private EventRepository $eventRepository, | ||
) { | ||
$this->dateTime = new DateTime("$year-$month-$day"); | ||
} | ||
|
||
#[BindGetter] | ||
public function getDateString():string { | ||
return $this->dateTime->format("jS M Y"); | ||
} | ||
|
||
/** @return Traversable<CalendarEvent> */ | ||
public function getIterator():Traversable { | ||
yield from $this->eventRepository->getEventsForDate( | ||
$this->year, | ||
$this->month, | ||
$this->day, | ||
); | ||
} | ||
} | ||
|
||
class CalendarEvent { | ||
public function __construct( | ||
public string $id, | ||
public DateTime $eventStart, | ||
public string $title, | ||
) {} | ||
|
||
#[BindGetter] | ||
public function getEventStartTimeText():string { | ||
return $this->eventStart->format("H:i"); | ||
} | ||
} | ||
|
||
// END OF EXAMPLE CODE | ||
|
||
class EventRepository { | ||
public function getEventsForDate(int $y, int $m, int $d):array { | ||
return [ | ||
new CalendarEvent(uniqid(), new DateTime(), "First event"), | ||
new CalendarEvent(uniqid(), new DateTime("+15 mins"), "Second event"), | ||
new CalendarEvent(uniqid(), new DateTime("+2 hours"), "Third event"), | ||
]; | ||
} | ||
} | ||
|
||
$calendar = new CalendarDay(2024, 02, 14, new EventRepository()); | ||
$document = new HTMLDocument($html); | ||
$binder = new DocumentBinder($document); | ||
$htmlAttributeBinder = new HTMLAttributeBinder(); | ||
$tableBinder = new TableBinder(); | ||
$listBinder = new ListBinder(); | ||
$placeholderBinder = new PlaceholderBinder(); | ||
$elementBinder = new ElementBinder(); | ||
$htmlAttributeCollection = new HTMLAttributeCollection(); | ||
$elementBinder->setDependencies($htmlAttributeBinder, $htmlAttributeCollection, $placeholderBinder); | ||
$listElementCollection = new ListElementCollection($document); | ||
$bindableCache = new BindableCache(); | ||
$listBinder->setDependencies($elementBinder, $listElementCollection, $bindableCache, $tableBinder); | ||
$htmlAttributeBinder->setDependencies($listBinder, $tableBinder); | ||
$binder->setDependencies( | ||
$elementBinder, | ||
$placeholderBinder, | ||
$tableBinder, | ||
$listBinder, | ||
$listElementCollection, | ||
$bindableCache, | ||
); | ||
|
||
example($binder, $calendar); | ||
$binder->cleanupDocument(); | ||
echo $document; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters