-
Notifications
You must be signed in to change notification settings - Fork 2
/
MdmAutoDateBehavior.php
76 lines (67 loc) · 1.72 KB
/
MdmAutoDateBehavior.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
<?php
/**
* Description of MdmAutoDateBehavior
*
* @author Misbahul D Munir, misbahuldmunir@gmail.com
*
*/
class MdmAutoDateBehavior extends CBehavior
{
/**
*
* @var array
* Mapping attribute from logical to phisical
*/
public $attributes = array();
/**
*
* @var string
*
*/
public $logicalFormat = 'd-m-Y';
/**
*
* @var string
*/
public $physicalFormat = 'Y-m-d';
public function __get($param)
{
if (isset($this->attributes[$param])) {
return $this->convertToLogical($this->owner->{$this->attributes[$param]});
} else {
return parent::__get($param);
}
}
public function __set($param, $value)
{
if (isset($this->attributes[$param])) {
$this->owner->{$this->attributes[$param]} = $this->convertToPhysical($value);
} else {
parent::__set($param, $value);
}
}
private function convertToPhysical($value)
{
if (empty($value)) {
return null;
}
$date = DateTime::createFromFormat($this->logicalFormat, $value);
return $date->format($this->physicalFormat);
}
private function convertToLogical($value)
{
if (empty($value)) {
return null;
}
$date = DateTime::createFromFormat($this->physicalFormat, $value);
return $date->format($this->logicalFormat);
}
public function canGetProperty($name)
{
return isset($this->attributes[$name]) || parent::canGetProperty($name);
}
public function canSetProperty($name)
{
return isset($this->attributes[$name]) || parent::canSetProperty($name);
}
}