Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Laracasts Value Object example.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Performance extends Model
{
public function getRevenueAttribute($revenue)
{
return new Revenue($revenue);
}
}
<?php
namespace App;
class Revenue
{
private $revenue;
public function __construct($revenue)
{
$this->revenue = $revenue;
}
public function inDollars()
{
return $this->revenue / 100; // 86
}
public function asCurrency()
{
return money_format('$%i', $this->inDollars()); // $86.00
}
public function __toString()
{
return (string) $this->AsCurrency();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment