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 Fields Validator
29: *
30: * @category Mage
31: * @package Mage_Api2
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Api2_Model_Resource_Validator_Fields extends Mage_Api2_Model_Resource_Validator
35: {
36: /**
37: * Config node key of current validator
38: */
39: const CONFIG_NODE_KEY = 'fields';
40:
41: /**
42: * Resource
43: *
44: * @var Mage_Api2_Model_Resource
45: */
46: protected $_resource;
47:
48: /**
49: * List of Validators (Zend_Validate_Interface)
50: * The key is a field name, a value is validator for this field
51: *
52: * @var array
53: */
54: protected $_validators;
55:
56: /**
57: * List of required fields
58: *
59: * @var array
60: */
61: protected $_requiredFields;
62:
63: /**
64: * Construct. Set all depends.
65: *
66: * Required parameteres for options:
67: * - resource
68: *
69: * @param array $options
70: * @throws Exception If passed parameter 'resource' is wrong
71: */
72: public function __construct($options)
73: {
74: if (!isset($options['resource']) || !$options['resource'] instanceof Mage_Api2_Model_Resource) {
75: throw new Exception("Passed parameter 'resource' is wrong.");
76: }
77: $this->_resource = $options['resource'];
78:
79: $validationConfig = $this->_resource->getConfig()->getValidationConfig(
80: $this->_resource->getResourceType(), self::CONFIG_NODE_KEY);
81: if (!is_array($validationConfig)) {
82: $validationConfig = array();
83: }
84: $this->_buildValidatorsChain($validationConfig);
85: }
86:
87: /**
88: * Build validator chain with config data
89: *
90: * @param array $validationConfig
91: * @throws Exception If validator type is not set
92: * @throws Exception If validator is not exist
93: */
94: protected function _buildValidatorsChain(array $validationConfig)
95: {
96: foreach ($validationConfig as $field => $validatorsConfig) {
97: if (count($validatorsConfig)) {
98: $chainForOneField = new Zend_Validate();
99: foreach ($validatorsConfig as $validatorName => $validatorConfig) {
100: // it is required field
101: if ('required' == $validatorName && 1 == $validatorConfig) {
102: $this->_requiredFields[] = $field;
103: continue;
104: }
105: // instantiation of the validator class
106: if (!isset($validatorConfig['type'])) {
107: throw new Exception("Validator type is not set for {$validatorName}");
108: }
109: $validator = $this->_getValidatorInstance(
110: $validatorConfig['type'],
111: !empty($validatorConfig['options']) ? $validatorConfig['options'] : array()
112: );
113: // set custom message
114: if (isset($validatorConfig['message'])) {
115: $validator->setMessage($validatorConfig['message']);
116: }
117: // add to list of validators
118: $chainForOneField->addValidator($validator);
119: }
120: $this->_validators[$field] = $chainForOneField;
121: }
122: }
123: }
124:
125: /**
126: * Get validator object instance
127: * Override the method if we need to use not only Zend validators!
128: *
129: * @param string $type
130: * @param array $options
131: * @return Zend_Validate_Interface
132: * @throws Exception If validator is not exist
133: */
134: protected function _getValidatorInstance($type, $options)
135: {
136: $validatorClass = 'Zend_Validate_' . $type;
137: if (!class_exists($validatorClass)) {
138: throw new Exception("Validator {$type} is not exist");
139: }
140: return new $validatorClass($options);
141: }
142:
143: /**
144: * Validate data.
145: * If fails validation, then this method returns false, and
146: * getErrors() will return an array of errors that explain why the
147: * validation failed.
148: *
149: * @param array $data
150: * @param bool $isPartial
151: * @return bool
152: */
153: public function isValidData(array $data, $isPartial = false)
154: {
155: $isValid = true;
156:
157: // required fields
158: if (!$isPartial && count($this->_requiredFields) > 0) {
159: $notEmptyValidator = new Zend_Validate_NotEmpty();
160: foreach ($this->_requiredFields as $requiredField) {
161: if (!$notEmptyValidator->isValid(isset($data[$requiredField]) ? $data[$requiredField] : null)) {
162: $isValid = false;
163: foreach ($notEmptyValidator->getMessages() as $message) {
164: $this->_addError(sprintf('%s: %s', $requiredField, $message));
165: }
166: }
167: }
168: }
169:
170: // fields rules
171: foreach ($data as $field => $value) {
172: if (isset($this->_validators[$field])) {
173: /* @var $validator Zend_Validate_Interface */
174: $validator = $this->_validators[$field];
175: if (!$validator->isValid($value)) {
176: $isValid = false;
177: foreach ($validator->getMessages() as $message) {
178: $this->_addError(sprintf('%s: %s', $field, $message));
179: }
180: }
181: }
182: }
183:
184: return $isValid;
185: }
186: }
187: