-
-
Save psamatt/7590313 to your computer and use it in GitHub Desktop.
DDD Car entity
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 CoreDomain\Car; | |
class Car | |
{ | |
private $id; | |
private $make; | |
private $model; | |
private $registrationNumber; | |
private $color; | |
private $engineSize; | |
private $category; | |
private $dateOwned; | |
public function __construct($make, $model, RegistrationNumber $registrationNumber, AvailableColor $color, $engineSize, AvailableCategory $category) | |
{ | |
$this->make = $make; | |
$this->model = $model; | |
$this->registrationNumber = $registrationNumber; | |
$this->color = $color; | |
$this->engineSize = $engineSize; | |
$this->category = $category; | |
// stamp the time that we owned the car | |
$this->dateOwned = new \DateTime; | |
} | |
public static function register($make, $model, RegistrationNumber $registrationNumber, AvailableColor $color, $engineSize, AvailableCategory $category) | |
{ | |
return new self($make, $model, $registrationNumber, $color, $engineSize, $category); | |
} | |
public function changeRegistrationNumber(RegistrationNumber $registrationNumber) | |
{ | |
$this->registrationNumber = $registrationNumber; | |
} | |
public function repaint(AvailableColor $color) | |
{ | |
$this->color = $color; | |
} | |
public function engineTune($engineSize) | |
{ | |
$this->engineSize = $engineSize; | |
} | |
/** | |
* If we have owned the car for over 3 years, then we need to sell this car as we dont want to a fleet of old cars | |
* | |
* @return boolean | |
*/ | |
public function isReadyForSale() | |
{ | |
$today = new \DateTime; | |
return $today->diff($this->dateOwned)->format('y') >= 3; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment