You are here

class FeedsExXmlEncoder in Feeds extensible parsers 7

Converts the encoding of an XML document to UTF-8.

Hierarchy

Expanded class hierarchy of FeedsExXmlEncoder

File

src/Xml/Utility.php, line 141
Contains FeedsExXmlUtility.

View source
class FeedsExXmlEncoder extends FeedsExTextEncoder {

  /**
   * The regex used to find the encoding.
   *
   * @var string
   */
  protected $findRegex = '/^<\\?xml[^>]+encoding\\s*=\\s*("|\')([\\w-]+)(\\1)/';

  /**
   * The regex used to replace the encoding.
   *
   * @var string
   */
  protected $replaceRegex = '/^(<\\?xml[^>]+encoding\\s*=\\s*("|\'))([\\w-]+)(\\2)/';

  /**
   * The replacement pattern.
   *
   * @var string
   */
  protected $replacePattern = '$1UTF-8$4';

  /**
   * {@inheritdoc}
   */
  public function convertEncoding($data) {

    // Check for an encoding declaration in the XML prolog.
    $matches = FALSE;
    $encoding = 'ascii';
    if (preg_match($this->findRegex, $data, $matches)) {
      $encoding = $matches[2];
    }
    elseif ($detected = $this
      ->detectEncoding($data)) {
      $encoding = $detected;
    }

    // Unsupported encodings are converted here into UTF-8.
    if (in_array(strtolower($encoding), self::$utf8Compatible)) {
      return $data;
    }
    $data = $this
      ->doConvert($data, $encoding);
    if ($matches) {
      $data = preg_replace($this->replaceRegex, $this->replacePattern, $data);
    }
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsExTextEncoder::$encodingList protected property The list of encodings to search for.
FeedsExTextEncoder::$isMultibyte protected property Whether the current system handles mb_* functions.
FeedsExTextEncoder::$utf8Compatible protected static property The set of encodings compatible with UTF-8.
FeedsExTextEncoder::configForm public function Returns the configuration form to select encodings. Overrides FeedsExEncoderInterface::configForm
FeedsExTextEncoder::configFormValidate public function Validates the encoding configuration form. Overrides FeedsExEncoderInterface::configFormValidate
FeedsExTextEncoder::detectEncoding protected function Detects the encoding of a string.
FeedsExTextEncoder::doConvert protected function Performs the actual encoding conversion.
FeedsExTextEncoder::__construct public function Constructs a FeedsExEncoderInterface object. Overrides FeedsExEncoderInterface::__construct
FeedsExXmlEncoder::$findRegex protected property The regex used to find the encoding. 1
FeedsExXmlEncoder::$replacePattern protected property The replacement pattern. 1
FeedsExXmlEncoder::$replaceRegex protected property The regex used to replace the encoding. 1
FeedsExXmlEncoder::convertEncoding public function Converts a string to UTF-8. Overrides FeedsExTextEncoder::convertEncoding