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_Customer
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 class for customer address rest
29: *
30: * @category Mage
31: * @package Mage_Customer
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Customer_Model_Api2_Customer_Address_Validator extends Mage_Api2_Model_Resource_Validator_Eav
35: {
36: /**
37: * Separator for multistreet
38: */
39: const STREET_SEPARATOR = '; ';
40:
41: /**
42: * Filter request data.
43: *
44: * @param array $data
45: * @return array Filtered data
46: */
47: public function filter(array $data)
48: {
49: $filteredData = parent::filter($data);
50:
51: // If the array contains more than two elements, then combine the extra elements in a string
52: if (isset($filteredData['street']) && is_array($filteredData['street']) && count($filteredData['street']) > 2) {
53: $filteredData['street'][1] .= self::STREET_SEPARATOR
54: . implode(self::STREET_SEPARATOR, array_slice($filteredData['street'], 2));
55: $filteredData['street'] = array_slice($filteredData['street'], 0, 2);
56: }
57: // pass default addresses info
58: if (isset($data['is_default_billing'])) {
59: $filteredData['is_default_billing'] = $data['is_default_billing'];
60: }
61: if (isset($data['is_default_shipping'])) {
62: $filteredData['is_default_shipping'] = $data['is_default_shipping'];
63: }
64: return $filteredData;
65: }
66:
67: /**
68: * Validate data for create association with the country
69: *
70: * @param array $data
71: * @return bool
72: */
73: public function isValidDataForCreateAssociationWithCountry(array $data)
74: {
75: return $this->_checkRegion($data, Mage::getModel('directory/country')->loadByCode($data['country_id']));
76: }
77:
78: /**
79: * Validate data for change association with the country
80: *
81: * @param Mage_Customer_Model_Address $address
82: * @param array $data
83: * @return bool
84: */
85: public function isValidDataForChangeAssociationWithCountry(Mage_Customer_Model_Address $address, array $data)
86: {
87: if (!isset($data['country_id']) && !isset($data['region'])) {
88: return true;
89: }
90: // If country is in data - it has been already validated. If no - load current country.
91: if (isset($data['country_id'])) {
92: $country = Mage::getModel('directory/country')->loadByCode($data['country_id']);
93: } else {
94: $country = $address->getCountryModel();
95: }
96: return $this->_checkRegion($data, $country);
97: }
98:
99: /**
100: * Check region
101: *
102: * @param array $data
103: * @param Mage_Directory_Model_Country $country
104: * @return bool
105: */
106: protected function _checkRegion($data, Mage_Directory_Model_Country $country)
107: {
108: /* @var $regions Mage_Directory_Model_Resource_Region_Collection */
109: $regions = $country->getRegions();
110: // Is it the country with predifined regions?
111: if ($regions->count()) {
112: if (!array_key_exists('region', $data) || empty($data['region'])) {
113: $this->_addError('"State/Province" is required.');
114: return false;
115: }
116:
117: if (!is_string($data['region'])) {
118: $this->_addError('Invalid "State/Province" type.');
119: return false;
120: }
121:
122: $count = $regions->addFieldToFilter(array('default_name', 'code'), array($data['region'], $data['region']))
123: ->clear()
124: ->count();
125: if (!$count) {
126: $this->_addError('State/Province does not exist.');
127: return false;
128: }
129: } else {
130: if (array_key_exists('region', $data) && !is_string($data['region'])) {
131: $this->_addError('Invalid "State/Province" type.');
132: return false;
133: }
134: }
135:
136: return true;
137: }
138: }
139: