View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFPROP:Proposal Zone Template}
{composition-setup}

{zone-data:component-name}
Zend_Filter_FindCaseKeys
{zone-data}

{zone-data:proposer-list}
[~xerkus]
{zone-data}

{zone-data:liaison}
TBD
{zone-data}

{zone-data:revision}
1.0 - 4 April 2011: Initial Draft.
{zone-data}

{zone-data:overview}
Zend_Filter_FindCaseKeys may be used to get all case variations of specified key. This is especially useful when using function array_change_key_case() is not an option.
{zone-data}

{zone-data:references}
{zone-data}

{zone-data:requirements}
I expect discussion if this component should accept array of keys to find

* This component *will* accept string to match key names against.
* This component *will* filter arrays and objects with toArray() method.
* This component *will* return array of matched keys.
{zone-data}

{zone-data:dependencies}
* Zend_Exception
* Zend_Filter_Exception
{zone-data}

{zone-data:operation}
{deck:id=operation}
{card:label=example}
{code}
$array = array(
'testKey' => 1,
'Testkey' => 2,
'tesTKey1' => 3,
'tesTKey' => 4,
'test' => 5,
'0' => 6,
);

$filter = new Zend_Filter_FindCaseKeys();

$keys = $filter->setKeyToFind('testkey')->filter($array);
// array('testKey', 'Testkey', 'tesTKey');
{code}
{card}
{card:label=Working code from my lib}
{code}
/**
* Return keys matched to string in case insensitive manner
*
* @author Xerkus
* @category Xrks_lib
* @package Xrks_Filter
* @copyright Copyright (c) 2011 Алексей 'Xerkus' Худяков (http://xerkus.ru)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @version $Id: FindCaseKeys.php 148 2011-04-03 18:45:17Z Xerkus $
*/
class Xrks_Filter_FindCaseKeys implements Zend_Filter_Interface
{
protected $_keyToFind;

/**
* Compares key to predefined string
*
* @param string $value key to compare
* @return boolean
*/
protected function _callbackCompareInsensitive($value)
{
return strcasecmp($value, $this->_keyToFind) == 0;
}

/**
* Constructor
*
* @param string|array $keyToFind OPTIONAL If array first element will be taken
*/
public function __construct($keyToFind = NULL)
{
if(!empty($keyToFind)) {
if(is_array($keyToFind) && array_key_exists('keyToFind', $keyToFind)) {
$keyToFind = $keyToFind['keyToFind'];
}
if(is_string($keyToFind)) {
$this->setKeyToFind($keyToFind);
}
}
}

/**
* String to compare array keys against
*
* @param string $key
* @return Xrks_Filter_FindCaseKeys Provides a fluent interface
*/
public function setKeyToFind($key)
{
$this->_keyToFind = $key;
return $this;
}


/**
* Defined by Zend_Filter_Interface
* Returns the array of keys matched to keyToFind in case insensitive manner
*
* @param array|object $value
* @return array
* @throws Zend_Filter_Exception
* @see Zend_Filter_Interface::filter()
*/
public function filter($value)
{
if(empty($this->_keyToFind) || !is_string($this->_keyToFind)) {
throw new Zend_Filter_Exception('KeyToFind is not set or is not a string');
}
if(is_object($value)) {
if(method_exists($value, 'toArray')) {
$value = $value->toArray();
} else {
$value = get_object_vars($value);
}
} elseif(!is_array($value)) {
throw new Zend_Filter_Exception('Filtered variable should be either array or object');
}

$keys = array_keys($value);
return array_filter($keys, array($this, '_callbackCompareInsensitive'));
}
}
{code}
{card}
{deck}
{zone-data}

{zone-data:milestones}
* Milestone 1: Get community input
* Milestone 2: Working prototype for ZF1
* Milestone 3: Unit tests exist, work, and are checked into SVN.
* Milestone 4: Initial documentation exists.
* Milestone 5: Updated code and unit tests for ZF2.

{zone-data}

{zone-data:class-list}
* Zend_Filter_FindCaseKeys
{zone-data}

{zone-data:use-cases}
||UC-01: in application resource||
{code}
/**
* Removes resource "modulesconfig" from loaded module config to prevent recursion
*
* @param array $config by reference
*/
protected function _removeResourceModulesconfig(array &$config)
{
//$this->_filter instanceof Zend_Filter_FindCaseKeys;
$keys = $this->_filter->setKeyToFind('modulesconfig')->filter($config);
foreach($keys as $key) {
unset($config['resources'][$key]);
}
}
{code}


{zone-data}

{zone-data:skeletons}
{code}
class Zend_Filter_FindCaseKeys implements Zend_Filter_Interface
{
/**
* Set string to compare array keys against
*
* @param string $key
* @return Zend_Filter_FindCaseKeys Provides a fluent interface
*/
public function setKeyToFind($key);

/**
* Defined by Zend_Filter_Interface
* Returns the array of keys matched to keyToFind in case insensitive manner
*
* @param array|object $value
* @return array
* @throws Zend_Filter_Exception
* @see Zend_Filter_Interface::filter()
*/
public function filter($value);
}
{code}
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>