Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

semour / semour_admin

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
Normal viewHistoryPermalink
Switch branch/tag
  • semour_admin
  • ..
  • Engine
  • ArrayArgumentHelper.php
ArrayArgumentHelper.php 5.21 KB
孙龙's avatar
订单
f785d072
 
孙龙 committed 2 years ago
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
<?php

namespace PhpOffice\PhpSpreadsheet\Calculation\Engine;

use PhpOffice\PhpSpreadsheet\Calculation\Exception;

class ArrayArgumentHelper
{
    /**
     * @var int
     */
    protected $indexStart = 0;

    /**
     * @var array
     */
    protected $arguments;

    /**
     * @var int
     */
    protected $argumentCount;

    /**
     * @var array
     */
    protected $rows;

    /**
     * @var array
     */
    protected $columns;

    public function initialise(array $arguments): void
    {
        $keys = array_keys($arguments);
        $this->indexStart = (int) array_shift($keys);
        $this->rows = $this->rows($arguments);
        $this->columns = $this->columns($arguments);

        $this->argumentCount = count($arguments);
        $this->arguments = $this->flattenSingleCellArrays($arguments, $this->rows, $this->columns);

        $this->rows = $this->rows($arguments);
        $this->columns = $this->columns($arguments);

        if ($this->arrayArguments() > 2) {
            throw new Exception('Formulae with more than two array arguments are not supported');
        }
    }

    public function arguments(): array
    {
        return $this->arguments;
    }

    public function hasArrayArgument(): bool
    {
        return $this->arrayArguments() > 0;
    }

    public function getFirstArrayArgumentNumber(): int
    {
        $rowArrays = $this->filterArray($this->rows);
        $columnArrays = $this->filterArray($this->columns);

        for ($index = $this->indexStart; $index < $this->argumentCount; ++$index) {
            if (isset($rowArrays[$index]) || isset($columnArrays[$index])) {
                return ++$index;
            }
        }

        return 0;
    }

    public function getSingleRowVector(): ?int
    {
        $rowVectors = $this->getRowVectors();

        return count($rowVectors) === 1 ? array_pop($rowVectors) : null;
    }

    private function getRowVectors(): array
    {
        $rowVectors = [];
        for ($index = $this->indexStart; $index < ($this->indexStart + $this->argumentCount); ++$index) {
            if ($this->rows[$index] === 1 && $this->columns[$index] > 1) {
                $rowVectors[] = $index;
            }
        }

        return $rowVectors;
    }

    public function getSingleColumnVector(): ?int
    {
        $columnVectors = $this->getColumnVectors();

        return count($columnVectors) === 1 ? array_pop($columnVectors) : null;
    }

    private function getColumnVectors(): array
    {
        $columnVectors = [];
        for ($index = $this->indexStart; $index < ($this->indexStart + $this->argumentCount); ++$index) {
            if ($this->rows[$index] > 1 && $this->columns[$index] === 1) {
                $columnVectors[] = $index;
            }
        }

        return $columnVectors;
    }

    public function getMatrixPair(): array
    {
        for ($i = $this->indexStart; $i < ($this->indexStart + $this->argumentCount - 1); ++$i) {
            for ($j = $i + 1; $j < $this->argumentCount; ++$j) {
                if (isset($this->rows[$i], $this->rows[$j])) {
                    return [$i, $j];
                }
            }
        }

        return [];
    }

    public function isVector(int $argument): bool
    {
        return $this->rows[$argument] === 1 || $this->columns[$argument] === 1;
    }

    public function isRowVector(int $argument): bool
    {
        return $this->rows[$argument] === 1;
    }

    public function isColumnVector(int $argument): bool
    {
        return $this->columns[$argument] === 1;
    }

    public function rowCount(int $argument): int
    {
        return $this->rows[$argument];
    }

    public function columnCount(int $argument): int
    {
        return $this->columns[$argument];
    }

    private function rows(array $arguments): array
    {
        return array_map(
            function ($argument) {
                return is_countable($argument) ? count($argument) : 1;
            },
            $arguments
        );
    }

    private function columns(array $arguments): array
    {
        return array_map(
            function ($argument) {
                return is_array($argument) && is_array($argument[array_keys($argument)[0]])
                    ? count($argument[array_keys($argument)[0]])
                    : 1;
            },
            $arguments
        );
    }

    public function arrayArguments(): int
    {
        $count = 0;
        foreach (array_keys($this->arguments) as $argument) {
            if ($this->rows[$argument] > 1 || $this->columns[$argument] > 1) {
                ++$count;
            }
        }

        return $count;
    }

    private function flattenSingleCellArrays(array $arguments, array $rows, array $columns): array
    {
        foreach ($arguments as $index => $argument) {
            if ($rows[$index] === 1 && $columns[$index] === 1) {
                while (is_array($argument)) {
                    $argument = array_pop($argument);
                }
                $arguments[$index] = $argument;
            }
        }

        return $arguments;
    }

    private function filterArray(array $array): array
    {
        return array_filter(
            $array,
            function ($value) {
                return $value > 1;
            }
        );
    }
}