/**
  * Find the styles that that match the passed size.
  *
  * @param RImg\Size $size
  *   The size instance to match styles to.
  *
  * @return \stdClass[]
  *   List of styles that fit the passed size. Same object definition as
  *   $this->getStyles().
  *
  * @see DrupalImageStyle::getStyles()
  */
 private function stylesMatchingSize(RImg\Size $size)
 {
     # @todo Support Size instances without aspect ratio restrictions.
     # @todo Support Size instances with only width or only height restrictions.
     $styles = F\filter($this->getStyles(), function ($style) use($size) {
         return $style->firm and $size->matchesAspectRatio($style->width / $style->height);
     });
     $min_width = $size->getMinWidth();
     $max_width = $size->getMaxWidth();
     $styles = F\group($styles, function ($style) use($min_width, $max_width) {
         if ($style->width < $min_width) {
             return 'less';
         }
         if ($style->width > $max_width * 2) {
             # Multiplied for high DPI displays.
             return 'greater';
         }
         return 'within';
     });
     if (!empty($styles['greater'])) {
         # Make sure the end of the range is covered.
         $styles['within'][] = F\head($styles['greater']);
     }
     if (empty($styles['within'])) {
         if (!empty($styles['less'])) {
             # Better to have something too small than something too non-existent.
             $styles = [F\last($styles['less'])];
         } else {
             $styles = [];
         }
     } else {
         $styles = $styles['within'];
     }
     return F\unique($styles, function ($s) {
         return $s->width;
     });
 }
Пример #2
0
 public function testPassNonCallable()
 {
     $this->expectArgumentError("Argument 2 passed to Functional\\group() must be callable");
     group($this->list, 'undefinedFunction');
 }
Пример #3
0
 /**
  * Process the compare feature.
  *
  * @param array $tables
  * @param Config $config
  *
  * @return array
  */
 private function processCompare(array $tables, Config $config)
 {
     if (!isset($config['compare'])) {
         return $tables;
     }
     $conditions = array_diff($config['cols'], $this->statKeys, [$config['compare']]);
     $compare = $config['compare'];
     $compareFields = $config['compare_fields'];
     return F\map($tables, function ($table) use($conditions, $compare, $compareFields) {
         $groups = F\group($table, function ($row) use($conditions, $compare, $compareFields) {
             $values = array_intersect_key($row->getArrayCopy(), array_flip($conditions));
             return F\reduce_left($values, function ($value, $i, $c, $reduction) {
                 return $reduction . $value;
             });
         });
         $table = [];
         $colNames = null;
         foreach ($groups as $group) {
             $firstRow = null;
             foreach ($group as $row) {
                 if (null === $firstRow) {
                     $firstRow = $row->newInstance(array_diff_key($row->getArrayCopy(), array_flip($this->statKeys)));
                     if (isset($firstRow[$compare])) {
                         unset($firstRow[$compare]);
                     }
                     foreach ($compareFields as $compareField) {
                         if (isset($firstRow[$compareField])) {
                             unset($firstRow[$compareField]);
                         }
                     }
                 }
                 if (null === $colNames) {
                     $colNames = array_combine($firstRow->getNames(), $firstRow->getNames());
                 }
                 $compared = $row[$compare];
                 foreach ($compareFields as $compareField) {
                     $name = $compare . ':' . $compared . ':' . $compareField;
                     $name = $this->resolveCompareColumnName($firstRow, $name);
                     $firstRow[$name] = $row[$compareField];
                     $colNames[$name] = $name;
                     // we invent a new col name here, use the compare field's class.
                     $this->classMap[$name] = $this->classMap[$compareField];
                 }
             }
             $table[] = $firstRow;
         }
         $table = F\map($table, function ($row) use($colNames) {
             $newRow = $row->newInstance([]);
             foreach ($colNames as $colName) {
                 $newRow[$colName] = isset($row[$colName]) ? $row[$colName] : null;
             }
             return $newRow;
         });
         return $table;
     });
     return $tables;
 }