Last active
February 2, 2016 15:24
-
-
Save nicholasruunu/ea6b4e55ae14c19e4c17 to your computer and use it in GitHub Desktop.
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 | |
final class MonthNumber | |
{ | |
private $number; | |
public function __construct(int $number) | |
{ | |
$this->number = $number; | |
} | |
public function toInt(): int | |
{ | |
if ($this->number < 1 || $this->number > 12) { | |
throw new Exception( | |
"Month number: {$this->number} needs to be between 1 and 12" | |
); | |
} | |
return $this->number; | |
} | |
} | |
final class MonthName | |
{ | |
private $monthNumber; | |
public function __construct(MonthNumber $monthNumber) | |
{ | |
$this->monthNumber = $monthNumber; | |
} | |
public function toString(): string | |
{ | |
try { | |
return date('F', mktime(0, 0, 0, $this->monthNumber->toInt()); | |
} catch (Exception $exception) { | |
throw new Exception( | |
'Could not get name representation of the month', | |
$exception | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment