1: <?php
2:
3: namespace Mypos\IPC;
4:
5: /**
6: * IPC Library helper functions
7: */
8: class Helper
9: {
10: private function __construct() { }
11:
12: /**
13: * Validate email address
14: *
15: * @param string $email
16: *
17: * @return boolean
18: */
19: public static function isValidEmail($email)
20: {
21: return filter_var($email, FILTER_VALIDATE_EMAIL);
22: }
23:
24: /**
25: * Validate URL address
26: *
27: * @param string $url
28: *
29: * @return boolean
30: */
31: public static function isValidURL($url)
32: {
33: return filter_var($url, FILTER_VALIDATE_URL);
34: }
35:
36: /**
37: * Validate IP address
38: *
39: * @param string $ip
40: *
41: * @return boolean
42: */
43: public static function isValidIP($ip)
44: {
45: return filter_var($ip, FILTER_VALIDATE_IP);
46: }
47:
48: /**
49: * Validate customer names
50: *
51: * @param string $name
52: *
53: * @return boolean
54: */
55: public static function isValidName($name)
56: {
57: return preg_match("/^[a-zA-Z ]*$/", $name);
58: }
59:
60: /**
61: * Validate amount.
62: *
63: * @param float $amt
64: *
65: * @return boolean
66: */
67: public static function isValidAmount($amt)
68: {
69: return preg_match('/^(-)?[0-9]+(?:\.[0-9]{0,2})?$/', $amt);
70: }
71:
72: /**
73: * Validate quantity
74: *
75: * @param int $quantity
76: *
77: * @return boolean
78: */
79: public static function isValidCartQuantity($quantity)
80: {
81: return is_int($quantity) && $quantity > 0;
82: }
83:
84: /**
85: * Validate transaction reference
86: *
87: * @param string $trnref
88: *
89: * @return boolean
90: */
91: public static function isValidTrnRef($trnref)
92: {
93: #TODO
94: return true;
95: }
96:
97: /**
98: * Validate Order ID
99: *
100: * @param string $trnref
101: *
102: * @return boolean
103: */
104: public static function isValidOrderId($trnref)
105: {
106: #TODO
107: return true;
108: }
109:
110: /**
111: * Validate output format
112: *
113: * @param string $outputFormat
114: *
115: * @return boolean
116: */
117: public static function isValidOutputFormat($outputFormat)
118: {
119: return in_array($outputFormat, [
120: Defines::COMMUNICATION_FORMAT_XML,
121: Defines::COMMUNICATION_FORMAT_JSON,
122: ]);
123: }
124:
125: /**
126: * Validate card number
127: *
128: * @param $cardNo
129: *
130: * @return boolean
131: */
132: public static function isValidCardNumber($cardNo)
133: {
134: $cardNo = str_replace(" ", "", trim($cardNo));
135: if ((!is_numeric($cardNo)) || (strlen($cardNo) > 19) || (strlen($cardNo) < 13)) {
136: return false;
137: }
138: $sum = $dub = $add = $chk = 0;
139: $even = 0;
140: for ($i = strlen($cardNo) - 1; $i >= 0; $i--) {
141: if ($even == 1) {
142: $dub = 2 * $cardNo[$i];
143: if ($dub > 9) {
144: $add = $dub - 9;
145: } else {
146: $add = $dub;
147: }
148: $even = 0;
149: } else {
150: $add = $cardNo[$i];
151: $even = 1;
152: }
153: $sum += $add;
154: }
155:
156: return (($sum % 10) == 0);
157: }
158:
159: /**
160: * Validate card CVC
161: *
162: * @param $cvc
163: *
164: * @return boolean
165: */
166: public static function isValidCVC($cvc)
167: {
168: return (is_numeric($cvc) && strlen($cvc) == 3);
169: }
170:
171: public static function versionCheck($current, $required)
172: {
173: return (int)str_replace('.', '', $current) >= (int)str_replace('.', '', $required);
174: }
175:
176: /**
177: * Escape HTML special chars
178: *
179: * @param string $text
180: *
181: * @return string type
182: */
183: public static function escape($text)
184: {
185: if ($text !== null){
186: $text = htmlspecialchars_decode($text, ENT_QUOTES);
187:
188: return htmlspecialchars($text, ENT_QUOTES);
189: }else{
190: return $text;
191: }
192:
193: }
194:
195: /**
196: * Unescape HTML special chars
197: *
198: * @param string $text
199: *
200: * @return string
201: */
202: public static function unescape($text)
203: {
204: if ($text !== null){
205: return htmlspecialchars_decode($text, ENT_QUOTES);
206: }else{
207: return $text;
208: }
209: }
210:
211: /**
212: * Return associative array element by key.
213: * If key not found in array returns $default
214: * If $notEmpty argument is TRUE returns $default even if key is found in array but the element has empty value(0, null, '')
215: *
216: * @param array $array
217: * @param mixed $key
218: * @param string $default
219: * @param bool $notEmpty
220: *
221: * @return mixed
222: */
223: public static function getArrayVal($array, $key, $default = '', $notEmpty = false)
224: {
225: if (!is_array($array)) {
226: return $default;
227: }
228: if ($notEmpty) {
229: if (array_key_exists($key, $array)) {
230: $val = trim($array[$key]);
231: if (!empty($val)) {
232: return $val;
233: }
234: }
235:
236: return $default;
237: } else {
238: return array_key_exists($key, $array) ? $array[$key] : $default;
239: }
240: }
241:
242: /**
243: * Returns one-dimensional array with all values from multi-dimensional array
244: * Useful when create request signature where only array values matter
245: *
246: * @param array $array
247: * @param array $values
248: *
249: * @return array
250: */
251: public static function getValuesFromMultiDimensionalArray($array, $values = [])
252: {
253: if (!is_array($array)) {
254: return $values;
255: }
256: foreach ($array as $k => $v) {
257: if (is_array($v)) {
258: $values = self::getValuesFromMultiDimensionalArray($v, $values);
259: } else {
260: $values[] = $v;
261: }
262: }
263:
264: return $values;
265: }
266: }
267: