-
Notifications
You must be signed in to change notification settings - Fork 11
/
BaseConverter.php
108 lines (97 loc) · 2.54 KB
/
BaseConverter.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
<?php
namespace mdm\converter;
use Yii;
use yii\base\NotSupportedException;
/**
* Description of BaseConverter
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class BaseConverter extends \yii\base\Behavior
{
/**
* @var array Attribute map for logical to physical
*/
public $attributes = [];
/**
* @var \Closure callback to check value is empty
*
* ```php
* function($value){
*
* }
* ```
*/
public $isEmpty;
/**
* @inheritdoc
*/
public function __get($name)
{
if (isset($this->attributes[$name])) {
$attrValue = $this->owner->{$this->attributes[$name]};
return $this->convertToLogical($attrValue, $name);
} else {
return parent::__get($name);
}
}
/**
* @inheritdoc
*/
public function __set($name, $value)
{
if (isset($this->attributes[$name])) {
$this->owner->{$this->attributes[$name]} = $this->convertToPhysical($value, $name);
} else {
parent::__set($name, $value);
}
}
/**
* @inheritdoc
*/
public function canGetProperty($name, $checkVars = true)
{
return isset($this->attributes[$name]) || parent::canGetProperty($name, $checkVars);
}
/**
* @inheritdoc
*/
public function canSetProperty($name, $checkVars = true)
{
return isset($this->attributes[$name]) || parent::canSetProperty($name, $checkVars);
}
/**
* Convert value to physical format
* @param mixed $value value to converted
* @param string $attribute Logical attribute
* @return mixed Converted value
*/
protected function convertToPhysical($value, $attribute)
{
throw new NotSupportedException(get_class($this) . ' does not support convertToPhysical().');
}
/**
* Convert value to logical format
* @param mixed $value value to converted
* @param string $attribute Logical attribute
* @return mixed Converted value
*/
protected function convertToLogical($value, $attribute)
{
throw new NotSupportedException(get_class($this) . ' does not support convertToLogical().');
}
/**
* Check empty value
* @param mixed $value
* @return boolean
*/
public function isEmpty($value)
{
if ($this->isEmpty !== null) {
return call_user_func($this->isEmpty, $value);
} else {
return $value === null || $value === '' || $value === [];
}
}
}