/**
  * Test function epArrayGet() in epUtils.php
  */
 function test_epArrayGet()
 {
     // make sure method exists
     $this->assertTrue(function_exists('epArrayGet'));
     // array to be tested
     $array = array('a' => array('b' => array('c' => array('d' => 'x'))));
     // tests '.'
     $this->assertTrue($rt = epArrayGet($array, '.'));
     $this->assertTrue($rt == $array);
     // tests '/'
     $this->assertTrue($rt = epArrayGet($array, '/'));
     $this->assertTrue($rt == $array);
     // tests 'a'
     $this->assertTrue($a = epArrayGet($array, 'a'));
     $this->assertTrue($a == $array['a']);
     // tests 'a.b'
     $this->assertTrue($a_b = epArrayGet($array, 'a.b'));
     $this->assertTrue($a_b == $array['a']['b']);
     // tests 'a/b'
     $this->assertTrue($a_b = epArrayGet($array, 'a.b'));
     $this->assertTrue($a_b == $array['a']['b']);
     // tests 'a.b.c'
     $this->assertTrue($a_b_c = epArrayGet($array, 'a.b.c'));
     $this->assertTrue($a_b_c == $array['a']['b']['c']);
     // tests 'a/b/c'
     $this->assertTrue($a_b_c = epArrayGet($array, 'a/b/c'));
     $this->assertTrue($a_b_c == $array['a']['b']['c']);
     // tests 'a.b.c.d'
     $this->assertTrue($a_b_c_d = epArrayGet($array, 'a.b.c.d'));
     $this->assertTrue($a_b_c_d == $array['a']['b']['c']['d']);
     // tests 'a/b/c/d'
     $this->assertTrue($a_b_c_d = epArrayGet($array, 'a/b/c/d'));
     $this->assertTrue($a_b_c_d == $array['a']['b']['c']['d']);
 }
Example #2
0
 /**
  * Returns the value of a configuration option, if found.
  * 
  * If the option's value is an array, it returns an array by default. 
  * It can also return an epConfig wrapper if $ret_wrapper is set to true. 
  * 
  * The name of an option can be given in a namespace ('a.b.c.d') or xpath 
  * ('a/b/c/d') format so the caller can reach any level of the options. 
  * See {@link epArrayGet()}.
  * 
  * @param string name of option
  * @param bool whether to return an epConfig wrapper
  * @return mixed value if found or null if not
  * @access public 
  */
 public function get($name, $ret_wrapper = false)
 {
     // get option value
     $value = epArrayGet($this->options, $name);
     // return if non-array value or no epConfig wrapper required
     if (!is_array($value) || !$ret_wrapper) {
         return $value;
     }
     // otherwise wrap array value into epConfig
     $cfg = new epConfig($value);
     if ($cfg) {
         $cfg->setSource($this->getSource());
     }
     return $cfg;
 }
Example #3
0
 /**
  * Sorts two objects 
  * @param epObject $a
  * @param epObject $b
  * @throws epExceptionDbObject
  */
 private function __sort($a, $b)
 {
     // tie if no orderbys
     if (!$this->orderbys) {
         return 0;
     }
     // go through each orderby
     foreach ($this->orderbys as $orderby) {
         // sign by direction
         $sign = $orderby['dir'] == SORT_DESC ? -1 : +1;
         // get values from a and b
         $path = $orderby['path'];
         $va = epArrayGet($a, $path);
         $vb = epArrayGet($b, $path);
         // numeric
         if (is_numeric($va)) {
             // a < b
             if ($va < $vb) {
                 return -1 * $sign;
             } else {
                 if ($va > $vb) {
                     return +1 * $sign;
                 }
             }
             continue;
         }
         // string
         if (is_string($va)) {
             if (($r = strcmp($va, $vb)) < 0) {
                 return -1 * $sign;
             } else {
                 if ($r > 0) {
                     return +1 * $sign;
                 }
             }
             continue;
         }
         // invalid orderby value
         throw new epExceptionArray('Invalid sorting parameters');
     }
     // tie
     return 0;
 }
 /**
  * Sorts two objects 
  * @param epObject $a
  * @param epObject $b
  * @throws epExceptionDbObject
  */
 private function __sort($a, $b)
 {
     // tie if no orderbys
     if (!$this->orderbys) {
         return 0;
     }
     // go through each orderby
     foreach ($this->orderbys as $orderby) {
         // sign by direction
         $sign = $orderby['dir'] == 'desc' ? -1 : +1;
         // get values from a and b
         $path = $orderby['path'];
         $va = epArrayGet($a, $path);
         $vb = epArrayGet($b, $path);
         // boolean or numeric
         if (is_bool($va) || is_numeric($va)) {
             // a < b
             if ($va < $vb) {
                 return -1 * $sign;
             } else {
                 if ($va > $vb) {
                     return +1 * $sign;
                 }
             }
             continue;
         }
         // string
         if (is_string($va)) {
             // a < b
             if (($r = strcmp($va, $vb)) < 0) {
                 return -1 * $sign;
             } else {
                 if ($r > 0) {
                     return +1 * $sign;
                 }
             }
             continue;
         }
         // invalid orderby value
         throw new epExceptionDbObject('Invalid ORDER BY [' . $path . '] value');
     }
     // tie
     return 0;
 }