Skip to content

Idea: Next level Model Properties #21

@JasonTheAdams

Description

@JasonTheAdams
class Model {
	protected static function properties() {
		return [
			(new ModelProperty('firstName')) // default type is 'string'
				->type('string'),
				->requiredOnSave() // can be made without, but cannot be saved without (as opposed to required())
				->validationRule(), // use the Validation library???
				
			(new ModelProperty('lastName'))
				->type('string')
				->default('Jones'),

			(new ModelProperty('weight'))
				->type('int', 'float') // variadic, assumes union type
				->default('Jones')
				->cast(function($value, $property) { return (float) $value }),

			(new UpdatedAtProperty()), // Supports ModelProperty subclasses 🤯

			'email' => ['string', 'foo@example.com']
		];
	}
}

Notes:

  • This is merged with $properties parsed into the $propertiesCollection static property once
  • The final shape is the key is the property name and the value is a ModelProperty
  • For explicit ModelProperty instances the key is taken from the instance
  • For short-hand properties they're parsed into a ModelProperty
  • Also introduce a ModelPropertiesCollection class to get things such as "required properties"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions