Passed
Pull Request — master (#4426)
by
unknown
13:11
created

Supervisor::updateHashBeforeUse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
5
use PhpOffice\PhpSpreadsheet\IComparable;
6
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
8
9
abstract class Supervisor implements IComparable
10
{
11
    /**
12
     * Supervisor?
13
     */
14
    protected bool $isSupervisor;
15
16
    /**
17
     * Parent. Only used for supervisor.
18
     *
19
     * @var Spreadsheet|Supervisor
20
     */
21
    protected $parent;
22
23
    /**
24
     * md5sum.
25
     */
26
    protected string $md5Sum = '';
27
28
    /**
29
     * do we need to recalculate the md5sum when next asked.
30
     */
31
    protected bool $updateMd5Sum = true;
32
33
    /**
34
     * Parent property name.
35 10729
     */
36
    protected ?string $parentPropertyName = null;
37
38 10729
    /**
39
     * Create a new Supervisor.
40
     *
41
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
42
     *                                    Leave this value at default unless you understand exactly what
43
     *                                        its ramifications are
44
     */
45
    public function __construct(bool $isSupervisor = false)
46 10505
    {
47
        // Supervisor?
48 10505
        $this->isSupervisor = $isSupervisor;
49 10505
    }
50
51 10505
    /**
52
     * Bind parent. Only used for supervisor.
53
     *
54
     * @return $this
55
     */
56
    public function bindParent(Spreadsheet|self $parent, ?string $parentPropertyName = null)
57 97
    {
58
        $this->parent = $parent;
59 97
        $this->parentPropertyName = $parentPropertyName;
60
61
        return $this;
62
    }
63
64
    /**
65 10125
     * update the md5sum before next use.
66
     */
67 10125
    public function updateHashBeforeUse(): void
68
    {
69
        $this->updateMd5Sum = true;
70
    }
71
72
    /**
73
     * Is this a supervisor or a cell style component?
74
     */
75
    public function getIsSupervisor(): bool
76 1157
    {
77
        return $this->isSupervisor;
78 1157
    }
79
80
    /**
81
     * Get the currently active sheet. Only used for supervisor.
82
     */
83
    public function getActiveSheet(): Worksheet
84
    {
85
        return $this->parent->getActiveSheet();
86
    }
87 10098
88
    /**
89 10098
     * Get the currently active cell coordinate in currently active sheet.
90
     * Only used for supervisor.
91
     *
92
     * @return string E.g. 'A1'
93
     */
94
    public function getSelectedCells(): string
95 1112
    {
96
        return $this->getActiveSheet()->getSelectedCells();
97 1112
    }
98 1112
99 1112
    /**
100 1100
     * Get the currently active cell coordinate in currently active sheet.
101
     * Only used for supervisor.
102 1112
     *
103
     * @return string E.g. 'A1'
104
     */
105
    public function getActiveCell(): string
106
    {
107
        return $this->getActiveSheet()->getActiveCell();
108
    }
109
110
    /**
111
     * Update Hash when something changes.
112
     */
113
    abstract protected function updateHash(): void;
114 16
115
    /**
116 16
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
117
     */
118
    public function __clone()
119
    {
120
        $vars = get_object_vars($this);
121
        foreach ($vars as $key => $value) {
122
            if ((is_object($value)) && ($key != 'parent')) {
123
                $this->$key = clone $value;
124
            } else {
125
                $this->$key = $value;
126
            }
127
        }
128
    }
129
130
    /**
131
     * Export style as array.
132
     *
133
     * Available to anything which extends this class:
134
     * Alignment, Border, Borders, Color, Fill, Font,
135
     * NumberFormat, Protection, and Style.
136
     */
137 16
    final public function exportArray(): array
138
    {
139 16
        return $this->exportArray1();
140 14
    }
141
142 16
    /**
143
     * Abstract method to be implemented in anything which
144
     * extends this class.
145
     *
146
     * This method invokes exportArray2 with the names and values
147
     * of all properties to be included in output array,
148
     * returning that array to exportArray, then to caller.
149
     */
150
    abstract protected function exportArray1(): array;
151
152
    /**
153
     * Populate array from exportArray1.
154
     * This method is available to anything which extends this class.
155
     * The parameter index is the key to be added to the array.
156
     * The parameter objOrValue is either a primitive type,
157
     * which is the value added to the array,
158
     * or a Style object to be recursively added via exportArray.
159
     */
160
    final protected function exportArray2(array &$exportedArray, string $index, mixed $objOrValue): void
161
    {
162
        if ($objOrValue instanceof self) {
163
            $exportedArray[$index] = $objOrValue->exportArray();
164
        } else {
165
            $exportedArray[$index] = $objOrValue;
166
        }
167
    }
168
169
    /**
170
     * Get the shared style component for the currently active cell in currently active sheet.
171
     * Only used for style supervisor.
172
     */
173
    abstract public function getSharedComponent(): mixed;
174
175
    /**
176
     * Build style array from subcomponents.
177
     */
178
    abstract public function getStyleArray(array $array): array;
179
}
180