HosipLan

Untitled

Oct 9th, 2011
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.68 KB | None | 0 0
  1.     /**
  2.      * @param array $collection
  3.      * @param array $lists
  4.      * @param array $mappers
  5.      * @param boolean $allowDuplications
  6.      */
  7.     public function assertContainsCombinations($collection, array $lists, array $mappers, $allowOnlyMentioned = TRUE, $allowDuplications = FALSE)
  8.     {
  9.         $mappers = array_map('callback', $mappers);
  10.         $this->assertSame(count($lists), count($mappers), "Count of given lists equals to count of given mappers");
  11.  
  12.         $valueCounts = array();
  13.         foreach ($collection as $item) {
  14.             $keys = array();
  15.             foreach ($mappers as $i => $mapper) {
  16.                 $keys[$i] = $mapper($item);
  17.             }
  18.             $value = Nette\Utils\Arrays::getRef($valueCounts, $keys);
  19.             $value = ($value ?: 0) + 1;
  20.         }
  21.  
  22.         if ($allowDuplications === FALSE) {
  23.             $counts = array_values(array_unique(Kdyby\Tools\Arrays::flatMap($valueCounts)));
  24.             $this->assertSame(array(1), $counts, "Collection contains duplications");
  25.         }
  26.  
  27.         $foundLists = array_fill(0, count($lists), array());
  28.         $inList = Kdyby\Tools\Arrays::flatMapAssoc($valueCounts, function ($value, $keys) use ($lists, &$foundLists) {
  29.             $return = TRUE;
  30.             foreach ($keys as $i => $key) {
  31.                 $foundLists[$i][] = $key;
  32.                 if (!in_array($key, $lists[$i])) {
  33.                     $return = FALSE;
  34.                 }
  35.             }
  36.             return $return;
  37.         });
  38.  
  39.         if ($allowOnlyMentioned === TRUE) {
  40.             $notMentioned = array_filter($inList, function ($isIn) { return $isIn === FALSE; });
  41.             $this->assertEmpty($notMentioned, "The collection contains combinations, that cannot be assembled from given lists.");
  42.         }
  43.  
  44.         foreach ($foundLists as $i => $list) {
  45.             $this->assertEmpty(array_diff($lists[$i], $list), "There are all values '" . implode("', '", $lists[$i]) . "' in collection");
  46.         }
  47.     }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment