Skip to content

Instantly share code, notes, and snippets.

@rdehouss
Created August 7, 2013 11:58
Show Gist options
  • Save rdehouss/6173451 to your computer and use it in GitHub Desktop.
Save rdehouss/6173451 to your computer and use it in GitHub Desktop.
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Filter
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
/**
* @see Zend_Filter_PregReplace
*/
require_once 'Zend/Filter/Word/Separator/Abstract.php';
/**
* @category Zend
* @package Zend_Filter
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Filter_Word_SeparatorToCamelCase extends Zend_Filter_Word_Separator_Abstract
{
public function filter($value)
{
// a unicode safe way of converting characters to \x00\x00 notation
$pregQuotedSeparator = preg_quote($this->_separator, '#');
if (self::isUnicodeSupportEnabled()) {
$patterns = array(
'#(' . $pregQuotedSeparator.')(\p{L}{1})#u',
'#(^\p{Ll}{1})#u',
);
if ( ! extension_loaded('mbstring') ) {
$replacements = array(
function ($matches) {
return strtoupper($matches[2]);
},
function ($matches) {
return strtoupper($matches[1]);
},
);
} else {
$replacements = array(
function ($matches) {
return mb_strtoupper($matches[2], 'UTF-8');
},
function ($matches) {
return mb_strtoupper($matches[1], 'UTF-8');
},
);
}
} else {
$patterns = array(
'#(' . $pregQuotedSeparator.')([A-Za-z]{1})#',
'#(^[A-Za-z]{1})#',
);
$replacements = array(
function ($matches) {
return strtoupper($matches[2]);
},
function ($matches) {
return strtoupper($matches[1]);
},
);
}
$filtered = $value;
foreach ($patterns as $index => $pattern) {
$filtered = preg_replace_callback($pattern, $replacements[$index], $filtered);
}
return $filtered;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment