Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nojimage/cakephp-time-interval
Time interval custom type for CakePHP
https://github.com/nojimage/cakephp-time-interval
cakephp cakephp-plugin database interval time
Last synced: about 2 months ago
JSON representation
Time interval custom type for CakePHP
- Host: GitHub
- URL: https://github.com/nojimage/cakephp-time-interval
- Owner: nojimage
- License: mit
- Created: 2019-04-18T10:17:14.000Z (over 5 years ago)
- Default Branch: cake5
- Last Pushed: 2024-05-01T06:50:12.000Z (5 months ago)
- Last Synced: 2024-05-29T06:12:04.914Z (4 months ago)
- Topics: cakephp, cakephp-plugin, database, interval, time
- Language: PHP
- Homepage:
- Size: 44.9 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# TimeInterval plugin for CakePHP
This plugin provide `time_interval` custom type for MySQL's `TIME`, Postgres's `INTERVAL`,
and provide `time_interval_int` custom type for seconds as `INTEGER`.
This is a custom type to represent intervals, which CakePHP can treat as a `TimeInterval` object that inherits from `DateInterval`.## Version Map
| CakePHP Version | Plugin Version | Branch |
|-----------------|----------------|----------------|
| 5.x | 3.x | cake5 |
| 4.x | 2.x | cake4 |
| 3.x | 0.3.x | cake3 |## Installation
You can install this plugin into your CakePHP application using [composer](http://getcomposer.org).
The recommended way to install composer packages is:
```
composer require elstc/cakephp-time-interval
```### Load plugin
Load the plugin by adding the following statement in your project's `src/Application.php`:
```
$this->addPlugin('Elastic/TimeInterval');
```## Usage
### Add column definitions to Table class
```php
use Cake\Database\Schema\TableSchema;class WorkTimesTable extends Table
{
protected function _initializeSchema(TableSchema $schema)
{
parent::_initializeSchema($schema);$schema->setColumnType('duration', 'time_interval');
// If your column type is seconds as INTEGER, Use `time_interval_int` instead.
$schema->setColumnType('duration_sec', 'time_interval_int');return $schema;
}
}
```### Add column validation to Table class
Use `timeInterval` rule instead of `time`.
The `timeInterval` rule is in the `timeInterval` validation provider.```php
use Cake\Validation\Validator;
use Elastic\TimeInterval\Validation\TimeIntervalValidation;class WorkTimesTable extends Table
{
public function validationDefault(Validator $validator)
{
// ...
$validator->add('duration', 'timeInterval', [
'rule' => 'timeInterval',
'provider' => 'timeInterval',
]);return $validator;
}
}
```### In addition, add mutator to Entity class, it is useful.
```php
use Cake\Database\Type;class WorkTime extends Entity
{
protected function _setDuration($value)
{
// convert to TimeInterval
return Type::build('time_interval')->marshal($value);
}
}$workTime->duration = '00:15:00';
$workTime->duration = ($startTime)->diff($endTime); // $startTime, $endTime is FrozenTime object.
$workTime->duration = 3600; // as a seconds
```## NOTE
### MySQL TIME column limitation.
[MySQL :: MySQL 8.0 Reference Manual :: 13.2.3 The TIME Type](https://dev.mysql.com/doc/refman/8.0/en/time.html)
By default, values that lie outside the TIME range but are otherwise valid are clipped to the closest endpoint of the range. For example,
'-850:00:00' and '850:00:00' are converted to '-838:59:59' and '838:59:59'. Invalid TIME values are converted to '00:00:00'.
Note that because '00:00:00' is itself a valid TIME value, there is no way to tell, from a value of '00:00:00' stored in a table,
whether the original value was specified as '00:00:00' or whether it was invalid.### DateInterval / TimeInterval construct with date part will be broken time
If you initialize DateInterval with date part, time will not be interpreted correctly.
```php
$workTime->duration = new DateInterval('PT75H4M5S'); // OK
$workTime->duration = new DateInterval('P1M2DT3H4M5S'); // can't get expected time
```