1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Api2
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * API2 Abstarct Validator
29: *
30: * This is an object to which we encapsulate all business logic of validation and different invariants.
31: * But instead of different validators, we group all logic in one class but in different methods.
32: *
33: * If fails validation, then validation method returns false, and
34: * getErrors() will return an array of errors that explain why the
35: * validation failed.
36: *
37: * @category Mage
38: * @package Mage_Api2
39: * @author Magento Core Team <core@magentocommerce.com>
40: */
41: abstract class Mage_Api2_Model_Resource_Validator
42: {
43: /**
44: * Array of validation failure errors.
45: *
46: * @var array
47: */
48: protected $_errors = array();
49:
50: /**
51: * Set an array of errors
52: *
53: * @param array $data
54: * @return Mage_Api2_Model_Resource_Validator
55: */
56: protected function _setErrors(array $data)
57: {
58: $this->_errors = array_values($data);
59: return $this;
60: }
61:
62: /**
63: * Add errors
64: *
65: * @param array $errors
66: * @return Mage_Api2_Model_Resource_Validator
67: */
68: protected function _addErrors($errors)
69: {
70: foreach ($errors as $error) {
71: $this->_addError($error);
72: }
73: return $this;
74: }
75:
76: /**
77: * Add error
78: *
79: * @param string $error
80: * @return Mage_Api2_Model_Resource_Validator
81: */
82: protected function _addError($error)
83: {
84: $this->_errors[] = $error;
85: return $this;
86: }
87:
88: /**
89: * Returns an array of errors that explain why the most recent isValidData()
90: * call returned false. The array keys are validation failure error identifiers,
91: * and the array values are the corresponding human-readable error strings.
92: *
93: * If isValidData() was never called or if the most recent isValidData() call
94: * returned true, then this method returns an empty array.
95: *
96: * @return array
97: */
98: public function getErrors()
99: {
100: return $this->_errors;
101: }
102: }
103: