cvs: pear /PHP_ArrayOf package.xml /PHP_ArrayOf/PHP ArrayOf.php ArrayOfInterface.php /PHP_ArrayOf/PHP/ArrayOf Exception.php /PHP_ArrayOf/docs license.txt /PHP_ArrayOf/docs/examples example1.php /PHP_ArrayOf/tests AcceptValue.phpt ArrayAccess_Brackets.phpt ArrayAccess_Methods.phpt ArrayOfAnything.inc ArrayOfInteger.inc ArrayOfString.inc Countable.phpt IteratorAggregate.phpt RejectValue.phpt __construct_ArrayObject.phpt __construct_array.phpt __construct_self.phpt __construct_subclass.phpt

[email protected] ("Philippe Jausions") Tue, 06 May 2008 17:25:04 -0000
Newsgroups php.pear.cvs
Message-ID <cvsjausions1210094704@cvsserver>
jausions		Tue May  6 17:25:04 2008 UTC

  Added files:                 
    /pear/PHP_ArrayOf/PHP/ArrayOf	Exception.php 
    /pear/PHP_ArrayOf/tests	__construct_subclass.phpt 
                           	__construct_array.phpt ArrayOfAnything.inc 
                           	ArrayAccess_Brackets.phpt Countable.phpt 
                           	__construct_self.phpt 
                           	IteratorAggregate.phpt ArrayOfInteger.inc 
                           	ArrayOfString.inc ArrayAccess_Methods.phpt 
                           	AcceptValue.phpt RejectValue.phpt 
                           	__construct_ArrayObject.phpt 
    /pear/PHP_ArrayOf/docs	license.txt 
    /pear/PHP_ArrayOf/docs/examples	example1.php 
    /pear/PHP_ArrayOf/PHP	ArrayOfInterface.php ArrayOf.php 
    /pear/PHP_ArrayOf	package.xml 
  Log:
  First commit
jausions-20080506172504.txt (text/plain, 51.2 KB)
http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/PHP/ArrayOf/Exception.php?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/PHP/ArrayOf/Exception.php
+++ pear/PHP_ArrayOf/PHP/ArrayOf/Exception.php
<?php

/**
 * Exception bundle
 *
 * PHP version 5
 *
 * LICENSE:
 *
 * Copyright (c) 2007-2008, Philippe Jausions / 11abacus
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of the 11abacus nor the names of its contributors may
 *     be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight (c) 2007-2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  CVS: $Id: Exception.php,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Require base exception class
 */
require_once 'PEAR/Exception.php';

/**
 * Base exception class for PHP_ArrayOf package
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight (c) 2007-2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  Release: @package_version@
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */
class PHP_ArrayOf_Exception extends PEAR_Exception
{
}

/**
 * Exception class thrown when argument passed is not an array
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight (c) 2007-2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  Release: @package_version@
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */
class PHP_ArrayOf_Exception_NotAnArray
extends PHP_ArrayOf_Exception
{
}

/**
 * Exception class thrown when array passed doesn't match the type accepted
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight (c) 2007-2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  Release: @package_version@
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */
class PHP_ArrayOf_Exception_WrongTypeOfArray
extends PHP_ArrayOf_Exception
{
}

/**
 * Exception class thrown when argument doesn't match the type accepted
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight (c) 2007-2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  Release: @package_version@
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */
class PHP_ArrayOf_Exception_WrongTypeOfElement
extends PHP_ArrayOf_Exception
{
}

/**
 * Exception class thrown when trying to set or access an unavailable offset
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight (c) 2007-2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  Release: @package_version@
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */
class PHP_ArrayOf_Exception_OffsetNotAvailable
extends PHP_ArrayOf_Exception
{
}

?>
http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/tests/__construct_subclass.phpt?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/tests/__construct_subclass.phpt
+++ pear/PHP_ArrayOf/tests/__construct_subclass.phpt
--TEST--
Initialize with subclass
--FILE--
<?php
/**
 * Checks class accepts an instance of the class itself
 *
 * PHP version 5
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight 2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  CVS: $Id: __construct_subclass.phpt,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Basic test class
 */
require 'ArrayOfAnything.inc';

/**
 * Main
 */

class PHP_ArrayOf_Anything2 extends PHP_ArrayOf_Anything
{
}
$ao = new PHP_ArrayOf_Anything2();

try {
    $instance = new PHP_ArrayOf_Anything($ao);
    echo "Empty subclass: OK\n";
} catch (Exception $e) {
    echo "Empty subclass: FAILED (".$e->getMessage().")\n";
}

$ao = new PHP_ArrayOf_Anything2(array('A', 'B', 1, false, null));
try {
    $instance = new PHP_ArrayOf_Anything($ao);
    echo "Populated subclass: OK\n";
} catch (Exception $e) {
    echo "Populated subclass: FAILED\n";
}

?>
--EXPECT--
Empty subclass: OK
Populated subclass: OK

http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/tests/__construct_array.phpt?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/tests/__construct_array.phpt
+++ pear/PHP_ArrayOf/tests/__construct_array.phpt
--TEST--
Initialize with regular PHP array
--FILE--
<?php
/**
 * Checks class accepts a regular array value
 *
 * PHP version 5
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight 2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  CVS: $Id: __construct_array.phpt,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Basic test class
 */
