https://github.com/mjkhajeh/wporm-extra-casts
Extra type casts for WPORM models, providing advanced validation and sanitization using WordPress functions.
https://github.com/mjkhajeh/wporm-extra-casts
eloquent orm wordpress wordpress-development wordpress-plugin wordpress-theme
Last synced: 5 months ago
JSON representation
Extra type casts for WPORM models, providing advanced validation and sanitization using WordPress functions.
- Host: GitHub
- URL: https://github.com/mjkhajeh/wporm-extra-casts
- Owner: mjkhajeh
- License: mit
- Created: 2025-07-24T18:00:17.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-09-02T21:35:30.000Z (10 months ago)
- Last Synced: 2025-09-02T23:21:45.990Z (10 months ago)
- Topics: eloquent, orm, wordpress, wordpress-development, wordpress-plugin, wordpress-theme
- Language: PHP
- Homepage: https://mjkhajeh.ir/
- Size: 6.84 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WPORM Extra Casts
Extra type casts for WPORM models, providing advanced validation and sanitization using WordPress functions.
## Features
- Seamless integration with WPORM
- Uses WordPress native functions for sanitization and validation
- Supports a wide range of data types
- Extensible for custom data types
## Requirements
- PHP 7.4+
- WordPress 5.0+
- [WPORM](https://github.com/mjkhajeh/wporm) library
## Installation
Install via Composer:
```
composer require mjkhajeh/wporm-extra-casts
```
## Supported Cast Types
| Cast Type | Description | WP Function Used |
|--------------|---------------------------------------------------------------------------------------------|---------------------------|
| Date | Normalizes date values to `Y-m-d` format | `date_i18n`, custom logic |
| Time | Normalizes time values to `H:i:s` format | `date_i18n`, custom logic |
| Serialized | Handles PHP serialized data | `maybe_serialize`, `maybe_unserialize` |
| Slug | Converts strings to URL-friendly slugs | `sanitize_title` |
| Email | Validates and normalizes email addresses | `sanitize_email` |
| URL | Validates and normalizes URLs | `esc_url_raw` |
| FilePath | Sanitizes and normalizes file paths | `sanitize_file_name` |
| PostID | Ensures values are valid WordPress post IDs | `absint` |
| UserID | Ensures values are valid WordPress user IDs | `absint` |
| CustomEnum | Restricts values to a defined set, with sanitization | `sanitize_text_field` |
| Base64 | Handles base64 encoding/decoding | `base64_encode`, `base64_decode` |
| Mobile | Validates and normalizes mobile phone numbers | `sanitize_text_field` |
## Usage
Add the desired cast to your WPORM model’s `$casts` property. Example:
```php
use MJ\WPROM\ExtraCasts\Date;
use MJ\WPROM\ExtraCasts\Slug;
class MyModel extends Model {
protected $casts = [
'published_at' => Date::class,
'post_slug' => Slug::class,
];
}
```
## CustomEnum Example
`CustomEnum` allows you to restrict a field to a set of allowed values, with WordPress sanitization:
```php
use MJ\WPROM\ExtraCasts\CustomEnum;
// Define allowed values
$enum = new CustomEnum(['draft', 'pending', 'published']);
// Getting a value
$value = $enum->get('published'); // returns 'published'
$value = $enum->get('trash'); // returns null
// Setting a value
$set = $enum->set('pending'); // returns 'pending'
$set = $enum->set('unknown'); // returns null
```
You can use `CustomEnum` in your model like this:
```php
class Post extends Model {
protected $casts = [
'status' => [CustomEnum::class, ['draft', 'pending', 'published']]
];
}
```
## License
MIT License © MohammadJafar Khajeh
## Author
MohammadJafar Khajeh
[mjkhajehg@gmail.com](mailto:mjkhajehg@gmail.com)
## Links
- [WPORM](https://github.com/mjkhajeh/wporm)
- [WordPress Developer Resources](https://developer.wordpress.org/reference/)