2017 © Pedro Peláez
 

library cast

Laravel Value Objects: Cast your Eloquent model attributes to value objects with ease!

image

redcrystal/cast

Laravel Value Objects: Cast your Eloquent model attributes to value objects with ease!

  • Sunday, December 6, 2015
  • by redcrystalcode
  • Repository
  • 5 Watchers
  • 17 Stars
  • 2,824 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 6 Versions
  • 5 % Grown

The README.md

Laravel Value Objects

Build Status Latest Stable Version Total Downloads, (*1)

Cast your Eloquent model attributes to value objects with ease!, (*2)

Requirements

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

Note: Running tests for this package requires PHP >=5.6., (*4)

Install

Require this package with composer using the following command:, (*5)

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., (*6)

<?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., (*7)

<?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., (*8)

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

$user->email; // returns instance of App\ValueObjects\Email
$user->email->toScalar(); // "someone@example.com"
(string) $user->email; // "someone@example.com"

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

$user = new User();

$user->email = "someone@example.com";
$user->email = new Email("someone@example.com");

License

This package is open-source software licensed under the MIT license., (*10)

The Versions

06/12 2015

dev-master

9999999-dev

Laravel Value Objects: Cast your Eloquent model attributes to value objects with ease!

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zain Mehdi

06/12 2015

dev-develop

dev-develop

Laravel Value Objects: Cast your Eloquent model attributes to value objects with ease!

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zain Mehdi

06/12 2015

v1.0.3

1.0.3.0

Laravel Value Objects: Cast your Eloquent model attributes to value objects with ease!

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zain Mehdi

26/11 2015

v1.0.2

1.0.2.0

Laravel Value Objects: Cast your Eloquent model attributes to value objects with ease!

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zain Mehdi

23/11 2015

v1.0.1

1.0.1.0

Cast your model attributes to value objects!

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zain Mehdi

23/11 2015

v1.0

1.0.0.0

Cast your model attributes to value objects!

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zain Mehdi