View source
<?php
namespace Drupal\feeds_ex\Encoder;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Form\FormStateInterface;
class TextEncoder implements EncoderInterface {
protected $isMultibyte = FALSE;
protected static $utf8Compatible = [
'utf-8',
'utf8',
'us-ascii',
'ascii',
];
protected $encodingList;
public function __construct(array $encoding_list) {
$this->encodingList = $encoding_list;
$this->isMultibyte = Unicode::getStatus() == Unicode::STATUS_MULTIBYTE;
}
public function convertEncoding($data) {
if (!($detected = $this
->detectEncoding($data))) {
return $data;
}
return $this
->doConvert($data, $detected);
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
if (!$this->isMultibyte) {
return $form;
}
$args = [
'%encodings' => implode(', ', mb_detect_order()),
];
$form['source_encoding'] = [
'#type' => 'textfield',
'#title' => t('Source encoding'),
'#description' => t('The possible encodings of the source files. auto: %encodings', $args),
'#default_value' => implode(', ', $this->encodingList),
'#autocomplete_path' => '_feeds_ex/encoding_autocomplete',
'#maxlength' => 1024,
];
return $form;
}
public function configFormValidate(array &$values) {
if (!$this->isMultibyte) {
return;
}
$encodings = array_map('strtolower', array_map('trim', explode(',', $values['source_encoding'])));
$values['source_encoding'] = [];
foreach (mb_list_encodings() as $encoding) {
$pos = array_search(strtolower($encoding), $encodings);
if ($pos !== FALSE) {
$values['source_encoding'][$pos] = $encoding;
}
}
ksort($values['source_encoding']);
if (!$values['source_encoding']) {
$values['source_encoding'][] = 'auto';
}
}
protected function detectEncoding($data) {
if (!$this->isMultibyte) {
return FALSE;
}
if ($detected = mb_detect_encoding($data, $this->encodingList, TRUE)) {
return $detected;
}
return mb_detect_encoding($data, $this->encodingList);
}
protected function doConvert($data, $source_encoding) {
if (in_array(strtolower($source_encoding), self::$utf8Compatible)) {
return $data;
}
$converted = Unicode::convertToUtf8($data, $source_encoding);
if ($converted === FALSE) {
return $data;
}
return $converted;
}
}