Skip to content

Instantly share code, notes, and snippets.

@nicholasruunu
Last active February 2, 2016 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicholasruunu/ea6b4e55ae14c19e4c17 to your computer and use it in GitHub Desktop.
Save nicholasruunu/ea6b4e55ae14c19e4c17 to your computer and use it in GitHub Desktop.
<?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