ValidationErrorCollection.php in Commerce Braintree 7
File
braintree_php/lib/Braintree/Error/ValidationErrorCollection.php
View source
<?php
class Braintree_Error_ValidationErrorCollection extends Braintree_Collection {
private $_errors = array();
private $_nested = array();
public function __construct($data) {
foreach ($data as $key => $errorData) {
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;
}
public function __get($name) {
$varName = "_{$name}";
return isset($this->{$varName}) ? $this->{$varName} : null;
}
public function __toString() {
$output = array();
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);
}
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;
}
}