Feature Request: Upgrade `Model` Class to Use PHP Attributes
Problem Statement
The Model
class in Hazaar MVC currently lacks support for PHP Attributes, a feature that was introduced in PHP 8. Attributes allow metadata to be defined in a cleaner, more structured way than traditional docblocks, making it easier to read, maintain, and extend model definitions. Leveraging PHP Attributes in the Model
class would enable developers to define field properties, validation rules, relationships, and other metadata directly on model properties.
Who Will Benefit?
Developers using Hazaar MVC who rely on the Model
class for managing application data. Attributes will simplify model code, reduce boilerplate, and make it easier to standardize metadata across models.
Benefits and Risks
Benefits
- Improved readability: Model definitions are cleaner, and properties are easier to understand at a glance.
- Reduced boilerplate: Attributes allow a more declarative syntax, reducing the need for repetitive code.
- Enhanced extensibility: Adding or modifying metadata becomes straightforward with structured attributes.
- Improved maintainability: Centralized attribute definitions reduce the risk of misconfigurations and simplify future modifications.
Risks
- Backward compatibility: Models may need migration to use attributes, and earlier PHP versions are not compatible.
- Learning curve: Developers unfamiliar with PHP Attributes will need guidance on best practices.
Proposed Solution
-
Add PHP Attribute Support to
Model
Properties:- Define custom PHP Attributes for common metadata used in models, including:
-
#[Column]
for specifying field names, data types, and length. -
#[PrimaryKey]
for denoting primary key fields. -
#[Relationship]
for defining relationships such as one-to-many or many-to-one. -
#[Validation]
for specifying validation rules such asrequired
,minLength
, andregex
.
-
- Define custom PHP Attributes for common metadata used in models, including:
-
Modify the
Model
Class to Parse Attributes:- Extend the
Model
constructor or initialization logic to parse and utilize attributes for setting up columns, validation, and relationships.
- Extend the
-
Add Attribute-Based Parsing Logic to Existing Functions:
- Modify any existing methods that rely on model metadata (e.g.,
toArray()
,validate()
, etc.) to consider attributes if they are present.
- Modify any existing methods that rely on model metadata (e.g.,
-
Documentation and Examples:
- Provide documentation detailing how to define model properties using attributes, including usage patterns and examples of each attribute type.
Example Usage
use Hazaar\Model\Attributes\Column;
use Hazaar\Model\Attributes\PrimaryKey;
use Hazaar\Model\Attributes\Validation;
class User extends Model
{
#[PrimaryKey]
#[Column(name: 'id', type: 'int')]
public int $id;
#[Column(name: 'username', type: 'string', length: 255)]
#[Validation(required: true, minLength: 3)]
public string $username;
#[Column(name: 'email', type: 'string', length: 255)]
#[Validation(required: true, regex: '/^.+@.+$/')]
public string $email;
}