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

Feature: Built-in system for converting from query data #19

Open
wants to merge 2 commits into
base: refactor/static-methods-properties
Choose a base branch
from

Conversation

JasonTheAdams
Copy link
Member

Resolves #15

As noted in the Issue, models didn't have a first-class way of building from query data, which was a problem since relationships relied on this.

This PR introduces the ability to build from query data right into the Model class. For models that only have primitive properties, no extra work is needed. If the model has class properties, then the way to convert them is by overloading the castValueForProperty static method. It will probably look something like this:

protected static function castValueForProperty( string $type, $value, string $property ) {
	switch($type) {
		case: DateTimeInterface:
			return new ImmutableDateTime($value);
		default:
			 // let the parent handle the primitives
			return parent::castValueForProperty($type, $value, $property);
	}
}

I decided to leave (although rename) the ModelBuildsFromQueryData interface. I'm really tempted to get rid of it. But I wanted to defer to @borkweb, who first added it, as to whether he thinks it's still necessary. I can't personally think of a use-case where we'd want to pass an interface whose only purpose is to be able to create models, when the underlying model is what's actually concretely passed. That sounds more like a factory.

@JasonTheAdams JasonTheAdams changed the base branch from release/2.0.0 to refactor/static-methods-properties March 8, 2025 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant