Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
metadataprovider.php
1<?php
2
4
7
9{
10 protected $metadata;
12
13 protected static $instance;
14
15 const PARSED_METADATA_FILENAME = 'metadata.php';
16
17 protected function __construct()
18 {
19 $this->loadMetadata();
20 }
21
26 public static function getInstance()
27 {
28 if(is_null(static::$instance))
29 {
30 static::$instance = new static();
31 }
32
33 return static::$instance;
34 }
35
41 public function getCountriesByCode($countryCode)
42 {
43 return is_array($this->codeToCountries[$countryCode]) ? $this->codeToCountries[$countryCode] : array();
44 }
45
46 public function isValidCountryCode($countryCode)
47 {
48 return isset($this->codeToCountries[$countryCode]);
49 }
50
56 public function getCountryMetadata($country)
57 {
58 $country = mb_strtoupper($country);
59 return $this->metadata[$country] ?? false;
60 }
61
62 public function toArray()
63 {
64 return array(
65 'codeToCountries' => $this->codeToCountries,
66 'metadata' => $this->metadata
67 );
68 }
69
76 public static function parseGoogleMetadata($fileName)
77 {
78 $metadataBuilder = new \Bitrix\Main\PhoneNumber\Tools\MetadataBuilder($fileName);
79
80 $metadata = $metadataBuilder->build();
81 $codeToCountries = array();
82
83 foreach ($metadata as $metadataRecord)
84 {
85 $country = mb_strtoupper($metadataRecord['id']);
86 if(!is_array($codeToCountries[$metadataRecord['countryCode']]))
87 {
88 $codeToCountries[$metadataRecord['countryCode']] = array();
89 }
90
91 if($metadataRecord['mainCountryForCode'])
92 array_unshift($codeToCountries[$metadataRecord['countryCode']], $country);
93 else
94 $codeToCountries[$metadataRecord['countryCode']][] = $country;
95 }
96
97 return array(
98 'codeToCountries' => $codeToCountries,
99 'metadata' => $metadata
100 );
101 }
102
108 protected function loadMetadata()
109 {
110 if(File::isFileExists(static::PARSED_METADATA_FILENAME))
111 throw new SystemException("Metadata file is not found");
112
113 $parsedMetadata = include(static::PARSED_METADATA_FILENAME);
114
115 $this->codeToCountries = $parsedMetadata['codeToCountries'];
116 foreach ($parsedMetadata['metadata'] as $metadataRecord)
117 {
118 $this->metadata[$metadataRecord['id']] = $metadataRecord;
119 }
120 }
121}