require 'ArrayOfAnything.inc';

/**
 * Main
 */

try {
    $instance = new PHP_ArrayOf_Anything(array());
    echo "Empty array: OK\n";
} catch (Exception $e) {
    echo "Empty array: FAILED\n";
}

try {
    $instance = new PHP_ArrayOf_Anything(array('A', 'B', 1, false, null));
    echo "Populated array: OK\n";
} catch (Exception $e) {
    echo "Populated array: FAILED\n";
}

?>
--EXPECT--
Empty array: OK
Populated array: OK

http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/tests/ArrayOfAnything.inc?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/tests/ArrayOfAnything.inc
+++ pear/PHP_ArrayOf/tests/ArrayOfAnything.inc
<?php
/**
 * Basic test class.
 *
 * PHP version 5
 *
 * @category   PHP
 * @package    PHP_ArrayOf
 * @subpackage Tests
 * @author     Philippe Jausions <[email protected]>
 * @copyight   2008 by Philippe Jausions / 11abacus
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    CVS: $Id: ArrayOfAnything.inc,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link       http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Required package to test
 */
error_reporting(E_ALL);
require_once 'PHP/ArrayOf.php';

/**
 * Basic test class.
 *
 * This class accepts anything
 *
 * @category   PHP
 * @package    PHP_ArrayOf
 * @subpackage Tests
 * @author     Philippe Jausions <[email protected]>
 * @copyight   2008 by Philippe Jausions / 11abacus
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link       http://pear.php.net/package/PHP_ArrayOf
 */
class PHP_ArrayOf_Anything extends PHP_ArrayOf
{
}

?>
http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/tests/ArrayAccess_Brackets.phpt?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/tests/ArrayAccess_Brackets.phpt
+++ pear/PHP_ArrayOf/tests/ArrayAccess_Brackets.phpt
--TEST--
ArrayAccess Interface (brackets)
--FILE--
<?php
/**
 * Checks class implements the ArrayAccess properly
 *
 * This tests the [] access style of the ArrayAccess interface
 *
 * PHP version 5
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight 2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  CVS: $Id: ArrayAccess_Brackets.phpt,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Basic test class
 */
require 'ArrayOfAnything.inc';

/**
 * Main
 */

$instance = new PHP_ArrayOf_Anything();

echo 'Index 0 exists:                       ';
var_dump(isset($instance[0]));

echo 'Set value at index 0, index exists:   ';
$instance[0] = 'a';
var_dump(isset($instance[0]));

echo 'Value at index 0:                     ';
var_dump($instance[0]);

echo 'Unset value at index 0, index exists: ';
unset($instance[0]);
var_dump(isset($instance[0]));

for ($i = 0; $i < 10; ++$i) {
    $instance[$i] = chr(65 + $i);
}

for ($i = 0; $i < 10; ++$i) {
    echo 'Value at index '.$i.':                     ';
    var_dump($instance[$i]);
}

for ($i = 3; $i < 6; ++$i) {
    unset($instance[$i]);
}
echo 'Unset value at index 3, index exists: ';
var_dump(isset($instance[3]));
echo 'Unset value at index 4, index exists: ';
var_dump(isset($instance[4]));
echo 'Unset value at index 5, index exists: ';
var_dump(isset($instance[5]));

for ($i = 0; $i < 3; ++$i) {
    echo 'Value at index '.$i.':                     ';
    var_dump($instance[$i]);
}
for ($i = 6; $i < 10; ++$i) {
    echo 'Value at index '.$i.':                     ';
    var_dump($instance[$i]);
}

echo 'Append value. Value at index 10:      ';
$instance[] = 'Z';
var_dump($instance[10]);

?>
--EXPECT--
Index 0 exists:                       bool(false)
Set value at index 0, index exists:   bool(true)
Value at index 0:                     string(1) "a"
Unset value at index 0, index exists: bool(false)
Value at index 0:                     string(1) "A"
Value at index 1:                     string(1) "B"
Value at index 2:                     string(1) "C"
Value at index 3:                     string(1) "D"
Value at index 4:                     string(1) "E"
Value at index 5:                     string(1) "F"
Value at index 6:                     string(1) "G"
Value at index 7:                     string(1) "H"
Value at index 8:                     string(1) "I"
Value at index 9:                     string(1) "J"
Unset value at index 3, index exists: bool(false)
Unset value at index 4, index exists: bool(false)
Unset value at index 5, index exists: bool(false)
Value at index 0:                     string(1) "A"
Value at index 1:                     string(1) "B"
Value at index 2:                     string(1) "C"
Value at index 6:                     string(1) "G"
Value at index 7:                     string(1) "H"
Value at index 8:                     string(1) "I"
Value at index 9:                     string(1) "J"
Append value. Value at index 10:      string(1) "Z"

http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/tests/Countable.phpt?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/tests/Countable.phpt
+++ pear/PHP_ArrayOf/tests/Countable.phpt
--TEST--
Countable Interface
--FILE--
<?php
/**
 * Checks count() function can be called
 *
 * PHP version 5
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight 2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  CVS: $Id: Countable.phpt,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Basic test class
 */
require 'ArrayOfAnything.inc';

/**
 * Main
 */

$instance = new PHP_ArrayOf_Anything();

echo count($instance)."\n";
for ($i = 0; $i < 4; ++$i) {
    $instance->offsetSet($i, 'a');
    echo count($instance)."\n";
}

