Skip to content

Commit

Permalink
add support for "nesbot/carbon"
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Dec 22, 2023
1 parent 1536d3d commit d30311c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ $model = Item::model()->findByPk($id);
var_dump($model->created_at); // outputs: object(DateTime)
```

This extension also supports [nesbot/carbon](https://packagist.org/packages/nesbot/carbon) package.
In order to convert dates to `\Carbon\Carbon` you should use following types:

- `\yii1tech\model\typecast\TypecastBehavior::TYPE_DATETIME_CARBON`
- `\yii1tech\model\typecast\TypecastBehavior::TYPE_TIMESTAMP_CARBON`


### Custom typecasting <span id="custom-typecasting"></span>

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"yiisoft/yii": "~1.1.0"
},
"require-dev": {
"nesbot/carbon": "^2.67",
"phpunit/phpunit": "^6.0 || ^7.0 || ^8.0 || ^9.3 || ^10.0.7"
},
"autoload": {
Expand Down
29 changes: 29 additions & 0 deletions src/TypecastBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace yii1tech\model\typecast;

use CActiveRecord;
use Carbon\Carbon;
use CBehavior;
use CBooleanValidator;
use CDbColumnSchema;
Expand Down Expand Up @@ -114,6 +115,14 @@ class TypecastBehavior extends CBehavior
* Converts integer Unix timestamp into {@see \DateTime} and vice versa.
*/
const TYPE_TIMESTAMP = 'timestamp';
/**
* Converts ISO datetime string into {@see \Carbon\Carbon} and vice versa.
*/
const TYPE_DATETIME_CARBON = 'datetime-carbon';
/**
* Converts integer Unix timestamp into {@see \Carbon\Carbon} and vice versa.
*/
const TYPE_TIMESTAMP_CARBON = 'timestamp-carbon';

/**
* @var array<string, string|callable>|null attribute typecast map in format: attributeName => type.
Expand Down Expand Up @@ -308,6 +317,26 @@ protected function typecastValue($value, $type)
}

return (new \DateTime())->setTimestamp((int) $value);
case self::TYPE_DATETIME_CARBON:
if ($value === null || $value instanceof \DateTime) {
return $value;
}

if (!class_exists(Carbon::class)) {
throw new \LogicException('Extension "nesbot/carbon" has not been installed');
}

return Carbon::createFromFormat('Y-m-d H:i:s', (string) $value);
case self::TYPE_TIMESTAMP_CARBON:
if ($value === null || $value instanceof \DateTime) {
return $value;
}

if (!class_exists(Carbon::class)) {
throw new \LogicException('Extension "nesbot/carbon" has not been installed');
}

return (new Carbon())->setTimestamp((int) $value);
default:
throw new InvalidArgumentException("Unsupported attribute type '{$type}'");
}
Expand Down
28 changes: 28 additions & 0 deletions tests/TypecastBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace yii1tech\model\typecast\test;

use ArrayObject;
use Carbon\Carbon;
use DateTime;
use yii1tech\model\typecast\TypecastBehavior;
use yii1tech\model\typecast\test\data\FormWithTypecast;
Expand Down Expand Up @@ -176,6 +177,33 @@ public function testDateTime(): void
$this->assertSame($createdDateTime->getTimestamp(), $model->created_timestamp->getTimestamp());
}

/**
* @depends testDateTime
*/
public function testDateTimeCarbon(): void
{
$baseBehavior = new TypecastBehavior();
$baseBehavior->attributeTypes = [
'created_date' => TypecastBehavior::TYPE_DATETIME_CARBON,
'created_timestamp' => TypecastBehavior::TYPE_TIMESTAMP_CARBON,
];

$timestamp = time();

$model = new Item();
$model->created_timestamp = $timestamp;
$model->created_date = date('Y-m-d H:i:s', $timestamp);

$baseBehavior->attach($model);
$baseBehavior->typecastAttributes();

$this->assertTrue($model->created_date instanceof Carbon);
$this->assertTrue($model->created_timestamp instanceof Carbon);

$this->assertSame($timestamp, $model->created_date->getTimestamp());
$this->assertSame($timestamp, $model->created_timestamp->getTimestamp());
}

/**
* @depends testTypecast
*/
Expand Down

0 comments on commit d30311c

Please sign in to comment.