/** * Index the given cell definitions by name. The name can * be dynamic, in which case the name must be evaluated. * * @param array $cellDefinitions * * @return array */ private function indexCellDefinitions(array $cellDefinitions) { $indexedDefinitions = array(); foreach ($cellDefinitions as $cellDefinition) { $cellName = $cellDefinition['name']; $cellItems = array(null); if (isset($cellDefinition['with_items'])) { $cellItems = $cellDefinition['with_items']; } // we need to evaluate the name for each column foreach ($cellItems as $cellItem) { $evaledCellName = $this->tokenReplacer->replaceTokens($cellName, null, $cellItem); $cellDefinition['_item'] = $cellItem; $indexedDefinitions[$evaledCellName] = $cellDefinition; } } return $indexedDefinitions; }
/** * Iterate over the definition and determine the number of columns and compiler * passes. * * @param Definition */ private function processMetadata(Definition $definition, $sourceDom = null) { $columns = array(); $passes = array(); if ($sourceDom) { $xpath = new \DOMXpath($sourceDom); $this->xpathResolver->registerXPathFunctions($xpath); } foreach ($definition['rows'] as &$rowDefinition) { foreach ($rowDefinition['cells'] as &$cellDefinition) { $cellName = $cellDefinition['name']; if (isset($cellDefinition['pass'])) { $passes[] = $cellDefinition['pass']; } // if an expression is given to with_items then we need to query // the source document to evaluate the item names. if (isset($cellDefinition['with_items']['selector'])) { if (null === $sourceDom) { throw new \RuntimeException('You must pass the source document to the loader in order to use `with_items_expr`'); } $itemsExpr = $cellDefinition['with_items']; $selector = $this->xpathResolver->replaceFunctions($itemsExpr['selector']); $nodes = $xpath->query($selector); $items = array(); foreach ($nodes as $node) { // todo: Support keys ? $value = $xpath->evaluate($itemsExpr['value'], $node); $items[] = $value; } $cellDefinition['with_items'] = $items; } $cellItems = array(null); if (isset($cellDefinition['with_items'])) { $cellItems = $cellDefinition['with_items']; } foreach ($cellItems as $cellItem) { $evaledCellName = $this->tokenReplacer->replaceTokens($cellName, null, $cellItem); $columns[$evaledCellName] = $evaledCellName; } } } sort($passes); $definition->setMetadata(array_values($columns), $passes); }