Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DTO / Value Object generator? #28

Open
dereuromark opened this issue Apr 18, 2020 · 2 comments
Open

DTO / Value Object generator? #28

dereuromark opened this issue Apr 18, 2020 · 2 comments
Labels
enhancement New feature or request rfc Request For Comments

Comments

@dereuromark
Copy link
Owner

dereuromark commented Apr 18, 2020

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

  • bin/cake value_object generate [fields]
  • Outputs standalone VO classes that don't require the plugin anymore.

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

  • state info (touched fields)
  • a lot of from/to transformation (inflections) the DTOs can offer (or bloat each object)

BUT on the other side

  • Immutable first could mean you need to construct it fully with all required fields and we don't need the setters here anymore.
  • Since it isn't a DTO, but VO, this should be fine.

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 dereuromark added enhancement New feature or request rfc Request For Comments labels Apr 18, 2020
@dereuromark
Copy link
Owner Author

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 dereuromark changed the title Value Object generator? DTO / Value Object generator? Nov 20, 2020
@dereuromark
Copy link
Owner Author

Should this be a bake command maybe even?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request rfc Request For Comments
Projects
None yet
Development

No branches or pull requests

1 participant