Skip to content

Instantly share code, notes, and snippets.

@psamatt psamatt/car
Created Nov 21, 2013

Embed
What would you like to do?
DDD Car entity
<?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
You can’t perform that action at this time.