Last active
November 5, 2015 15:55
-
-
Save caferrari/26a53f7a0a363cf968ed to your computer and use it in GitHub Desktop.
carro
This file contains hidden or 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 | |
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