dereuromark / cakephp-dto

CakePHP DTO plugin - quickly generate useful data transfer objects for your app (mutable/immutable)
MIT License
24 stars 6 forks source link

DTO / Value Object generator? #28

Open dereuromark opened 4 years ago

dereuromark commented 4 years ago

Besides the useful and always regeneratable DTOs, I also saw sometimes the need for some simple standalone objects that are also customizable on some of the properties and behavior. Both for plugins or project level.

So what if we could

Similar to bake a migration, one would define fields as

field:type field_other:type_other|null

on that CLI call

|null indicates optional field.

Alternatively, we could also allow/keep a vo.xml file in config, or allow this as input param to easily regenerate if the need arises.

A result could be:

class Item {

    /**
     * @var string
     */
    protected $name;

    /**
     * @param string $name
     *
     * @return $this
     */
    public function setName(string $name) {
        $this->name = $name;

        return $this;
    }

    /**
     * @return string
     */
    public function getName(): string {
        return $this->name;
    }

    /**
     * @return bool
     */
    public function hasName(): bool {
        return $this->name !== null;
    }

}

It could NOT have

BUT on the other side

If we include this there should be the option to include this as require-dev dependency on other plugins, so they can easily generate those for plugins as well, using e.g.

vendor/bin/vo-generate

or alike.

// @asaliev would do you think?

dereuromark commented 4 years ago

Immutable first could go into the direction of

class ItemObject {

    /**
     * @var string
     */
    protected $name;

    /**
     * @param array $fields
     */
    public function __construct(array $fields = []) {
        foreach ($fields as $field => $value) {
            $this->$field = $value;
        }
    }

    /**
     * @return string
     */
    public function getName(): string {
        return $this->name;
    }

    /**
     * Only exists for nullable properties.
     *
     * @return bool
     */
    public function hasName(): bool {
        return $this->name !== null;
    }

}

Maybe Object should be the suffix here to avoid clashing with Entity names, as they don't have any suffix in the Cake world right now.

dereuromark commented 6 months ago

Should this be a bake command maybe even?

bin/cake bake object Item field:type,field_other:type_other|null,...