Skip to content

Commit 2bb2d92

Browse files
authored
Merge pull request #1 from peckadesign/initial
Přidána knihovna s CZ a SK lokalizací
2 parents b910f56 + 20546a9 commit 2bb2d92

24 files changed

+771
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor
2+
/composer.lock
3+
/output.cs
4+
/tests/**/*.actual
5+
/tests/**/*.expected

Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.PHONY: build-staging
2+
.PHONY: clean
3+
.PHONY: clean-cache
4+
.PHONY: cs
5+
6+
7+
build-staging:
8+
- composer install --no-interaction -o -a --no-suggest
9+
10+
11+
clean:
12+
git clean -xdf .
13+
14+
15+
clean-cache:
16+
git clean -xdf tests/
17+
18+
19+
run-tests:
20+
git clean -xdf tests/
21+
composer install --no-interaction
22+
- vendor/bin/tester -p php -c tests/php.ini -o tap tests > output.tap
23+
git clean -xdf tests/
24+
25+
26+
cs:
27+
git clean -xdf output.cs output-strict.cs
28+
- vendor/bin/phpcs src/ --standard=vendor/pd/coding-standard/src/PeckaCodingStandard/ruleset.xml --report-file=output.cs
29+
- vendor/bin/phpcs src/ --standard=vendor/pd/coding-standard/src/PeckaCodingStandardStrict/ruleset.xml --report-file=output-strict.cs
30+
- test -f output-strict.cs && cat output-strict.cs >> output.cs && rm output-strict.cs

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "pd/holidays",
3+
"type": "library",
4+
"description": "Přehled svátků pro různé lokalizace",
5+
"authors": [
6+
{
7+
"name": "Milan Pála",
8+
"email": "milan.pala@peckadesign.cz"
9+
}
10+
],
11+
"require": {
12+
"php": "^7.1.0",
13+
"ext-mbstring": "*",
14+
"nette/utils": "2.3.*|2.4.*",
15+
"nette/di": "2.3.*|2.4.*",
16+
"nette/neon": "2.*"
17+
},
18+
"minimum-stability": "stable",
19+
"require-dev": {
20+
"nette/tester": "1.7.*",
21+
"pd/coding-standard": "1.7.*"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Pd\\Holidays\\": "src"
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"PdTests\\Holidays\\": "tests"
31+
}
32+
}
33+
34+
}

src/DI/Extension.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pd\Holidays\DI;
4+
5+
use Nette;
6+
use Pd;
7+
8+
9+
final class Extension extends Nette\DI\CompilerExtension
10+
{
11+
12+
public function loadConfiguration()
13+
{
14+
parent::loadConfiguration();
15+
16+
$builder = $this->getContainerBuilder();
17+
18+
$holidayFacade = $builder
19+
->addDefinition($this->prefix('holidayFacade'))
20+
->setFactory(Pd\Holidays\HolidayFacade::class)
21+
;
22+
23+
$this->addLocalization($holidayFacade, Pd\Holidays\Localizations\Czech::class, Pd\Holidays\Localizations\ICzech::COUNTRY_CODE_CZECH);
24+
$this->addLocalization($holidayFacade, Pd\Holidays\Localizations\Slovak::class, Pd\Holidays\Localizations\ISlovak::COUNTRY_CODE_SLOVAK);
25+
}
26+
27+
28+
private function addLocalization(Nette\DI\ServiceDefinition $holidayFacade, string $localizationClass, string $countryCode)
29+
{
30+
$builder = $this->getContainerBuilder();
31+
$countryCodePrefix = Nette\Utils\Strings::lower($countryCode);
32+
33+
$translations = Nette\Neon\Neon::decode(file_get_contents(__DIR__ . '/../Localizations/' . Nette\Utils\Strings::lower(Nette\Utils\Strings::substring($localizationClass, strrpos($localizationClass, '\\') + 1)) . '.neon'));
34+
35+
$czechHolidayFactory = $builder
36+
->addDefinition($this->prefix($countryCodePrefix . 'HolidayFactory'))
37+
->setFactory(Pd\Holidays\HolidayFactory::class, [$translations])
38+
->setAutowired(FALSE)
39+
;
40+
41+
$localization = $builder
42+
->addDefinition($this->prefix($countryCodePrefix))
43+
->setFactory($localizationClass, [$czechHolidayFactory])
44+
->setAutowired(FALSE)
45+
;
46+
47+
$holidayFacade
48+
->addSetup('addLocalization', [$countryCode, $localization]);
49+
}
50+
51+
}

