示例#1
0
 /**
  * Resets keys of an array containing a collection of objects to numerical values starting with `0`.
  * 
  * @param array|object $objects Collection of objects - either an array or a collection object that implements `toArray` method.
  * @return array Resetted array.
  * 
  * @throws InvalidArgumentException When the `$objects` argument is not an array or cannot be converted to an array.
  */
 public static function resetKeys($objects)
 {
     // convert to array, for example for Doctrine collections
     if (is_object($objects) && method_exists($objects, 'toArray')) {
         $objects = $objects->toArray();
     }
     if (!is_array($objects)) {
         throw new InvalidArgumentException('array or object convertible to array', $objects);
     }
     return ArrayUtils::resetKeys($objects);
 }
示例#2
0
 public function testResetKeys()
 {
     foreach (array('empty_array', 'char_keys', 'int_unsorted_keys', 'mixed_unsorted_keys', 'int_single_wrong_key', '2D_collection_5_named') as $arrayName) {
         $this->assertTrue(ArrayUtils::isCollection(ArrayUtils::resetKeys($this->_getArrayPreset($arrayName))), 'Failed to assert that  MD\\Foundation\\Utils\\ArrayUtils::resetKeys() returns a collection for array preset "' . $arrayName . '".');
     }
 }
示例#3
0
 /**
  * Returns a list of ancestors of the given object in an array.
  *
  * Example:
  *
  *      $logger = new \Psr\Log\NullLogger();
  *      echo \MD\Foundation\Debug\Debugger::getObjectAncestors($logger);
  *      // -> array('Psr\Log\AbstractLogger')
  *
  *      echo \MD\Foundation\Debug\Debugger::getObjectAncestors('\Psr\Log\NullLogger');
  *      // -> array('Psr\Log\AbstractLogger')
  * 
  * @param object|string $object Object or string name of a class.
  * @return array
  */
 public static function getObjectAncestors($object)
 {
     $parents = class_parents($object);
     $parents = ArrayUtils::resetKeys($parents);
     $parents = array_map(function ($parent) {
         return ltrim($parent, NS);
     }, $parents);
     return $parents;
 }