PHP code example of waavi / laravel-value-objects

1. Go to this page and download the library: Download waavi/laravel-value-objects 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/ */

    

waavi / laravel-value-objects example snippets


    use Illuminate\Database\Eloquent\Model;

    class Device extends Model
    {
        use \Waavi\ValueObjects\Traits\CastsValueObjects;

        /**
         * The attributes that should be casted to native types.
         *
         * @var array
         */
        protected $casts = [
            'temperature'   => Temperature::class,
        ];
    }

    use Waavi\ValueObjects\Single;

    class Temperature extends Single
    {
        /**
         * @return float
         */
        public function inCelsius()
        {
            // In single field value objects, the name of the field is always $value
            return (float) $this->value;
        }

        /**
         * @return float
         */
        public function inKelvin()
        {
            return (float) $this->value + 273.15;
        }

        /**
         * @return float
         */
        public function inFahrenheit()
        {
            return (float) $this->value * 1.8 + 32;
        }
    }

    $device = new Device;
    $device->temperature = new Temperature(30);
    $device->temperature = 30;          // This also works
    echo $device->temperature;          // Prints '30'
    echo $device->temperature->value;         // Prints '30'
    echo $device->temperature->inKelvin();    // Prints 303.15

    use Waavi\ValueObjects\Single;

    class Temperature extends Single
    {
        /**
         *  Make all subzero temperatures equal to zero.
         *  @return void
         */
        public function setValueAttribute($value)
        {
            $this->attributes['value'] = $value > 0 ? $value : 0;
        }

        /**
         * @return float
         */
        public function getCelsiusAttribute()
        {
            return (float) $this->value;
        }

        /**
         * @return float
         */
        public function getKelvinAttribute()
        {
            return (float) $this->value + 273.15;
        }

        /**
         * @return float
         */
        public function getFahrenheitAttribute()
        {
            return (float) $this->value * 1.8 + 32;
        }
    }

    $device = new Device;
    $device->temperature = 30;
    echo $device->temperature;            // Prints '30'
    echo $device->temperature->value;     // Prints '30'
    echo $device->temperature->kelvin;    // Prints 303.15

    use Illuminate\Database\Eloquent\Model;

    class Trip extends Model
    {
        use \Waavi\ValueObjects\Traits\CastsValueObjects;

        /**
         * The attributes that should be casted to native types.
         *
         * @var array
         */
        protected $casts = [
            'origin'        => Coordinate::class,
            'destination'   => Coordinate::class,
        ];
    }

    use Waavi\ValueObjects\Json;

    class Coordinate extends Json
    {
        protected $fillable = ['lat', 'lng'];

        /**
         *  Return the distance between this coordinate and another
         *
         *  @return float
         */
        public function distanceTo(Coordinate $coord) {
            /** Calculate distance **/
            return $distance;
        }
    }

    $trip = new Trip;
    $trip->origin = new Coordinate(['lat' => 20.1221, 'lng' => 12.1231]);
    $trip->destination = new Coordinate(['lat' => 10.13, 'lng' => 12.14]);
    echo $trip->origin;                     // Prints json representation {'lat':'20.1221','lng':'12.1231'}
    echo $trip->origin->lat;              // Prints '20.1221'
    echo $trip->origin->distanceTo($trip->destination);      // Prints distance between them.