PHP code example of igorwanbarros / custom-accessor-and-mutator

1. Go to this page and download the library: Download igorwanbarros/custom-accessor-and-mutator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

igorwanbarros / custom-accessor-and-mutator example snippets


             "laravel/framework": "5.1.*",
            "igorwanbarros/custom-accessor-and-mutator": "v1.*"
        }

use Illuminate\Database\Eloquent\Model;
use Igorwanbarros\CustomAccessorAndMutator\CustomAccessorsAndMutators;

class Order extends Model
{
    use CustomAccessorsAndMutators;
    
    protected $table = 'order';
    
    protected $customAccessors = [
        //attribute name you model   => your default method for treating this type of data
        'you_attribute_name'         => '_yourMethodGet',
        'you_other_attribute_name'   => '_yourMethodGet',
    ];
    
    protected $customMutators = [
        'you_atribute_name'         => '_yourMethodGet',
        'you_other_atribute_name'   => '_yourMethodGet',
    ];
    
    
    protected function _youtMethodGet($value)
    {
        //you logic here
        return $value;
    }
    
    
    protected function _youtMethodSet($value)
    {
        //you logic here
        return $value;
    }
}

class ModelBase extends Model 
{
    use CustomAccessorsAndMutators
}


class Order extends Model
{
    protected $table = 'order';
    
    protected $customAccessors = [
        'you_attribute_name'    => '_yourMethodGet',
    ];
    
    protected $customMutators = [
        'you_attribute_name'    => '_yourMethodGet',
    ];
    
    
    protected function _youtMethodGet($value)
    {
        //you logic here
        return $value;
    }
    
    
    protected function _youtMethodSet($value)
    {
        //you logic here
        return $value;
    }
}

namespace App\FormatAccessorsAndMutators;

use Igorwanbarros\CustomAccessorAndMutator\FormatAccessorAndMutator;

class MoneyFormatAccessorsAndMutators implements FormatAccessorAndMutator
{
    public static function get($value)
    {
        //you logic here
        return $value;
    }
    
    
    public static function set($value)
    {
        //you logic here
        return $value;
    }
}

use Illuminate\Database\Eloquent\Model;
use Igorwanbarros\CustomAccessorAndMutator\CustomAccessorsAndMutators;
use App\FormatAccessorsAndMutators\MoneyFormatAccessorsAndMutators;

class Order extends Model
{
    use CustomAccessorsAndMutators;
    
    protected $table = 'order';
    
    protected $customAccessors = [
        'you_attribute_name'         => MoneyFormatAccessorsAndMutators::class,
        'you_other_attribute_name'   => MoneyFormatAccessorsAndMutators::class,
    ];
    
    protected $customMutators = [
        'you_atribute_name'         => MoneyFormatAccessorsAndMutators::class,
        'you_other_atribute_name'   => MoneyFormatAccessorsAndMutators::class,
    ];
    
}