Commit 8c52629a authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Polish de l'extraction de CALC

parent 2c917fa6
Loading
Loading
Loading
Loading
Loading
+6 −2
Changes for src/Calc.php: 6 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

namespace Unicaen\OpenDocument;

use Unicaen\OpenDocument\Calc\Cell;
use Unicaen\OpenDocument\Calc\Sheet;

class Calc
@@ -52,7 +51,12 @@ class Calc
    public function getSheet(int $index): Sheet
    {
        if (!isset($this->sheets[$index])) {
            $this->sheets[$index] = new Sheet($this, $index);
            $content = $this->document->getContent();

            $spreadsheet = $this->document->find($content, 'office:spreadsheet')[0];
            $node        = $this->document->find($spreadsheet, 'table:table')[$index];

            $this->sheets[$index] = new Sheet($this, $index, $node);
        }

        return $this->sheets[$index];
+44 −97
Changes for src/Calc/Cell.php: 44 added lines, 97 removed lines.
Original line number Diff line number Diff line
@@ -3,9 +3,15 @@
namespace Unicaen\OpenDocument\Calc;

use Unicaen\OpenDocument\Calc;
use DOMNode;

class Cell
{
    /**
     * @var DOMNode
     */
    private $node;

    /**
     * @var int
     */
@@ -16,35 +22,25 @@ class Cell
     */
    private $col;

    /***
     * @var string
     */
    private string $type;

    /**
     * @var string
     */
    private $content;

    /**
     * @var string
     * @inheritDoc
     */
    private $formule;
    public function __construct(DOMNode $node)
    {
        $this->node = $node;
    }

    /**
     * @var mixed
     */
    private $value;

    /**
     * @var int
     */
    private int $colSpan = 1;

    /**
     * @var int
     * @return DOMNode
     */
    private int $rowSpan = 1;
    public function getNode(): DOMNode
    {
        return $this->node;
    }



@@ -101,21 +97,10 @@ class Cell
     */
    public function getType(): string
    {
        return $this->type;
        if ($this->node->hasAttributes()) {
            $type = $this->node->attributes->getNamedItem('value-type');
            if ($type) return $type->value;
        }



    /**
     * @param string $type
     *
     * @return Cell
     */
    public function setType(string $type): Cell
    {
        $this->type = $type;

        return $this;
    }


@@ -125,21 +110,7 @@ class Cell
     */
    public function getContent(): string
    {
        return $this->content;
    }



    /**
     * @param string $content
     *
     * @return Cell
     */
    public function setContent(string $content): Cell
    {
        $this->content = $content;

        return $this;
        return $this->node->textContent;
    }


@@ -149,21 +120,12 @@ class Cell
     */
    public function getFormule(): ?string
    {
        return $this->formule;
        if ($this->node->hasAttributes()) {
            $formule = $this->node->attributes->getNamedItem('formula');
            if ($formule) return $formule->value;
        }



    /**
     * @param string $formule
     *
     * @return Cell
     */
    public function setFormule(string $formule): Cell
    {
        $this->formule = $formule;

        return $this;
        return null;
    }


@@ -173,21 +135,12 @@ class Cell
     */
    public function getValue()
    {
        return $this->value;
        if ($this->node->hasAttributes()) {
            $value = $this->node->attributes->getNamedItem('value');
            if ($value) return $value->value;
        }



    /**
     * @param mixed $value
     *
     * @return Cell
     */
    public function setValue($value)
    {
        $this->value = $value;

        return $this;
        return null;
    }


@@ -197,21 +150,24 @@ class Cell
     */
    public function getColSpan(): int
    {
        return $this->colSpan;
        if ($this->node->hasAttributes()) {
            $colSpan = $this->node->attributes->getNamedItem('number-columns-spanned');
            if ($colSpan) return (int)$colSpan->value;
        }

        return 1;
    }


    /**
     * @param int $colSpan
     *
     * @return Cell
     */
    public function setColSpan(int $colSpan): Cell

    public function getRepeated(): ?int
    {
        $this->colSpan = $colSpan;
        if ($this->node->hasAttributes()) {
            $colSpan = $this->node->attributes->getNamedItem('number-columns-repeated');
            if ($colSpan) return (int)$colSpan->value;
        }

        return $this;
        return null;
    }


@@ -221,21 +177,12 @@ class Cell
     */
    public function getRowSpan(): int
    {
        return $this->rowSpan;
        if ($this->node->hasAttributes()) {
            $rowSpan = $this->node->attributes->getNamedItem('number-rows-spanned');
            if ($rowSpan) return (int)$rowSpan->value;
        }



    /**
     * @param int $rowSpan
     *
     * @return Cell
     */
    public function setRowSpan(int $rowSpan): Cell
    {
        $this->rowSpan = $rowSpan;

        return $this;
        return 1;
    }


+43 −36
Changes for src/Calc/Sheet.php: 43 added lines, 36 removed lines.
Original line number Diff line number Diff line
@@ -19,6 +19,11 @@ class Sheet
     */
    private int $index;

    /**
     * @var DOMNode
     */
    private $node;

    /**
     * @var Cell[][]
     */
@@ -41,27 +46,34 @@ class Sheet



    public function __construct(Calc $calc, int $index)
    public function __construct(Calc $calc, int $index, DOMNode $node)
    {
        $this->calc = $calc;
        $this->index = $index;
        $this->node = $node;
        $this->read();
    }



    public function read()
    /**
     * @return DOMNode
     */
    public function getNode(): DOMNode
    {
        $content = $this->calc->getDocument()->getContent();
        return $this->node;
    }


        $spreadsheet = $this->calc->getDocument()->find($content, 'office:spreadsheet')[0];
        $feuille       = $this->calc->getDocument()->find($spreadsheet, 'table:table')[$this->index];

    public function read()
    {
        $this->cellTab = [];
        $this->cells   = [];
        $this->maxRow  = 0;
        $this->maxCol  = 0;
        $rowIndex      = 1;
        $rcs           = $feuille->childNodes;
        $rcs           = $this->node->childNodes;
        foreach ($rcs as $rc) {
            switch ($rc->tagName) {
                case 'office:forms':
@@ -83,7 +95,11 @@ class Sheet
        $col   = 1;
        foreach ($cells as $c) {
            if ('table:table-cell' == $c->tagName) {
                $cell = $this->readCell($c);
                $cell = new Cell($c);
                if ($r = $cell->getRepeated()){
                    $col += $r;
                    continue;
                }
                $init = true;
                for ($c = 0; $c < $cell->getColSpan(); $c++) {
                    while ($this->hasCellByCoords($col, $row)) {
@@ -111,33 +127,6 @@ class Sheet



    protected function readCell(DOMNode $cell): Cell
    {
        $c = new Cell;

        if ($cell->hasAttributes()) {
            $colSpan = $cell->attributes->getNamedItem('number-columns-spanned');
            if ($colSpan) $c->setColSpan((int)$colSpan->value);

            $rowSpan = $cell->attributes->getNamedItem('number-rows-spanned');
            if ($rowSpan) $c->setRowSpan((int)$rowSpan->value);

            $formule = $cell->attributes->getNamedItem('formula');
            if ($formule) $c->setFormule($formule->value);

            $value = $cell->attributes->getNamedItem('value');
            if ($value) $c->setValue($value->value);

            $type = $cell->attributes->getNamedItem('value-type');
            if ($type) $c->setType($type->value);
        }
        $c->setContent($cell->textContent);

        return $c;
    }



    public function getCells(): array
    {
        return $this->cells;
@@ -157,8 +146,8 @@ class Sheet
            $variants = [
                "[.$col$row]",
                "[.\$$col$row]",
                "[.$col\$$row]",
                "[.\$$col\$$row]",
                "[.$col\$"."$row]",
                "[.\$$col\$"."$row]",
            ];
            foreach( $variants as $vn ){
                if (false !== strpos($formule, $vn)){
@@ -229,4 +218,22 @@ class Sheet
        return null;
    }



    public function html(): string
    {
        $h = '<table class="table table-bordered table-condensed">';
        for( $r=1;$r<=$this->getMaxRow();$r++){
            $h .= '<tr>';
            for( $c=1;$c<=$this->getMaxCol();$c++){
                $cell = $this->getCellByCoords($c,$r);
                $h .= '<td>'.($cell ? $cell->getContent() : '').'</td>';
            }
            $h .= '</tr>';
        }

        $h .= '</table>';

        return $h;
    }
}
 No newline at end of file