Completed
Push — master ( d93a30...6a259d )
by Mark
41s queued 30s
created

StructuredReference::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\Engine\Operands;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
6
7
final class StructuredReference implements Operand
8
{
9
    public const NAME = 'Structured Reference';
10
11
    private const OPEN_BRACE = '[';
12
    private const CLOSE_BRACE = ']';
13
14
    private string $value;
15
16 9
    public function __construct(string $structuredReference)
17
    {
18 9
        $this->value = $structuredReference;
19
    }
20
21 9
    public static function fromParser(string $formula, int $index, array $matches): self
22
    {
23 9
        $val = $matches[0];
24
25 9
        $srCount = substr_count($val, self::OPEN_BRACE)
26 9
            - substr_count($val, self::CLOSE_BRACE);
27 9
        while ($srCount > 0) {
28 9
            $srIndex = strlen($val);
29 9
            $srStringRemainder = substr($formula, $index + $srIndex);
30 9
            $closingPos = strpos($srStringRemainder, self::CLOSE_BRACE);
31 9
            if ($closingPos === false) {
32
                throw new Exception("Formula Error: No closing ']' to match opening '['");
33
            }
34 9
            $srStringRemainder = substr($srStringRemainder, 0, $closingPos + 1);
35 9
            --$srCount;
36 9
            if (strpos($srStringRemainder, self::OPEN_BRACE) !== false) {
37 7
                ++$srCount;
38
            }
39 9
            $val .= $srStringRemainder;
40
        }
41
42 9
        return new self($val);
43
    }
44
45 9
    public function value(): string
46
    {
47 9
        return $this->value;
48
    }
49
}
50