$instance->offsetUnset(2);
echo count($instance)."\n";

?>
--EXPECT--
0
1
2
3
4
3
http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/tests/__construct_self.phpt?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/tests/__construct_self.phpt
+++ pear/PHP_ArrayOf/tests/__construct_self.phpt
--TEST--
Initialize with same class as class itself
--FILE--
<?php
/**
 * Checks class accepts an instance of the class itself
 *
 * PHP version 5
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight 2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  CVS: $Id: __construct_self.phpt,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Basic test class
 */
require 'ArrayOfAnything.inc';

/**
 * Main
 */

$ao = new PHP_ArrayOf_Anything();

try {
    $instance = new PHP_ArrayOf_Anything($ao);
    echo "Empty self class: OK\n";
} catch (Exception $e) {
    echo "Empty self class: FAILED (".$e->getMessage().")\n";
}

$ao = new PHP_ArrayOf_Anything(array('A', 'B', 1, false, null));
try {
    $instance = new PHP_ArrayOf_Anything($ao);
    echo "Populated self class: OK\n";
} catch (Exception $e) {
    echo "Populated self class: FAILED\n";
}

?>
--EXPECT--
Empty self class: OK
Populated self class: OK

http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/tests/IteratorAggregate.phpt?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/tests/IteratorAggregate.phpt
+++ pear/PHP_ArrayOf/tests/IteratorAggregate.phpt
--TEST--
IteratorAggregate Interface
--FILE--
<?php
/**
 * Checks class implements the IteratorAggregate properly
 *
 * This tests the access methods of the IteratorAggregate interface
 *
 * PHP version 5
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight 2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  CVS: $Id: IteratorAggregate.phpt,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Basic test class
 */
require 'ArrayOfAnything.inc';

/**
 * Main
 */

$instance = new PHP_ArrayOf_Anything(array('A', 'B', 1, false, null));

foreach ($instance as $i => $v) {
    echo 'Value at index '.$i.': ';
    var_dump($v);
}

?>
--EXPECT--
Value at index 0: string(1) "A"
Value at index 1: string(1) "B"
Value at index 2: int(1)
Value at index 3: bool(false)
Value at index 4: NULL

http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/tests/ArrayOfInteger.inc?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/tests/ArrayOfInteger.inc
+++ pear/PHP_ArrayOf/tests/ArrayOfInteger.inc
<?php
/**
 * Basic test class.
 *
 * PHP version 5
 *
 * @category   PHP
 * @package    PHP_ArrayOf
 * @subpackage Tests
 * @author     Philippe Jausions <[email protected]>
 * @copyight   2008 by Philippe Jausions / 11abacus
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    CVS: $Id: ArrayOfInteger.inc,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link       http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Required package to test
 */
error_reporting(E_ALL);
require_once 'PHP/ArrayOf.php';

/**
 * Basic test class.
 *
 * This class is supposed to only accept PHP integers
 *
 * @category   PHP
 * @package    PHP_ArrayOf
 * @subpackage Tests
 * @author     Philippe Jausions <[email protected]>
 * @copyight   2008 by Philippe Jausions / 11abacus
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link       http://pear.php.net/package/PHP_ArrayOf
 */
class PHP_ArrayOf_Integer extends PHP_ArrayOf
{
    /**
     * Checks whether a value is suitable for the array
     *
     * @param mixed $value value to check
     *
     * @return boolean TRUE if valid, FALSE otherwise
     * @access protected
     */
    public function isValidElement($value)
    {
        return is_int($value);
    }
}

?>
http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/tests/ArrayOfString.inc?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/tests/ArrayOfString.inc
+++ pear/PHP_ArrayOf/tests/ArrayOfString.inc
<?php
/**
 * Basic test class.
 *
 * PHP version 5
 *
 * @category   PHP
 * @package    PHP_ArrayOf
 * @subpackage Tests
 * @author     Philippe Jausions <[email protected]>
 * @copyight   2008 by Philippe Jausions / 11abacus
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    CVS: $Id: ArrayOfString.inc,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link       http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Required package to test
 */
error_reporting(E_ALL);
require_once 'PHP/ArrayOf.php';

/**
 * Basic test class.
 *
 * This class is supposed to only accept PHP strings
 *
 * @category   PHP
 * @package    PHP_ArrayOf
 * @subpackage Tests
 * @author     Philippe Jausions <[email protected]>
 * @copyight   2008 by Philippe Jausions / 11abacus
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link       http://pear.php.net/package/PHP_ArrayOf
 */
class PHP_ArrayOf_String extends PHP_ArrayOf
{
    /**
     * Checks whether a value is suitable for the array
     *
     * @param mixed $value value to check
     *
     * @return boolean TRUE if valid, FALSE otherwise
     * @access protected
     */
    public function isValidElement($value)
    {
        return is_string($value);
    }
}

?>
http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/tests/ArrayAccess_Methods.phpt?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/tests/ArrayAccess_Methods.phpt
+++ pear/PHP_ArrayOf/tests/ArrayAccess_Methods.phpt
--TEST--
ArrayAccess Interface (methods)
--FILE--
<?php
/**
 * Checks class implements the ArrayAccess properly
 *
 * This tests the access methods of the ArrayAccess interface
 *
 * PHP version 5
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight 2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  CVS: $Id: ArrayAccess_Methods.phpt,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Basic test class
 */
