Download the PHP package coder-at-heart/object-models without Composer
On this page you can find all versions of the php package coder-at-heart/object-models. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download coder-at-heart/object-models
More information about coder-at-heart/object-models
Files in coder-at-heart/object-models
Package object-models
Short Description Add schema, control and cast php json as objects and arrays for Laravel projects
License MIT
Informations about the package object-models
A handy way to define, use and convert object and arrays to json
This Laravel package:
- Provides a fluent way to define structure / schema over objects
- Allows you to validate those objects in a consistent way
- Easily cast eloquent json attributes to objectModels for easy handling
- Reduces the number of meta tables in your app
Requirements
This package requires
- PHP 8.1 and
- Laravel 8.0 or higher (including 10).
Installation
Install it:
Defining ObjectModels
Extend the ObjectModel class and extend override the properties()
method
I usually create an app/ObjectModels
folder to store my models
Here's an example from the tests folder:
And the Address
The Country
ENUM
And the Phone
:
Note the use of the docBlock at the top of the classes - this helps with type hinting in your ide.
Instantiating Objects
Can be done through arrays, json or directly
ArrayModels
You don't need an Object Model to use an Array Object:
Casting eloquent model attributes and json columns
You can use ObjectModels and ArrayModels when creating custom casts in your app - Especially useful when casting json columns.
ObjectsModels:
And ArrayModels:
Built-in Property Types
Type | Create With | Underlying Object |
---|---|---|
Another Model | Property::objectModel(); |
ObjectModel |
Bool | Property::bool(); |
php bool |
Date | Property::date(); |
Carbon |
DateTime | Property::dateTime(); |
Carbon |
ENUM | Property::enum(); |
php ENUM |
Property::email(); |
php string | |
Float | Property::float(); |
php string |
Integer | Property::integer(); |
php integer |
Strings | Property::string(); |
php string |
Time | Property::time(); |
Carbon |
an Array | Property::array(); |
php array |
an Array of other models | `Property::objectModelArray(); | arrayModel |
an Array of property | `Property::propertyArray(); | arrayModel |
an Object | Property::object(); |
the Object |
mixed | Property::mixed(); |
any |
Custom Property Types
Extend the Property
class to add in your own propertires.
The two main callbacks are jsonCallback
called when the property is going to be turned into json, and setCallback
when the property is assigned a value.
Here's an object using Custom properties
Object Validation
Object and Arrays can be validated. What you get back is a ObjectValidation Object
Note: ObjectModels or ArrayModels are not validated automatically.
Adding Rules
When you're defining object you can add any valid laravel validation rules
You can get rules on an objectModel using $person->getRules()
Ignoring undefined properties
By default ObjectModels throws an exception if you try and set a property that hasn't been defined.
Get around that by using the IgnoreUndefinedProperties trait
Got an idea / Suggestion / Improvement?
Let me know... somehow.
Support
License
ObjectModel is licensed under the MIT License.
v1.1.14
All versions of object-models with dependencies
ext-json Version *
nesbot/carbon Version ^2.61
illuminate/support Version ^10.0 || ^9.0 || ^10.0
illuminate/validation Version ^8.0 || ^9.0 || ^10.0