-
Notifications
You must be signed in to change notification settings - Fork 321
/
Copy pathModel.php
75 lines (65 loc) · 1.51 KB
/
Model.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
<?php
/**
* Created by Cristian.
* Date: 01/10/16 03:02 PM.
*/
namespace Reliese\Database\Eloquent;
class Model extends \Illuminate\Database\Eloquent\Model
{
/**
* {@inheritdoc}
*/
protected function castAttribute($key, $value)
{
if ($this->hasCustomGetCaster($key)) {
return $this->{$this->getCustomGetCaster($key)}($value);
}
return parent::castAttribute($key, $value);
}
/**
* @param string $key
*
* @return bool
*/
protected function hasCustomGetCaster($key)
{
return $this->hasCast($key) && method_exists($this, $this->getCustomGetCaster($key));
}
/**
* @param string $key
*
* @return string
*/
protected function getCustomGetCaster($key)
{
return 'from'.ucfirst($this->getCastType($key));
}
/**
* {@inheritdoc}
*/
public function setAttribute($key, $value)
{
if ($this->hasCustomSetCaster($key)) {
$value = $this->{$this->getCustomSetCaster($key)}($value);
}
return parent::setAttribute($key, $value);
}
/**
* @param string $key
*
* @return bool
*/
private function hasCustomSetCaster($key)
{
return $this->hasCast($key) && method_exists($this, $this->getCustomSetCaster($key));
}
/**
* @param string $key
*
* @return string
*/
private function getCustomSetCaster($key)
{
return 'to'.ucfirst($this->getCastType($key));
}
}