Skip to content

Instantly share code, notes, and snippets.

@psamatt
Created November 21, 2013 21:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psamatt/7590313 to your computer and use it in GitHub Desktop.
Save psamatt/7590313 to your computer and use it in GitHub Desktop.
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