Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Api2_Adminhtml_Api2_AttributeController
  • Mage_Api2_Adminhtml_Api2_RoleController
  • Mage_Api2_Block_Adminhtml_Attribute
  • Mage_Api2_Block_Adminhtml_Attribute_Buttons
  • Mage_Api2_Block_Adminhtml_Attribute_Edit
  • Mage_Api2_Block_Adminhtml_Attribute_Edit_Form
  • Mage_Api2_Block_Adminhtml_Attribute_Grid
  • Mage_Api2_Block_Adminhtml_Attribute_Tab_Resource
  • Mage_Api2_Block_Adminhtml_Attribute_Tabs
  • Mage_Api2_Block_Adminhtml_Permissions_User_Edit_Tab_Roles
  • Mage_Api2_Block_Adminhtml_Roles
  • Mage_Api2_Block_Adminhtml_Roles_Buttons
  • Mage_Api2_Block_Adminhtml_Roles_Grid
  • Mage_Api2_Block_Adminhtml_Roles_Tab_Info
  • Mage_Api2_Block_Adminhtml_Roles_Tab_Resources
  • Mage_Api2_Block_Adminhtml_Roles_Tab_Users
  • Mage_Api2_Block_Adminhtml_Roles_Tabs
  • Mage_Api2_Helper_Data
  • Mage_Api2_Model_Acl
  • Mage_Api2_Model_Acl_Filter
  • Mage_Api2_Model_Acl_Filter_Attribute
  • Mage_Api2_Model_Acl_Filter_Attribute_Operation
  • Mage_Api2_Model_Acl_Filter_Attribute_ResourcePermission
  • Mage_Api2_Model_Acl_Global
  • Mage_Api2_Model_Acl_Global_Role
  • Mage_Api2_Model_Acl_Global_Rule
  • Mage_Api2_Model_Acl_Global_Rule_Permission
  • Mage_Api2_Model_Acl_Global_Rule_Privilege
  • Mage_Api2_Model_Acl_Global_Rule_ResourcePermission
  • Mage_Api2_Model_Acl_Global_Rule_Tree
  • Mage_Api2_Model_Auth
  • Mage_Api2_Model_Auth_Adapter
  • Mage_Api2_Model_Auth_Adapter_Abstract
  • Mage_Api2_Model_Auth_Adapter_Oauth
  • Mage_Api2_Model_Auth_User
  • Mage_Api2_Model_Auth_User_Abstract
  • Mage_Api2_Model_Auth_User_Admin
  • Mage_Api2_Model_Auth_User_Customer
  • Mage_Api2_Model_Auth_User_Guest
  • Mage_Api2_Model_Config
  • Mage_Api2_Model_Dispatcher
  • Mage_Api2_Model_Multicall
  • Mage_Api2_Model_Observer
  • Mage_Api2_Model_Renderer
  • Mage_Api2_Model_Renderer_Json
  • Mage_Api2_Model_Renderer_Query
  • Mage_Api2_Model_Renderer_Xml
  • Mage_Api2_Model_Renderer_Xml_Writer
  • Mage_Api2_Model_Request
  • Mage_Api2_Model_Request_Internal
  • Mage_Api2_Model_Request_Interpreter
  • Mage_Api2_Model_Request_Interpreter_Json
  • Mage_Api2_Model_Request_Interpreter_Query
  • Mage_Api2_Model_Request_Interpreter_Xml
  • Mage_Api2_Model_Resource
  • Mage_Api2_Model_Resource_Acl_Filter_Attribute
  • Mage_Api2_Model_Resource_Acl_Filter_Attribute_Collection
  • Mage_Api2_Model_Resource_Acl_Global_Role
  • Mage_Api2_Model_Resource_Acl_Global_Role_Collection
  • Mage_Api2_Model_Resource_Acl_Global_Rule
  • Mage_Api2_Model_Resource_Acl_Global_Rule_Collection
  • Mage_Api2_Model_Resource_Setup
  • Mage_Api2_Model_Resource_Validator
  • Mage_Api2_Model_Resource_Validator_Eav
  • Mage_Api2_Model_Resource_Validator_Fields
  • Mage_Api2_Model_Response
  • Mage_Api2_Model_Route_Abstract
  • Mage_Api2_Model_Route_ApiType
  • Mage_Api2_Model_Route_Rest
  • Mage_Api2_Model_Router
  • Mage_Api2_Model_Server

Interfaces

  • Mage_Api2_Model_Acl_PermissionInterface
  • Mage_Api2_Model_Renderer_Interface
  • Mage_Api2_Model_Request_Interpreter_Interface
  • Mage_Api2_Model_Route_Interface

Exceptions

  • Mage_Api2_Exception
  • Overview
  • Package
  • Class
  • Tree
  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 EAV 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_Eav extends Mage_Api2_Model_Resource_Validator
 35: {
 36:     /**
 37:      * Config node key of current validator
 38:      */
 39:     const CONFIG_NODE_KEY = 'eav';
 40: 
 41:     /**
 42:      * Form path
 43:      *
 44:      * @var string
 45:      */
 46:     protected $_formPath;
 47: 
 48:     /**
 49:      * Entity model
 50:      *
 51:      * @var Mage_Core_Model_Abstract
 52:      */
 53:     protected $_entity;
 54: 
 55:     /**
 56:      * Form code
 57:      *
 58:      * @var string
 59:      */
 60:     protected $_formCode;
 61: 
 62:     /**
 63:      * Eav form model
 64:      *
 65:      * @var Mage_Eav_Model_Form
 66:      */
 67:     protected $_eavForm;
 68: 
 69:     /**
 70:      * Construct. Set all depends.
 71:      *
 72:      * Required parameteres for options:
 73:      * - resource
 74:      *
 75:      * @param array $options
 76:      * @throws Exception If passed parameter 'resource' is wrong
 77:      * @throws Exception If config parameter 'formPath' is empty
 78:      * @throws Exception If config parameter 'formCode' is empty
 79:      * @throws Exception If config parameter 'entity' is wrong
 80:      * @throws Exception If entity is not model
 81:      * @throws Exception If eav form is not found
 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:      * Validate attribute value for attributes with source models
123:      *
124:      * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
125:      * @param mixed $attrValue
126:      * @return array|bool
127:      */
128:     protected function _validateAttributeWithSource(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $attrValue)
129:     {
130:         $errors = array();
131: 
132:         // validate attributes with source models
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:                         // comparison without types check is performed only when both values are numeric
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:      * Filter request data.
164:      *
165:      * @param  array $data
166:      * @return array Filtered data
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:      * Validate entity.
175:      * If the $partial parameter is TRUE, then we validate only those parameters that were passed.
176:      *
177:      * If fails validation, then this method returns false, and
178:      * getErrors() will return an array of errors that explain why the
179:      * validation failed.
180:      *
181:      * @param array $data
182:      * @param bool $partial
183:      * @return bool
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:      * Returns an array of errors
218:      *
219:      * @return array
220:      */
221:     public function getErrors()
222:     {
223:         // business asked to avoid additional validation message, so we filter it here
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:         // find all required attributes labels
233:         foreach ($this->_errors as $error) {
234:             if (preg_match($isRequiredRE, $error, $matches)) {
235:                 $requiredAttrs[$matches[1]] = true;
236:             }
237:         }
238:         // exclude additional messages for required attributes been failed
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: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0