PHPackages                             ipagdevs/schema-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. ipagdevs/schema-builder

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

ipagdevs/schema-builder
=======================

A powerful library for building schema-driven data models in PHP.

1.0.0(7mo ago)0126↓81.4%1[1 PRs](https://github.com/ipagdevs/schema-builder/pulls)MITPHPPHP &gt;=8.1CI passing

Since Nov 13Pushed 7mo agoCompare

[ Source](https://github.com/ipagdevs/schema-builder)[ Packagist](https://packagist.org/packages/ipagdevs/schema-builder)[ Docs](https://github.com/ipagdevs/schema-builder)[ RSS](/packages/ipagdevs-schema-builder/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (6)Versions (3)Used By (0)

PHP Schema Builder
==================

[](#php-schema-builder)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5beb0ae26690d1658ba200be4e0c951dac6c019d0b1b7dc2fbf8f52f6bf135a6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f69706167646576732f736368656d612d6275696c6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ipagdevs/schema-builder)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://github.com/ipagdevs/schema-builder/actions/workflows/ci.yml/badge.svg)](https://github.com/ipagdevs/schema-builder/actions/workflows/ci.yml)[![Total Downloads](https://camo.githubusercontent.com/0826838795fc559adb57a269e2a5180d5345fb4593bf5e01c1570e29e9b69d0a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f69706167646576732f736368656d612d6275696c6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ipagdevs/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
--------

[](#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) and `hasMany` (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
------------

[](#installation)

Install the library via Composer:

```
composer require ipagdevs/schema-builder
```

Core Concepts
-------------

[](#core-concepts)

### 1. Model

[](#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

[](#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

[](#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
---------------------------------

[](#getting-started-a-simple-example)

Let's create a simple `Review` model.

```