src/Holiday.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pd\Holidays;
4+
5+
class Holiday implements IHoliday
6+
{
7+
8+
/**
9+
* @var int
10+
*/
11+
private $month;
12+
13+
/**
14+
* @var int
15+
*/
16+
private $day;
17+
18+
/**
19+
* @var string
20+
*/
21+
private $name;
22+
23+
24+
public function __construct(
25+
int $month,
26+
int $day,
27+
string $name
28+
) {
29+
$this->month = $month;
30+
$this->day = $day;
31+
$this->name = $name;
32+
}
33+
34+
35+
public function getMonth(): int
36+
{
37+
return $this->month;
38+
}
39+
40+
41+
public function getDay(): int
42+
{
43+
return $this->day;
44+
}
45+
46+
47+
public function getName(): string
48+
{
49+
return $this->name;
50+
}
51+
52+
}

src/HolidayFacade.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pd\Holidays;
4+
5+
use Pd;
6+
7+
8+
class HolidayFacade implements IHolidayFacade
9+
{
10+
11+
/** @var array|ILocalization[] */
12+
private $localizations;
13+
14+
15+
public function getHoliday(string $countryCode, \DateTimeInterface $dateTime): ?IHoliday
16+
{
17+
$localization = $this->localizations[$countryCode] ?? NULL;
18+
19+
if ( ! $localization) {
20+
return NULL;
21+
}
22+
23+
$year = $localization->getHolidays((int) $dateTime->format('Y'));
24+
25+
if ( ! $year) {
26+
return NULL;
27+
}
28+
29+
return $year->getHoliday($dateTime);
30+
}
31+
32+
33+
public function getHolidays(string $countryCode, int $year): IYear
34+
{
35+
$localization = $this->localizations[$countryCode] ?? NULL;
36+
37+
if ( ! $localization) {
38+
return new Year([]);
39+
}
40+
41+
return $localization->getHolidays($year);
42+
}
43+
44+
45+
public function addLocalization(string $countryCode, ILocalization $localization): void
46+
{
47+
$this->localizations[$countryCode] = $localization;
48+
}
49+
50+
}

src/HolidayFactory.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pd\Holidays;
4+
5+
class HolidayFactory
6+
{
7+
8+
/**
9+
* @var array ['_lang' => 'value']
10+
*/
11+
private $translates;
12+
13+
14+
public function __construct(array $translates)
15+
{
16+
$this->translates = $translates;
17+
}
18+
19+
20+
public function create(int $month, int $day, string $name): Holiday
21+
{
22+
return new Holiday($month, $day, $this->translates[$name] ?? $name);
23+
}
24+
}

src/IHoliday.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pd\Holidays;
4+
5+
interface IHoliday
6+
{
7+
8+
public function getMonth(): int;
9+
10+
11+
public function getDay(): int;
12+
13+
14+
public function getName(): string;
15+
16+
}

src/IHolidayFacade.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pd\Holidays;
4+
5+
interface IHolidayFacade
6+
{
7+
8+
public function getHoliday(string $countryCode, \DateTimeInterface $dateTime): ?IHoliday;
9+
10+
public function getHolidays(string $countryCode, int $year): IYear;
11+
12+
}

src/ILocalization.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pd\Holidays;
4+
5+
interface ILocalization
6+
{
7+
8+
public function getHolidays(int $year): IYear;
9+
10+
}

0 commit comments

Comments
 (0)