mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-12 12:33:26 +00:00
Minor tweaks
This commit is contained in:
@@ -16,7 +16,6 @@
|
|||||||
*/
|
*/
|
||||||
class unicode
|
class unicode
|
||||||
{
|
{
|
||||||
// second argument to convert_to(...)
|
|
||||||
const CONVERT_TO_LC = 'lc'; // lowercase
|
const CONVERT_TO_LC = 'lc'; // lowercase
|
||||||
const CONVERT_TO_ND = 'nd'; // no-diacritics
|
const CONVERT_TO_ND = 'nd'; // no-diacritics
|
||||||
const CONVERT_TO_LCND = 'lcnd'; // lowercase no-diacritics
|
const CONVERT_TO_LCND = 'lcnd'; // lowercase no-diacritics
|
||||||
@@ -1561,36 +1560,40 @@ class unicode
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* convert a utf8 string using method:
|
* Converts a string
|
||||||
* self::CONVERT_TO_LC : lowercase
|
*
|
||||||
* self::CONVERT_TO_ND : no-diacritics
|
* @param string $string
|
||||||
* self::CONVERT_TO_LCND : lowercase no-diacritics
|
* @param string $target One of the unicode::CONVERT_TO_* constants
|
||||||
*
|
*
|
||||||
* @param string $s
|
|
||||||
* @param string $method
|
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception_InvalidArgument
|
|
||||||
*
|
*
|
||||||
|
* @throws Exception_InvalidArgument
|
||||||
*/
|
*/
|
||||||
public function convert_to($s, $method)
|
public function convert($string, $target)
|
||||||
{
|
{
|
||||||
$ok_methods = array_keys(self::$map);
|
$ok_methods = array_keys(self::$map);
|
||||||
if (!in_array($method, $ok_methods)) {
|
|
||||||
|
if (!in_array($target, $ok_methods)) {
|
||||||
throw new Exception_InvalidArgument(
|
throw new Exception_InvalidArgument(
|
||||||
sprintf('Invalid argument 2 "%s", valid values are [%s].'
|
sprintf('Invalid argument 2 "%s", valid values are [%s].'
|
||||||
, $method
|
, $target
|
||||||
, implode('|', $ok_methods)
|
, implode('|', $ok_methods)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (function_exists('phrasea_utf8_convert_to')) {
|
if (function_exists('phrasea_utf8_convert_to')) {
|
||||||
// function exists in phrasea extension
|
return phrasea_utf8_convert_to($string, $target);
|
||||||
return phrasea_utf8_convert_to($s, $method);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$out = '';
|
$out = '';
|
||||||
$_map = &self::$map[$method]; // faster in loop
|
$_map = &self::$map[$target]; // faster in loop
|
||||||
for ($i = 0; $i < mb_strlen($s, 'UTF-8'); $i++) {
|
|
||||||
$out .= array_key_exists(($c = mb_substr($s, $i, 1, 'UTF-8')), $_map) ? $_map[$c] : $c;
|
for ($i = 0; $i < mb_strlen($string, 'UTF-8'); $i++) {
|
||||||
|
if (true === array_key_exists(($c = mb_substr($string, $i, 1, 'UTF-8')), $_map)) {
|
||||||
|
$out .= $_map[$c];
|
||||||
|
} else {
|
||||||
|
$out .= $c;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $out;
|
return $out;
|
||||||
@@ -1600,7 +1603,7 @@ class unicode
|
|||||||
{
|
{
|
||||||
$so = "";
|
$so = "";
|
||||||
|
|
||||||
$string = $this->convert_to($string, 'lcnd');
|
$string = $this->convert($string, static::CONVERT_TO_LCND);
|
||||||
|
|
||||||
$l = mb_strlen($string, "UTF-8");
|
$l = mb_strlen($string, "UTF-8");
|
||||||
$lastwasblank = false;
|
$lastwasblank = false;
|
||||||
@@ -1621,7 +1624,7 @@ class unicode
|
|||||||
|
|
||||||
public function remove_diacritics($string)
|
public function remove_diacritics($string)
|
||||||
{
|
{
|
||||||
return $this->convert_to($string, 'nd');
|
return $this->convert($string, static::CONVERT_TO_ND);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function remove_nonazAZ09($string, $keep_underscores = true, $keep_minus = true, $keep_dot = false)
|
public function remove_nonazAZ09($string, $keep_underscores = true, $keep_minus = true, $keep_dot = false)
|
||||||
|
@@ -14,22 +14,22 @@ class unicodeTest extends PhraseanetPHPUnitAbstract
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \unicode::convert_to
|
* @covers \unicode::convert
|
||||||
*/
|
*/
|
||||||
public function testConvert_to()
|
public function testConvert()
|
||||||
{
|
{
|
||||||
$this->assertEquals('éléphant à rôtir', $this->object->convert_to('ÉLÉPHANT à rôtir', unicode::CONVERT_TO_LC));
|
$this->assertEquals('éléphant à rôtir', $this->object->convert('ÉLÉPHANT à rôtir', unicode::CONVERT_TO_LC));
|
||||||
$this->assertEquals('ELEPHANT a rotir', $this->object->convert_to('ÉLÉPHANT à rôtir', unicode::CONVERT_TO_ND));
|
$this->assertEquals('ELEPHANT a rotir', $this->object->convert('ÉLÉPHANT à rôtir', unicode::CONVERT_TO_ND));
|
||||||
$this->assertEquals('elephant a rotir', $this->object->convert_to('ÉLÉPHANT à rôtir', unicode::CONVERT_TO_LCND));
|
$this->assertEquals('elephant a rotir', $this->object->convert('ÉLÉPHANT à rôtir', unicode::CONVERT_TO_LCND));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \unicode::convert_to
|
* @covers \unicode::convert
|
||||||
* @expectedException Exception_InvalidArgument
|
* @expectedException Exception_InvalidArgument
|
||||||
*/
|
*/
|
||||||
public function testConvert_to_ex()
|
public function testConvertError()
|
||||||
{
|
{
|
||||||
$this->object->Convert_to('ÉLÉPHANT à rôtir', 'unexistant contant');
|
$this->object->convert('ÉLÉPHANT à rôtir', 'unexistant contant');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,28 +38,21 @@ class unicodeTest extends PhraseanetPHPUnitAbstract
|
|||||||
public function testRemove_diacritics()
|
public function testRemove_diacritics()
|
||||||
{
|
{
|
||||||
$this->assertEquals('Elephant', $this->object->remove_diacritics('Éléphant'));
|
$this->assertEquals('Elephant', $this->object->remove_diacritics('Éléphant'));
|
||||||
$this->assertEquals('&e"\'(-eE_ca)=$*u:;,?./§%μ£°0987654321Œ3~#{[|^`@]}e32÷׿',
|
$this->assertEquals('&e"\'(-eE_ca)=$*u:;,?./§%μ£°0987654321Œ3~#{[|^`@]}e32÷׿',$this->object->remove_diacritics('&é"\'(-èÉ_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿'));
|
||||||
$this->object->remove_diacritics('&é"\'(-èÉ_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿'));
|
|
||||||
$this->assertEquals('PeTARDS', $this->object->remove_diacritics('PéTARDS'));
|
$this->assertEquals('PeTARDS', $this->object->remove_diacritics('PéTARDS'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @covers \unicode::remove_nonazAZ09
|
* @covers \unicode::remove_nonazAZ09
|
||||||
*/
|
*/
|
||||||
public function testRemove_nonazAZ09()
|
public function testRemove_nonazAZ09()
|
||||||
{
|
{
|
||||||
$this->assertEquals('Elephant', $this->object->remove_nonazAZ09('Eléphant'));
|
$this->assertEquals('Elephant', $this->object->remove_nonazAZ09('Eléphant'));
|
||||||
$this->assertEquals('Ee-e_cau.09876543213e32',
|
$this->assertEquals('Ee-e_cau.09876543213e32', $this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', true, true, true));
|
||||||
$this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', true, true, true));
|
$this->assertEquals('Ee-e_cau09876543213e32', $this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', true, true, false));
|
||||||
$this->assertEquals('Ee-e_cau09876543213e32',
|
$this->assertEquals('Eee_cau.09876543213e32', $this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', true, false, true));
|
||||||
$this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', true, true, false));
|
$this->assertEquals('Ee-ecau.09876543213e32', $this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', false, true, true));
|
||||||
$this->assertEquals('Eee_cau.09876543213e32',
|
$this->assertEquals('Eeecau09876543213e32', $this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', false, false, false));
|
||||||
$this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', true, false, true));
|
|
||||||
$this->assertEquals('Ee-ecau.09876543213e32',
|
|
||||||
$this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', false, true, true));
|
|
||||||
$this->assertEquals('Eeecau09876543213e32',
|
|
||||||
$this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', false, false, false));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user