Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
6562680 committed Jun 1, 2024
1 parent 546ea59 commit d2756ba
Show file tree
Hide file tree
Showing 13 changed files with 679 additions and 723 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.editorconfig
/.idea

/vendor
composer.lock
composer.lock
228 changes: 114 additions & 114 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,147 +22,147 @@

Это история про то, что ООП это та еще беда... Иногда нужно просто делать как в Javascript - наращивать объекты функционалом, использовать композицию... И тут же понимать, что композиция отвязывает тебя от Структур данных, и привязывает к Поведению. В общем танцы с бубном каждый день.

## Установка

```
composer require gzhegow/calendar;
```

## Пример

```php
<?php

use Gzhegow\Calendar\Lib;
use Gzhegow\Calendar\Calendar;
use function Gzhegow\Calendar\_calendar;
use function Gzhegow\Calendar\_php_dump;
use function Gzhegow\Calendar\_assert_true;
use function Gzhegow\Calendar\_calendar_now;
use function Gzhegow\Calendar\_calendar_diff;
use function Gzhegow\Calendar\_calendar_date;
use function Gzhegow\Calendar\_calendar_interval;
use function Gzhegow\Calendar\_calendar_timezone;
use function Gzhegow\Calendar\_calendar_now_fixed;
use function Gzhegow\Calendar\_calendar_now_immutable;
use function Gzhegow\Calendar\_calendar_date_immutable;
use function Gzhegow\Calendar\_calendar_now_fixed_clear;


require_once __DIR__ . '/vendor/autoload.php';

// > подключение хелперов не обязательно, используется в демонстрационных целях
require_once __DIR__ . '/helpers/calendar.example.php';


// > настраиваем PHP
ini_set('memory_limit', '32M');

// > настраиваем обработку ошибок
error_reporting(E_ALL);
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
if (error_reporting() & $errno) {
throw new \ErrorException($errstr, -1, $errno, $errfile, $errline);
}
});
set_exception_handler(function (\Throwable $throwable) {
var_dump($throwable);
set_exception_handler(function ($e) {
var_dump(Lib::php_dump($e));
var_dump($e->getMessage());
var_dump(($e->getFile() ?? '{file}') . ': ' . ($e->getLine() ?? '{line}'));

die();
});


function main()
{
_calendar($calendar = new Calendar());

$tests[ '_calendar_date' ] = _calendar_date($datetime = 'now', $formats = null, $timezoneIfParsed = null);
_assert_true('is_a', [ $tests[ '_calendar_date' ], DateTime::class ]);

$tests[ '_calendar_date_immutable' ] = _calendar_date_immutable($datetime = 'now', $formats = null, $timezoneIfParsed = null);
_assert_true('is_a', [ $tests[ '_calendar_date_immutable' ], DateTimeImmutable::class ]);
$tests[ '_calendar_date_immutable_add' ] = $tests[ '_calendar_date_immutable' ]->add(new \DateInterval('P1D'));
_assert_true('is_a', [ $tests[ '_calendar_date_immutable_add' ], DateTimeImmutable::class ]);
$tests[ '_calendar_date_immutable_sub' ] = $tests[ '_calendar_date_immutable' ]->sub(new \DateInterval('P1D'));
_assert_true('is_a', [ $tests[ '_calendar_date_immutable_sub' ], DateTimeImmutable::class ]);
$tests[ '_calendar_date_immutable_modify' ] = $tests[ '_calendar_date_immutable' ]->modify('+ 10 hours');
_assert_true('is_a', [ $tests[ '_calendar_date_immutable_modify' ], DateTimeImmutable::class ]);

$tests[ '_calendar_now' ] = _calendar_now($timezone = null);
_assert_true('is_a', [ $tests[ '_calendar_now' ], DateTime::class ]);

$tests[ '_calendar_now_immutable' ] = _calendar_now_immutable($timezone = null);
_assert_true('is_a', [ $tests[ '_calendar_now_immutable' ], DateTimeImmutable::class ]);

$tests[ '_calendar_now_fixed0' ] = _calendar_now_fixed($datetime = null, $timezone = null);
_assert_true('is_a', [ $tests[ '_calendar_now_fixed0' ], DateTimeImmutable::class ]);
_calendar_now_fixed_clear();
$tests[ '_calendar_now_fixed1' ] = _calendar_now_fixed($datetime = null, $timezone = null);
$tests[ '_calendar_now_fixed2' ] = _calendar_now_fixed($datetime = null, $timezone = null);
_assert_true('is_a', [ $tests[ '_calendar_now_fixed1' ], DateTimeImmutable::class ]);
_assert_true('is_a', [ $tests[ '_calendar_now_fixed2' ], DateTimeImmutable::class ]);
_assert_true(static function () use ($tests) {
return $tests[ '_calendar_now_fixed1' ] === $tests[ '_calendar_now_fixed2' ];
});

$tests[ '_calendar_timezone' ] = _calendar_timezone($timezone = 'UTC');
_assert_true('is_a', [ $tests[ '_calendar_timezone' ], DateTimeZone::class ]);
_assert_true(function () use ($tests) {
return 'UTC' === $tests[ '_calendar_timezone' ]->getName();
});

$tests[ '_calendar_interval' ] = _calendar_interval($interval = 'P0D', $formats = null);
_assert_true('is_a', [ $tests[ '_calendar_interval' ], DateInterval::class ]);
_assert_true(function () use ($tests) {
return 'P0D' === $tests[ '_calendar_interval' ]->jsonSerialize();
});

$now = _calendar_now_immutable();
$past = $now->modify('- 10 hours');
$tests[ '_calendar_diff' ] = _calendar_diff($now, $past, $absolute = false); // : ?DateInterval;
_assert_true('is_a', [ $tests[ '_calendar_diff' ], DateInterval::class ]);
_assert_true(function () use ($tests) {
return 'PT10H' === $tests[ '_calendar_diff' ]->jsonSerialize();
});

var_dump(json_encode($tests, JSON_PRETTY_PRINT));

// string(730) "{
// "_calendar_date": "2024-05-09T19:47:39.074+03:00",
// "_calendar_date_immutable": "2024-05-09T19:47:39.074+03:00",
// "_calendar_date_immutable_add": "2024-05-10T19:47:39.074+03:00",
// "_calendar_date_immutable_sub": "2024-05-08T19:47:39.074+03:00",
// "_calendar_date_immutable_modify": "2024-05-10T05:47:39.074+03:00",
// "_calendar_now": "2024-05-09T19:47:39.074+03:00",
// "_calendar_now_immutable": "2024-05-09T19:47:39.074+03:00",
// "_calendar_now_fixed0": "2024-05-09T19:47:39.074+03:00",
// "_calendar_now_fixed1": "2024-05-09T19:47:39.074+03:00",
// "_calendar_now_fixed2": "2024-05-09T19:47:39.074+03:00",
// "_calendar_timezone": "UTC",
// "_calendar_interval": "P0D",
// "_calendar_diff": "PT10H"
// }"

$dump = [];
foreach ( $tests as $i => $test ) {
$dump[ $i ] = _php_dump($test);
}
var_dump($dump);

// array(13) {
// ["_calendar_date"]=>
// string(48) "{ object(Gzhegow\Calendar\Struct\DateTime # 8) }"
// ["_calendar_date_immutable"]=>
// string(57) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 9) }"
// ["_calendar_date_immutable_add"]=>
// string(58) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 10) }"
// ["_calendar_date_immutable_sub"]=>
// string(58) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 11) }"
// ["_calendar_date_immutable_modify"]=>
// string(57) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 7) }"
// ["_calendar_now"]=>
// string(49) "{ object(Gzhegow\Calendar\Struct\DateTime # 12) }"
// ["_calendar_now_immutable"]=>
// string(58) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 13) }"
// ["_calendar_now_fixed0"]=>
// string(58) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 14) }"
// ["_calendar_now_fixed1"]=>
// string(58) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 15) }"
// ["_calendar_now_fixed2"]=>
// string(58) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 15) }"
// ["_calendar_timezone"]=>
// string(53) "{ object(Gzhegow\Calendar\Struct\DateTimeZone # 16) }"
// ["_calendar_interval"]=>
// string(53) "{ object(Gzhegow\Calendar\Struct\DateInterval # 17) }"
// ["_calendar_diff"]=>
// string(53) "{ object(Gzhegow\Calendar\Struct\DateInterval # 20) }"
// }
// > создаем календарь
_calendar($calendar = new Calendar());

// > создаем дату
$tests[ '_calendar_date' ] = _calendar_date($datetime = 'now', $formats = null, $timezoneIfParsed = null);
Lib::assert_true('is_a', [ $tests[ '_calendar_date' ], DateTime::class ]);

// > создаем/распознаем дату
$tests[ '_calendar_date_immutable' ] = _calendar_date_immutable($datetime = 'now', $formats = null, $timezoneIfParsed = null);
Lib::assert_true('is_a', [ $tests[ '_calendar_date_immutable' ], DateTimeImmutable::class ]);

// > проводим действия над датой, чтобы убедится что Immutable работает
$tests[ '_calendar_date_immutable_add' ] = $tests[ '_calendar_date_immutable' ]->add(new \DateInterval('P1D'));
$tests[ '_calendar_date_immutable_sub' ] = $tests[ '_calendar_date_immutable' ]->sub(new \DateInterval('P1D'));
$tests[ '_calendar_date_immutable_modify' ] = $tests[ '_calendar_date_immutable' ]->modify('+ 10 hours');
Lib::assert_true('is_a', [ $tests[ '_calendar_date_immutable_add' ], DateTimeImmutable::class ]);
Lib::assert_true('is_a', [ $tests[ '_calendar_date_immutable_sub' ], DateTimeImmutable::class ]);
Lib::assert_true('is_a', [ $tests[ '_calendar_date_immutable_modify' ], DateTimeImmutable::class ]);
if ($tests[ '_calendar_date_immutable_add' ] === $tests[ '_calendar_date_immutable_sub' ]) throw new \RuntimeException();
if ($tests[ '_calendar_date_immutable_sub' ] === $tests[ '_calendar_date_immutable_modify' ]) throw new \RuntimeException();
if ($tests[ '_calendar_date_immutable_add' ] === $tests[ '_calendar_date_immutable_modify' ]) throw new \RuntimeException();

// > создает дату "сейчас", просто alias для _calendar_date($date)
$tests[ '_calendar_now' ] = _calendar_now($timezone = null);
$tests[ '_calendar_now_immutable' ] = _calendar_now_immutable($timezone = null);
Lib::assert_true('is_a', [ $tests[ '_calendar_now' ], DateTime::class ]);
Lib::assert_true('is_a', [ $tests[ '_calendar_now_immutable' ], DateTimeImmutable::class ]);

// > создает/распознает временную зону
$tests[ '_calendar_timezone' ] = _calendar_timezone($timezone = 'UTC');
Lib::assert_true('is_a', [ $tests[ '_calendar_timezone' ], DateTimeZone::class ]);
Lib::assert_true(function () use ($tests) {
return 'UTC' === $tests[ '_calendar_timezone' ]->getName();
});

}
// > создает/распознает интервал
$tests[ '_calendar_interval' ] = _calendar_interval($interval = 'P0D', $formats = null);
Lib::assert_true('is_a', [ $tests[ '_calendar_interval' ], DateInterval::class ]);
Lib::assert_true(function () use ($tests) {
return 'P0D' === $tests[ '_calendar_interval' ]->jsonSerialize();
});

main();
```
// > возвращает разницу между датами
$now = _calendar_now_immutable();
$past = $now->modify('- 10 hours');
$tests[ '_calendar_diff' ] = _calendar_diff($now, $past, $absolute = false); // : ?DateInterval;
Lib::assert_true('is_a', [ $tests[ '_calendar_diff' ], DateInterval::class ]);
Lib::assert_true(function () use ($tests) {
return 'PT10H' === $tests[ '_calendar_diff' ]->jsonSerialize();
});