require 'ArrayOfAnything.inc';

/**
 * Main
 */

$instance = new PHP_ArrayOf_Anything();

echo 'Index 0 exists:                       ';
var_dump($instance->offsetExists(0));

echo 'Set value at index 0, index exists:   ';
$instance->offsetSet(0, 'a');
var_dump($instance->offsetExists(0));

echo 'Value at index 0:                     ';
var_dump($instance->offsetGet(0));

echo 'Unset value at index 0, index exists: ';
$instance->offsetUnset(0);
var_dump($instance->offsetExists(0));

for ($i = 0; $i < 10; ++$i) {
    $instance->offsetSet($i, chr(65 + $i));
}

for ($i = 0; $i < 10; ++$i) {
    echo 'Value at index '.$i.':                     ';
    var_dump($instance->offsetGet($i));
}

for ($i = 3; $i < 6; ++$i) {
    $instance->offsetUnset($i);
}
echo 'Unset value at index 3, index exists: ';
var_dump($instance->offsetExists(3));
echo 'Unset value at index 4, index exists: ';
var_dump($instance->offsetExists(4));
echo 'Unset value at index 5, index exists: ';
var_dump($instance->offsetExists(5));

for ($i = 0; $i < 3; ++$i) {
    echo 'Value at index '.$i.':                     ';
    var_dump($instance->offsetGet($i));
}
for ($i = 6; $i < 10; ++$i) {
    echo 'Value at index '.$i.':                     ';
    var_dump($instance->offsetGet($i));
}

echo 'Append value. Value at index 10:      ';
$instance->append('Z');
var_dump($instance->offsetGet(10));

?>
--EXPECT--
Index 0 exists:                       bool(false)
Set value at index 0, index exists:   bool(true)
Value at index 0:                     string(1) "a"
Unset value at index 0, index exists: bool(false)
Value at index 0:                     string(1) "A"
Value at index 1:                     string(1) "B"
Value at index 2:                     string(1) "C"
Value at index 3:                     string(1) "D"
Value at index 4:                     string(1) "E"
Value at index 5:                     string(1) "F"
Value at index 6:                     string(1) "G"
Value at index 7:                     string(1) "H"
Value at index 8:                     string(1) "I"
Value at index 9:                     string(1) "J"
Unset value at index 3, index exists: bool(false)
Unset value at index 4, index exists: bool(false)
Unset value at index 5, index exists: bool(false)
Value at index 0:                     string(1) "A"
Value at index 1:                     string(1) "B"
Value at index 2:                     string(1) "C"
Value at index 6:                     string(1) "G"
Value at index 7:                     string(1) "H"
Value at index 8:                     string(1) "I"
Value at index 9:                     string(1) "J"
Append value. Value at index 10:      string(1) "Z"

http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/tests/AcceptValue.phpt?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/tests/AcceptValue.phpt
+++ pear/PHP_ArrayOf/tests/AcceptValue.phpt
--TEST--
Accept acceptable values
--FILE--
<?php
/**
 * Checks class accepts acceptable values
 *
 * PHP version 5
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight 2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  CVS: $Id: AcceptValue.phpt,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Basic test class
 */
require 'ArrayOfString.inc';
require 'ArrayOfAnything.inc';

/**
 * Main
 */

$instance = new PHP_ArrayOf_String();

echo 'Append value: ';
try {
    $instance[] = 'a';
    echo "OK\n";
} catch (Exception $e) {
    echo "FAILED (".$e->getMessage().")\n";
}

echo 'Modify index: ';
try {
    $instance[0] = 'b';
    echo "OK\n";
} catch (Exception $e) {
    echo "FAILED (".$e->getMessage().")\n";
}

$data = array('a', 'b');
echo 'Init with array: ';
try {
    $instance = new PHP_ArrayOf_String($data);
    echo "OK\n";
} catch (Exception $e) {
    echo "FAILED (".$e->getMessage().")\n";
}

$data = new ArrayObject(array('a', 'b'));
echo 'Init with ArrayObject: ';
try {
    $instance = new PHP_ArrayOf_String($data);
    echo "OK\n";
} catch (Exception $e) {
    echo "FAILED (".$e->getMessage().")\n";
}

$data = new PHP_ArrayOf_String(array('a', 'b'));
echo 'Init with self class: ';
try {
    $instance = new PHP_ArrayOf_String($data);
    echo "OK\n";
} catch (Exception $e) {
    echo "FAILED (".$e->getMessage().")\n";
}

$data = new PHP_ArrayOf_Anything(array('a', 'b'));
echo 'Init with base class: ';
try {
    $instance = new PHP_ArrayOf_String($data);
    echo "OK\n";
} catch (Exception $e) {
    echo "FAILED (".$e->getMessage().")\n";
}

?>
--EXPECT--
Append value: OK
Modify index: OK
Init with array: OK
Init with ArrayObject: OK
Init with self class: OK
Init with base class: OK
http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/tests/RejectValue.phpt?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/tests/RejectValue.phpt
+++ pear/PHP_ArrayOf/tests/RejectValue.phpt
--TEST--
Reject unacceptable values
--FILE--
<?php
/**
 * Checks class rejects unacceptable values
 *
 * PHP version 5
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight 2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  CVS: $Id: RejectValue.phpt,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Basic test class
 */
