예제 #1
0
파일: Tabular.php 프로젝트: hason/tabular
 /**
  * Process the source document using the given Tabular definition and return a table document.
  *
  * The definition can be passed either as an array, a Defintion class or a file name.
  *
  * @param \DOMDocument $sourceDom
  * @param array|string|Definition $definition
  * @param array $parameters
  *
  * @return TableDom
  */
 public function tabulate(\DOMDocument $sourceDom, $definition, array $parameters = array())
 {
     $definition = $this->definitionLoader->load($definition, $sourceDom);
     if (isset($definition['params'])) {
         $parameters = array_merge($definition['params'], $parameters);
     }
     $this->expander->expand($definition, $parameters);
     $tableDom = $this->tableBuilder->buildTable($sourceDom, $definition, $parameters);
     if (isset($definition['sort'])) {
         Sort::sortTable($tableDom, $definition['sort']);
     }
     if (isset($definition['classes'])) {
         foreach ($definition['classes'] as $class => $classDefinition) {
             $formatters = array();
             foreach ($classDefinition as $formatDefinition) {
                 $options = array();
                 if (count($formatDefinition) == 2) {
                     list($formatter, $options) = $formatDefinition;
                 } else {
                     list($formatter) = $formatDefinition;
                 }
                 $formatters[] = array($formatter, $options ?: array());
             }
             $this->formatter->setClassDefinition($class, $formatters);
         }
     }
     $this->formatter->formatTable($tableDom);
     return $tableDom;
 }
예제 #2
0
파일: SortTest.php 프로젝트: hason/tabular
    /**
     * It should sort with multiple columns.
     */
    public function testSortMultipleColumns()
    {
        $table = $this->createTable(<<<EOT
<?xml version="1.0"?>
<table>
    <group name="_default">
        <row>
            <cell name="chocolate">10</cell>
            <cell name="coffee">20</cell>
        </row>
        <row>
            <cell name="chocolate">10</cell>
            <cell name="coffee">10</cell>
        </row>
        <row>
            <cell name="chocolate">5</cell>
            <cell name="coffee">20</cell>
        </row>
    </group>
</table>
EOT
);
        Sort::sortTable($table, array('chocolate', 'coffee'));
        $this->assertSortOrder($table, 0, 'chocolate', 5);
        $this->assertSortOrder($table, 0, 'coffee', 20);
        $this->assertSortOrder($table, 1, 'chocolate', 10);
        $this->assertSortOrder($table, 1, 'coffee', 10);
        $this->assertSortOrder($table, 2, 'chocolate', 10);
        $this->assertSortOrder($table, 2, 'coffee', 20);
    }