Content (DTO)

When passing on data to your view and partials the data gain MaplePHP´s DTO traversal functionality.

Example

Set partial

Let´s say we pass on this data to a new template named family.

$this->provider->view()->setPartial("main.family", [
    "tagline" => "The Doe`s",
    "family" => [
        "x" => ["firstname" => "<em>john</em>", "lastname" => "doe"],
        "y" => ["firstname" => "<em>jane</em>", "lastname" => "doe"],
        "children" => [
            ["firstname" => "David", "lastname" => "Doe"],
            ["firstname" => "Sara", "lastname" => "Doe"]
        ]
    ]
]);

Create partial file

Let´s take a quick look at the advanced DTO traverse example bellow.

<div class="article bg-secondary border-bottom">
    <div class="wrapper max-w-screen-md align-center">
        <?php echo $obj->tagline("Dom")->create("h2")->attr("class", "title mb-50"); ?>
        <div class="columns-2">
            <section class="mb-30">
                <h6 class="title">Husband</h6>
                <h3 class="title"><?php echo $obj->family()->x()->combine(["firstname", "lastname"])->format("Str")->stripTags()->ucfirst(); ?></h3>
            </section>
            <section class="mb-30">
                <h6 class="title">Wife</h6>
                <h3 class="title"><?php echo $obj->family()->y()->combine(["firstname", "lastname"])->format("Str")->stripTags()->ucfirst(); ?></h3>
            </section>
        </div>
        <aside>
            <h3>Has children:</h3>
            <ul>
                <?php foreach($obj->family()->children()->fetch()->get() as $row): ?>
                <li><?php echo $row->combine(["firstname", "lastname"]); ?></li>
                <?php endforeach; ?>
            </ul>
        </aside>
    </div>
</div>

How does it work?

When you pass data to the template engine it will make it possible for you to easily traverse the data. you can then use one of the Handlers to modify the data when you have traversed to the right position.

$obj->arrayKeyA()->arrayKeyB("HANDLER")->formatMethodA()->formatMethodB();

If you compare the setPartial method example above with bellow you will see that you can very easily traverse the data with object and also format the strings.

echo $obj->family()->x()->firstname;
echo $obj->family()->x()->combine(["firstname", "lastname"])->format("Str")->stripTags()->ucfirst()
// <em>john</em>
// John doe

Traversing the feed

You can even traverse arrays:


foreach($obj->family()->children()->fetch()->get() as $row) {
	echo $row->firstname("Str")->toUpper()."<br>";
	echo $row->lastname."<br>";
}
// DAVID Doe
// SARA Doe

Handlers

You can access some Handler to make your life easier. There is different ways to access a DTO handler and I will start showing how you can access the Str handler in 2 ways.

Using the last traverse column argument

echo $obj->family()->x()->firstname("Str")->stripTags()->ucfirst();
// John

Using the the format method

echo $obj->family()->x()->firstname()->format("Str")->stripTags()->ucfirst();
// John

DTO Handlers you can use:

  • Str: Modify/format strings

  • Num: Modify/format numbers

  • Arr: Modify/format arrays

  • DateTime: Access the PSR Clock and PHP DateTime class object

  • Encode: Encode string

  • Dom: Access MaplePHP Dom

Handler examples

echo $obj->firstname("Str")->stripTags()->ucfirst()."<br>";
// Daniel

echo $obj->price("Num")->toFilesize()."<br>";
// 1.95 kb

echo $obj->price("Num")->round(2)->currency("SEK", 2)."<br>";
// 1 999,99 kr

echo $obj->date("DateTime")->format("y/m/d, \k\l. H:i")."<br>";
// 23/08/21, kl. 14:35

echo  $obj->tagline("Dom")->create("h2")->attr("class", "title")."<br>";
// <h2 class="title">Lorem ipsum</h2>

Using a DTO library in PHP provides benefits such as encapsulating data, enforcing immutability, validating data, facilitating data transformation, maintaining API compatibility, reducing coupling, improving code readability.

Static access

You can also just access the handlers statically:

use MaplePHP\DTO\Format;

Format\Str::value([STRING]);
Format\Num::value([NUMBER]);
Format\Arr::value([ARRAY]);
Format\DateTime::value([STRING]);
Format\Encode::value([STRING]);
Format\Dom::value([STRING]);

Example

echo Format\Str::value("lorem")->ucfirst();
// Lorem

Last updated