-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCarbonBehavior.php
126 lines (116 loc) · 3.5 KB
/
CarbonBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace yii2mod\behaviors;
use Carbon\Carbon;
use yii\base\Behavior;
use yii\db\ActiveRecord;
/**
* CarbonBehavior automatically creates a Carbon Instance for one or multiple attributes of an ActiveRecord
* object when `afterFind` event happen.
*
* To use CarbonBehavior, configure the [[attributes]] property which should specify the list of attributes
* that need to be converted.
*
* ```php
*
* public function behaviors()
* {
* return [
* [
* 'class' => CarbonBehavior::className(),
* 'attributes' => [
* 'createdAt'
* ],
* ],
* ];
* }
*
* $user = UserModel::findOne(1);
*
* var_dump($user->createdAt->year);
* var_dump($user->createdAt->month);
* var_dump($user->createdAt->day);
*
* // change date
*
* $user->createdAt->addYear();
* $user->save();
*
* ```
*
* @see http://carbon.nesbot.com/docs/#api-introduction
*
* @package yii2mod\behaviors
*/
class CarbonBehavior extends Behavior
{
/**
* @var ActiveRecord the owner of this behavior
*/
public $owner;
/**
* @var array list of attributes that will be converted to carbon instance
*/
public $attributes = [];
/**
* @var string date format for carbon
*/
public $dateFormat = 'Y-m-d H:i:s';
/**
* @return array
*/
public function events()
{
return [
ActiveRecord::EVENT_AFTER_FIND => 'attributesToCarbon',
ActiveRecord::EVENT_BEFORE_UPDATE => 'attributesToDefaultFormat',
ActiveRecord::EVENT_AFTER_UPDATE => 'attributesToCarbon',
];
}
/**
* Convert the model's attributes to an Carbon instance.
*
* @param $event
*
* @return static
*/
public function attributesToCarbon($event)
{
foreach ($this->attributes as $attribute) {
$value = $this->owner->$attribute;
if (!empty($value)) {
// If this value is an integer, we will assume it is a UNIX timestamp's value
// and format a Carbon object from this timestamp.
if (is_numeric($value)) {
$this->owner->$attribute = Carbon::createFromTimestamp($value);
}
// If the value is in simply year, month, day format, we will instantiate the
// Carbon instances from that format.
elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) {
$this->owner->$attribute = Carbon::createFromFormat('Y-m-d', $value)->startOfDay();
} else {
$this->owner->$attribute = Carbon::createFromFormat($this->dateFormat, $this->owner->$attribute);
}
}
}
}
/**
* Handles owner 'beforeUpdate' event for converting attributes values to the default format
*
* @param $event
*
* @return bool
*/
public function attributesToDefaultFormat($event)
{
$oldAttributes = $this->owner->oldAttributes;
foreach ($this->attributes as $attribute) {
$oldAttributeValue = $oldAttributes[$attribute];
if ($this->owner->$attribute instanceof Carbon) {
//If old attribute value is an integer, then we convert the current attribute value to the timestamp for prevent db errors
if (is_numeric($oldAttributeValue)) {
$this->owner->$attribute = $this->owner->$attribute->timestamp;
}
}
}
}
}