for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace vipnytt\RobotsTxtParser\Parser;
/**
* Class CharacterEncodingConvert
*
* @package vipnytt\RobotsTxtParser\Parser
*/
class CharacterEncodingConvert implements RobotsTxtInterface
{
* String to convert
* @var string
protected $string;
* Source encoding
protected $source;
* Destination encoding
protected $destination;
* CharacterEncodingConvert constructor.
* @param string $string
* @param string $source
* @param string $destination
public function __construct($string, $source, $destination = self::ENCODING)
$this->string = $string;
$this->source = $source;
$this->destination = $destination;
}
* Auto mode
* @return string|false
public function auto()
if ($this->source == $this->destination) {
return $this->string;
} elseif (($iconv = $this->iconv()) !== false) {
return $iconv;
} elseif (($mbstring = $this->mbstring()) !== false) {
return $mbstring;
$this->fallback();
return false;
* iconv
* @param string $outSuffix
public function iconv($outSuffix = '//TRANSLIT//IGNORE')
try {
$converted = iconv($this->source, $this->destination . $outSuffix, $this->string);
} catch (\Exception $msg) {
return $converted;
* mb_convert_encoding
* @param array|string|null $fromOverride
public function mbstring($fromOverride = null)
$from = $this->source;
if ($fromOverride !== null) {
$from = $fromOverride;
$converted = mb_convert_encoding($this->string, $this->destination, $from);
* mb_internal_encoding
* @return bool
protected function fallback()
mb_internal_encoding($this->destination);
mb_internal_encoding($this->source);
return true;