Validator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 63
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A payment() 0 15 1
A release() 0 9 1
A query() 0 8 1
A validate() 0 8 3
A _validation() 0 14 2
1
<?php
2
3
/**
4
 * The MIT License (MIT)
5
 * Copyright (c) 2016 Angel Cruz <[email protected]>.
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the “Software”), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 *
25
 * @author José Gómez <[email protected]>
26
 * @license MIT License
27
 * @copyright 2016 José Gómez
28
 */
29
30
namespace Instapago;
31
32
/**
33
 * Validator.
34
 *
35
 * Valida las entradas de datos para los métodos del API.
36
 */
37
class Validator
38
{
39
	protected $validations = [];
40
41
	public function payment()
42
	{
43
		$this->validations = [
44
	  'amount'         => [FILTER_VALIDATE_FLOAT],
45
	  'description'    => [FILTER_VALIDATE_REGEXP, '/^(.{0,140})$/'],
46
	  'card_holder'    => [FILTER_VALIDATE_REGEXP, '/^([a-zA-ZáéíóúñÁÉÍÓÚÑ\ ]+)$/'],
47
	  'card_holder_id' => [FILTER_VALIDATE_REGEXP, '/^(\d{5,8})$/'],
48
	  'card_number'    => [FILTER_VALIDATE_REGEXP, '/^(\d{16})$/'],
49
	  'cvc'            => [FILTER_VALIDATE_INT],
50
	  'expiration'     => [FILTER_VALIDATE_REGEXP, '/^(\d{2})\/(\d{4})$/'],
51
	  'ip'             => [FILTER_VALIDATE_IP],
52
	];
53
54
		return $this;
55
	}
56
57
	public function release()
58
	{
59
		$this->validations = [
60
	  'amount' => [FILTER_VALIDATE_FLOAT],
61
	  'id'     => [FILTER_VALIDATE_REGEXP, '/^([0-9a-f]{8})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{12})$/'],
62
	];
63
64
		return $this;
65
	}
66
67
	public function query()
68
	{
69
		$this->validations = [
70
	  'id' => [FILTER_VALIDATE_REGEXP, '/^([0-9a-f]{8})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{12})$/'],
71
	];
72
73
		return $this;
74
	}
75
76
	public function validate($fields)
77
	{
78
		foreach ($this->validations as $key => $filters) {
79
			if ( ! $this->_validation($fields[$key], $filters)) {
80
				throw new Exceptions\ValidationException("Error {$key}: {$fields[$key]}");
81
			}
82
		}
83
	}
84
85
	private function _validation($value, $filters)
86
	{
87
		$filter = $filters[0];
88
		$flags = [];
89
		if ($filter === FILTER_VALIDATE_REGEXP) {
90
			$flags = [
91
		'options' => [
92
		  'regexp'=> $filters[1],
93
		],
94
	  ];
95
		}
96
97
		return filter_var($value, $filter, $flags);
98
	}
99
}
100