|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This is the part of Povils open-source library. |
| 5 | + * |
| 6 | + * @author Povilas Susinskas |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Omnipay\Paysera\Common; |
| 10 | + |
| 11 | +use Omnipay\Common\Exception\InvalidRequestException; |
| 12 | + |
| 13 | +/** |
| 14 | + * Class PurchaseParameterValidator |
| 15 | + * |
| 16 | + * @package Omnipay\Paysera\Common |
| 17 | + */ |
| 18 | +class PurchaseParameterValidator |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @param array $data |
| 22 | + * |
| 23 | + * @throws InvalidRequestException |
| 24 | + */ |
| 25 | + public static function validate(array $data) |
| 26 | + { |
| 27 | + foreach (self::getRequestSpecifications() as $specification) { |
| 28 | + list($name, $maxLength, $required, $regexp) = $specification; |
| 29 | + if ($required && false === isset($data[$name])) { |
| 30 | + throw new InvalidRequestException(sprintf("'%s' is required but missing.", $name)); |
| 31 | + } |
| 32 | + |
| 33 | + if (false === empty($data[$name])) { |
| 34 | + if ($maxLength && strlen($data[$name]) > $maxLength) { |
| 35 | + throw new InvalidRequestException(sprintf( |
| 36 | + "'%s' value is too long (%d), %d characters allowed.", |
| 37 | + $name, |
| 38 | + strlen($data[$name]), |
| 39 | + $maxLength |
| 40 | + )); |
| 41 | + } |
| 42 | + |
| 43 | + if ($regexp !== '' && !preg_match($regexp, $data[$name])) { |
| 44 | + throw new InvalidRequestException(sprintf("'%s' value '%s' is invalid.", $name, $data[$name])); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Array structure: |
| 52 | + * name – request parameter name |
| 53 | + * maxLength – max allowed length for parameter |
| 54 | + * required – is this parameter required |
| 55 | + * regexp – regexp to test parameter value |
| 56 | + * |
| 57 | + * @return array |
| 58 | + */ |
| 59 | + protected static function getRequestSpecifications() |
| 60 | + { |
| 61 | + return [ |
| 62 | + ['orderid', 40, true, ''], |
| 63 | + ['accepturl', 255, true, ''], |
| 64 | + ['cancelurl', 255, true, ''], |
| 65 | + ['callbackurl', 255, true, ''], |
| 66 | + ['lang', 3, false, '/^[a-z]{3}$/i'], |
| 67 | + ['amount', 11, false, '/^\d+$/'], |
| 68 | + ['currency', 3, false, '/^[a-z]{3}$/i'], |
| 69 | + ['payment', 20, false, ''], |
| 70 | + ['country', 2, false, '/^[a-z_]{2}$/i'], |
| 71 | + ['p_firstname', 255, false, ''], |
| 72 | + ['p_lastname', 255, false, ''], |
| 73 | + ['p_email', 255, false, ''], |
| 74 | + ['p_street', 255, false, ''], |
| 75 | + ['p_city', 255, false, ''], |
| 76 | + ['p_state', 20, false, ''], |
| 77 | + ['p_zip', 20, false, ''], |
| 78 | + ['p_countrycode', 2, false, '/^[a-z]{2}$/i'], |
| 79 | + ['test', 1, false, '/^[01]$/'], |
| 80 | + ]; |
| 81 | + } |
| 82 | +} |
0 commit comments