Last active
August 29, 2015 14:23
-
-
Save MacDada/b71688cbe674b18540af to your computer and use it in GitHub Desktop.
This file contains 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 ShopOpenings | |
{ | |
/** | |
* @var array | |
*/ | |
private $config; | |
/** | |
* @param array $config | |
* @throws InvalidArgumentException | |
*/ | |
public function __construct(array $config) | |
{ | |
$this->validateConfig($config); | |
$this->config = $config; | |
} | |
/** | |
* @param int $dayOfWeek | |
* @return array | |
* @throws InvalidArgumentException | |
*/ | |
public function getForDayOfWeek($dayOfWeek) | |
{ | |
if ($dayOfWeek < 1 || $dayOfWeek > 7) { | |
throw new InvalidArgumentException(); | |
} | |
return $this->config[$dayOfWeek - 1]; | |
} | |
public function isOpenOn(DateTime $date) | |
{ | |
// możesz pokombinować z takimi metodami pomocniczymi | |
// ale uważaj, bo php lubi uznawać niedzielę za pierwszy dzień tygodnia ;-) | |
// i w ogóle zwróć uwagę na wszystkie jazdy ze strefami czasowymi, etc | |
} | |
/** | |
* @param array $config | |
*/ | |
private function validateConfig(array $config) | |
{ | |
if (7 !== count($config)) { | |
throw new InvalidArgumentException(sprintf('You must configure 7 days (%s given)', count($config))); | |
} | |
// todo: validate the openings | |
} | |
} |
This file contains 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 | |
$shopOpenings = new ShopOpenings([ | |
[[8, 20]], | |
[[8, 20]], | |
[[8, 20]], | |
[[8, 20]], | |
[[8, 20]], | |
[[10, 18]], | |
[[10, 12], [14, 16]], | |
]); | |
// pobranie danych bezpośrednio | |
assert([[8, 20]] === $shopOpenings->getForDayOfWeek(2)); | |
assert([[10, 12], [14, 16]] === $shopOpenings->getForDayOfWeek(7)); | |
// metoda pomocnicza, sprawdzamy dla niedzieli | |
assert($shopOpenings->isOpenOn(new DateTime('21-06-2015 13:58'))); | |
assert(!$shopOpenings->isOpenOn(new DateTime('21-06-2015 14:02'))); | |
echo 'W niedziele otwarte jest: '; | |
foreach ($shopOpenings->getForDayOfWeek(7) as $opening) { | |
echo sprintf('%s-%s ', $opening[0], $opening[1]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment