1 <?php
2 /**
3 * Copyright (C) 2014 MyAllocator
4 *
5 * A copy of the LICENSE can be found in the LICENSE file within
6 * the root directory of this library.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * IN THE SOFTWARE.
25 */
26
27 namespace MyAllocator\phpsdk\src;
28
29 /**
30 * The MyAllocator base class to be extended by API's and Utilities.
31 */
32 class MaBaseClass
33 {
34 /**
35 * @var array MyAllocator API configuration.
36 */
37 protected $config = null;
38
39 /**
40 * Class contructor attempts to assign configuration parameters.
41 *
42 * @param mixed $cfg API configuration potentially containing a
43 * 'cfg' key with configurations to overwrite Config/Config.php.
44 */
45 public function __construct($cfg = null)
46 {
47 // Load configuration from parameters or file
48 if (isset($cfg) && isset($cfg['cfg'])) {
49 $this->config = $this->sanitizeCfg($cfg['cfg']);
50 } else {
51 // Throws exception if cannot find config file
52 $cfg = require(dirname(__FILE__) . '/Config/Config.php');
53 $this->config = $this->sanitizeCfg($cfg);
54 }
55 }
56
57 /**
58 * Set an API configuration key.
59 *
60 * @param key $key The configuration key.
61 * @param value $value The configuration key value.
62 *
63 * @return boolean|null Result of the set.
64 */
65 public function setConfig($key = null, $value = null)
66 {
67 if ($key == null || $value == null) {
68 return null;
69 }
70 return ($this->config[$key] = $value);
71 }
72
73 /**
74 * Get an API configuration value by key.
75 *
76 * @param key $key The configuration key.
77 *
78 * @return mixed|null The configuration value.
79 */
80 public function getConfig($key)
81 {
82 return (isset($this->config[$key])) ? $this->config[$key] : null;
83 }
84
85 /**
86 * Sanitize parameter config data. Ensure keys/values are valid data.
87 * Unknown keys are removed.
88 *
89 * @param array $cfg API configurations.
90 *
91 * @return array Configuration containing a valid configuration set. It
92 * may or may not include the supplied configuration parameters,
93 * depending on their validity.
94 */
95 private function sanitizeCfg($cfg)
96 {
97 $sanitize = array(
98 'paramValidationEnabled' => array(
99 'type' => 'boolean',
100 'default' => true,
101 'valid' => array(true, false)
102 ),
103 'dataFormat' => array(
104 'type' => 'string',
105 'default' => 'array',
106 'valid' => array('array', 'json', 'xml')
107 ),
108 'dataResponse' => array(
109 'type' => 'array',
110 'default' => array('timeRequest', 'timeResponse', 'request'),
111 'valid' => array('timeRequest', 'timeResponse', 'request')
112 ),
113 'httpErrorThrowsException' => array(
114 'type' => 'boolean',
115 'default' => false,
116 'valid' => array(true, false)
117 ),
118 'debugsEnabled' => array(
119 'type' => 'boolean',
120 'default' => false,
121 'valid' => array(true, false)
122 )
123 );
124
125 $result = array();
126 foreach ($sanitize as $k => $v) {
127 if ($v['type'] == 'array') {
128 if (!isset($cfg[$k]) || !is_array($cfg[$k])) {
129 // Set to default if not set or invalid value
130 $result[$k] = $v['default'];
131 } else {
132 // Validate array keys
133 foreach ($cfg[$k] as $index => $item) {
134 if (!in_array($item, $v['valid'])) {
135 unset($cfg[$k][$index]);
136 }
137 }
138 // Set to parameter if value is set and valid
139 $result[$k] = $cfg[$k];
140 }
141 } else {
142 if (!isset($cfg[$k]) || !in_array($cfg[$k], $v['valid'], true)) {
143 // Set to default if not set or invalid value
144 $result[$k] = $v['default'];
145 } else {
146 // Set to parameter if value is set and valid
147 $result[$k] = $cfg[$k];
148 }
149 }
150 }
151
152 return $result;
153 }
154
155 /**
156 * Echoes a string if debugsEnabled is set to true.
157 *
158 * @param string $str The string to echo.
159 *
160 */
161 protected function debug_echo($str)
162 {
163 $this->debug('echo', $str);
164 }
165
166 /**
167 * Dumps an object/variable if debugsEnabled is set to true.
168 *
169 * @param mixed $obj The object or vairable to dump.
170 */
171 protected function debug_var_dump($obj)
172 {
173 $this->debug('var_dump', $obj);
174 }
175
176 /**
177 * Prints an array if debugsEnabled is set to true.
178 *
179 * @param array $array The array to print.
180 */
181 protected function debug_print_r($array)
182 {
183 $this->debug('print_r', $array);
184 }
185
186 /**
187 * Generates some output if debugsEnabled is set to true.
188 *
189 * @param string $type The output type.
190 * @param mixed $mixed The object, array, or variable.
191 */
192 protected function debug($type, $mixed)
193 {
194 if ($this->config && $this->config['debugsEnabled']) {
195 switch ($type) {
196 case 'echo':
197 echo $mixed;
198 break;
199 case 'print_r':
200 print_r($mixed);
201 break;
202 case 'var_dump':
203 var_dump($mixed);
204 break;
205 }
206 }
207 }
208 }
209