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.
8 * Copyright (C) 2004, 2005, 2006 Martin Geisler.
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.
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.
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
25 namespace lsolesen\pel;
28 * Abstract class for numbers.
30 * @author Martin Geisler <mgeisler@users.sourceforge.net>
31 * @license http://www.gnu.org/licenses/gpl.html GNU General Public
37 * Class for holding numbers.
39 * This class can hold numbers, with range checks.
41 * @author Martin Geisler <mgeisler@users.sourceforge.net>
44 abstract class PelEntryNumber extends PelEntry
48 * The value held by this entry.
52 protected $value = array();
55 * The minimum allowed value.
57 * Any attempt to change the value below this variable will result
58 * in a {@link PelOverflowException} being thrown.
65 * The maximum allowed value.
67 * Any attempt to change the value over this variable will result in
68 * a {@link PelOverflowException} being thrown.
75 * The dimension of the number held.
77 * Normal numbers have a dimension of one, pairs have a dimension of
82 protected $dimension = 1;
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.
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.
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.
104 public function setValue($value)
106 $value = func_get_args();
107 $this->setValueArray($value);
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.
119 * array the new values. The array must contain the new
124 public function setValueArray($value)
126 foreach ($value as $v) {
127 $this->validateNumber($v);
130 $this->components = count($value);
131 $this->value = $value;
135 * Return the numeric value held.
137 * @return int|array this will either be a single number if there is
138 * only one component, or an array of numbers otherwise.
140 public function getValue()
142 if ($this->components == 1) {
143 return $this->value[0];
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.
157 * int|array the number in question.
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.
163 public function validateNumber($n)
165 if ($this->dimension == 1) {
166 if ($n < $this->min || $n > $this->max) {
167 Pel::maybeThrow(new PelOverflowException($n, $this->min, $this->max));
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));
181 * This appends a number to the numbers already held by this entry,
182 * thereby increasing the number of components by one.
185 * int|array the number to be added.
187 public function addNumber($n)
189 $this->validateNumber($n);
191 $this->components ++;
195 * Convert a number into bytes.
197 * The concrete subclasses will have to implement this method so
198 * that the numbers represented can be turned into bytes.
200 * The method will be called once for each number held by the entry.
203 * int the number that should be converted.
206 * PelByteOrder one of {@link PelConvert::LITTLE_ENDIAN} and
207 * {@link PelConvert::BIG_ENDIAN}, specifying the target byte order.
209 * @return string bytes representing the number given.
211 abstract public function numberToBytes($number, $order);
214 * Turn this entry into bytes.
217 * PelByteOrder the desired byte order, which must be either
218 * {@link PelConvert::LITTLE_ENDIAN} or {@link
219 * PelConvert::BIG_ENDIAN}.
221 * @return string bytes representing this entry.
223 public function getBytes($o)
226 for ($i = 0; $i < $this->components; $i ++) {
227 if ($this->dimension == 1) {
228 $bytes .= $this->numberToBytes($this->value[$i], $o);
230 for ($j = 0; $j < $this->dimension; $j ++) {
231 $bytes .= $this->numberToBytes($this->value[$i][$j], $o);
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
247 * int the number which will be formatted.
250 * boolean it could be that there is both a verbose and a
251 * brief formatting available, and this argument controls that.
253 * @return string the number formatted as a string suitable for
256 public function formatNumber($number, $brief = false)
262 * Get the numeric value of this entry as text.
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.
269 * @return string the numbers(s) held by this entry.
271 public function getText($brief = false)
273 if ($this->components == 0) {
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]);