Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
 
5
class Protection extends Supervisor
6
{
7
    /** Protection styles */
8
    const PROTECTION_INHERIT = 'inherit';
9
    const PROTECTION_PROTECTED = 'protected';
10
    const PROTECTION_UNPROTECTED = 'unprotected';
11
 
12
    /**
13
     * Locked.
14
     */
15
    protected ?string $locked = null;
16
 
17
    /**
18
     * Hidden.
19
     */
20
    protected ?string $hidden = null;
21
 
22
    /**
23
     * Create a new Protection.
24
     *
25
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
26
     *                                    Leave this value at default unless you understand exactly what
27
     *                                        its ramifications are
28
     * @param bool $isConditional Flag indicating if this is a conditional style or not
29
     *                                    Leave this value at default unless you understand exactly what
30
     *                                        its ramifications are
31
     */
32
    public function __construct(bool $isSupervisor = false, bool $isConditional = false)
33
    {
34
        // Supervisor?
35
        parent::__construct($isSupervisor);
36
 
37
        // Initialise values
38
        if (!$isConditional) {
39
            $this->locked = self::PROTECTION_INHERIT;
40
            $this->hidden = self::PROTECTION_INHERIT;
41
        }
42
    }
43
 
44
    /**
45
     * Get the shared style component for the currently active cell in currently active sheet.
46
     * Only used for style supervisor.
47
     */
48
    public function getSharedComponent(): self
49
    {
50
        /** @var Style $parent */
51
        $parent = $this->parent;
52
 
53
        return $parent->getSharedComponent()->getProtection();
54
    }
55
 
56
    /**
57
     * Build style array from subcomponents.
58
     */
59
    public function getStyleArray(array $array): array
60
    {
61
        return ['protection' => $array];
62
    }
63
 
64
    /**
65
     * Apply styles from array.
66
     *
67
     * <code>
68
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getLocked()->applyFromArray(
69
     *     [
70
     *         'locked' => TRUE,
71
     *         'hidden' => FALSE
72
     *     ]
73
     * );
74
     * </code>
75
     *
76
     * @param array $styleArray Array containing style information
77
     *
78
     * @return $this
79
     */
80
    public function applyFromArray(array $styleArray): static
81
    {
82
        if ($this->isSupervisor) {
83
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
84
        } else {
85
            if (isset($styleArray['locked'])) {
86
                $this->setLocked($styleArray['locked']);
87
            }
88
            if (isset($styleArray['hidden'])) {
89
                $this->setHidden($styleArray['hidden']);
90
            }
91
        }
92
 
93
        return $this;
94
    }
95
 
96
    /**
97
     * Get locked.
98
     */
99
    public function getLocked(): ?string
100
    {
101
        if ($this->isSupervisor) {
102
            return $this->getSharedComponent()->getLocked();
103
        }
104
 
105
        return $this->locked;
106
    }
107
 
108
    /**
109
     * Set locked.
110
     *
111
     * @param string $lockType see self::PROTECTION_*
112
     *
113
     * @return $this
114
     */
115
    public function setLocked(string $lockType): static
116
    {
117
        if ($this->isSupervisor) {
118
            $styleArray = $this->getStyleArray(['locked' => $lockType]);
119
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
120
        } else {
121
            $this->locked = $lockType;
122
        }
123
 
124
        return $this;
125
    }
126
 
127
    /**
128
     * Get hidden.
129
     */
130
    public function getHidden(): ?string
131
    {
132
        if ($this->isSupervisor) {
133
            return $this->getSharedComponent()->getHidden();
134
        }
135
 
136
        return $this->hidden;
137
    }
138
 
139
    /**
140
     * Set hidden.
141
     *
142
     * @param string $hiddenType see self::PROTECTION_*
143
     *
144
     * @return $this
145
     */
146
    public function setHidden(string $hiddenType): static
147
    {
148
        if ($this->isSupervisor) {
149
            $styleArray = $this->getStyleArray(['hidden' => $hiddenType]);
150
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
151
        } else {
152
            $this->hidden = $hiddenType;
153
        }
154
 
155
        return $this;
156
    }
157
 
158
    /**
159
     * Get hash code.
160
     *
161
     * @return string Hash code
162
     */
163
    public function getHashCode(): string
164
    {
165
        if ($this->isSupervisor) {
166
            return $this->getSharedComponent()->getHashCode();
167
        }
168
 
169
        return md5(
170
            $this->locked
171
            . $this->hidden
172
            . __CLASS__
173
        );
174
    }
175
 
176
    protected function exportArray1(): array
177
    {
178
        $exportedArray = [];
179
        $this->exportArray2($exportedArray, 'locked', $this->getLocked());
180
        $this->exportArray2($exportedArray, 'hidden', $this->getHidden());
181
 
182
        return $exportedArray;
183
    }
184
}