PhpBench\Formatter\Formatter::applyClasses PHP Method

applyClasses() public method

Parameters can be given: parameters are used to replcae any tokens used in the format options; this is required when options should be overridden on a per-subject basis.
public applyClasses ( array $classNames, string $value, array $params = [] ) : string
$classNames array
$value string
$params array
return string
    public function applyClasses(array $classNames, $value, $params = [])
    {
        if (!is_scalar($value)) {
            throw new \InvalidArgumentException(sprintf('Value is not a scalar, is a "%s"', is_object($value) ? get_class($value) : gettype($value)));
        }
        foreach ($classNames as $class) {
            if (!isset($this->classes[$class])) {
                throw new \InvalidArgumentException(sprintf('Unknown class "%s", known classNames: "%s"', $class, implode('", "', array_keys($this->classes))));
            }
            $classDefinition = $this->classes[$class];
            try {
                foreach ($classDefinition as $formatDefinition) {
                    if (!isset($formatDefinition[1])) {
                        $formatDefinition[1] = [];
                    }
                    list($formatName, $formatConfig) = $formatDefinition;
                    $formatConfig = $this->substituteTokens($formatConfig, $params);
                    $format = $this->formatRegistry->get($formatName);
                    $defaultOptions = $format->getDefaultOptions();
                    $diff = array_diff(array_keys($formatConfig), array_keys($defaultOptions));
                    if ($diff) {
                        throw new \InvalidArgumentException(sprintf('Invalid options "%s" for format "%s", valid options: "%s"', implode('", "', $diff), $formatName, implode('", "', array_keys($defaultOptions))));
                    }
                    $formatConfig = array_merge($defaultOptions, $formatConfig);
                    $value = $format->format($value, $formatConfig);
                }
            } catch (\InvalidArgumentException $e) {
                throw new \InvalidArgumentException(sprintf('Could not load class definition: %s (format %s)', json_encode($classDefinition), $formatName), null, $e);
            }
        }
        return $value;
    }

Usage Example

示例#1
0
 protected function renderTableElement(Element $tableEl, $config)
 {
     $rows = [];
     $colNames = [];
     foreach ($tableEl->query('.//col') as $colEl) {
         $colNames[] = $colEl->getAttribute('label');
     }
     foreach ($tableEl->query('.//row') as $rowEl) {
         $row = [];
         $formatterParams = [];
         foreach ($rowEl->query('./formatter-param') as $paramEl) {
             $formatterParams[$paramEl->getAttribute('name')] = $paramEl->nodeValue;
         }
         foreach ($rowEl->query('.//cell') as $cellEl) {
             $colName = $cellEl->getAttribute('name');
             $value = $cellEl->nodeValue;
             if ('' !== $value && $cellEl->hasAttribute('class')) {
                 $classes = explode(' ', $cellEl->getAttribute('class'));
                 $value = $this->formatter->applyClasses($classes, $value, $formatterParams);
             }
             $row[$colName] = $value;
         }
         $rows[] = $row;
     }
     $table = $this->createTable();
     // style only supported in Symfony > 2.4
     if (method_exists($table, 'setStyle')) {
         $table->setStyle($config['table_style']);
     }
     $table->setHeaders($colNames);
     $table->setRows($rows);
     $this->renderTable($table);
     $this->output->writeln('');
 }
All Usage Examples Of PhpBench\Formatter\Formatter::applyClasses