Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
Protection
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
11 / 11
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getEditing
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setEditing
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getPassword
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPassword
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getSpinCount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSpinCount
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getAlgorithm
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setAlgorithm
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getSalt
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSalt
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * This file is part of PHPWord - A pure PHP library for reading and writing
5 * word processing documents.
6 *
7 * PHPWord is free software distributed under the terms of the GNU Lesser
8 * General Public License version 3 as published by the Free Software Foundation.
9 *
10 * For the full copyright and license information, please read the LICENSE
11 * file that was distributed with this source code. For the full list of
12 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13 *
14 * @see         https://github.com/PHPOffice/PHPWord
15 *
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19namespace PhpOffice\PhpWord\Metadata;
20
21use InvalidArgumentException;
22use PhpOffice\PhpWord\Shared\Microsoft\PasswordEncoder;
23use PhpOffice\PhpWord\SimpleType\DocProtect;
24
25/**
26 * Document protection class.
27 *
28 * @since 0.12.0
29 * @see http://www.datypic.com/sc/ooxml/t-w_CT_DocProtect.html
30 */
31class Protection
32{
33    /**
34     * Editing restriction none|readOnly|comments|trackedChanges|forms.
35     *
36     * @var string
37     *
38     * @see  http://www.datypic.com/sc/ooxml/a-w_edit-1.html
39     */
40    private $editing;
41
42    /**
43     * password.
44     *
45     * @var string
46     */
47    private $password;
48
49    /**
50     * Iterations to Run Hashing Algorithm.
51     *
52     * @var int
53     */
54    private $spinCount = 100000;
55
56    /**
57     * Cryptographic Hashing Algorithm (see constants defined in \PhpOffice\PhpWord\Shared\Microsoft\PasswordEncoder).
58     *
59     * @var string
60     */
61    private $algorithm = PasswordEncoder::ALGORITHM_SHA_1;
62
63    /**
64     * Salt for Password Verifier.
65     *
66     * @var string
67     */
68    private $salt;
69
70    /**
71     * Create a new instance.
72     *
73     * @param string $editing
74     */
75    public function __construct($editing = null)
76    {
77        if ($editing != null) {
78            $this->setEditing($editing);
79        }
80    }
81
82    /**
83     * Get editing protection.
84     *
85     * @return string
86     */
87    public function getEditing()
88    {
89        return $this->editing;
90    }
91
92    /**
93     * Set editing protection.
94     *
95     * @param string $editing Any value of \PhpOffice\PhpWord\SimpleType\DocProtect
96     *
97     * @return self
98     */
99    public function setEditing($editing = null)
100    {
101        DocProtect::validate($editing);
102        $this->editing = $editing;
103
104        return $this;
105    }
106
107    /**
108     * Get password.
109     *
110     * @return string
111     */
112    public function getPassword()
113    {
114        return $this->password;
115    }
116
117    /**
118     * Set password.
119     *
120     * @param string $password
121     *
122     * @return self
123     */
124    public function setPassword($password)
125    {
126        $this->password = $password;
127
128        return $this;
129    }
130
131    /**
132     * Get count for hash iterations.
133     *
134     * @return int
135     */
136    public function getSpinCount()
137    {
138        return $this->spinCount;
139    }
140
141    /**
142     * Set count for hash iterations.
143     *
144     * @param int $spinCount
145     *
146     * @return self
147     */
148    public function setSpinCount($spinCount)
149    {
150        $this->spinCount = $spinCount;
151
152        return $this;
153    }
154
155    /**
156     * Get algorithm.
157     *
158     * @return string
159     */
160    public function getAlgorithm()
161    {
162        return $this->algorithm;
163    }
164
165    /**
166     * Set algorithm.
167     *
168     * @param string $algorithm
169     *
170     * @return self
171     */
172    public function setAlgorithm($algorithm)
173    {
174        $this->algorithm = $algorithm;
175
176        return $this;
177    }
178
179    /**
180     * Get salt.
181     *
182     * @return string
183     */
184    public function getSalt()
185    {
186        return $this->salt;
187    }
188
189    /**
190     * Set salt. Salt HAS to be 16 characters, or an exception will be thrown.
191     *
192     * @param string $salt
193     *
194     * @return self
195     */
196    public function setSalt($salt)
197    {
198        if ($salt !== null && strlen($salt) !== 16) {
199            throw new InvalidArgumentException('salt has to be of exactly 16 bytes length');
200        }
201
202        $this->salt = $salt;
203
204        return $this;
205    }
206}