Use a Value Object in your Entity Accessors

Written by James McDonald

May 24, 2022

Just looking at Laracasts https://laracasts.com/series/whip-monstrous-code-into-shape/episodes/6 and God Object cleanup and found you can return a Value Object which can have multiple methods in the object with allows you to return different formatting or return a default value if you access it as a property (e.g. $item->code_desc).

Example of using a Value Object in Cakephp 4 Entity

https://book.cakephp.org/4/en/orm/entities.html#accessors-mutators

// Entity class. Note the _getCodeDesc method now returns a value object

<?php

declare(strict_types=1);

namespace App\Model\Entity;

use Cake\ORM\Entity;
use Cake\I18n\Number;

class Item extends Entity
{
    
    protected $_virtual = ['code_desc', 'price_as_dollars'];
    protected $_accessible = [
        'active' => true,
        //...
    ];

    // cake creates a code_desc property on this entity
    // which is now an object with methods available to do cool stuff :)
    protected function _getCodeDesc()
    {
        return new CodeDescription($this->code, $this->description);
    }

    protected function _getPriceAsDollars()
    {
        return Number::format($this->price / 100000, ['places' => 4, 'locale' => 'en_AU']);
    }
}

Class created to allow multiple types of formatting to be available for an entity property

<?php
// App/Model/Entity//CodeDescription.php

declare(strict_types=1);

namespace App\Model\Entity;

class CodeDescription
{
    // auto promotion!!!
    public function __construct(private ?string $code, private ?string $description)
    {
    }

    public function asEntered()
    {
        return $this->code . ' - ' . $this->description;
    }

    public function toUpper()
    {
        return strtoupper($this->asEntered());
    }

    public function __toString()
    {
        return $this->toUpper();
    }
}

Examples showing calling the Value Object methods in a view class.

In a view. Get the default value which calls the __toString() method which returns code description in upper case.

<div class="row">
    <div class="col">
        <h3><?= h($item->code_desc) ?></h3>
        <!-- or this way is the same --> <!-- <h3><?= h($item->codeDesc->toUpper()) ?></h3> -->
    </div>
</div>

Or to get it without upper case just as entered

<div class="row">
    <div class="col">
        <h3><?= h($item->code_desc->asEntered()) ?></h3>
    </div>
</div>

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Network speed test host to host

On Ubuntu / Debian apt-get install iperf3 On Windows download it from https://iperf.fr/iperf-download.php#windows Make...

Clear HSTS Settings in CHrome

Open chrome://net-internals/#hsts enter the domain in the query field and click Query to confirm it has HSTS settings...