Completed
Push — develop ( f99eb8...c5339b )
by Adrien
31:45
created

Protection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
5
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 *
22
 * @category   PhpSpreadsheet
23
 *
24
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
25
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
26
 */
27
class Protection extends Supervisor implements \PhpOffice\PhpSpreadsheet\IComparable
28
{
29
    /** Protection styles */
30
    const PROTECTION_INHERIT = 'inherit';
31
    const PROTECTION_PROTECTED = 'protected';
32
    const PROTECTION_UNPROTECTED = 'unprotected';
33
34
    /**
35
     * Locked.
36
     *
37
     * @var string
38
     */
39
    protected $locked;
40
41
    /**
42
     * Hidden.
43
     *
44
     * @var string
45
     */
46
    protected $hidden;
47
48
    /**
49
     * Create a new Protection.
50
     *
51
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
52
     *                                    Leave this value at default unless you understand exactly what
53
     *                                        its ramifications are
54
     * @param bool $isConditional Flag indicating if this is a conditional style or not
55
     *                                    Leave this value at default unless you understand exactly what
56
     *                                        its ramifications are
57
     */
58 73
    public function __construct($isSupervisor = false, $isConditional = false)
59
    {
60
        // Supervisor?
61 73
        parent::__construct($isSupervisor);
62
63
        // Initialise values
64 73
        if (!$isConditional) {
65 73
            $this->locked = self::PROTECTION_INHERIT;
66 73
            $this->hidden = self::PROTECTION_INHERIT;
67
        }
68 73
    }
69
70
    /**
71
     * Get the shared style component for the currently active cell in currently active sheet.
72
     * Only used for style supervisor.
73
     *
74
     * @return Protection
75
     */
76
    public function getSharedComponent()
77
    {
78
        return $this->parent->getSharedComponent()->getProtection();
79
    }
80
81
    /**
82
     * Build style array from subcomponents.
83
     *
84
     * @param array $array
85
     *
86
     * @return array
87
     */
88 13
    public function getStyleArray($array)
89
    {
90 13
        return ['protection' => $array];
91
    }
92
93
    /**
94
     * Apply styles from array.
95
     *
96
     * <code>
97
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getLocked()->applyFromArray(
98
     *        array(
99
     *            'locked' => TRUE,
100
     *            'hidden' => FALSE
101
     *        )
102
     * );
103
     * </code>
104
     *
105
     * @param array $pStyles Array containing style information
106
     *
107
     * @throws \PhpOffice\PhpSpreadsheet\Exception
108
     *
109
     * @return Protection
110
     */
111 13 View Code Duplication
    public function applyFromArray($pStyles = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113 13
        if (is_array($pStyles)) {
114 13
            if ($this->isSupervisor) {
115
                $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
116
            } else {
117 13
                if (isset($pStyles['locked'])) {
118 13
                    $this->setLocked($pStyles['locked']);
119
                }
120 13
                if (isset($pStyles['hidden'])) {
121
                    $this->setHidden($pStyles['hidden']);
122
                }
123
            }
124
        } else {
125
            throw new \PhpOffice\PhpSpreadsheet\Exception('Invalid style array passed.');
126
        }
127
128 13
        return $this;
129
    }
130
131
    /**
132
     * Get locked.
133
     *
134
     * @return string
135
     */
136 58
    public function getLocked()
137
    {
138 58
        if ($this->isSupervisor) {
139
            return $this->getSharedComponent()->getLocked();
140
        }
141
142 58
        return $this->locked;
143
    }
144
145
    /**
146
     * Set locked.
147
     *
148
     * @param string $pValue
149
     *
150
     * @return Protection
151
     */
152 16 View Code Duplication
    public function setLocked($pValue = self::PROTECTION_INHERIT)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154 16
        if ($this->isSupervisor) {
155 13
            $styleArray = $this->getStyleArray(['locked' => $pValue]);
156 13
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
157
        } else {
158 16
            $this->locked = $pValue;
159
        }
160
161 16
        return $this;
162
    }
163
164
    /**
165
     * Get hidden.
166
     *
167
     * @return string
168
     */
169 58
    public function getHidden()
170
    {
171 58
        if ($this->isSupervisor) {
172
            return $this->getSharedComponent()->getHidden();
173
        }
174
175 58
        return $this->hidden;
176
    }
177
178
    /**
179
     * Set hidden.
180
     *
181
     * @param string $pValue
182
     *
183
     * @return Protection
184
     */
185 4 View Code Duplication
    public function setHidden($pValue = self::PROTECTION_INHERIT)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
    {
187 4
        if ($this->isSupervisor) {
188
            $styleArray = $this->getStyleArray(['hidden' => $pValue]);
189
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
190
        } else {
191 4
            $this->hidden = $pValue;
192
        }
193
194 4
        return $this;
195
    }
196
197
    /**
198
     * Get hash code.
199
     *
200
     * @return string Hash code
201
     */
202 69
    public function getHashCode()
203
    {
204 69
        if ($this->isSupervisor) {
205
            return $this->getSharedComponent()->getHashCode();
206
        }
207
208 69
        return md5(
209 69
            $this->locked .
210 69
            $this->hidden .
211 69
            __CLASS__
212
        );
213
    }
214
}
215