require 'ArrayOfString.inc';
require 'ArrayOfAnything.inc';

/**
 * Main
 */

$instance = new PHP_ArrayOf_String();

echo 'Append value: ';
try {
    $instance[] = 3;
    echo "FAILED (Value was accepted)\n";
} catch (PHP_ArrayOf_Exception_WrongTypeOfElement $e) {
    echo "OK\n";
} catch (Exception $e) {
    echo "FAILED (".$e->getMessage().")\n";
}

$instance[0] = 'a';
echo 'Modify index: ';
try {
    $instance[0] = 3;
    echo "FAILED (Value was accepted)\n";
} catch (PHP_ArrayOf_Exception_WrongTypeOfElement $e) {
    echo "OK\n";
} catch (Exception $e) {
    echo "FAILED (".$e->getMessage().")\n";
}

$data = array('a', 1);
echo 'Init with array: ';
try {
    $instance = new PHP_ArrayOf_String($data);
    echo "FAILED (Value was accepted)\n";
} catch (PHP_ArrayOf_Exception_WrongTypeOfElement $e) {
    echo "OK\n";
} catch (Exception $e) {
    echo "FAILED (".$e->getMessage().")\n";
}

$data = new ArrayObject(array('a', 1));
echo 'Init with ArrayObject: ';
try {
    $instance = new PHP_ArrayOf_String($data);
    echo "FAILED (Value was accepted)\n";
} catch (PHP_ArrayOf_Exception_WrongTypeOfElement $e) {
    echo "OK\n";
} catch (Exception $e) {
    echo "FAILED (".$e->getMessage().")\n";
}

$data = new PHP_ArrayOf_Anything(array('a', 1));
echo 'Init with base class: ';
try {
    $instance = new PHP_ArrayOf_String($data);
    echo "FAILED (Value was accepted)\n";
} catch (PHP_ArrayOf_Exception_WrongTypeOfElement $e) {
    echo "OK\n";
} catch (Exception $e) {
    echo "FAILED (".$e->getMessage().")\n";
}

?>
--EXPECT--
Append value: OK
Modify index: OK
Init with array: OK
Init with ArrayObject: OK
Init with base class: OK
http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/tests/__construct_ArrayObject.phpt?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/tests/__construct_ArrayObject.phpt
+++ pear/PHP_ArrayOf/tests/__construct_ArrayObject.phpt
--TEST--
Initialize with SPL ArrayObject
--FILE--
<?php
/**
 * Checks class accepts a SPL ArrayObject instance
 *
 * PHP version 5
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight 2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  CVS: $Id: __construct_ArrayObject.phpt,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Basic test class
 */
require 'ArrayOfAnything.inc';

/**
 * Main
 */

$ao = new ArrayObject();

try {
    $instance = new PHP_ArrayOf_Anything($ao);
    echo "Empty ArrayObject: OK\n";
} catch (Exception $e) {
    echo "Empty ArrayObject: FAILED (".$e->getMessage().")\n";
}

$ao = new ArrayObject(array('A', 'B', 1, false, null));
try {
    $instance = new PHP_ArrayOf_Anything($ao);
    echo "Populated ArrayObject: OK\n";
} catch (Exception $e) {
    echo "Populated ArrayObject: FAILED\n";
}

?>
--EXPECT--
Empty ArrayObject: OK
Populated ArrayObject: OK

http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/docs/license.txt?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/docs/license.txt
+++ pear/PHP_ArrayOf/docs/license.txt
LICENSE:

Copyright (c) 2007-2008, Philippe Jausions / 11abacus

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
  - Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
  - Neither the name of the 11abacus nor the names of its contributors may
    be used to endorse or promote products derived from this software
    without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/docs/examples/example1.php?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/docs/examples/example1.php
+++ pear/PHP_ArrayOf/docs/examples/example1.php
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Example usage of PHP_ArrayOf package
 *
 * PHP version 5
 *
 * LICENSE:
 *
 * Copyright (c) 2007-2008, Philippe Jausions / 11abacus
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of the 11abacus nor the names of its contributors may
 *     be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @category   PHP
 * @package    PHP_ArrayOf
 * @subpackage Documentation
 * @author     Philippe Jausions <[email protected]>
 * @copyight   (c) 2007-2008 by Philippe Jausions / 11abacus
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    CVS: $Id: example1.php,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link       http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Requires base class
 */
require_once 'PHP/ArrayOf.php';


/**
 * Some simple classes for the examples
 *
 * @ignore
 */
class A
{
}

/**
 * Some simple classes for the examples
 *
 * @ignore
 */
class B
{
}

$a = new A();
$b = new B();

/**
 * An array class that only accepts instances of class A
 *
 * @ignore
 */
class ArrayOfA extends PHP_ArrayOf
{
    /**
     * Checks whether a value is suitable for the array
     *
     * @param mixed $value  value to check
     * @param mixed $offset offset the value would be stored at
     *
     * @return boolean TRUE if valid, FALSE otherwise
     * @access protected
     */
    public function isValidElement($value, $offset = null)
    {
        return ($value instanceof A);
    }
}

$aOfA   = new ArrayOfA();
$aOfA[] = $a;

try {
    $aOfA[] = $b;
} catch (PHP_ArrayOf_Exception $e) {
    echo '['.__LINE__.'] '.$e->getMessage()."\r\n";
}

