Skip to content

Instantly share code, notes, and snippets.

@laracasts
Created April 18, 2016 16:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save laracasts/747205459a93ae08011fd42d5aabcab1 to your computer and use it in GitHub Desktop.
Save laracasts/747205459a93ae08011fd42d5aabcab1 to your computer and use it in GitHub Desktop.
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