Download the PHP package ipagdevs/schema-builder without Composer
On this page you can find all versions of the php package ipagdevs/schema-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ipagdevs/schema-builder
More information about ipagdevs/schema-builder
Files in ipagdevs/schema-builder
Package schema-builder
Short Description A powerful library for building schema-driven data models in PHP.
License MIT
Homepage https://github.com/ipagdevs/schema-builder
Informations about the package schema-builder
PHP Schema Builder
A powerful and intuitive library for building schema-driven data models in PHP. Validate, parse, and serialize complex data structures with ease, ensuring your data models are robust and reliable.
Features
- Fluent Schema Definition: Define your model's structure using a clean and fluent API.
- Rich Type System: Supports
int,string,float,bool,date,enum, and complex types like arrays and nested relationships. - Powerful Validation: Built-in validation rules like
required,nullable,min,max,limit,between, and more. - Relationships: Easily define
has(one) andhasMany(many) relationships between models. - Mutators: Transform attribute values with custom logic on get or set.
- Smart Serialization: Control how your models are converted to JSON, including hiding sensitive attributes.
- Defaults and Nullables: Effortlessly handle default values and nullable attributes.
- Strongly Typed: Designed to work well with modern, strongly-typed PHP (8.1+).
Installation
Install the library via Composer:
Core Concepts
1. Model
The Model is the heart of the library. You extend the base IpagDevs\Model\Model class to create your own data models. Each model is responsible for defining its structure through a schema.
2. Schema
The Schema defines the "shape" of your data: its attributes, types, and validation rules. You define the schema within your model by implementing the schema() method.
3. Mutators
Mutators allow you to apply custom transformations to data when an attribute is set or retrieved. This is perfect for formatting, sanitizing, or deriving values.
Getting Started: A Simple Example
Let's create a simple Review model.
Now, let's use it to parse and handle data:
JSON Output:
Advanced Usage & Features
Here's a more comprehensive Product model that showcases many of the library's features.
Parsing Complex Data
You can now parse a complete data structure that matches the schema.
Attribute Types and Validation
| Method | Description |
|---|---|
->required() |
The attribute must be present in the input data. |
->nullable() |
The attribute can be null. |
->default($value) |
Sets a default value if the attribute is not provided. |
->min($value) |
For float or int, sets a minimum value. |
->max($value) |
For float or int, sets a maximum value. |
->limit($chars) |
For string, enforces a maximum character length. |
->truncate($chars) |
For string, truncates the string if it exceeds the character limit. |
->between($min, $max) |
For string, enforces a min and max character length. |
->positives([...]) |
For bool, defines values that should be parsed as true. |
->negatives([...]) |
For bool, defines values that should be parsed as false. |
->list() or ->array() |
Converts an attribute into an array of its original type. Chainable. |
->hidden() |
Hides the attribute from jsonSerialize() output. |
->hiddenIf(callable $check) |
Hides the attribute from jsonSerialize() if the callback returns true. |
->hiddenIfNull() |
A shortcut for hiding an attribute if its value is null. |
Serialization
The library provides two ways to convert a model to an array.
jsonSerialize()
This method is automatically called by json_encode(). It respects the hidden(), hiddenIf(), and hiddenIfNull() rules, making it safe for public API responses. It also serializes nested models and collections.
toArray()
This method converts the model and all its relations into a "raw" array, including all attributes (even hidden ones). It's useful for debugging or internal data transfer.
Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue for any bugs or feature requests.
License
The MIT License (MIT). Please see License File for more information.
All versions of schema-builder with dependencies
symfony/polyfill-php81 Version ^1.28
symfony/polyfill-php80 Version ^1.28