View source
<?php
namespace Drupal\geocoder;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\geocoder\Annotation\GeocoderDumper;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Field\FieldConfigInterface;
use Drupal\Core\Locale\CountryManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
class DumperPluginManager extends GeocoderPluginManagerBase {
private $maxLengthFieldTypes = [
'text',
'string',
];
protected $countryManager;
protected $logger;
protected $messenger;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, CountryManagerInterface $country_manager, LoggerChannelFactoryInterface $logger_factory, MessengerInterface $messenger) {
parent::__construct('Plugin/Geocoder/Dumper', $namespaces, $module_handler, DumperInterface::class, GeocoderDumper::class);
$this
->alterInfo('geocoder_dumper_info');
$this
->setCacheBackend($cache_backend, 'geocoder_dumper_plugins');
$this->countryManager = $country_manager;
$this->logger = $logger_factory;
$this->messenger = $messenger;
}
public function setAddressFieldFromGeojson($geojson) : array {
$geojson_array = Json::decode($geojson);
$geojson_array['properties'] += [
'adminLevels' => '',
'streetName' => '',
'streetNumber' => '',
'postalCode' => '',
'locality' => '',
];
$administrative_area = '';
if (!empty($geojson_array['properties']['adminLevels'])) {
$administrative_area = array_shift($geojson_array['properties']['adminLevels'])['name'];
}
$address_line1 = $geojson_array['properties']['streetName'];
if (!empty($geojson_array['properties']['streetNumber'])) {
$address_line1 .= ' ' . $geojson_array['properties']['streetNumber'];
}
return [
'country_code' => $this
->setCountryFromGeojson($geojson),
'address_line1' => $address_line1,
'postal_code' => $geojson_array['properties']['postalCode'],
'locality' => $geojson_array['properties']['locality'],
'administrative_area' => $administrative_area,
];
}
public function setCountryFromGeojson($geojson) : string {
$geojson_array = Json::decode($geojson);
$country_code = isset($geojson_array['properties']['countryCode']) ? strtoupper(substr($geojson_array['properties']['countryCode'], 0, 2)) : '';
if (!array_key_exists($country_code, $this->countryManager
->getList()) && isset($geojson_array['properties']['country'])) {
$country_code = strtoupper(substr($geojson_array['properties']['country'], 0, 2));
}
$this->moduleHandler
->alter('geocode_country_code', $country_code, $geojson_array);
return $country_code;
}
public function fixDumperFieldIncompatibility(&$dumper_result, $dumper, FieldConfigInterface $field_config) : void {
if (\is_string($dumper_result)) {
if (!preg_match('//u', $dumper_result)) {
$dumper_result = utf8_encode($dumper_result);
}
}
if (\in_array($field_config
->getType(), $this->maxLengthFieldTypes, TRUE) && \strlen($dumper_result) > $field_config
->getFieldStorageDefinition()
->getSetting('max_length')) {
$incompatibility_warning_message = t("The '@field_name' field 'max length' property is not compatible with the chosen '@dumper' dumper.<br>Thus <b>be aware</b> <u>the dumper output result has been truncated to @max_length chars (max length)</u>.<br> You are advised to change the '@field_name' field definition or chose another compatible dumper.", [
'@field_name' => $field_config
->getLabel(),
'@dumper' => $dumper
->getPluginId(),
'@max_length' => $field_config
->getFieldStorageDefinition()
->getSetting('max_length'),
]);
$dumper_result = substr($dumper_result, 0, $field_config
->getFieldStorageDefinition()
->getSetting('max_length'));
$this->messenger
->addWarning($incompatibility_warning_message);
$this->logger
->get('geocoder')
->warning($incompatibility_warning_message);
}
}
}