Source for file PelEntryNumber.php

Documentation is available at PelEntryNumber.php

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

SourceForge.net Logo Documentation generated on Wed, 21 Jul 2004 19:13:02 +0200 by phpDocumentor 1.3.0RC3