Example #1
0
 /**
  * testCreate
  *
  * @return void
  * @access public
  * @see    ____func_see____
  * @since  1.0.0
  */
 public function testCreate()
 {
     $c = new \XLite\Model\Country();
     foreach ($this->entityData as $field => $testValue) {
         $setterMethod = 'set' . \XLite\Core\Converter::getInstance()->convertToCamelCase($field);
         $getterMethod = 'get' . \XLite\Core\Converter::getInstance()->convertToCamelCase($field);
         $c->{$setterMethod}($testValue);
         $value = $c->{$getterMethod}();
         $this->assertEquals($testValue, $value, 'Creation checking (' . $field . ')');
     }
     $s = new \XLite\Model\State();
     $s->setState('test state');
     $s->setCode('ttt');
     $c->addStates($s);
     $em = \XLite\Core\Database::getEM();
     $em->persist($c);
     $em->flush();
     $em->clear();
     $c = \XLite\Core\Database::getEM()->merge($c);
     foreach ($this->entityData as $field => $testValue) {
         $getterMethod = 'get' . \XLite\Core\Converter::getInstance()->convertToCamelCase($field);
         $this->assertEquals($testValue, $c->{$getterMethod}(), 'Creation checking (' . $field . ') #2');
     }
     $this->assertEquals($s->getStateId(), $c->getStates()->get(0)->getStateId(), 'check state');
     $em->remove($c);
     $em->flush();
 }
Example #2
0
 /**
  * Sanitize
  *
  * @param mixed $data Daa
  *
  * @return mixed
  */
 public function sanitize($data)
 {
     // Get country
     $countryCodeValidator = new \XLite\Core\Validator\Pair\Simple();
     $countryCodeValidator->setName('country');
     $countryCodeValidator->setValidator(new \XLite\Core\Validator\String\ObjectId\Country(true));
     $country = $countryCodeValidator->getValidator()->sanitize($data['country']);
     // Get state
     $customState = isset($data['is_custom_state']) ? (bool) $data['is_custom_state'] : false;
     if ($customState) {
         $state = new \XLite\Model\State();
         $state->setState($data['state']);
         $state->setCountry($country);
     } else {
         $stateValidator = new \XLite\Core\Validator\String\ObjectId\State(true);
         $state = $stateValidator->sanitize($data['state']);
     }
     return array('country' => $country, 'state' => $state);
 }
Example #3
0
 /**
  * testGetState
  *
  * @return void
  * @access public
  * @see    ____func_see____
  * @since  1.0.0
  */
 public function testGetState()
 {
     $address = new \XLite\Model\Address();
     $address->map($this->addressFields);
     $address->setCountry(\XLite\Core\Database::getRepo('XLite\\Model\\Country')->find('US'));
     $address->setState(\XLite\Core\Database::getRepo('XLite\\Model\\State')->findOneByCountryAndCode('US', 'NY'));
     $this->assertInstanceOf('\\XLite\\Model\\State', $address->getState(), 'State checking');
     $address->setState('Test state');
     $this->assertInstanceOf('\\XLite\\Model\\State', $address->getState(), 'State checking #2');
     $this->assertEquals('Test state', $address->getState()->getState(), 'State name checking');
     $this->assertNull($address->getState()->getStateId(), 'State id checking');
     $s = new \XLite\Model\State();
     $s->setState('Test state 2');
     $address->setState($s);
     $this->assertInstanceOf('\\XLite\\Model\\State', $address->getState(), 'State checking #3');
     $this->assertEquals('Test state 2', $address->getState()->getState(), 'State name checking #3');
     $this->assertNull($address->getState()->getStateId(), 'State id checking #3');
     $address->setCustomState('Test state 3');
     $this->assertInstanceOf('\\XLite\\Model\\State', $address->getState(), 'State checking #4');
     $this->assertEquals('Test state 3', $address->getState()->getState(), 'State name checking #4');
     $this->assertNull($address->getState()->getStateId(), 'State id checking #4');
 }
Example #4
0
 /**
  * Sanitize
  *
  * @param mixed $data Daa
  *
  * @return mixed
  */
 public function sanitize($data)
 {
     // Check country
     if (!isset($data[static::FIELD_COUNTRY]) && !$this->isFieldAvailable(static::FIELD_COUNTRY)) {
         $data[static::FIELD_COUNTRY] = \XLite\Model\Address::getDefaultFieldValue('country')->getCode();
     }
     // Get country
     $countryCodeValidator = new \XLite\Core\Validator\Pair\Simple();
     $countryCodeValidator->setName(static::FIELD_COUNTRY);
     $countryCodeValidator->setValidator(new \XLite\Core\Validator\String\ObjectId\Country(true));
     $country = $countryCodeValidator->getValidator()->sanitize($data[static::FIELD_COUNTRY]);
     // Get state
     if ($country->hasStates()) {
         $stateValidator = new \XLite\Core\Validator\String\ObjectId\State(true);
         $state = $stateValidator->sanitize($data[static::FIELD_STATE]);
     } elseif (!empty($data[static::FIELD_CUSTOM_STATE])) {
         $state = new \XLite\Model\State();
         $state->setState($data[static::FIELD_CUSTOM_STATE]);
         $state->setCountry($country);
         $data[static::FIELD_STATE] = $data[static::FIELD_CUSTOM_STATE];
     } else {
         $state = null;
     }
     return array('country' => $country, 'state' => $state, static::FIELD_COUNTRY => $data[static::FIELD_COUNTRY], static::FIELD_STATE => $state ? $data[static::FIELD_STATE] : null);
 }
