/** * {@inheritdoc} */ public function getAbbreviation(AddressInterface $address) { if (null !== $address->getProvinceName()) { return $address->getProvinceName(); } if (null === $address->getProvinceCode()) { return ''; } $province = $this->provinceRepository->findOneBy(['code' => $address->getProvinceCode()]); Assert::notNull($province, sprintf('Province with code "%s" not found.', $address->getProvinceCode())); return $province->getAbbreviation() ?: $province->getName(); }
private function setAddress(AddressInterface $address, AddressInterface $lastOrderAddress) { if ($address->getFirstName() === null) { $address->setFirstName($lastOrderAddress->getFirstName()); } if ($address->getLastName() === null) { $address->setLastName($lastOrderAddress->getLastName()); } if ($address->getPhoneNumber() === null) { $address->setPhoneNumber($lastOrderAddress->getPhoneNumber()); } if ($address->getCountryCode() === null) { $address->setCountryCode($lastOrderAddress->getCountryCode()); } if ($address->getProvinceCode() === null) { $address->setProvinceCode($lastOrderAddress->getProvinceCode()); } if ($address->getStreet() === null) { $address->setStreet($lastOrderAddress->getStreet()); } if ($address->getCity() === null) { $address->setCity($lastOrderAddress->getCity()); } if ($address->getPostcode() === null) { $address->setPostcode($lastOrderAddress->getPostcode()); } }
/** * @param AddressInterface $address * * @return bool */ protected function isProvinceValid(AddressInterface $address) { $countryCode = $address->getCountryCode(); if (null === ($country = $this->countryRepository->findOneBy(array('code' => $countryCode)))) { return true; } if (!$country->hasProvinces()) { return true; } if (null === $address->getProvinceCode()) { return false; } if ($country->hasProvince($address->getProvinceCode())) { return true; } return false; }
function it_gets_province_name_if_its_abbreviation_is_not_set_but_province_exists_in_database(RepositoryInterface $provinceRepository, ProvinceInterface $province, AddressInterface $address) { $address->getProvinceName()->willReturn(null); $address->getProvinceCode()->willReturn('IE-UL'); $provinceRepository->findOneBy(['code' => 'IE-UL'])->willReturn($province); $province->getAbbreviation()->willReturn(null); $province->getName()->willReturn('Ulster'); $this->getAbbreviation($address)->shouldReturn('Ulster'); }
function it_adds_violation_because_address_has_no_province(AddressInterface $address, Country $country, ProvinceAddressConstraint $constraint, ExecutionContextInterface $context, RepositoryInterface $repository) { $country->getCode()->willReturn('IE'); $address->getCountryCode()->willreturn('IE'); $repository->findOneBy(array('code' => 'IE'))->willReturn($country); $country->hasProvinces()->willreturn(true); $address->getProvinceCode()->willreturn(null); $this->initialize($context); $context->getPropertyPath()->willReturn('property_path'); $context->getViolations()->willReturn(new \ArrayIterator(array($this->createViolation('other_property_path')))); $context->addViolation(Argument::any())->shouldBeCalled(); $this->validate($address, $constraint); }
function it_does_not_add_a_violation_if_province_is_valid(AddressInterface $address, Country $country, Province $province, ProvinceAddressConstraint $constraint, ExecutionContextInterface $context, RepositoryInterface $countryRepository, RepositoryInterface $provinceRepository) { $country->getCode()->willReturn('US'); $address->getCountryCode()->willReturn('US'); $countryRepository->findOneBy(['code' => 'US'])->willReturn($country); $country->hasProvinces()->willReturn(true); $address->getProvinceCode()->willReturn('US-AK'); $province->getCode()->willReturn('US-AK'); $provinceRepository->findOneBy(['code' => 'US-AK'])->willReturn($province); $country->hasProvince($province)->willReturn(true); $this->initialize($context); $context->getPropertyPath()->willReturn('property_path'); $context->getViolations()->willReturn(new \ArrayIterator([$this->createViolation('other_property_path')])); $context->addViolation(Argument::any())->shouldNotBeCalled(); $this->validate($address, $constraint); }
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([$zoneProvince, $zoneCountry, $zoneZone]); $address->getProvinceCode()->willReturn('TX'); $memberProvince->getCode()->willReturn('TX'); $memberProvince->getBelongsTo()->willReturn($zoneProvince); $zoneProvince->getType()->willReturn(ZoneInterface::TYPE_PROVINCE); $zoneProvince->getMembers()->willReturn([$memberProvince]); $address->getCountryCode()->willReturn('US'); $memberCountry->getCode()->willReturn('US'); $zoneCountry->getType()->willReturn(ZoneInterface::TYPE_COUNTRY); $zoneCountry->getMembers()->willReturn([$memberCountry]); $zoneCountry->getCode()->willReturn('USA'); $memberCountry->getBelongsTo()->willReturn($zoneCountry); $memberZone->getCode()->willReturn('USA'); $zoneZone->getType()->willReturn(ZoneInterface::TYPE_ZONE); $zoneZone->getMembers()->willReturn([$memberZone]); $memberZone->getBelongsTo()->willReturn($zoneZone); $repository->findOneBy(['code' => 'USA'])->willReturn($zoneCountry); $this->matchAll($address)->shouldReturn([$zoneProvince, $zoneCountry, $zoneZone]); }
function it_ignores_leading_and_trailing_spaces_or_letter_cases(AddressInterface $firstAddress, AddressInterface $secondAddress) { $firstAddress->getCity()->willReturn('TOOWOOMBA'); $firstAddress->getStreet()->willReturn('Ryans Dr '); $firstAddress->getCompany()->willReturn(' Burger'); $firstAddress->getPostcode()->willReturn(' 4350 '); $firstAddress->getLastName()->willReturn('jones '); $firstAddress->getFirstName()->willReturn('mIa'); $firstAddress->getPhoneNumber()->willReturn(' 999'); $firstAddress->getCountryCode()->willReturn('au'); $firstAddress->getProvinceCode()->willReturn(null); $firstAddress->getProvinceName()->willReturn('qUEENSLAND'); $secondAddress->getCity()->willReturn('Toowoomba'); $secondAddress->getStreet()->willReturn('Ryans Dr'); $secondAddress->getCompany()->willReturn('Burger'); $secondAddress->getPostcode()->willReturn('4350'); $secondAddress->getLastName()->willReturn('Jones'); $secondAddress->getFirstName()->willReturn('Mia'); $secondAddress->getPhoneNumber()->willReturn('999'); $secondAddress->getCountryCode()->willReturn('AU'); $secondAddress->getProvinceCode()->willReturn(null); $secondAddress->getProvinceName()->willReturn('Queensland'); $this->equal($firstAddress, $secondAddress)->shouldReturn(true); }
/** * @param AddressInterface $address * @param ZoneMemberInterface $member * * @return bool * * @throws \InvalidArgumentException */ protected function addressBelongsToZoneMember(AddressInterface $address, ZoneMemberInterface $member) { switch ($type = $member->getBelongsTo()->getType()) { case ZoneInterface::TYPE_PROVINCE: return null !== $address->getProvinceCode() && $address->getProvinceCode() === $member->getCode(); case ZoneInterface::TYPE_COUNTRY: return null !== $address->getCountryCode() && $address->getCountryCode() === $member->getCode(); case ZoneInterface::TYPE_ZONE: $zone = $this->getZoneByCode($member->getCode()); return $this->addressBelongsToZone($address, $zone); default: throw new \InvalidArgumentException(sprintf('Unexpected zone type "%s".', $type)); } }
/** * @param AddressInterface $address * * @return array */ private function normalizeAddress(AddressInterface $address) { return array_map(function ($value) { return trim(strtolower($value)); }, [$address->getCity(), $address->getCompany(), $address->getCountryCode(), $address->getFirstName(), $address->getLastName(), $address->getPhoneNumber(), $address->getPostcode(), $address->getProvinceCode(), $address->getProvinceName(), $address->getStreet()]); }