1: <?php
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: class Mage_Api2_Model_Resource_Validator_Eav extends Mage_Api2_Model_Resource_Validator
35: {
36: 37: 38:
39: const CONFIG_NODE_KEY = 'eav';
40:
41: 42: 43: 44: 45:
46: protected $_formPath;
47:
48: 49: 50: 51: 52:
53: protected $_entity;
54:
55: 56: 57: 58: 59:
60: protected $_formCode;
61:
62: 63: 64: 65: 66:
67: protected $_eavForm;
68:
69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82:
83: public function __construct($options)
84: {
85: if (!isset($options['resource']) || !$options['resource'] instanceof Mage_Api2_Model_Resource) {
86: throw new Exception("Passed parameter 'resource' is wrong.");
87: }
88: $resource = $options['resource'];
89: $userType = $resource->getUserType();
90:
91: $validationConfig = $resource->getConfig()->getValidationConfig(
92: $resource->getResourceType(), self::CONFIG_NODE_KEY);
93:
94: if (empty($validationConfig[$userType]['form_model'])) {
95: throw new Exception("Config parameter 'formPath' is empty.");
96: }
97: $this->_formPath = $validationConfig[$userType]['form_model'];
98:
99: if (empty($validationConfig[$userType]['form_code'])) {
100: throw new Exception("Config parameter 'formCode' is empty.");
101: }
102: $this->_formCode = $validationConfig[$userType]['form_code'];
103:
104: if (empty($validationConfig[$userType]['entity_model'])) {
105: throw new Exception("Config parameter 'entity' is wrong.");
106: }
107: $this->_entity = Mage::getModel($validationConfig[$userType]['entity_model']);
108: if (empty($this->_entity) || !$this->_entity instanceof Mage_Core_Model_Abstract) {
109: throw new Exception("Entity is not model.");
110: }
111:
112: $this->_eavForm = Mage::getModel($this->_formPath);
113: if (empty($this->_eavForm) || !$this->_eavForm instanceof Mage_Eav_Model_Form) {
114: throw new Exception("Eav form '{$this->_formPath}' is not found.");
115: }
116: $this->_eavForm->setEntity($this->_entity)
117: ->setFormCode($this->_formCode)
118: ->ignoreInvisible(false);
119: }
120:
121: 122: 123: 124: 125: 126: 127:
128: protected function _validateAttributeWithSource(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $attrValue)
129: {
130: $errors = array();
131:
132:
133: if (null !== $attrValue && $attribute->getSourceModel()) {
134: if ('multiselect' !== $attribute->getFrontendInput() && is_array($attrValue)) {
135: return array('Invalid value type for ' . $attribute->getAttributeCode());
136: }
137: $possibleValues = $attribute->getSource()->getAllOptions(false);
138:
139: foreach ((array) $attrValue as $value) {
140: if (is_scalar($value)) {
141: $value = (string) $value;
142: $isValid = false;
143: foreach ($possibleValues as $optionData) {
144:
145: $useStrictMode = !(is_numeric($value) && is_numeric($optionData['value']));
146: $isValid = $useStrictMode ? $value === $optionData['value'] : $value == $optionData['value'];
147: if ($isValid) {
148: break;
149: }
150: }
151: if (!$isValid) {
152: $errors[] = 'Invalid value "' . $value . '" for '. $attribute->getAttributeCode();
153: }
154: } else {
155: $errors[] = 'Invalid value type for ' . $attribute->getAttributeCode();
156: }
157: }
158: }
159: return $errors ? $errors : true;
160: }
161:
162: 163: 164: 165: 166: 167:
168: public function filter(array $data)
169: {
170: return array_intersect_key($this->_eavForm->extractData($this->_eavForm->prepareRequest($data)), $data);
171: }
172:
173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184:
185: public function isValidData(array $data, $partial = false)
186: {
187: $errors = array();
188: foreach ($this->_eavForm->getAttributes() as $attribute) {
189: if ($partial && !array_key_exists($attribute->getAttributeCode(), $data)) {
190: continue;
191: }
192: if ($this->_eavForm->ignoreInvisible() && !$attribute->getIsVisible()) {
193: continue;
194: }
195: $attrValue = isset($data[$attribute->getAttributeCode()]) ? $data[$attribute->getAttributeCode()] : null;
196:
197: $result = Mage_Eav_Model_Attribute_Data::factory($attribute, $this->_eavForm->getEntity())
198: ->setExtractedData($data)
199: ->validateValue($attrValue);
200:
201: if ($result !== true) {
202: $errors = array_merge($errors, $result);
203: } else {
204: $result = $this->_validateAttributeWithSource($attribute, $attrValue);
205:
206: if (true !== $result) {
207: $errors = array_merge($errors, $result);
208: }
209: }
210: }
211: $this->_setErrors($errors);
212:
213: return $errors ? false : true;
214: }
215:
216: 217: 218: 219: 220:
221: public function getErrors()
222: {
223:
224: $errors = array();
225: $helper = Mage::helper('eav');
226: $requiredAttrs = array();
227: $isRequiredRE = '/^' . str_replace('%s', '(.+)', preg_quote($helper->__('"%s" is a required value.'))). '$/';
228: $greaterThanRE = '/^' . str_replace(
229: '%s', '(.+)', preg_quote($helper->__('"%s" length must be equal or greater than %s characters.'))
230: ) . '$/';
231:
232:
233: foreach ($this->_errors as $error) {
234: if (preg_match($isRequiredRE, $error, $matches)) {
235: $requiredAttrs[$matches[1]] = true;
236: }
237: }
238:
239: foreach ($this->_errors as $error) {
240: if (preg_match($isRequiredRE, $error)
241: || !preg_match($greaterThanRE, $error, $matches)
242: || !isset($requiredAttrs[$matches[1]])
243: ) {
244: $errors[] = $error;
245: }
246: }
247: return $errors;
248: }
249: }
250: