1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* collection of errors enumerating all validation errors for a given request
*
* <b>== More information ==</b>
*
* For more detailed information on Validation errors, see {@link http://www.braintreepayments.com/gateway/validation-errors http://www.braintreepaymentsolutions.com/gateway/validation-errors}
*
* @package Braintree
* @subpackage Error
* @copyright 2014 Braintree, a division of PayPal, Inc.
*
* @property-read array $errors
* @property-read array $nested
*/
class Braintree_Error_ValidationErrorCollection extends Braintree_Collection
{
private $_errors = array();
private $_nested = array();
/**
* @ignore
*/
public function __construct($data)
{
foreach($data AS $key => $errorData)
// map errors to new collections recursively
if ($key == 'errors') {
foreach ($errorData AS $error) {
$this->_errors[] = new Braintree_Error_Validation($error);
}
} else {
$this->_nested[$key] = new Braintree_Error_ValidationErrorCollection($errorData);
}
}
public function deepAll()
{
$validationErrors = array_merge(array(), $this->_errors);
foreach($this->_nested as $nestedErrors)
{
$validationErrors = array_merge($validationErrors, $nestedErrors->deepAll());
}
return $validationErrors;
}
public function deepSize()
{
$total = sizeof($this->_errors);
foreach($this->_nested as $_nestedErrors)
{
$total = $total + $_nestedErrors->deepSize();
}
return $total;
}
public function forIndex($index)
{
return $this->forKey("index" . $index);
}
public function forKey($key)
{
return isset($this->_nested[$key]) ? $this->_nested[$key] : null;
}
public function onAttribute($attribute)
{
$matches = array();
foreach ($this->_errors AS $key => $error) {
if($error->attribute == $attribute) {
$matches[] = $error;
}
}
return $matches;
}
public function shallowAll()
{
return $this->_errors;
}
/**
*
* @ignore
*/
public function __get($name)
{
$varName = "_$name";
return isset($this->$varName) ? $this->$varName : null;
}
/**
* @ignore
*/
public function __toString()
{
$output = array();
// TODO: implement scope
if (!empty($this->_errors)) {
$output[] = $this->_inspect($this->_errors);
}
if (!empty($this->_nested)) {
foreach ($this->_nested AS $key => $values) {
$output[] = $this->_inspect($this->_nested);
}
}
return join(', ', $output);
}
/**
* @ignore
*/
private function _inspect($errors, $scope = null)
{
$eOutput = '[' . __CLASS__ . '/errors:[';
foreach($errors AS $error => $errorObj) {
$outputErrs[] = "({$errorObj->error['code']} {$errorObj->error['message']})";
}
$eOutput .= join(', ', $outputErrs) . ']]';
return $eOutput;
}
}