Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / lsolesen / pel / src / PelEntryNumber.php
1 <?php
2
3 /**
4  * PEL: PHP Exif Library.
5  * A library with support for reading and
6  * writing all Exif headers in JPEG and TIFF images using PHP.
7  *
8  * Copyright (C) 2004, 2005, 2006 Martin Geisler.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program in the file COPYING; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301 USA
24  */
25 namespace lsolesen\pel;
26
27 /**
28  * Abstract class for numbers.
29  *
30  * @author Martin Geisler <mgeisler@users.sourceforge.net>
31  * @license http://www.gnu.org/licenses/gpl.html GNU General Public
32  *          License (GPL)
33  * @package PEL
34  */
35
36 /**
37  * Class for holding numbers.
38  *
39  * This class can hold numbers, with range checks.
40  *
41  * @author Martin Geisler <mgeisler@users.sourceforge.net>
42  * @package PEL
43  */
44 abstract class PelEntryNumber extends PelEntry
45 {
46
47     /**
48      * The value held by this entry.
49      *
50      * @var array
51      */
52     protected $value = array();
53
54     /**
55      * The minimum allowed value.
56      *
57      * Any attempt to change the value below this variable will result
58      * in a {@link PelOverflowException} being thrown.
59      *
60      * @var int
61      */
62     protected $min;
63
64     /**
65      * The maximum allowed value.
66      *
67      * Any attempt to change the value over this variable will result in
68      * a {@link PelOverflowException} being thrown.
69      *
70      * @var int
71      */
72     protected $max;
73
74     /**
75      * The dimension of the number held.
76      *
77      * Normal numbers have a dimension of one, pairs have a dimension of
78      * two, etc.
79      *
80      * @var int
81      */
82     protected $dimension = 1;
83
84     /**
85      * Change the value.
86      *
87      * This method can change both the number of components and the
88      * value of the components. Range checks will be made on the new
89      * value, and a {@link PelOverflowException} will be thrown if the
90      * value is found to be outside the legal range.
91      *
92      * The method accept several number arguments. The {@link getValue}
93      * method will always return an array except for when a single
94      * number is given here.
95      *
96      * @param int|array $value...
97      *            the new value(s). This can be zero or
98      *            more numbers, that is, either integers or arrays. The input will
99      *            be checked to ensure that the numbers are within the valid range.
100      *            If not, then a {@link PelOverflowException} will be thrown.
101      *
102      * @see getValue
103      */
104     public function setValue($value)
105     {
106         $value = func_get_args();
107         $this->setValueArray($value);
108     }
109
110     /**
111      * Change the value.
112      *
113      * This method can change both the number of components and the
114      * value of the components. Range checks will be made on the new
115      * value, and a {@link PelOverflowException} will be thrown if the
116      * value is found to be outside the legal range.
117      *
118      * @param
119      *            array the new values. The array must contain the new
120      *            numbers.
121      *
122      * @see getValue
123      */
124     public function setValueArray($value)
125     {
126         foreach ($value as $v) {
127             $this->validateNumber($v);
128         }
129
130         $this->components = count($value);
131         $this->value = $value;
132     }
133
134     /**
135      * Return the numeric value held.
136      *
137      * @return int|array this will either be a single number if there is
138      *         only one component, or an array of numbers otherwise.
139      */
140     public function getValue()
141     {
142         if ($this->components == 1) {
143             return $this->value[0];
144         } else {
145             return $this->value;
146         }
147     }
148
149     /**
150      * Validate a number.
151      *
152      * This method will check that the number given is within the range
153      * given my {@link getMin()} and {@link getMax()}, inclusive. If
154      * not, then a {@link PelOverflowException} is thrown.
155      *
156      * @param
157      *            int|array the number in question.
158      *
159      * @return void nothing, but will throw a {@link
160      *         PelOverflowException} if the number is found to be outside the
161      *         legal range and {@link Pel::$strict} is true.
162      */
163     public function validateNumber($n)
164     {
165         if ($this->dimension == 1) {
166             if ($n < $this->min || $n > $this->max) {
167                 Pel::maybeThrow(new PelOverflowException($n, $this->min, $this->max));
168             }
169         } else {
170             for ($i = 0; $i < $this->dimension; $i ++) {
171                 if ($n[$i] < $this->min || $n[$i] > $this->max) {
172                     Pel::maybeThrow(new PelOverflowException($n[$i], $this->min, $this->max));
173                 }
174             }
175         }
176     }
177
178     /**
179      * Add a number.
180      *
181      * This appends a number to the numbers already held by this entry,
182      * thereby increasing the number of components by one.
183      *
184      * @param
185      *            int|array the number to be added.
186      */
187     public function addNumber($n)
188     {
189         $this->validateNumber($n);
190         $this->value[] = $n;
191         $this->components ++;
192     }
193
194     /**
195      * Convert a number into bytes.
196      *
197      * The concrete subclasses will have to implement this method so
198      * that the numbers represented can be turned into bytes.
199      *
200      * The method will be called once for each number held by the entry.
201      *
202      * @param
203      *            int the number that should be converted.
204      *
205      * @param
206      *            PelByteOrder one of {@link PelConvert::LITTLE_ENDIAN} and
207      *            {@link PelConvert::BIG_ENDIAN}, specifying the target byte order.
208      *
209      * @return string bytes representing the number given.
210      */
211     abstract public function numberToBytes($number, $order);
212
213     /**
214      * Turn this entry into bytes.
215      *
216      * @param
217      *            PelByteOrder the desired byte order, which must be either
218      *            {@link PelConvert::LITTLE_ENDIAN} or {@link
219      *            PelConvert::BIG_ENDIAN}.
220      *
221      * @return string bytes representing this entry.
222      */
223     public function getBytes($o)
224     {
225         $bytes = '';
226         for ($i = 0; $i < $this->components; $i ++) {
227             if ($this->dimension == 1) {
228                 $bytes .= $this->numberToBytes($this->value[$i], $o);
229             } else {
230                 for ($j = 0; $j < $this->dimension; $j ++) {
231                     $bytes .= $this->numberToBytes($this->value[$i][$j], $o);
232                 }
233             }
234         }
235         return $bytes;
236     }
237
238     /**
239      * Format a number.
240      *
241      * This method is called by {@link getText} to format numbers.
242      * Subclasses should override this method if they need more
243      * sophisticated behavior than the default, which is to just return
244      * the number as is.
245      *
246      * @param
247      *            int the number which will be formatted.
248      *
249      * @param
250      *            boolean it could be that there is both a verbose and a
251      *            brief formatting available, and this argument controls that.
252      *
253      * @return string the number formatted as a string suitable for
254      *         display.
255      */
256     public function formatNumber($number, $brief = false)
257     {
258         return $number;
259     }
260
261     /**
262      * Get the numeric value of this entry as text.
263      *
264      * @param
265      *            boolean use brief output? The numbers will be separated
266      *            by a single space if brief output is requested, otherwise a space
267      *            and a comma will be used.
268      *
269      * @return string the numbers(s) held by this entry.
270      */
271     public function getText($brief = false)
272     {
273         if ($this->components == 0) {
274             return '';
275         }
276
277         $str = $this->formatNumber($this->value[0]);
278         for ($i = 1; $i < $this->components; $i ++) {
279             $str .= ($brief ? ' ' : ', ');
280             $str .= $this->formatNumber($this->value[$i]);
281         }
282
283         return $str;
284     }
285 }