Passed
Push — master ( 25ef95...88bd2b )
by Stephen
02:06
created

ExtractCityStateAction::execute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Sfneal\GooglePlaces;
5
6
7
use Sfneal\Actions\AbstractActionStatic;
8
use Sfneal\GooglePlaces\Actions\Traits\States;
9
10
class ExtractCityStateAction extends AbstractActionStatic
11
{
12
    use States;
13
14
    /**
15
     * Extract a (city, state) string from a google places API return that may contain a zip code
16
     *
17
     * @param string $description
18
     * @return array
19
     */
20
    public static function execute(string $description)
21
    {
22
        // Check that the place description contains a comma string
23
        if (strpos($description, ', ') !== false) {
24
            // Separate city and state strings
25
            list($city, $state) = explode(', ', $description);
26
27
            // Check if the state string contains more than a state code
28
            if (strlen(trim($state)) > 2 && strpos($state, ' ') !== false) {
29
                // Extract the zip code
30
                list($state, $zip) = explode(' ', $state);
31
            }
32
            return [$city, $state];
33
34
        } else {
35
            return ['', self::stateAbbreviation($description)];
36
        }
37
    }
38
}
39