Completed
Pull Request — master (#19)
by
unknown
01:47
created

Validator::_validation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 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
  protected $validations = [];
39
40
  public function payment() {
41
    $this->validations = [
42
      'amount' => [FILTER_VALIDATE_FLOAT],
43
      'description' => [FILTER_VALIDATE_REGEXP, '/^(.{0,140})$/'],
44
      'card_holder' => [FILTER_VALIDATE_REGEXP, '/^([a-zA-Z\ ]+)$/'],
45
      'card_holder_id' => [FILTER_VALIDATE_REGEXP, '/^(\d{5,8})$/'],
46
      'card_number' => [FILTER_VALIDATE_REGEXP, '/^(\d{16})$/'],
47
      'cvc' => [FILTER_VALIDATE_INT],
48
      'expiration' => [FILTER_VALIDATE_REGEXP, '/^(\d{2})\/(\d{4})$/'],
49
      'ip' => [FILTER_VALIDATE_IP],
50
    ];
51
    return $this;
52
  }
53
54
  public function release(){
55
    $this->validations = [
56
      'amount' => [FILTER_VALIDATE_FLOAT],
57
      'id' => [FILTER_VALIDATE_REGEXP, '/^([0-9a-f]{8})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{12})$/'],
58
    ];
59
    return $this;
60
  }
61
62
  public function query(){
63
    $this->validations = [
64
      'id' => [FILTER_VALIDATE_REGEXP, '/^([0-9a-f]{8})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{4})\-([0-9a-f]{12})$/'],
65
    ];
66
    return $this;
67
  }
68
69
  public function validate($fields){
70
    foreach ($this->validations as $key => $filters) {
71
      if ( !$this->_validation($fields[$key], $filters) ){
72
        throw new Exceptions\ValidationException("Error {$key}: {$fields[$key]}");
73
      }
74
    }
75
76
  }
77
78
  private function _validation ($value, $filters)
79
  {
80
    $filter = $filters[0];
81
    $flags = [];
82
    if ($filter === FILTER_VALIDATE_REGEXP) {
83
      $flags = [
84
        "options" => [
85
          "regexp"=> $filters[1],
86
        ],
87
      ];
88
    }
89
    return filter_var($value, $filter, $flags);
90
  }
91
}