Example #5
0
 /**
  * Get state
  *
  * @return \XLite\Model\State
  */
 public function getState()
 {
     if ($this->state) {
         // Real state object
         $state = $this->state;
     } else {
         // Custom state
         $state = new \XLite\Model\State();
         $state->setState($this->getCustomState());
     }
     return $state;
 }
Example #6
0
 /**
  * Change shipping method
  *
  * @return void
  */
 protected function doActionChangeMethod()
 {
     if (\XLite\Core\Request::getInstance()->methodId && $this->getCart()->getShippingId() != \XLite\Core\Request::getInstance()->methodId) {
         $this->getCart()->setShippingId(\XLite\Core\Request::getInstance()->methodId);
         $address = $this->getCartProfile()->getShippingAddress();
         if (!$address) {
             // Default address
             $profile = $this->getCartProfile();
             $address = new \XLite\Model\Address();
             $addr = $this->getAddress();
             // Country
             $c = 'US';
             if ($addr && isset($addr['country'])) {
                 $c = $addr['country'];
             } elseif (\XLite\Core\Config::getInstance()->General->default_country) {
                 $c = \XLite\Core\Config::getInstance()->General->default_country;
             }
             $country = \XLite\Core\Database::getRepo('XLite\\Model\\Country')->find($c);
             if ($country) {
                 $address->setCountry($country);
             }
             // State
             $state = null;
             if ($addr && !empty($addr['state'])) {
                 $state = \XLite\Core\Database::getRepo('XLite\\Model\\State')->find($addr['state']);
             } elseif (!$addr && \XLite\Core\Config::getInstance()->Shipping->anonymous_custom_state) {
                 $state = new \XLite\Model\State();
                 $state->setState(\XLite\Core\Config::getInstance()->Shipping->anonymous_custom_state);
             }
             if ($state) {
                 $address->setState($state);
             }
             // Zip code
             $address->setZipcode(\XLite\Core\Config::getInstance()->General->default_zipcode);
             $address->setProfile($profile);
             $address->setIsShipping(true);
             $profile->addAddresses($address);
             \XLite\Core\Database::getEM()->persist($address);
         }
         $this->updateCart();
         \XLite\Core\Event::updateCart(array('items' => array(), 'shipping' => $this->getCart()->getShippingId()));
     }
     $this->valid = true;
     $this->setSilenceClose();
 }
Example #7
0
 /**
  * Get state
  *
  * @return \XLite\Model\State
  */
 protected function getState()
 {
     $address = $this->getAddress();
     $state = null;
     // From getDestinationAddress()
     if ($address && isset($address['state']) && $address['state']) {
         $state = \XLite\Core\Database::getRepo('XLite\\Model\\State')->find($address['state']);
     } elseif ($this->getCart()->getProfile() && $this->getCart()->getProfile()->getShippingAddress() && $this->getCart()->getProfile()->getShippingAddress()->getState()) {
         // From shipping address
         $state = $this->getCart()->getProfile()->getShippingAddress()->getState();
     } elseif (!$address && \XLite\Core\Config::getInstance()->Shipping->anonymous_custom_state) {
         // From config
         $state = new \XLite\Model\State();
         $state->setState(\XLite\Core\Config::getInstance()->Shipping->anonymous_custom_state);
     }
     return $state;
 }
Example #8
0
 /**
  * doActionAdd
  *
  * @return void
  */
 protected function doActionAdd()
 {
     $fields = array('country_code', 'code', 'state');
     $postData = \XLite\Core\Request::getInstance()->getData();
     foreach ($postData as $k => $v) {
         if (in_array($k, $fields)) {
             $postData[$k] = trim($v);
         }
     }
     $country = \XLite\Core\Database::getRepo('XLite\\Model\\Country')->find($postData['country_code']);
     if (!$country) {
         $this->set('valid', false);
     } elseif (empty($postData['code'])) {
         $this->set('valid', false);
     } elseif (empty($postData['state'])) {
         $this->set('valid', false);
     } else {
         $found = false;
         foreach ($country->getStates() as $s) {
             if ($s->getCode() == $postData['code'] || $s->getState() == $postData['state']) {
                 $found = true;
                 break;
             }
         }
         if ($found) {
             $this->set('valid', false);
         } else {
             $state = new \XLite\Model\State();
             $state->map($postData);
             $state->country = $country;
             \XLite\Core\Database::getEM()->persist($state);
             \XLite\Core\Database::getEM()->flush();
             \XLite\Core\Database::getRepo('XLite\\Model\\Country')->cleanCache();
         }
     }
 }