Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #82 from lsolesen/clean-up
Browse files Browse the repository at this point in the history
Simplified PelFormat and added tests
  • Loading branch information
weberhofer committed Jan 5, 2017
2 parents 41aef2c + 6c3f861 commit b224117
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 91 deletions.
20 changes: 7 additions & 13 deletions src/PelException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,17 @@
namespace lsolesen\pel;

/**
* Standard PEL exception.
* Standard PEL printf() capable exception.
* This class is a simple extension of the standard Exception class in
* PHP, and all the methods defined there retain their original
* meaning.
*
* @author Martin Geisler <mgeisler@users.sourceforge.net>
* @license http://www.gnu.org/licenses/gpl.html GNU General Public
* License (GPL)
* @package PEL
*/

/**
* A printf() capable exception.
*
* This class is a simple extension of the standard Exception class in
* PHP, and all the methods defined there retain their original
* meaning.
*
* @package PEL
* @subpackage Exception
* @subpackage PelException
*/
class PelException extends \Exception
{
Expand All @@ -55,11 +49,11 @@ class PelException extends \Exception
* arguments will be available for the format string as usual with
* vprintf().
*
* @param mixed $args,...
* @param mixed ...$args
* any number of arguments to be used with
* the format string.
*/
public function __construct()
public function __construct($fmt, $args)
{
$args = func_get_args();
$fmt = array_shift($args);
Expand Down
135 changes: 57 additions & 78 deletions src/PelFormat.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

/**
/*
* PEL: PHP Exif Library.
* A library with support for reading and
* writing all Exif headers in JPEG and TIFF images using PHP.
*
* Copyright (C) 2004, 2005 Martin Geisler.
* Copyright (C) 2017 Johannes Weberhofer.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -24,15 +24,6 @@
*/
namespace lsolesen\pel;

/**
* Namespace for functions operating on Exif formats.
*
* @author Martin Geisler <mgeisler@users.sourceforge.net>
* @license http://www.gnu.org/licenses/gpl.html GNU General Public
* License (GPL)
* @package PEL
*/

/**
* Namespace for functions operating on Exif formats.
*
Expand All @@ -45,7 +36,11 @@
* single argument which should be one of the class constants.
*
* @author Martin Geisler <mgeisler@users.sourceforge.net>
* @package PEL
* @author Johannes Weberhofer <jweberhofer@weberhofer.at>
* @license http://www.gnu.org/licenses/gpl.html GNU General Public
* License (GPL)
* @package
*
*/
class PelFormat
{
Expand Down Expand Up @@ -165,84 +160,68 @@ class PelFormat
const DOUBLE = 12;

/**
* Returns the name of a format.
*
* @param PelFormat $type
* the format.
* Values for format's short names
*/
protected static $formatName = array(
self::ASCII => 'Ascii',
self::BYTE => 'Byte',
self::SHORT => 'Short',
self::LONG => 'Long',
self::RATIONAL => 'Rational',
self::SBYTE => 'SByte',
self::SSHORT => 'SShort',
self::SLONG => 'SLong',
self::SRATIONAL => 'SRational',
self::FLOAT => 'Float',
self::DOUBLE => 'Double',
self::UNDEFINED => 'Undefined'
);

protected static $formatLength = array(
self::ASCII => 1,
self::BYTE => 1,
self::SHORT => 2,
self::LONG => 4,
self::RATIONAL => 8,
self::SBYTE => 1,
self::SSHORT => 2,
self::SLONG => 4,
self::SRATIONAL => 8,
self::FLOAT => 4,
self::DOUBLE => 8,
self::UNDEFINED => 1
);

/**
* Returns the name of a format like 'Ascii' for the {@link ASCII} format
*
* @return string the name of the format, e.g., 'Ascii' for the
* {@link ASCII} format etc.
* @param integer $type
* as defined in {@link PelFormat}
* @return string
*/
public static function getName($type)
{
switch ($type) {
case self::ASCII:
return 'Ascii';
case self::BYTE:
return 'Byte';
case self::SHORT:
return 'Short';
case self::LONG:
return 'Long';
case self::RATIONAL:
return 'Rational';
case self::SBYTE:
return 'SByte';
case self::SSHORT:
return 'SShort';
case self::SLONG:
return 'SLong';
case self::SRATIONAL:
return 'SRational';
case self::FLOAT:
return 'Float';
case self::DOUBLE:
return 'Double';
case self::UNDEFINED:
return 'Undefined';
default:
return Pel::fmt('Unknown format: 0x%X', $type);
if (array_key_exists($type, self::$formatName)) {
return self::$formatName[$type];
} else {
return Pel::fmt('Unknown format: 0x%X', $type);
}
}

/**
* Return the size of components in a given format.
*
* @param PelFormat $type
* the format.
* Return the size of components in a given format in bytes needed to store one component with the
* given format.
*
* @return the size in bytes needed to store one component with the
* given format.
* @param integer $type
* as defined in {@link PelFormat}
* @return integer|string
*/
public static function getSize($type)
{
switch ($type) {
case self::ASCII:
return 1;
case self::BYTE:
return 1;
case self::SHORT:
return 2;
case self::LONG:
return 4;
case self::RATIONAL:
return 8;
case self::SBYTE:
return 1;
case self::SSHORT:
return 2;
case self::SLONG:
return 4;
case self::SRATIONAL:
return 8;
case self::FLOAT:
return 4;
case self::DOUBLE:
return 8;
case self::UNDEFINED:
return 1;
default:
return Pel::fmt('Unknown format: 0x%X', $type);
if (array_key_exists($type, self::$formatLength)) {
return self::$formatLength[$type];
} else {
return Pel::fmt('Unknown format: 0x%X', $type);
}
}
}
50 changes: 50 additions & 0 deletions test/PelFormatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* PEL: PHP Exif Library.
* A library with support for reading and
* writing all Exif headers in JPEG and TIFF images using PHP.
*
* Copyright (C) 2004, 2006 Martin Geisler.
* Copyright (C) 2017 Johannes Weberhofer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in the file COPYING; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
use PHPUnit\Framework\TestCase;
use lsolesen\pel\Pel;
use lsolesen\pel\PelFormat;

class PelFormatTest extends TestCase
{

function testNames()
{
$pelFormat = new PelFormat();
$this->assertEquals($pelFormat::getName(PelFormat::ASCII), 'Ascii');
$this->assertEquals($pelFormat::getName(PelFormat::FLOAT), 'Float');
$this->assertEquals($pelFormat::getName(PelFormat::UNDEFINED), 'Undefined');
$this->assertEquals($pelFormat::getName(100), Pel::fmt('Unknown format: 0x%X', 100));
}

function testDescriptions()
{
$pelFormat = new PelFormat();
$this->assertEquals($pelFormat::getSize(PelFormat::ASCII), 1);
$this->assertEquals($pelFormat::getSize(PelFormat::FLOAT), 4);
$this->assertEquals($pelFormat::getSize(PelFormat::UNDEFINED), 1);
$this->assertEquals($pelFormat::getSize(100), Pel::fmt('Unknown format: 0x%X', 100));
}
}

0 comments on commit b224117

Please sign in to comment.