Skip to content

Instantly share code, notes, and snippets.

@caferrari
Last active November 5, 2015 15:55
Show Gist options
  • Save caferrari/26a53f7a0a363cf968ed to your computer and use it in GitHub Desktop.
Save caferrari/26a53f7a0a363cf968ed to your computer and use it in GitHub Desktop.
carro
<?php
class Color
{
private const VALID_COLORS = ["branco", "preto", "vermelho", "azul"];
private $color;
private function __construct($color) {
$this->color = $color;
}
public static createFromString($color)
{
$color = mb_strtolower($color);
if (!in_array($color, static::VALID_COLORS)) {
throw new OutOfBoundsException("Invalid color: $color");
}
return new static($color);
}
}
class Car
{
private $color;
private function __construct(Cor $color)
{
$this->color = $color;
}
public static function create($color)
{
// por enquanto passa, mas o ideal seria um builder =)
return new static(Color::createFromString($color));
}
}
$carro = Car::create("preto");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment