forked from geocoder-php/php-common
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStringFormatter.php
More file actions
75 lines (58 loc) · 2.04 KB
/
Copy pathStringFormatter.php
File metadata and controls
75 lines (58 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Formatter;
use Geocoder\Model\Address;
use Geocoder\Model\AdminLevel;
use Geocoder\Model\AdminLevelCollection;
/**
* @author William Durand <[email protected]>
*/
class StringFormatter
{
const STREET_NUMBER = '%n';
const STREET_NAME = '%S';
const LOCALITY = '%L';
const POSTAL_CODE = '%z';
const SUB_LOCALITY = '%D';
const ADMIN_LEVEL = '%A';
const ADMIN_LEVEL_CODE = '%a';
const COUNTRY = '%C';
const COUNTRY_CODE = '%c';
const TIMEZONE = '%T';
/**
* Transform an `Address` instance into a string representation.
*
* @param Address $address
* @param string $format
*
* @return string
*/
public function format(Address $address, $format)
{
$replace = [
self::STREET_NUMBER => $address->getStreetNumber(),
self::STREET_NAME => $address->getStreetName(),
self::LOCALITY => $address->getLocality(),
self::POSTAL_CODE => $address->getPostalCode(),
self::SUB_LOCALITY => $address->getSubLocality(),
self::COUNTRY => $address->getCountry()->getName(),
self::COUNTRY_CODE => $address->getCountry()->getCode(),
self::TIMEZONE => $address->getTimezone(),
];
for ($level = 1; $level <= AdminLevelCollection::MAX_LEVEL_DEPTH; $level ++) {
$replace[self::ADMIN_LEVEL . $level] = null;
$replace[self::ADMIN_LEVEL_CODE . $level] = null;
}
foreach ($address->getAdminLevels() as $level => $adminLevel) {
$replace[self::ADMIN_LEVEL . $level] = $adminLevel->getName();
$replace[self::ADMIN_LEVEL_CODE . $level] = $adminLevel->getCode();
}
return strtr($format, $replace);
}
}