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

ExtractZipAction::execute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
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
9
class ExtractZipAction extends AbstractActionStatic
10
{
11
    /**
12
     * Extract a (city, state) string from a google places API return that may contain a zip code
13
     *
14
     * @param string $description
15
     * @return array
16
     */
17
    public static function execute(string $description)
18
    {
19
        // Check that the place description contains a comma string
20
        if (strpos($description, ', ') !== false) {
21
            // Separate city and state strings
22
            list($city, $state) = explode(', ', $description);
23
24
            // Check if the state string contains more than a state code
25
            if (strlen(trim($state)) > 2 && strpos($state, ' ') !== false) {
26
                // Extract the zip code
27
                list($state, $zip) = explode(' ', $state);
28
                return $zip;
29
            }
30
31
        }
32
        return null;
33
    }
34
}
35