-
-
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.
Simplified casting of bind value (#399)
* build: upgrade dom requirement and loosen version range * docs: update examples * feature: trim whitespace when there are only template children closes #363 * feature: simplify casting in value binding closes #398
- Loading branch information
Showing
8 changed files
with
117 additions
and
5 deletions.
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,45 @@ | ||
<?php | ||
use Gt\Dom\HTMLDocument; | ||
use Gt\DomTemplate\Bind; | ||
use Gt\DomTemplate\DocumentBinder; | ||
|
||
require __DIR__ . "/../../vendor/autoload.php"; | ||
|
||
// EXAMPLE CODE: https://github.com/PhpGt/DomTemplate/wiki/Binding-objects | ||
|
||
class Customer { | ||
public function __construct( | ||
public readonly string $id, | ||
public readonly string $name, | ||
public readonly DateTime $createdAt, | ||
) {} | ||
|
||
#[Bind("years-active")] | ||
public function calculateYearsSinceCreation():int { | ||
$now = new DateTime(); | ||
$diff = $now->diff($this->createdAt); | ||
return $diff->y; | ||
} | ||
} | ||
|
||
$html = <<<HTML | ||
<h1>Welcome back, <span data-bind:text="name">your name</span>!</h1> | ||
<p>You have been a customer for <time data-bind:text="createdAt">0</time> years.</p> | ||
HTML; | ||
|
||
function example(DocumentBinder $binder):void { | ||
// The $customer variable could come from a database in a real-world project. | ||
$customer = new Customer( | ||
id: "abc123", | ||
name: "Cody", | ||
createdAt: new DateTime("2016-07-06 15:04:00"), | ||
); | ||
$binder->bindData($customer); | ||
} | ||
|
||
$document = new HTMLDocument($html); | ||
$binder = new DocumentBinder($document); | ||
|
||
example($binder); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
namespace Gt\DomTemplate; | ||
|
||
use DateTimeInterface; | ||
use Stringable; | ||
|
||
class BindValue implements Stringable { | ||
public function __construct( | ||
private readonly mixed $rawValue | ||
) {} | ||
|
||
public function __toString():string { | ||
$value = $this->rawValue ?? ""; | ||
|
||
if($value instanceof DateTimeInterface) { | ||
$value = $value->format(DateTimeInterface::RFC7231); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
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
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