|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhMap\Wrapper; |
|
4
|
|
|
|
|
5
|
|
|
use \PhMap\Mapper, |
|
6
|
|
|
\PhMap\Wrapper, |
|
7
|
|
|
\PhMap\Exception\SmartValidator; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Smart |
|
11
|
|
|
* @package PhMap\Wrapper |
|
12
|
|
|
*/ |
|
13
|
|
|
class Smart extends Wrapper { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string|object|array |
|
17
|
|
|
*/ |
|
18
|
|
|
private $inputValue; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return array|object|string |
|
22
|
|
|
*/ |
|
23
|
5 |
|
public function getInputValue() { |
|
24
|
5 |
|
return $this->inputValue; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param array|object|string $value |
|
29
|
|
|
* @return $this |
|
30
|
|
|
*/ |
|
31
|
2 |
|
public function setInputValue($value) { |
|
32
|
2 |
|
if (gettype($this->getInputValue()) !== gettype($value)) { |
|
33
|
2 |
|
$this->inputValue = $value; |
|
34
|
|
|
|
|
35
|
2 |
|
$this->createMapper($this->getOutputObject(), $this->getAnnotationAdapterType()); |
|
36
|
2 |
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array|object|string $inputValue |
|
43
|
|
|
* @param string|object $outputClassOrObject |
|
44
|
|
|
* @param integer $adapter |
|
45
|
|
|
*/ |
|
46
|
5 |
|
public function __construct( |
|
47
|
|
|
$inputValue, |
|
48
|
|
|
$outputClassOrObject, |
|
49
|
|
|
$adapter = Mapper::MEMORY_ANNOTATION_ADAPTER |
|
50
|
|
|
) { |
|
51
|
5 |
|
$this->inputValue = $inputValue; |
|
52
|
5 |
|
$this->createMapper($outputClassOrObject, $adapter); |
|
53
|
4 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string|object $outputClassOrObject |
|
57
|
|
|
* @param integer $adapter |
|
58
|
|
|
* @return $this |
|
59
|
|
|
* @throws SmartValidator |
|
60
|
|
|
*/ |
|
61
|
5 |
|
protected function createMapper( |
|
62
|
|
|
$outputClassOrObject, |
|
63
|
|
|
$adapter = Mapper::MEMORY_ANNOTATION_ADAPTER |
|
64
|
|
|
) { |
|
65
|
5 |
|
$mapperClass = $this->detectMapperClass(); |
|
66
|
|
|
|
|
67
|
|
|
/** @var Mapper $mapper */ |
|
68
|
4 |
|
$mapper = new $mapperClass( |
|
69
|
4 |
|
$this->getInputValue(), |
|
70
|
4 |
|
$outputClassOrObject, |
|
71
|
|
|
$adapter |
|
72
|
4 |
|
); |
|
73
|
|
|
|
|
74
|
4 |
|
return $this->setMapper($mapper); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string |
|
79
|
|
|
* @throws SmartValidator |
|
80
|
|
|
*/ |
|
81
|
5 |
|
protected function detectMapperClass() { |
|
82
|
5 |
|
if (is_string($this->getInputValue())) { |
|
83
|
4 |
|
return '\PhMap\Wrapper\Json'; |
|
84
|
5 |
|
} elseif (is_object($this->getInputValue())) { |
|
85
|
3 |
|
return '\PhMap\Mapper\Structure\Object'; |
|
86
|
5 |
|
} elseif (is_array($this->getInputValue())) { |
|
87
|
4 |
|
return '\PhMap\Mapper\Structure\Associative'; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
throw new SmartValidator($this->getInputValue()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
} |