var_dump(json_encode($tests, JSON_PRETTY_PRINT));

// string(730) "{
// "_calendar_date": "2024-05-09T19:47:39.074+03:00",
// "_calendar_date_immutable": "2024-05-09T19:47:39.074+03:00",
// "_calendar_date_immutable_add": "2024-05-10T19:47:39.074+03:00",
// "_calendar_date_immutable_sub": "2024-05-08T19:47:39.074+03:00",
// "_calendar_date_immutable_modify": "2024-05-10T05:47:39.074+03:00",
// "_calendar_now": "2024-05-09T19:47:39.074+03:00",
// "_calendar_now_immutable": "2024-05-09T19:47:39.074+03:00",
// "_calendar_timezone": "UTC",
// "_calendar_interval": "P0D",
// "_calendar_diff": "PT10H"
// }"

$dump = [];
foreach ( $tests as $i => $test ) {
$dump[ $i ] = Lib::php_dump($test);
}
var_dump($dump);

// array(13) {
// ["_calendar_date"]=>
// string(48) "{ object(Gzhegow\Calendar\Struct\DateTime # 8) }"
// ["_calendar_date_immutable"]=>
// string(57) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 9) }"
// ["_calendar_date_immutable_add"]=>
// string(58) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 10) }"
// ["_calendar_date_immutable_sub"]=>
// string(58) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 11) }"
// ["_calendar_date_immutable_modify"]=>
// string(57) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 7) }"
// ["_calendar_now"]=>
// string(49) "{ object(Gzhegow\Calendar\Struct\DateTime # 12) }"
// ["_calendar_now_immutable"]=>
// string(58) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 13) }"
// string(58) "{ object(Gzhegow\Calendar\Struct\DateTimeImmutable # 15) }"
// ["_calendar_timezone"]=>
// string(53) "{ object(Gzhegow\Calendar\Struct\DateTimeZone # 16) }"
// ["_calendar_interval"]=>
// string(53) "{ object(Gzhegow\Calendar\Struct\DateInterval # 17) }"
// ["_calendar_diff"]=>
// string(53) "{ object(Gzhegow\Calendar\Struct\DateInterval # 20) }"
// }
```
21 changes: 8 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,26 @@
"name": "gzhegow",
"email": "[email protected]"
}
],
],

"require": {
"php": "^7.2|^8.0",

"ext-json": "*"
},

"autoload": {
"psr-4": {
"Gzhegow\\Calendar\\": "src/"
},
"files": [
"helpers/assert.php",
"helpers/calendar.php",
"helpers/php.php"
]
}
},

"minimum-stability": "dev",
"prefer-stable": true,

"config": {
"optimize-autoloader": true,

"sort-packages": false
}
}
Loading

0 comments on commit d2756ba

Please sign in to comment.