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

Documentation generated on Tue, 19 Dec 2006 01:08:20 +0100 by phpDocumentor 1.3.0 SourceForge.net Logo