Cast your Eloquent model attributes to Value Objects with ease.

Overview

Laravel Value Objects

Build Status Latest Stable Version Total Downloads

Cast your Eloquent model attributes to value objects with ease!

Requirements

This package requires PHP >= 5.4. Using the latest version of PHP is highly recommended. Laravel 4.x and 5.x are supported.

Note: Running tests for this package requires PHP >=5.6.

Install

Require this package with composer using the following command:

composer require redcrystal/cast

Set Up

This package lets you easily cast your model attributes to Value Objects that implement our RedCrystal\Cast\ValueObject interface. A simple example is provided below.

<?php
namespace App\ValueObjects;

use RedCrystal\Cast\ValueObject;

class Email implements ValueObject
{
    protected $value;

    public function __construct($value)
    {
        $this->value = $value;
    }

    public function toScalar()
    {
        return $this->value;
    }

    public function __toString() {
        return $this->toScalar();
    }
}

Set up your model by using the included Trait and adding a tiny bit of configuration.

<?php

namespace App;

use App\ValueObjects\Email;
use Illuminate\Database\Eloquent\Model;
use RedCrystal\Cast\CastsValueObjects;

class User extends extends Model {
	use CastsValueObjects;

	
	protected $objects = [
		// name of the attribute => name of the value object class
		'email' => Email::class
	];
	
	// ...
}

Usage

When accessing attributes of your model normally, any attribute you've set up for casting will be returned as an instance of the Value Object.

$user = User::find($id);

$user->email; // returns instance of App\ValueObjects\Email
$user->email->toScalar(); // "[email protected]"
(string) $user->email; // "[email protected]"

You can set an attribute set up for casting with either a scalar (native) value, or an instance of the Value Object.

$user = new User();

$user->email = "[email protected]";
$user->email = new Email("[email protected]");

License

This package is open-source software licensed under the MIT license.

You might also like...
A laravel package to handle sanitize process of model data to create/update model records.

Laravel Model UUID A simple package to sanitize model data to create/update table records. Installation Require the package using composer: composer r

A laravel package to generate model hashid based on model id column.

Laravel Model Hashid A package to generate model hash id from the model auto increment id for laravel models Installation Require the package using co

A package to filter laravel model based on query params or retrieved model collection

Laravel Filterable A package to filter laravel model based on query params or retrived model collection. Installation Require/Install the package usin

Cache chunks of your Blade markup with ease. 🔪

Blade Cache Directive Cache chunks of your Blade markup with ease. Installation You can install the package via Composer: composer require ryangjchand

Use Kafka Producers and Consumers in your laravel app with ease!
Use Kafka Producers and Consumers in your laravel app with ease!

Laravel Kafka Do you use Kafka in your laravel packages? All packages I've seen until today, including some built by myself, does not provide a nice s

Create inline partials in your Blade templates with ease

Create inline partials in your Blade templates with ease. This package introduces a new @capture directive that allows you to capture small parts of y

Turn any Eloquent model into a list!

Listify Turn any Eloquent model into a list! Description Listify provides the capabilities for sorting and reordering a number of objects in a list. T

Collection of the Laravel/Eloquent Model classes that allows you to get data directly from a Magento 2 database.

Laragento LAravel MAgento Micro services Magento 2 has legacy code based on abandoned Zend Framework 1 with really ugly ORM on top of outdated Zend_DB

Laravel Quran is static Eloquent model for Quran.

Laravel Quran بِسْمِ ٱللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ Laravel Quran is static Eloquent model for Quran. The Quran has never changed and never will, bec

Releases(v1.0.3)
Owner
Red Crystal Code
Red Crystal Code
Make your own custom cast type for Laravel model attributes

Laravel Custom Casts Make your own cast type for Laravel model attributes Laravel custom casts works similarly to Eloquent attribute casting, but with

Vladimir Ković 220 Oct 28, 2022
Encryption cast for Eloquent

Encrypted A custom cast class for Laravel Eloquent that encrypts or hashes your values. Package is small and provides just a few, simple, well tested

null 58 Oct 1, 2022
A PHP package that provides common Data and Value Objects, that are Laravel castable.

Common Casts A PHP package that provides common Data and Value Objects, that are Laravel castable. Installation composer require juststeveking/common-

Steve McDougall 2 Sep 21, 2022
Ani Cast - Anime List & Trending App. (Powered by Jikan API)

(Under Development) Ani Cast - Anime Shows App.

Noval 2 Jun 15, 2022
Guess attributes for Laravel model factories

Eloquent Populator This package provides default attributes for Laravel model factories by guessing the best Faker formatters from columns' names and

Guido Cella 68 Aug 11, 2022
Generate previous attributes when saving Eloquent models

This package provides a trait that will generate previous attributes when saving any Eloquent model.

Ricardo Sawir 33 Nov 6, 2022
Generate Data Transfer Objects directly from JSON objects

Json 2 DTO Spatie's Data Transfer Object library is awesome, but typing out DTOs can quickly become a chore. Inspired by Json2Typescript style tools,

null 111 Jan 3, 2023
Store and retrieve settings generally or for model objects in Laravel.

Store and retrieve settings generally or for model objects in Laravel. Documentation You can find the detailed documentation here in Laravel Settings

Pharaonic 7 Dec 19, 2022
🔥 Fire events on attribute changes of your Eloquent model

class Order extends Model { protected $dispatchesEvents = [ 'status:shipped' => OrderShipped::class, 'note:*' => OrderNoteChanged:

Jan-Paul Kleemans 252 Dec 7, 2022
Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

Rubik 4 May 12, 2022