protected function getChinaSubdivisionRegionCodeByAddress(AddressInterface $address)
 {
     $isoName = $address->getProvince()->getIsoName();
     foreach ($this->chinaSubdivisions as $subdivision) {
         if ($subdivision['iso_name'] === $isoName) {
             return $subdivision['sf_region_code'];
         }
     }
     throw new CalculatorException(sprintf('Region %s, %s is not supported.', $address->getProvince()->getName(), $address->getCountry()->getName()));
 }
 /**
  * Override this method to implement your logic
  *
  * @param AddressInterface $address
  *
  * @return bool
  */
 protected function isProvinceValid(AddressInterface $address)
 {
     if (null === ($country = $address->getCountry())) {
         return true;
     }
     if (!$country->hasProvinces()) {
         return true;
     }
     if (null === $address->getProvince()) {
         return false;
     }
     if ($country->hasProvince($address->getProvince())) {
         return true;
     }
     return false;
 }
 /**
  * @param AddressInterface $address
  *
  * @return bool
  */
 protected function isProvinceValid(AddressInterface $address)
 {
     $countryCode = $address->getCountry();
     if (null === ($country = $this->countryRepository->findOneBy(array('code' => $countryCode)))) {
         return true;
     }
     if (!$country->hasProvinces()) {
         return true;
     }
     if (null === $address->getProvince()) {
         return false;
     }
     if ($country->hasProvince($address->getProvince())) {
         return true;
     }
     return false;
 }
Example #4
0
 public static function createAddress(AddressInterface $address)
 {
     $result = (new Address())->setStreetLines([$address->getStreet()])->setCity($address->getCity())->setPostalCode($address->getPostcode())->setCountryCode($address->getCountry()->getIsoName());
     if (null === ($province = $address->getProvince())) {
         return $result;
     }
     return $result->setStateOrProvinceCode($province->getIsoName());
 }
 function it_adds_violation_because_address_has_not_province(AddressInterface $address, Country $country, ProvinceAddressConstraint $constraint, ExecutionContextInterface $context)
 {
     $address->getCountry()->shouldBeCalled()->willreturn($country);
     $country->hasProvinces()->shouldBeCalled()->willreturn(true);
     $address->getProvince()->shouldBeCalled()->willreturn(null);
     $this->initialize($context);
     $context->getPropertyPath()->shouldBeCalled()->willReturn('property_path');
     $context->getViolations()->shouldBeCalled()->willReturn(new \ArrayIterator(array($this->createViolation('other_property_path'))));
     $context->addViolation(Argument::any())->shouldBeCalled();
     $this->validate($address, $constraint);
 }
Example #6
0
 function it_should_match_address_from_province_when_many_are_found_by_scope($repository, CountryInterface $country, ProvinceInterface $province, AddressInterface $address, ZoneMemberCountry $memberCountry, ZoneMemberProvince $memberProvince, ZoneInterface $zoneCountry, ZoneInterface $zoneProvince)
 {
     $address->getProvince()->willReturn($province);
     $address->getCountry()->willReturn($country);
     $memberProvince->getProvince()->willReturn($province);
     $memberCountry->getCountry()->willReturn($country);
     $zoneProvince->getMembers()->willReturn(array($memberProvince));
     $zoneProvince->getType()->willReturn(ZoneInterface::TYPE_PROVINCE);
     $zoneCountry->getMembers()->willReturn(array($memberCountry));
     $zoneCountry->getType()->willReturn(ZoneInterface::TYPE_COUNTRY);
     $repository->findBy(array('scope' => 'shipping'))->shouldBeCalled()->willReturn(array($zoneCountry, $zoneProvince));
     $memberProvince->getBelongsTo()->willReturn($zoneProvince);
     $memberCountry->getBelongsTo()->willReturn($zoneCountry);
     $this->match($address, 'shipping')->shouldReturn($zoneProvince);
 }
Example #7
0
 /**
  * Checks if address belongs to particular zone member.
  *
  * @param AddressInterface    $address
  * @param ZoneMemberInterface $member
  *
  * @return Boolean
  *
  * @throws \InvalidArgumentException
  */
 protected function addressBelongsToZoneMember(AddressInterface $address, ZoneMemberInterface $member)
 {
     $type = $member->getBelongsTo()->getType();
     switch ($type) {
         case ZoneInterface::TYPE_PROVINCE:
             return null !== $address->getProvince() && $address->getProvince() === $member->getProvince();
         case ZoneInterface::TYPE_COUNTRY:
             return null !== $address->getCountry() && $address->getCountry() === $member->getCountry();
         case ZoneInterface::TYPE_ZONE:
             return $this->addressBelongsToZone($address, $member->getZone());
         default:
             throw new \InvalidArgumentException(sprintf('Unexpected zone type "%s".', $type));
     }
 }
Example #8
0
 function it_matches_all_zones_with_given_address(RepositoryInterface $repository, AddressInterface $address, ZoneMemberInterface $memberProvince, ZoneMemberInterface $memberCountry, ZoneMemberInterface $memberZone, ZoneInterface $zoneProvince, ZoneInterface $zoneCountry, ZoneInterface $zoneZone)
 {
     $repository->findAll()->willReturn(array($zoneProvince, $zoneCountry, $zoneZone));
     $address->getProvince()->willReturn('TX');
     $memberProvince->getCode()->willReturn('TX');
     $memberProvince->getBelongsTo()->willReturn($zoneProvince);
     $zoneProvince->getType()->willReturn(ZoneInterface::TYPE_PROVINCE);
     $zoneProvince->getMembers()->willReturn(array($memberProvince));
     $address->getCountry()->willReturn('US');
     $memberCountry->getCode()->willReturn('US');
     $zoneCountry->getType()->willReturn(ZoneInterface::TYPE_COUNTRY);
     $zoneCountry->getMembers()->willReturn(array($memberCountry));
     $zoneCountry->getCode()->willReturn('USA');
     $memberCountry->getBelongsTo()->willReturn($zoneCountry);
     $memberZone->getCode()->willReturn('USA');
     $zoneZone->getType()->willReturn(ZoneInterface::TYPE_ZONE);
     $zoneZone->getMembers()->willReturn(array($memberZone));
     $memberZone->getBelongsTo()->willReturn($zoneZone);
     $repository->findOneBy(array('code' => 'USA'))->willReturn($zoneCountry);
     $this->matchAll($address)->shouldReturn(array($zoneProvince, $zoneCountry, $zoneZone));
 }