*/ abstract class Mage_Customer_Model_Api2_Customer_Rest extends Mage_Customer_Model_Api2_Customer { /** * Create customer * * @param array $data * @return string */ protected function _create(array $data) { /** @var $validator Mage_Api2_Model_Resource_Validator_Eav */ $validator = Mage::getResourceModel('api2/validator_eav', array('resource' => $this)); $data = $validator->filter($data); if (!$validator->isValidData($data)) { foreach ($validator->getErrors() as $error) { $this->_error($error, Mage_Api2_Model_Server::HTTP_BAD_REQUEST); } $this->_critical(self::RESOURCE_DATA_PRE_VALIDATION_ERROR); } /** @var $customer Mage_Customer_Model_Customer */ $customer = Mage::getModel('customer/customer'); $customer->setData($data); try { $customer->save(); } catch (Mage_Core_Exception $e) { $this->_error($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR); } catch (Exception $e) { $this->_critical(self::RESOURCE_INTERNAL_ERROR); } return $this->_getLocation($customer); } /** * Retrieve information about customer * * @throws Mage_Api2_Exception * @return array */ protected function _retrieve() { /** @var $customer Mage_Customer_Model_Customer */ $customer = $this->_loadCustomerById($this->getRequest()->getParam('id')); return $customer->getData(); } /** * Get customers list * * @return array */ protected function _retrieveCollection() { $data = $this->_getCollectionForRetrieve()->load()->toArray(); return isset($data['items']) ? $data['items'] : $data; } /** * Update customer * * @param array $data * @throws Mage_Api2_Exception */ protected function _update(array $data) { /** @var $customer Mage_Customer_Model_Customer */ $customer = $this->_loadCustomerById($this->getRequest()->getParam('id')); /** @var $validator Mage_Api2_Model_Resource_Validator_Eav */ $validator = Mage::getResourceModel('api2/validator_eav', array('resource' => $this)); $data = $validator->filter($data); unset($data['website_id']); // website is not allowed to change if (!$validator->isValidData($data, true)) { foreach ($validator->getErrors() as $error) { $this->_error($error, Mage_Api2_Model_Server::HTTP_BAD_REQUEST); } $this->_critical(self::RESOURCE_DATA_PRE_VALIDATION_ERROR); } $customer->addData($data); try { $customer->save(); } catch (Mage_Core_Exception $e) { $this->_error($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR); } catch (Exception $e) { $this->_critical(self::RESOURCE_INTERNAL_ERROR); } } /** * Load customer by id * * @param int $id * @throws Mage_Api2_Exception * @return Mage_Customer_Model_Customer */ protected function _loadCustomerById($id) { /** @var $customer Mage_Customer_Model_Customer */ $customer = Mage::getModel('customer/customer')->load($id); if (!$customer->getId()) { $this->_critical(self::RESOURCE_NOT_FOUND); } return $customer; } /** * Retrieve collection instances * * @return Mage_Customer_Model_Resource_Customer_Collection */ protected function _getCollectionForRetrieve() { /** @var $collection Mage_Customer_Model_Resource_Customer_Collection */ $collection = Mage::getResourceModel('customer/customer_collection'); $collection->addAttributeToSelect(array_keys( $this->getAvailableAttributes($this->getUserType(), Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_READ) )); $this->_applyCollectionModifiers($collection); return $collection; } }