From d9928bff2278ca6f2c238fd607ae9e141e373858 Mon Sep 17 00:00:00 2001 From: Eric Devenport Date: Mon, 13 May 2024 14:16:54 -0600 Subject: [PATCH] Removed US Autocomplete Basic --- src/US_Autocomplete/Client.php | 67 ------------ src/US_Autocomplete/GeolocateType.php | 28 ----- src/US_Autocomplete/Lookup.php | 141 -------------------------- src/US_Autocomplete/Result.php | 37 ------- src/US_Autocomplete/Suggestion.php | 42 -------- tests/US_Autocomplete/ClientTest.php | 115 --------------------- tests/US_Autocomplete/LookupTest.php | 20 ---- tests/US_Autocomplete/ResultTest.php | 34 ------- 8 files changed, 484 deletions(-) delete mode 100644 src/US_Autocomplete/Client.php delete mode 100644 src/US_Autocomplete/GeolocateType.php delete mode 100644 src/US_Autocomplete/Lookup.php delete mode 100644 src/US_Autocomplete/Result.php delete mode 100644 src/US_Autocomplete/Suggestion.php delete mode 100644 tests/US_Autocomplete/ClientTest.php delete mode 100644 tests/US_Autocomplete/LookupTest.php delete mode 100644 tests/US_Autocomplete/ResultTest.php diff --git a/src/US_Autocomplete/Client.php b/src/US_Autocomplete/Client.php deleted file mode 100644 index 70fc320..0000000 --- a/src/US_Autocomplete/Client.php +++ /dev/null @@ -1,67 +0,0 @@ - - * and attaches the results to the appropriate Lookup objects. - */ -class Client { - private $sender, - $serializer; - - public function __construct(Sender $sender, Serializer $serializer = null) { - $this->sender = $sender; - $this->serializer = $serializer; - } - - public function sendLookup(Lookup $lookup) { - if ($lookup == null || $lookup->getPrefix() == null || strlen($lookup->getPrefix()) == 0) - throw new SmartyException("sendLookup() must be passed a Lookup with the prefix field set."); - - $request = $this->buildRequest($lookup); - $response = $this->sender->send($request); - - $result = $this->serializer->deserialize($response->getPayload()); - if ($result == null) - return; - - $lookup->setResult((new Result($result))->getSuggestions()); - } - - private function buildRequest(Lookup $lookup) { - $request = new Request(); - - $request->setParameter("prefix", $lookup->getPrefix()); - $request->setParameter("suggestions", $lookup->getMaxSuggestionsStringIfSet()); - $request->setParameter("city_filter", $this->buildFilterString($lookup->getCityFilter())); - $request->setParameter("state_filter", $this->buildFilterString($lookup->getStateFilter())); - $request->setParameter("prefer", $this->buildFilterString($lookup->getPrefer())); - $request->setParameter("prefer_ratio", $lookup->getPreferRatioStringIfSet()); - if ($lookup->getGeolocateType()->getName() != GEOLOCATE_TYPE_NONE) { - $request->setParameter("geolocate", "true"); - $request->setParameter("geolocate_precision", $lookup->getGeolocateType()->getName()); - } - else $request->setParameter("geolocate", "false"); - - return $request; - } - - private function buildFilterString($list) { - if (empty($list)) - return null; - - return join(',', $list); - } -} \ No newline at end of file diff --git a/src/US_Autocomplete/GeolocateType.php b/src/US_Autocomplete/GeolocateType.php deleted file mode 100644 index 0a8b4b9..0000000 --- a/src/US_Autocomplete/GeolocateType.php +++ /dev/null @@ -1,28 +0,0 @@ -geolocate and geolocate_precision fields in the US Autocomplete API. - * - * @see "https://smartystreets.com/docs/cloud/us-autocomplete-api#http-request-input-fields" - */ -class GeolocateType { - private $name; - - public function __construct($name) { - $this->name = $name; - } - - public function getName() { - return $this->name; - } -} \ No newline at end of file diff --git a/src/US_Autocomplete/Lookup.php b/src/US_Autocomplete/Lookup.php deleted file mode 100644 index 8eb7c80..0000000 --- a/src/US_Autocomplete/Lookup.php +++ /dev/null @@ -1,141 +0,0 @@ - - * will contain the result of the lookup after it comes back from the API. - * @see "https://smartystreets.com/docs/cloud/us-autocomplete-api#http-request-input-fields" - */ -class Lookup { - //region [ Fields ] - const MAX_SUGGESTIONS_DEFAULT = 10; - const PREFER_RATIO_DEFAULT = 1 / 3.0; - - private $result, - $prefix, - $maxSuggestions, - $cityFilter, - $stateFilter, - $prefer, - $preferRatio, - $geolocateType; - - //endregion - - /** - * If you use this constructor, don't forget to set the prefix. It is required. - * @param $prefix string The beginning of an address - */ - public function __construct($prefix = null) { - $this->prefix = $prefix; - $this->maxSuggestions = Lookup::MAX_SUGGESTIONS_DEFAULT; - $this->cityFilter = array(); - $this->stateFilter = array(); - $this->prefer = array(); - $this->preferRatio = Lookup::PREFER_RATIO_DEFAULT; - $this->geolocateType = new GeolocateType(GEOLOCATE_TYPE_CITY); - } - - public function addCityFilter($city) { - $this->cityFilter[] = $city; - } - - public function addStateFilter($stateAbbreviation) { - $this->stateFilter[] = $stateAbbreviation; - } - - public function addPrefer($cityOrState) { - $this->prefer[] = $cityOrState; - } - - //region [ Getters ] - - public function getResult() { - return $this->result; - } - - public function getResultAtIndex($index) { - return $this->result[$index]; - } - - public function getPrefix() { - return $this->prefix; - } - - public function getMaxSuggestions() { - return $this->maxSuggestions; - } - - public function getCityFilter() { - return $this->cityFilter; - } - - public function getStateFilter() { - return $this->stateFilter; - } - - public function getPrefer() { - return $this->prefer; - } - - public function getPreferRatio() { - return $this->preferRatio; - } - - public function getGeolocateType() { - return $this->geolocateType; - } - - function getMaxSuggestionsStringIfSet() { - if ($this->maxSuggestions == Lookup::MAX_SUGGESTIONS_DEFAULT) - return null; - return strval($this->maxSuggestions); - } - - function getPreferRatioStringIfSet() { - if ($this->preferRatio == Lookup::PREFER_RATIO_DEFAULT) - return null; - return strval($this->preferRatio); - } - //endregion - - //region [ Setter ] - - public function setResult($result) { - $this->result = $result; - } - - public function setPrefix($prefix) { - $this->prefix = $prefix; - } - - public function setMaxSuggestions($maxSuggestions) { - if ($maxSuggestions > 0 && $maxSuggestions <= 10) - $this->maxSuggestions = $maxSuggestions; - else - throw new \InvalidArgumentException("Max suggestions must be a positive integer no larger than 10."); - } - - public function setCityFilter($cityFilter) { - $this->cityFilter = $cityFilter; - } - - public function setStateFilter($stateFilter) { - $this->stateFilter = $stateFilter; - } - - public function setPrefer($prefer) { - $this->prefer = $prefer; - } - - public function setPreferRatio($preferRatio) { - $this->preferRatio = $preferRatio; - } - - public function setGeolocateType(GeolocateType $geolocateType) { - $this->geolocateType = $geolocateType; - } - - //endregion -} \ No newline at end of file diff --git a/src/US_Autocomplete/Result.php b/src/US_Autocomplete/Result.php deleted file mode 100644 index e8e838c..0000000 --- a/src/US_Autocomplete/Result.php +++ /dev/null @@ -1,37 +0,0 @@ -suggestions = ArrayUtil::setField($obj, 'suggestions', array()); - - $this->suggestions = $this->convertToSuggestionObjects(); - } - - private function convertToSuggestionObjects() { - $suggestionObjects = array(); - - foreach ($this->suggestions as $suggestion) - $suggestionObjects[] = new Suggestion($suggestion); - - return $suggestionObjects; - } - - public function getSuggestions() { - return $this->suggestions; - } - - public function getSuggestion($index) { - return $this->suggestions[$index]; - } -} \ No newline at end of file diff --git a/src/US_Autocomplete/Suggestion.php b/src/US_Autocomplete/Suggestion.php deleted file mode 100644 index 6b857d0..0000000 --- a/src/US_Autocomplete/Suggestion.php +++ /dev/null @@ -1,42 +0,0 @@ -text = ArrayUtil::setField($obj,'text'); - $this->streetLine = ArrayUtil::setField($obj,'street_line'); - $this->city = ArrayUtil::setField($obj,'city'); - $this->state = ArrayUtil::setField($obj,'state'); - } - - public function getText() { - return $this->text; - } - - public function getStreetLine() { - return $this->streetLine; - } - - public function getCity() { - return $this->city; - } - - public function getState() { - return $this->state; - } -} \ No newline at end of file diff --git a/tests/US_Autocomplete/ClientTest.php b/tests/US_Autocomplete/ClientTest.php deleted file mode 100644 index c93a0db..0000000 --- a/tests/US_Autocomplete/ClientTest.php +++ /dev/null @@ -1,115 +0,0 @@ -sendLookup(new Lookup('1')); - - $this->assertEquals("http://localhost/?prefix=1&geolocate=true&geolocate_precision=city", - $capturingSender->getRequest()->getUrl()); - } - - public function testSendingSingleFullyPopulatedLookup() { - $capturingSender = new RequestCapturingSender(); - $sender = new URLPrefixSender("http://localhost/", $capturingSender); - $serializer = new MockSerializer(new Result()); - $client = new Client($sender, $serializer); - $expectedURL = "http://localhost/?prefix=1&suggestions=2&city_filter=3&state_filter=4&prefer=5&prefer_ratio=0.6&geolocate=true&geolocate_precision=state"; - $lookup = new Lookup(); - $lookup->setPrefix('1'); - $lookup->setMaxSuggestions(2); - $lookup->addCityFilter("3"); - $lookup->addStateFilter("4"); - $lookup->addPrefer("5"); - $lookup->setPreferRatio("0.6"); - $lookup->setGeolocateType(new GeolocateType(GEOLOCATE_TYPE_STATE)); - - $client->sendLookup($lookup); - - $this->assertEquals($expectedURL, $capturingSender->getRequest()->getUrl()); - } - - public function testSendingLookupWithGeolocateTypeSetToNone() { - $capturingSender = new RequestCapturingSender(); - $sender = new URLPrefixSender("http://localhost/", $capturingSender); - $serializer = new MockSerializer(new Result()); - $expectedURL = "http://localhost/?prefix=1&geolocate=false"; - $client = new Client($sender, $serializer); - $lookup = new Lookup(); - $lookup->setPrefix('1'); - $lookup->setGeolocateType(new GeolocateType(GEOLOCATE_TYPE_NONE)); - - $client->sendLookup($lookup); - - $this->assertEquals($expectedURL, $capturingSender->getRequest()->getUrl()); - } - - //endregion - - //region [ Response Handling ] - - public function testDeserializeCalledWithResponseBody() { - $response = new Response(0, "Hello, World!", ""); - $mockSender = new MockSender($response); - $sender = new URLPrefixSender("http://localhost/", $mockSender); - $deserializer = new MockDeserializer(null); - $client = new Client($sender, $deserializer); - - $client->sendLookup(new Lookup('1')); - - $this->assertEquals($response->getPayload(), $deserializer->getPayload()); - } - - public function testResultCorrectlyAssignedToCorrespondingLookup() { - $rawSuggestions = array(array('text' => '1'), array('text' => '2')); - $rawResult = array('suggestions' => $rawSuggestions); - $expectedResult = new Result($rawResult); - - $lookup = new Lookup('1'); - - $mockSender = new MockSender(new Response(0, "{[]}", "")); - $sender = new URLPrefixSender("http://localhost/", $mockSender); - $deserializer = new MockDeserializer($rawResult); - $client = new Client($sender, $deserializer); - - $client->sendLookup($lookup); - - $this->assertEquals($expectedResult->getSuggestions()[0]->getText(), $lookup->getResult()[0]->getText()); - $this->assertEquals($expectedResult->getSuggestions()[1]->getText(), $lookup->getResult()[1]->getText()); - } - - //endregion -} diff --git a/tests/US_Autocomplete/LookupTest.php b/tests/US_Autocomplete/LookupTest.php deleted file mode 100644 index e989d4d..0000000 --- a/tests/US_Autocomplete/LookupTest.php +++ /dev/null @@ -1,20 +0,0 @@ -expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Max suggestions must be a positive integer no larger than 10.'); - - $lookup->setMaxSuggestions(11); - } -} \ No newline at end of file diff --git a/tests/US_Autocomplete/ResultTest.php b/tests/US_Autocomplete/ResultTest.php deleted file mode 100644 index 3b12796..0000000 --- a/tests/US_Autocomplete/ResultTest.php +++ /dev/null @@ -1,34 +0,0 @@ -obj = array( - 'suggestions' => array( array( - 'text' => '1', - 'street_line' => '2', - 'city' => '3', - 'state' => '4') - ) - ); - } - - public function testAllFieldsGetFilledInCorrectly() { - $result = new Result($this->obj); - - $suggestion = $result->getSuggestion(0); - - $this->assertEquals('1', $suggestion->getText()); - $this->assertEquals('2', $suggestion->getStreetLine()); - $this->assertEquals('3', $suggestion->getCity()); - $this->assertEquals('4', $suggestion->getState()); - } -}