Skip to content
No description, website, or topics provided.
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.editorconfig
.gitattributes
.gitignore
.travis.yml
LICENSE
README.md
composer.json
phpunit.xml.dist
renovate.json

README.md

Eloquent Castable

Build Status PHP from Packagist Latest Version License

Installation

Require this package, with Composer, in the root directory of your project.

$ composer require artisanry/eloquent-castable

Usage

<?php

class Product extends Eloquent
{
    use \Artisanry\Castable\Castable;

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'price' => 'money'
    ];

    /**
     * The user defined caster classes.
     *
     * @var array
     */
    protected $customCasters = [
        'money' => \App\Casters\MoneyCaster::class
    ];

    /**
     * Config for \App\Casters\MoneyCaster
     *
     * @return array
     */
    public function getMoneyCasterConfig()
    {
        return ['currency' => $this->invoice->currency->code];
    }
}
<?php

namespace App\Casters;

use Artisanry\Castable\Casters\AbstractCaster;

class MoneyCaster extends AbstractCaster
{
    /**
     * {@inheritdoc}
     */
    public function save($value)
    {
        return $value;
    }

    /**
     * {@inheritdoc}
     */
    public function load($value)
    {
        return new Money((int) $value, new Currency($this->options['currency']));
    }
}

Testing

$ phpunit

Security

If you discover a security vulnerability within this package, please send an e-mail to hello@basecode.sh. All security vulnerabilities will be promptly addressed.

Credits

This project exists thanks to all the people who contribute.

License

Mozilla Public License Version 2.0 (MPL-2.0).

You can’t perform that action at this time.