|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wonnova\SDK\Model; |
|
3
|
|
|
|
|
4
|
|
|
use Wonnova\SDK\Common\ArraySerializableInterface; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class AbstractModel |
|
8
|
|
|
* @author Wonnova |
|
9
|
|
|
* @link http://www.wonnova.com |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class AbstractModel implements ArraySerializableInterface, \JsonSerializable |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Used to map virtual to real fields |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $fieldMapping = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Populates this object from an array of data |
|
22
|
|
|
* |
|
23
|
|
|
* @param array $data |
|
24
|
|
|
*/ |
|
25
|
5 |
|
public function fromArray(array $data) |
|
26
|
|
|
{ |
|
27
|
5 |
|
foreach ($data as $property => $value) { |
|
28
|
5 |
|
$setter = 'set' . ucfirst($property); |
|
29
|
5 |
|
if (method_exists($this, $setter)) { |
|
30
|
5 |
|
$this->{$setter}($value); |
|
31
|
5 |
|
} elseif (isset($this->fieldMapping[$property])) { |
|
32
|
|
|
// Try an alternative property from the mapping definition |
|
33
|
3 |
|
$property = $this->fieldMapping[$property]; |
|
34
|
3 |
|
$setter = 'set' . ucfirst($property); |
|
35
|
3 |
|
if (method_exists($this, $setter)) { |
|
36
|
3 |
|
$this->{$setter}($value); |
|
37
|
3 |
|
} |
|
38
|
3 |
|
} |
|
39
|
5 |
|
} |
|
40
|
5 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns a copy data array of this object |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function toArray() |
|
48
|
|
|
{ |
|
49
|
1 |
|
return []; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* (PHP 5 >= 5.4.0)<br/> |
|
54
|
|
|
* Specify data which should be serialized to JSON |
|
55
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
56
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
|
57
|
|
|
* which is a value of any type other than a resource. |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function jsonSerialize() |
|
60
|
|
|
{ |
|
61
|
2 |
|
return $this->toArray(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|