find() public method

Example: $data->find('parentkey.subkey.subsubkey');
public find ( string $key, mixed $default = null, mixed $filter = null, string $separator = '.' ) : mixed
$key string The key to search for. Can be composed using $separator as the key/subkey separator
$default mixed The default value
$filter mixed Filter returned value
$separator string The separator to use when searching for subkeys. Default is '.'
return mixed
Ejemplo n.º 1
0
 public function testFilter()
 {
     $value = '  123.456 string <i>qwerty</i>  ';
     $data = new Data(array('key' => $value, 'array' => array('inner' => $value)));
     // Default value
     isSame(null, $data->get('undefined'));
     isSame(42.123, $data->get('undefined', 42.123));
     isSame(42, $data->get('undefined', 42.123, 'int'));
     isSame(42.123, $data->get('undefined', '42.123', 'float'));
     // Get & find
     isSame($value, $data->get('key'));
     isSame($value, $data->find('key'));
     isSame($value, $data->find('array.inner'));
     // One filter
     isSame(123, $data->get('key', null, 'int'));
     isSame(123, $data->find('key', null, 'int'));
     isSame(123, $data->find('array.inner', null, 'int'));
     // Several filters
     isSame('stringqwerty', $data->get('key', null, 'strip, trim, alpha'));
     isSame('stringqwerty', $data->find('key', null, 'strip, trim, alpha'));
     isSame('stringqwerty', $data->find('array.inner', null, 'strip, trim, alpha'));
     // Several filters
     isSame('123.456 string qwerty', $data->get('key', null, function ($value) {
         return trim(strip_tags($value));
     }));
     isSame('123.456 string qwerty', $data->find('key', null, function ($value) {
         return trim(strip_tags($value));
     }));
     isSame('123.456 string qwerty', $data->find('array.inner', null, function ($value) {
         return trim(strip_tags($value));
     }));
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 protected function _compactResponse($apiResponse)
 {
     $dataResponse = new Data($apiResponse);
     $response = array('code' => $dataResponse->find('response.code', 0, 'int'), 'headers' => array_change_key_case((array) $dataResponse->get('headers', array()), CASE_LOWER), 'body' => $dataResponse->get('body'));
     $response = new Data($response);
     return $response;
 }
Ejemplo n.º 3
0
 /**
  * @return bool
  * @throws \Exception
  */
 protected function _userAuth()
 {
     $isAuth = \JFactory::getApplication()->login(array('username' => $this->_globConfig->find('auth.login'), 'password' => $this->_globConfig->find('auth.pass'), 'secretkey' => $this->_globConfig->find('auth.secretkey')));
     if (!$isAuth) {
         throw new \Exception('Can\'t login as admin!');
     }
     return true;
 }
Ejemplo n.º 4
0
 public function testNumeric()
 {
     $data = new Data(array(0 => 0, 1 => 1, 'string' => 'test', 2 => array(1), 'nested' => array('0', 1)));
     isSame(0, $data->get(0));
     isSame(1, $data->find('2.0'));
     isSame('0', $data->find('nested.0'));
     isSame(0, $data['0']);
     isSame(1, $data[2][0]);
 }
Ejemplo n.º 5
0
 public function testForReadme()
 {
     $times = 10000;
     $this->_data = array('prop' => uniqid('', true), 'prop1' => uniqid('', true), 'prop2' => uniqid('', true), 'prop3' => uniqid('', true), 'prop4' => uniqid('', true), 'inner' => array('prop' => uniqid('', true), 'prop1' => uniqid('', true), 'prop2' => uniqid('', true), 'prop3' => uniqid('', true), 'prop4' => uniqid('', true), 'inner' => array('prop' => uniqid('', true), 'prop1' => uniqid('', true), 'prop2' => uniqid('', true), 'prop3' => uniqid('', true), 'prop4' => uniqid('', true))));
     $array = $this->_data;
     $data = new Data($this->_data);
     $arrobj = new \ArrayObject($this->_data);
     Benchmark::compare(array('Array' => function () use($array) {
         $var = $array;
         // for clean experiment
         return $var;
     }, 'ArrayObject' => function () use($array) {
         $var = new \ArrayObject($array);
         return $var;
     }, 'Data' => function () use($array) {
         $var = new Data($array);
         return $var;
     }), array('name' => 'For Readme: Create', 'count' => $times));
     Benchmark::compare(array('Array' => function () use($array) {
         return array_key_exists('prop', $array) ? $array['prop'] : null;
     }, 'ArrayObject' => function () use($arrobj) {
         return $arrobj->offsetGet('prop');
     }, 'Data' => function () use($data) {
         return $data->get('prop');
     }), array('name' => 'For Readme: Get by key', 'count' => $times));
     Benchmark::compare(array('Array' => function () use($array) {
         if (array_key_exists('inner', $array) && array_key_exists('inner', $array['inner']) && array_key_exists('prop', $array['inner']['inner'])) {
             return $array['inner']['inner']['prop'];
         }
         return 42;
     }, 'ArrayObject' => function () use($arrobj) {
         if (array_key_exists('inner', $arrobj) && array_key_exists('inner', $arrobj['inner']) && array_key_exists('prop', $arrobj['inner']['inner'])) {
             return $arrobj['inner']['inner']['prop'];
         }
         return 42;
     }, 'Data' => function () use($data) {
         return $data->find('inner.inner.prop', 42);
     }), array('name' => 'For Readme: Find nested defined var', 'count' => $times));
     Benchmark::compare(array('Array' => function () use($array) {
         if (array_key_exists('inner', $array) && array_key_exists('inner', $array['inner']) && array_key_exists('undefined', $array['inner']['inner'])) {
             return $array['inner']['inner']['prop'];
         }
         return 42;
     }, 'ArrayObject' => function () use($arrobj) {
         if (array_key_exists('inner', $arrobj) && array_key_exists('inner', $arrobj['inner']) && array_key_exists('undefined', $arrobj['inner']['inner'])) {
             return $arrobj['inner']['inner']['undefined'];
         }
         return 42;
     }, 'Data' => function () use($data) {
         return $data->find('inner.inner.undefined', 42);
     }), array('name' => 'For Readme: Find nested undefined var', 'count' => $times));
 }