/**
 * Nested array of ArrayOfA. This array accepts instances of A and ArrayOfA.
 * Normal PHP arrays are automatically converted to instances of NestedArrayOfA
 * therefore enforcing that the array only contains instances of A or ArrayOfA
 *
 * @ignore
 */
class NestedArrayOfA extends PHP_ArrayOf
{
    /**
     * Checks whether a value is suitable for the array
     *
     * @param mixed $value  value to check
     * @param mixed $offset offset the value would be stored at
     *
     * @return boolean TRUE if valid, FALSE otherwise
     * @access protected
     */
    public function isValidElement($value, $offset = null)
    {
        return ($value instanceof A || $value instanceof self);
    }

    /**
     * Converts an offset/value pair
     *
     * @param mixed $offset offset to convert
     * @param mixed $value  value to convert
     *
     * @return array 2-element array [new offset to use, new value to use]
     * @access public
     */
    public function convertOffsetValue($offset, $value)
    {
        if (is_array($value)) {
            $value = new self($value);
        }
        return array($offset, $value);
    }
}

$nOfA2 = new NestedArrayOfA(array(array($a), $a));

$nOfA = new NestedArrayOfA();
echo '['.__LINE__.'] ';
print_r($nOfA);

$nOfA->exchangeArray($nOfA2);

try {
    $nOfA[0][] = $b;
} catch (PHP_ArrayOf_Exception $e) {
    echo '['.__LINE__.'] '.$e->getMessage()."\r\n";
}
$nOfA[0][] = $a;

$nOfA[] = array();

echo '['.__LINE__.'] ';
print_r($nOfA);

/**
 * An array class that only accept 3 elements maximum
 *
 * @ignore
 */
class Array3Max extends PHP_ArrayOf
{
    /**
     * Checks if an element can be set at the offset
     *
     * @param mixed $offset offset to use
     * @param mixed $value  value that would be set
     *
     * @return boolean TRUE if can be set, FALSE otherwise
     * @access public
     */
    public function offsetAvailable($offset, $value)
    {
        return array_key_exists($offset, $this->data)
               || (count($this->data) < 3);
    }
}

$m = new Array3Max();

$m['a'] = 'apple';
$m['o'] = 'orange';
$m['b'] = 'banana';
try {
    $m['p'] = 'pear';
} catch (PHP_ArrayOf_Exception $e) {
    echo '['.__LINE__.'] '.$e->getMessage()."\r\n";
}

?>

http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/PHP/ArrayOfInterface.php?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/PHP/ArrayOfInterface.php
+++ pear/PHP_ArrayOf/PHP/ArrayOfInterface.php
<?php

/**
 * Basic array class with restrictions on elements type and/or count
 *
 * PHP version 5
 *
 * LICENSE:
 *
 * Copyright (c) 2007-2008, Philippe Jausions / 11abacus
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of the 11abacus nor the names of its contributors may
 *     be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight (c) 2007-2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  CVS: $Id: ArrayOfInterface.php,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Interface declaration
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight (c) 2007-2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  Release: @package_version@
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */
interface PHP_ArrayOfInterface
extends ArrayAccess, Countable, IteratorAggregate
{
    /**
     * Checks whether a value is suitable for the array
     *
     * @param mixed $value value to check
     *
     * @return boolean TRUE if valid, FALSE otherwise
     * @access protected
     */
    public function isValidElement($value);

    /**
     * Checks if an element can be set at the offset
     *
     * @param mixed $offset offset to use
     * @param mixed $value  value that would be set
     *
     * @return boolean TRUE if can be set, FALSE otherwise
     * @access public
     */
    public function offsetAvailable($offset, $value);

    /**
     * Converts an offset/value pair
     *
     * @param mixed $offset offset to convert
     * @param mixed $value  value to convert
     *
     * @return array 2-element array [new offset to use, new value to use]
     * @access public
     */
    public function convertOffsetValue($offset, $value);
}

?>
http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/PHP/ArrayOf.php?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/PHP/ArrayOf.php
+++ pear/PHP_ArrayOf/PHP/ArrayOf.php
<?php

/**
 * Basic array class
 *
 * PHP version 5
 *
 * LICENSE:
 *
 * Copyright (c) 2007-2008, Philippe Jausions / 11abacus
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of the 11abacus nor the names of its contributors may
 *     be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight (c) 2007-2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  CVS: $Id: ArrayOf.php,v 1.1 2008/05/06 17:25:03 jausions Exp $
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */

/**
 * Get interface declaration and exceptions
 */
require_once 'PHP/ArrayOfInterface.php';
require_once 'PHP/ArrayOf/Exception.php';

/**
 * Basic array class
 *
 * @category PHP
 * @package  PHP_ArrayOf
 * @author   Philippe Jausions <[email protected]>
 * @copyight (c) 2007-2008 by Philippe Jausions / 11abacus
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version  Release: @package_version@
 * @link     http://pear.php.net/package/PHP_ArrayOf
 */
abstract class PHP_ArrayOf implements PHP_ArrayOfInterface
{
    /**
     * Iterator
     *
     * @var Iterator
     * @access private
     */
    private $_iterator;

    /**
     * Iterator class
     *
     * @var string
     * @access private
     */
    private $_iteratorClass;

