|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2015-present Ganbaro Digital Ltd |
|
5
|
|
|
* All rights reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
8
|
|
|
* modification, are permitted provided that the following conditions |
|
9
|
|
|
* are met: |
|
10
|
|
|
* |
|
11
|
|
|
* * Redistributions of source code must retain the above copyright |
|
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
|
13
|
|
|
* |
|
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
|
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
|
16
|
|
|
* the documentation and/or other materials provided with the |
|
17
|
|
|
* distribution. |
|
18
|
|
|
* |
|
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
|
20
|
|
|
* contributors may be used to endorse or promote products derived |
|
21
|
|
|
* from this software without specific prior written permission. |
|
22
|
|
|
* |
|
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
35
|
|
|
* |
|
36
|
|
|
* @category Libraries |
|
37
|
|
|
* @package DataContainers/Containers |
|
38
|
|
|
* @author Stuart Herbert <[email protected]> |
|
39
|
|
|
* @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com |
|
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
41
|
|
|
* @link http://code.ganbarodigital.com/php-data-containers |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
namespace GanbaroDigital\DataContainers\Containers; |
|
45
|
|
|
|
|
46
|
|
|
use JsonSerializable; |
|
47
|
|
|
use RuntimeException; |
|
48
|
|
|
use GanbaroDigital\DataContainers\Exceptions\E4xx_NoSuchMethod; |
|
49
|
|
|
|
|
50
|
|
|
class LazyValueObject extends BaseContainer implements JsonSerializable |
|
51
|
|
|
{ |
|
52
|
|
|
/** @var array a list of fake operations supported */ |
|
53
|
|
|
protected $supportedOperations = [ |
|
54
|
|
|
'get' => 'getData', |
|
55
|
|
|
'has' => 'hasData', |
|
56
|
|
|
'set' => 'setData', |
|
57
|
|
|
'reset' => 'resetData', |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* make this value object read-only |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
public function makeReadOnly() |
|
66
|
|
|
{ |
|
67
|
|
|
unset($this->supportedOperations['set']); |
|
68
|
|
|
unset($this->supportedOperations['reset']); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* magic method, which provides fake getter / setting support |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $methodName |
|
75
|
|
|
* the fake method that was called |
|
76
|
|
|
* @param array $args |
|
77
|
|
|
* a list of the args that were passed to us |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
* depends on the fake method being called |
|
80
|
|
|
*/ |
|
81
|
|
|
public function __call($methodName, $args) |
|
82
|
|
|
{ |
|
83
|
|
|
list($verb, $infoName) = $this->convertMethodName($methodName); |
|
84
|
|
|
|
|
85
|
|
|
if (!isset($this->supportedOperations[$verb])) { |
|
86
|
|
|
throw new E4xx_NoSuchMethod(get_class($this), $methodName); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$callable = [ $this, $verb . 'Data']; |
|
90
|
|
|
$argsToPass = array_merge([ $infoName ], $args); |
|
91
|
|
|
return call_user_func_array($callable, $argsToPass); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* given the 'get/set' kind of method name, convert it into: |
|
96
|
|
|
* |
|
97
|
|
|
* 1. the operation being performed (get, set, et al) |
|
98
|
|
|
* 2. a normalised version of the name of the information |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $methodName the method name to decode |
|
101
|
|
|
* @return array<string> |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function convertMethodName($methodName) |
|
104
|
|
|
{ |
|
105
|
|
|
// turn the method name into an array of words |
|
106
|
|
|
$words = explode(' ', preg_replace('/([^A-Z])([A-Z])/', "$1 $2", $methodName)); |
|
107
|
|
|
|
|
108
|
|
|
// lose the first word |
|
109
|
|
|
$verb = array_shift($words); |
|
110
|
|
|
|
|
111
|
|
|
// concat into underscore_format |
|
112
|
|
|
$retval = implode("_", $words); |
|
113
|
|
|
|
|
114
|
|
|
// all done |
|
115
|
|
|
return [$verb, $retval]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* returns the data to turn into a JSON data structure |
|
120
|
|
|
* @return object |
|
121
|
|
|
*/ |
|
122
|
|
|
public function jsonSerialize() |
|
123
|
|
|
{ |
|
124
|
|
|
return (object)$this->getAllData(); |
|
125
|
|
|
} |
|
126
|
|
|
} |