-
-
Save laracasts/747205459a93ae08011fd42d5aabcab1 to your computer and use it in GitHub Desktop.
Laracasts Value Object example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Performance extends Model | |
{ | |
public function getRevenueAttribute($revenue) | |
{ | |
return new Revenue($revenue); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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