    /**
     * To hold array data
     *
     * @var array
     * @access protected
     */
    protected $data = array();

    /**
     * Class constructor
     *
     * @param array  $array         initialization array
     * @param string $iteratorClass Iterator class to use
     *
     * @throws PHP_ArrayOf_Exception
     */
    public function __construct($array = null, $iteratorClass = 'ArrayIterator')
    {
        if ($array === null) {
            $array = array();
        }
        $this->exchangeArray($array);
        $this->_iteratorClass = $iteratorClass;
    }

    /**
     * Appends an element to the array
     *
     * @param mixed $value value to append
     *
     * @return void
     * @throws PHP_ArrayOf_Exception
     */
    public function append($value)
    {
        $this->offsetSet(null, $value);
    }

    /**
     * Merges arrays to the current object
     *
     * @param array $array1 First array to merge
     * @param array $array2 Second array to merge
     * @param array ...     Etc
     *
     * @return void
     * @throws PHP_ArrayOf_Exception
     */
    public function merge()
    {
        $args = func_get_args();
        foreach ($args as &$array) {
            foreach ($array as $i => &$v) {
                if (ctype_digit((string)$i)) {
                    $this[] = $v;
                } else {
                    $this->offsetSet($i, $v);
                }
            }
        }
    }

    /**
     * Returns iterator for the object
     *
     * @return Iterator
     */
    public function getIterator()
    {
        if (!isset($this->_iterator)) {
            $this->_iterator = new $this->_iteratorClass($this->data);
        }
        return $this->_iterator;
    }

    /**
     * Returns iterator class name for the object
     *
     * @return string
     */
    public function getIteratorClass()
    {
        return $this->_iteratorClass;
    }

    /**
     * Sets the iterator class name for the object
     *
     * @param string $iteratorClass new class name
     *
     * @return void
     */
    public function setIteratorClass($iteratorClass)
    {
        $this->_iteratorClass = $iteratorClass;
        unset($this->_iterator);
    }

    /**
     * Returns whether the offset exists
     *
     * @param mixed $offset to check
     *
     * @return boolean whether the offset exists
     */
    public function offsetExists($offset)
    {
        return array_key_exists($offset, $this->data);
    }

    /**
     * Returns the element for the offset
     *
     * @param mixed $offset to retrieve
     *
     * @return mixed value at given offset
     * @throws PHP_ArrayOf_Exception
     */
    public function offsetGet($offset)
    {
        if (array_key_exists($offset, $this->data)) {
            return $this->data[$offset];
        }

        throw new PHP_ArrayOf_Exception_OffsetNotAvailable('Undefined "'
                                                      .$offset.'" offset');
    }

    /**
     * Sets the element for the offset
     *
     * @param mixed $offset to modify
     * @param mixed $value  new value
     *
     * @return void
     * @throws PHP_ArrayOf_Exception
     */
    public function offsetSet($offset, $value)
    {
        $originalOffset = $offset;

        list($offset, $value) = $this->convertOffsetValue($offset, $value);

        if (!$this->isValidElement($value, $offset)) {
            throw new PHP_ArrayOf_Exception_WrongTypeOfElement('Element is not of a valid type');
        }
        if (!$this->offsetAvailable($offset, $value)) {
            throw new PHP_ArrayOf_Exception_OffsetNotAvailable('Element cannot be put at offset ['.$originalOffset.']');
        }
        if ($offset === null) {
            $this->data[] = $value;
        } else {
            $this->data[$offset] = $value;
        }
    }

    /**
     * Deletes the element for the offset
     *
     * @param mixed $offset to delete
     *
     * @return void
     */
    public function offsetUnset($offset)
    {
        unset($this->data[$offset]);
    }

    /**
     * Checks whether a value is suitable for the array
     *
     * @param mixed $value  value to check
     * @param mixed $offset offset the value would be stored at
     *
     * @return boolean TRUE if valid, FALSE otherwise
     * @access protected
     */
    public function isValidElement($value, $offset = null)
    {
        return true;
    }

    /**
     * Checks if an element can be set at the offset
     *
     * @param mixed $offset offset to use
     * @param mixed $value  value that would be set
     *
     * @return boolean TRUE if can be set, FALSE otherwise
     */
    public function offsetAvailable($offset, $value)
    {
        return true;
    }

    /**
     * Converts an offset/value pair
     *
     * @param mixed $offset offset to convert
     * @param mixed $value  value to convert
     *
     * @return array 2-element array [new offset to use, new value to use]
     * @throws PHP_ArrayOf_Exception
     */
    public function convertOffsetValue($offset, $value)
    {
        return array($offset, $value);
    }

    /**
     * Returns the count of elements
     *
     * @return integer
     */
    public function count()
    {
        return count($this->data);
    }

    /**
     * Sets a whole new array replacement
     *
     * @param array|PHP_ArrayOfInterface|ArrayObject $array New array
     *
     * @return void
     * @throws PHP_ArrayOf_Exception
     */
    public function exchangeArray($array)
    {
        $class = get_class($this);
        $error = 'Argument passed is neither a standard array, a '.$class
                 .', an ArrayObject, nor a PHP_ArrayOfInterface instance';

        if (is_array($array)) {
            $array = $this->_verifyArray($array);

        } elseif (is_object($array)) {
            if ($array instanceof ArrayObject) {
                $array = $this->_verifyArray($array);

            } elseif ($array instanceof $class) {
                $array = $array->data;

            } elseif ($array instanceof PHP_ArrayOfInterface) {
                $array = $this->_verifyArray($array);

            } else {
                throw new PHP_ArrayOf_Exception_WrongTypeOfArray($error);
            }

        } else {
            throw new PHP_ArrayOf_Exception_NotAnArray($error);
        }
        $this->data = $array;
    }

    /**
     * Verifies an array is suitable for the class
     *
     * @param array $array array to verify
     *
     * @return array
     */
    private function _verifyArray($array)
    {
        if (count($array) === 0) {
            return $array;
        }
        $class = get_class($this);
        $data  = new $class();
        foreach ($array as $offset => $value) {
            $data[$offset] = $value;
        }
        return $data->data;
    }
}

?>
http://cvs.php.net/viewvc.cgi/pear/PHP_ArrayOf/package.xml?view=markup&rev=1.1
Index: pear/PHP_ArrayOf/package.xml
+++ pear/PHP_ArrayOf/package.xml
<?xml version="1.0" encoding="UTF-8"?>
<package version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0
http://pear.php.net/dtd/tasks-1.0.xsd
http://pear.php.net/dtd/package-2.0
http://pear.php.net/dtd/package-2.0.xsd">
 <name>PHP_ArrayOf</name>
 <channel>pear.php.net</channel>
 <summary>Abstract class package to create arrays of specific element types</summary>
 <description>Allows to create classes similar to ArrayObject with additional element type or index constraints.
 </description>
 <lead>
  <name>Philippe Jausions</name>
  <user>jausions</user>
  <email>[email protected]</email>
  <active>yes</active>
 </lead>
 <date>2008-05-06</date>
 <time>13:08:01</time>
 <version>
  <release>0.2.1</release>
  <api>0.1.0</api>
 </version>
 <stability>
  <release>alpha</release>
  <api>alpha</api>
 </stability>
 <license uri="http://www.opensource.org/licenses/bsd-license.php">New BSD License</license>
 <notes>
- First release as PEAR package
 </notes>
 <contents>
  <dir name="/">
   <dir name="PHP">
    <file name="ArrayOf.php" role="php">
     <tasks:replace from="@package_version@" to="version" type="package-info"/>
    </file>
    <file name="ArrayOfInterface.php" role="php">
     <tasks:replace from="@package_version@" to="version" type="package-info"/>
    </file>
    <dir name="ArrayOf">
     <file name="Exception.php" role="php">
      <tasks:replace from="@package_version@" to="version" type="package-info"/>
     </file>
    </dir> <!-- /PHP/ArrayOf -->
   </dir> <!-- /PHP -->
   <dir name="docs">
    <file name="license.txt" role="doc" />
    <dir name="examples">
     <file name="example1.php" role="doc" />
    </dir> <!-- /PHP/ArrayOf/docs/examples -->
   </dir> <!-- /PHP/ArrayOf/docs -->
   <dir name="tests">
    <file name="__construct_array.phpt" role="test" />
    <file name="__construct_ArrayObject.phpt" role="test" />
    <file name="__construct_self.phpt" role="test" />
    <file name="__construct_subclass.phpt" role="test" />
    <file name="AcceptValue.phpt" role="test" />
    <file name="ArrayAccess_Brackets.phpt" role="test" />
    <file name="ArrayAccess_Methods.phpt" role="test" />
    <file name="ArrayOfAnything.inc" role="test" />
    <file name="ArrayOfInteger.inc" role="test" />
    <file name="ArrayOfString.inc" role="test" />
    <file name="Countable.phpt" role="test" />
    <file name="IteratorAggregate.phpt" role="test" />
    <file name="RejectValue.phpt" role="test" />
   </dir> <!-- /PHP/ArrayOf/tests -->
  </dir> <!-- / -->
 </contents>
 <dependencies>
  <required>
   <php>
    <min>5.0.0</min>
   </php>
   <pearinstaller>
    <min>1.6.2</min>
   </pearinstaller>
  </required>
 </dependencies>
 <phprelease />
 <changelog>
  <release>
   <version>
    <release>0.2.1</release>
    <api>0.1.0</api>
   </version>
   <stability>
    <release>alpha</release>
    <api>alpha</api>
   </stability>
   <date>2008-05-06</date>
   <license uri="http://www.opensource.org/licenses/bsd-license.php">New BSD License</license>
   <notes>
- First release as PEAR package 
   </notes>
  </release>
  <release>
   <version>
    <release>0.2.0</release>
    <api>0.1.0</api>
   </version>
   <stability>
    <release>alpha</release>
    <api>alpha</api>
   </stability>
   <date>2008-04-15</date>
   <license uri="http://www.opensource.org/licenses/bsd-license.php">New BSD License</license>
   <notes>
- Added PHPT regression tests
- Fixed accepting SPL ArrayObjects
- Updated license links
   </notes>
  </release>
  <release>
   <version>
    <release>0.1.0</release>
    <api>0.1.0</api>
   </version>
   <stability>
    <release>alpha</release>
    <api>alpha</api>
   </stability>
   <date>2007-10-15</date>
   <license uri="http://www.11abacus.com/license/NewBSD.php">New BSD License</license>
   <notes>This is the first release for peer-review.
   </notes>
  </release>
 </changelog>
</package>