add ArrayObject
This is a pure PHP implementation of ArrayObject. It does't match the reference semantics of zend's implementation, but it is much easier to build this in pure PHP, so its a good first step. It is better to have it like this, than to not have it at all.
Esse commit está contido em:
@@ -0,0 +1,458 @@
|
||||
<?php
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* ( excerpt from http://php.net/manual/en/class.arrayobject.php )
|
||||
*
|
||||
* This class allows objects to work as arrays.
|
||||
*
|
||||
*/
|
||||
class ArrayObject implements IteratorAggregate, Traversable, ArrayAccess,
|
||||
Serializable, Countable {
|
||||
|
||||
const integer STD_PROP_LIST = 1;
|
||||
const integer ARRAY_AS_PROPS = 2;
|
||||
|
||||
private $storage;
|
||||
private $flags;
|
||||
private $iteratorClass;
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* ( excerpt from http://php.net/manual/en/arrayobject.construct.php )
|
||||
*
|
||||
* This constructs a new array object.
|
||||
*
|
||||
* @input mixed The input parameter accepts an array or an Object.
|
||||
* @flags mixed Flags to control the behaviour of the ArrayObject
|
||||
* object. See ArrayObject::setFlags().
|
||||
* @iterator_class
|
||||
* mixed Specify the class that will be used for iteration of
|
||||
* the ArrayObject object.
|
||||
*
|
||||
* @return mixed Returns an ArrayObject object on success.
|
||||
*/
|
||||
public function __construct($input = array(),
|
||||
int $flags = 0,
|
||||
string $iterator_class = "ArrayIterator") {
|
||||
$this->storage = $input;
|
||||
$this->flags = $flags;
|
||||
$this->iteratorClass = $iterator_class;
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* ( excerpt from http://php.net/manual/en/arrayobject.append.php )
|
||||
*
|
||||
* Appends a new value as the last element. Note:
|
||||
*
|
||||
* This method cannot be called when the ArrayObject was constructed from
|
||||
* an object. Use ArrayObject::offsetSet() instead.
|
||||
*
|
||||
* @value mixed The value being appended.
|
||||
*
|
||||
* @return mixed No value is returned.
|
||||
*/
|
||||
public function append($value) {
|
||||
if (!$this->isArray()) {
|
||||
throw new Exception(
|
||||
'Cannot append properties to objects, '.
|
||||
'use ArrayObject::offsetSet() instead'
|
||||
);
|
||||
}
|
||||
$this->storage[] = $value;
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.asort.php )
|
||||
*
|
||||
* Sorts the entries such that the keys maintain their correlation with
|
||||
* the entries they are associated with. This is used mainly when sorting
|
||||
* associative arrays where the actual element order is significant.
|
||||
*
|
||||
* @return mixed No value is returned.
|
||||
*/
|
||||
public function asort() {
|
||||
return asort($this->storage);
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.count.php )
|
||||
*
|
||||
* Get the number of public properties in the ArrayObject.
|
||||
*
|
||||
* @return mixed The number of public properties in the ArrayObject.
|
||||
* Note:
|
||||
*
|
||||
* When the ArrayObject is constructed from an array
|
||||
* all properties are public.
|
||||
*/
|
||||
public function count() {
|
||||
return count($this->storage);
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.exchangearray.php )
|
||||
*
|
||||
* Exchange the current array with another array or object.
|
||||
*
|
||||
* @input mixed The new array or object to exchange with the current
|
||||
* array.
|
||||
*
|
||||
* @return mixed Returns the old array.
|
||||
*/
|
||||
public function exchangeArray($input) {
|
||||
$old = $this->storage;
|
||||
$this->storage = $input;
|
||||
return $old;
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.getarraycopy.php )
|
||||
*
|
||||
* Exports the ArrayObject to an array.
|
||||
*
|
||||
* @return mixed Returns a copy of the array. When the ArrayObject
|
||||
* refers to an object an array of the public
|
||||
* properties of that object will be returned.
|
||||
*/
|
||||
public function getArrayCopy() {
|
||||
return (array) $this->storage;
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.getflags.php )
|
||||
*
|
||||
* Gets the behavior flags of the ArrayObject. See the
|
||||
* ArrayObject::setFlags method for a list of the available flags.
|
||||
*
|
||||
* @return mixed Returns the behavior flags of the ArrayObject.
|
||||
*/
|
||||
public function getFlags() {
|
||||
return $this->flags;
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.getiterator.php )
|
||||
*
|
||||
* Create a new iterator from an ArrayObject instance.
|
||||
*
|
||||
* @return mixed An iterator from an ArrayObject.
|
||||
*/
|
||||
public function getIterator() {
|
||||
$class = $this->iteratorClass;
|
||||
return new $class($this->storage, $this->flags);
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.getiteratorclass.php
|
||||
* )
|
||||
*
|
||||
* Gets the class name of the array iterator that is used by
|
||||
* ArrayObject::getIterator().
|
||||
*
|
||||
* @return mixed Returns the iterator class name that is used to
|
||||
* iterate over this object.
|
||||
*/
|
||||
public function getIteratorClass() {
|
||||
return $this->iteratorClass;
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.ksort.php )
|
||||
*
|
||||
* Sorts the entries by key, maintaining key to entry correlations. This
|
||||
* is useful mainly for associative arrays.
|
||||
*
|
||||
* @return mixed No value is returned.
|
||||
*/
|
||||
public function ksort() {
|
||||
return ksort($this->storage);
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.natcasesort.php )
|
||||
*
|
||||
* This method is a case insensitive version of ArrayObject::natsort.
|
||||
*
|
||||
* This method implements a sort algorithm that orders alphanumeric
|
||||
* strings in the way a human being would while maintaining key/value
|
||||
* associations. This is described as a "natural ordering".
|
||||
*
|
||||
* @return mixed No value is returned.
|
||||
*/
|
||||
public function natcasesort() {
|
||||
return natcasesort($this->storage);
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.natsort.php )
|
||||
*
|
||||
* This method implements a sort algorithm that orders alphanumeric
|
||||
* strings in the way a human being would while maintaining key/value
|
||||
* associations. This is described as a "natural ordering". An example of
|
||||
* the difference between this algorithm and the regular computer string
|
||||
* sorting algorithms(used in ArrayObject::asort) method can be seen in
|
||||
* the example below.
|
||||
*
|
||||
* @return mixed No value is returned.
|
||||
*/
|
||||
public function natsort() {
|
||||
return natsort($this->storage);
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.offsetexists.php )
|
||||
*
|
||||
*
|
||||
* @index mixed The index being checked.
|
||||
*
|
||||
* @return mixed TRUE if the requested index exists, otherwise FALSE
|
||||
*/
|
||||
public function offsetExists($index) {
|
||||
if ($this->isArray()) {
|
||||
return isset($this->storage[$index]);
|
||||
} else {
|
||||
return property_exists($this->storage, $index);
|
||||
}
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.offsetget.php )
|
||||
*
|
||||
*
|
||||
* @index mixed The index with the value.
|
||||
*
|
||||
* @return mixed The value at the specified index or NULL.
|
||||
*/
|
||||
public function offsetGet($index) {
|
||||
if ($this->isArray()) {
|
||||
return $this->storage[$index];
|
||||
} else {
|
||||
$obj = $this->storage;
|
||||
return $obj->$index;
|
||||
}
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.offsetset.php )
|
||||
*
|
||||
* Sets the value at the specified index to newval.
|
||||
*
|
||||
* @index mixed The index being set.
|
||||
* @newval mixed The new value for the index.
|
||||
*
|
||||
* @return mixed No value is returned.
|
||||
*/
|
||||
public function offsetSet($index, $newval) {
|
||||
if ($this->isArray()) {
|
||||
$this->storage[$index] = $newval;
|
||||
} else {
|
||||
$obj = $this->storage;
|
||||
$obj->$index = $newval;
|
||||
}
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.offsetunset.php )
|
||||
*
|
||||
* Unsets the value at the specified index.
|
||||
*
|
||||
* @index mixed The index being unset.
|
||||
*
|
||||
* @return mixed No value is returned.
|
||||
*/
|
||||
public function offsetUnset($index) {
|
||||
if ($this->isArray()) {
|
||||
unset($this->storage[$index]);
|
||||
} else {
|
||||
$obj = $this->storage;
|
||||
unset($obj->$index);
|
||||
}
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.serialize.php )
|
||||
*
|
||||
* Serializes an ArrayObject. WarningThis function is currently not
|
||||
* documented; only its argument list is available.
|
||||
*
|
||||
* @return mixed The serialized representation of the ArrayObject.
|
||||
*/
|
||||
public function serialize() {
|
||||
return serialize(array(
|
||||
'storage' => $this->storage,
|
||||
'flags' => $this->flags,
|
||||
'iteratorClass' => $this->iteratorClass,
|
||||
));
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.setflags.php )
|
||||
*
|
||||
* Set the flags that change the behavior of the ArrayObject.
|
||||
*
|
||||
* @flags mixed The new ArrayObject behavior. It takes on either a
|
||||
* bitmask, or named constants. Using named constants
|
||||
* is strongly encouraged to ensure compatibility for
|
||||
* future versions.
|
||||
*
|
||||
* The available behavior flags are listed below. The
|
||||
* actual meanings of these flags are described in the
|
||||
* predefined constants. ArrayObject behavior flags
|
||||
* value constant 1 ArrayObject::STD_PROP_LIST 2
|
||||
* ArrayObject::ARRAY_AS_PROPS
|
||||
*
|
||||
* @return mixed No value is returned.
|
||||
*/
|
||||
public function setFlags(int $flags) {
|
||||
$this->flags = $flags;
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.setiteratorclass.php
|
||||
* )
|
||||
*
|
||||
* Sets the classname of the array iterator that is used by
|
||||
* ArrayObject::getIterator().
|
||||
*
|
||||
* @iterator_class
|
||||
* mixed The classname of the array iterator to use when
|
||||
* iterating over this object.
|
||||
*
|
||||
* @return mixed No value is returned.
|
||||
*/
|
||||
public function setIteratorClass(string $iterator_class) {
|
||||
$this->iteratorClass = $iterator_class;
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.uasort.php )
|
||||
*
|
||||
* This function sorts the entries such that keys maintain their
|
||||
* correlation with the entry that they are associated with, using a
|
||||
* user-defined comparison function.
|
||||
*
|
||||
* This is used mainly when sorting associative arrays where the actual
|
||||
* element order is significant.
|
||||
*
|
||||
* @cmp_function
|
||||
* mixed Function cmp_function should accept two parameters
|
||||
* which will be filled by pairs of entries. The
|
||||
* comparison function must return an integer less
|
||||
* than, equal to, or greater than zero if the first
|
||||
* argument is considered to be respectively less than,
|
||||
* equal to, or greater than the second.
|
||||
*
|
||||
* @return mixed No value is returned.
|
||||
*/
|
||||
public function uasort($cmp_function) {
|
||||
uasort($this->storage, $cmp_function);
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.uksort.php )
|
||||
*
|
||||
* This function sorts the keys of the entries using a user-supplied
|
||||
* comparison function. The key to entry correlations will be maintained.
|
||||
*
|
||||
* @cmp_function
|
||||
* mixed The callback comparison function.
|
||||
*
|
||||
* Function cmp_function should accept two parameters
|
||||
* which will be filled by pairs of entry keys. The
|
||||
* comparison function must return an integer less
|
||||
* than, equal to, or greater than zero if the first
|
||||
* argument is considered to be respectively less than,
|
||||
* equal to, or greater than the second.
|
||||
*
|
||||
* @return mixed No value is returned.
|
||||
*/
|
||||
public function uksort($cmp_function) {
|
||||
uksort($this->storage, $cmp_function);
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
/**
|
||||
* * excerpt from http://php.net/manual/en/arrayobject.unserialize.php )
|
||||
*
|
||||
* Unserializes a serialized ArrayObject. WarningThis function is
|
||||
* currently not documented; only its argument list is available.
|
||||
*
|
||||
* @serialized mixed The serialized ArrayObject.
|
||||
*
|
||||
* @return mixed The unserialized ArrayObject.
|
||||
*/
|
||||
public function unserialize($serialized) {
|
||||
if (empty($serialized)) {
|
||||
throw new UnexpectedValueException(
|
||||
'Empty serialized string cannot be empty'
|
||||
);
|
||||
}
|
||||
$data = unserialize($serialized);
|
||||
$this->storage = $data['storage'];
|
||||
$this->flags = $data['flags'];
|
||||
$this->iteratorClass = $data['iteratorClass'];
|
||||
}
|
||||
|
||||
public function __set (string $name, $value) {
|
||||
if (!$this->hasProps()) {
|
||||
$this->$name = $value;
|
||||
} else {
|
||||
return $this->offsetSet($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
public function __get (string $name) {
|
||||
if (!$this->hasProps()) {
|
||||
return $this->$name;
|
||||
} else {
|
||||
return $this->offsetGet($name);
|
||||
}
|
||||
}
|
||||
|
||||
public function __isset (string $name) {
|
||||
if (!$this->hasProps()) {
|
||||
return isset($this->$name);
|
||||
} else {
|
||||
return $this->offsetExists($name);
|
||||
}
|
||||
}
|
||||
|
||||
public function __unset (string $name) {
|
||||
if (!$this->hasProps()) {
|
||||
unset($this->$name);
|
||||
} else {
|
||||
return $this->offsetUnset($name);
|
||||
}
|
||||
}
|
||||
|
||||
private function isArray() {
|
||||
return is_array($this->storage);
|
||||
}
|
||||
|
||||
private function hasProps() {
|
||||
return $this->flags & self::ARRAY_AS_PROPS;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -355,8 +355,8 @@ class ArrayIterator implements ArrayAccess, SeekableIterator, Countable {
|
||||
const STD_PROP_LIST = 1;
|
||||
const ARRAY_AS_PROPS = 2;
|
||||
|
||||
public function __construct($array, $flags = 0) {
|
||||
$this->arr = $array;
|
||||
public function __construct($array = array(), $flags = 0) {
|
||||
$this->arr = (array) $array;
|
||||
$this->flags = $flags;
|
||||
reset($this->arr);
|
||||
}
|
||||
@@ -386,7 +386,7 @@ class ArrayIterator implements ArrayAccess, SeekableIterator, Countable {
|
||||
* @return mixed No value is returned.
|
||||
*/
|
||||
public function asort() {
|
||||
return asort($this->arr, $this->flags);
|
||||
return asort($this->arr);
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
@@ -464,7 +464,7 @@ class ArrayIterator implements ArrayAccess, SeekableIterator, Countable {
|
||||
* @return mixed No value is returned.
|
||||
*/
|
||||
public function ksort() {
|
||||
return ksort($this->arr, $this->flags);
|
||||
return ksort($this->arr);
|
||||
}
|
||||
|
||||
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
$arrayobj = new ArrayObject(array('first','second','third'));
|
||||
$arrayobj->append('fourth');
|
||||
$arrayobj->append(array('five', 'six'));
|
||||
var_dump($arrayobj);
|
||||
@@ -0,0 +1,24 @@
|
||||
object(ArrayObject)#1 (3) {
|
||||
["storage":"ArrayObject":private]=>
|
||||
array(5) {
|
||||
[0]=>
|
||||
string(5) "first"
|
||||
[1]=>
|
||||
string(6) "second"
|
||||
[2]=>
|
||||
string(5) "third"
|
||||
[3]=>
|
||||
string(6) "fourth"
|
||||
[4]=>
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(4) "five"
|
||||
[1]=>
|
||||
string(3) "six"
|
||||
}
|
||||
}
|
||||
["flags":"ArrayObject":private]=>
|
||||
int(0)
|
||||
["iteratorClass":"ArrayObject":private]=>
|
||||
string(13) "ArrayIterator"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
$fruits = array(
|
||||
"d" => "lemon",
|
||||
"a" => "orange",
|
||||
"b" => "banana",
|
||||
"c" => "apple"
|
||||
);
|
||||
$fruitArrayObject = new ArrayObject($fruits);
|
||||
$fruitArrayObject->asort();
|
||||
|
||||
foreach ($fruitArrayObject as $key => $val) {
|
||||
echo "$key = $val\n";
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
c = apple
|
||||
b = banana
|
||||
d = lemon
|
||||
a = orange
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
class Example {
|
||||
public $public = 'prop:public';
|
||||
private $prv = 'prop:private';
|
||||
protected $prt = 'prop:protected';
|
||||
}
|
||||
|
||||
$arrayobj = new ArrayObject(new Example());
|
||||
var_dump($arrayobj->count());
|
||||
|
||||
$arrayobj = new ArrayObject(array('first','second','third'));
|
||||
var_dump($arrayobj->count());
|
||||
@@ -0,0 +1,2 @@
|
||||
int(1)
|
||||
int(3)
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
// Array of available fruits
|
||||
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);
|
||||
// Array of locations in Europe
|
||||
$locations = array('Amsterdam', 'Paris', 'London');
|
||||
|
||||
$fruitsArrayObject = new ArrayObject($fruits);
|
||||
|
||||
// Now exchange fruits for locations
|
||||
$old = $fruitsArrayObject->exchangeArray($locations);
|
||||
print_r($old);
|
||||
print_r($fruitsArrayObject);
|
||||
@@ -0,0 +1,19 @@
|
||||
Array
|
||||
(
|
||||
[lemons] => 1
|
||||
[oranges] => 4
|
||||
[bananas] => 5
|
||||
[apples] => 10
|
||||
)
|
||||
ArrayObject Object
|
||||
(
|
||||
[storage:ArrayObject:private] => Array
|
||||
(
|
||||
[0] => Amsterdam
|
||||
[1] => Paris
|
||||
[2] => London
|
||||
)
|
||||
|
||||
[flags:ArrayObject:private] => 0
|
||||
[iteratorClass:ArrayObject:private] => ArrayIterator
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
// Array of available fruits
|
||||
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);
|
||||
|
||||
$fruitsArrayObject = new ArrayObject($fruits);
|
||||
$fruitsArrayObject['pears'] = 4;
|
||||
|
||||
// create a copy of the array
|
||||
$copy = $fruitsArrayObject->getArrayCopy();
|
||||
print_r($copy);
|
||||
@@ -0,0 +1,8 @@
|
||||
Array
|
||||
(
|
||||
[lemons] => 1
|
||||
[oranges] => 4
|
||||
[bananas] => 5
|
||||
[apples] => 10
|
||||
[pears] => 4
|
||||
)
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
// Array of available fruits
|
||||
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);
|
||||
|
||||
$fruitsArrayObject = new ArrayObject($fruits);
|
||||
|
||||
// Get the current flags
|
||||
$flags = $fruitsArrayObject->getFlags();
|
||||
var_dump($flags);
|
||||
|
||||
// Set new flags
|
||||
$fruitsArrayObject->setFlags(ArrayObject::ARRAY_AS_PROPS);
|
||||
|
||||
// Get the new flags
|
||||
$flags = $fruitsArrayObject->getFlags();
|
||||
var_dump($flags);
|
||||
@@ -0,0 +1,2 @@
|
||||
int(0)
|
||||
int(2)
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
$array = array('1' => 'one',
|
||||
'2' => 'two',
|
||||
'3' => 'three');
|
||||
|
||||
$arrayobject = new ArrayObject($array);
|
||||
|
||||
$iterator = $arrayobject->getIterator();
|
||||
|
||||
while($iterator->valid()) {
|
||||
echo $iterator->key() . ' => ' . $iterator->current() . "\n";
|
||||
|
||||
$iterator->next();
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
1 => one
|
||||
2 => two
|
||||
3 => three
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
// Custom ArrayIterator (inherits from ArrayIterator)
|
||||
class MyArrayIterator extends ArrayIterator {
|
||||
// custom implementation
|
||||
}
|
||||
|
||||
// Array of available fruits
|
||||
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);
|
||||
|
||||
$fruitsArrayObject = new ArrayObject($fruits);
|
||||
|
||||
// Get the current class name
|
||||
$className = $fruitsArrayObject->getIteratorClass();
|
||||
var_dump($className);
|
||||
|
||||
// Set new classname
|
||||
$fruitsArrayObject->setIteratorClass('MyArrayIterator');
|
||||
|
||||
// Get the new iterator classname
|
||||
$className = $fruitsArrayObject->getIteratorClass();
|
||||
var_dump($className);
|
||||
@@ -0,0 +1,2 @@
|
||||
string(13) "ArrayIterator"
|
||||
string(15) "MyArrayIterator"
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
$fruits = array(
|
||||
"d" => "lemon",
|
||||
"a" => "orange",
|
||||
"b" => "banana",
|
||||
"c" => "apple"
|
||||
);
|
||||
$fruitArrayObject = new ArrayObject($fruits);
|
||||
$fruitArrayObject->ksort();
|
||||
|
||||
foreach ($fruitArrayObject as $key => $val) {
|
||||
echo "$key = $val\n";
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
a = orange
|
||||
b = banana
|
||||
c = apple
|
||||
d = lemon
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
$array = array(
|
||||
'IMG0.png',
|
||||
'img12.png',
|
||||
'img10.png',
|
||||
'img2.png',
|
||||
'img1.png',
|
||||
'IMG3.png'
|
||||
);
|
||||
|
||||
$arr1 = new ArrayObject($array);
|
||||
$arr2 = clone $arr1;
|
||||
|
||||
$arr1->asort();
|
||||
echo "Standard sorting\n";
|
||||
print_r($arr1);
|
||||
|
||||
$arr2->natcasesort();
|
||||
echo "\nNatural order sorting (case-insensitive)\n";
|
||||
print_r($arr2);
|
||||
@@ -0,0 +1,33 @@
|
||||
Standard sorting
|
||||
ArrayObject Object
|
||||
(
|
||||
[storage:ArrayObject:private] => Array
|
||||
(
|
||||
[0] => IMG0.png
|
||||
[5] => IMG3.png
|
||||
[4] => img1.png
|
||||
[2] => img10.png
|
||||
[1] => img12.png
|
||||
[3] => img2.png
|
||||
)
|
||||
|
||||
[flags:ArrayObject:private] => 0
|
||||
[iteratorClass:ArrayObject:private] => ArrayIterator
|
||||
)
|
||||
|
||||
Natural order sorting (case-insensitive)
|
||||
ArrayObject Object
|
||||
(
|
||||
[storage:ArrayObject:private] => Array
|
||||
(
|
||||
[0] => IMG0.png
|
||||
[4] => img1.png
|
||||
[3] => img2.png
|
||||
[5] => IMG3.png
|
||||
[2] => img10.png
|
||||
[1] => img12.png
|
||||
)
|
||||
|
||||
[flags:ArrayObject:private] => 0
|
||||
[iteratorClass:ArrayObject:private] => ArrayIterator
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
$array = array("img12.png", "img10.png", "img2.png", "img1.png");
|
||||
|
||||
$arr1 = new ArrayObject($array);
|
||||
$arr2 = clone $arr1;
|
||||
|
||||
$arr1->asort();
|
||||
echo "Standard sorting\n";
|
||||
print_r($arr1);
|
||||
|
||||
$arr2->natsort();
|
||||
echo "\nNatural order sorting\n";
|
||||
print_r($arr2);
|
||||
@@ -0,0 +1,29 @@
|
||||
Standard sorting
|
||||
ArrayObject Object
|
||||
(
|
||||
[storage:ArrayObject:private] => Array
|
||||
(
|
||||
[3] => img1.png
|
||||
[1] => img10.png
|
||||
[0] => img12.png
|
||||
[2] => img2.png
|
||||
)
|
||||
|
||||
[flags:ArrayObject:private] => 0
|
||||
[iteratorClass:ArrayObject:private] => ArrayIterator
|
||||
)
|
||||
|
||||
Natural order sorting
|
||||
ArrayObject Object
|
||||
(
|
||||
[storage:ArrayObject:private] => Array
|
||||
(
|
||||
[3] => img1.png
|
||||
[2] => img2.png
|
||||
[1] => img10.png
|
||||
[0] => img12.png
|
||||
)
|
||||
|
||||
[flags:ArrayObject:private] => 0
|
||||
[iteratorClass:ArrayObject:private] => ArrayIterator
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
$arrayobj = new ArrayObject(array('zero', 'one', 'example'=>'e.g.'));
|
||||
var_dump($arrayobj->offsetExists(1));
|
||||
var_dump($arrayobj->offsetExists('example'));
|
||||
var_dump($arrayobj->offsetExists('notfound'));
|
||||
@@ -0,0 +1,3 @@
|
||||
bool(true)
|
||||
bool(true)
|
||||
bool(false)
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
$arrayobj = new ArrayObject(array('zero', 7, 'example'=>'e.g.'));
|
||||
var_dump($arrayobj->offsetGet(1));
|
||||
var_dump($arrayobj->offsetGet('example'));
|
||||
var_dump($arrayobj->offsetExists('notfound'));
|
||||
@@ -0,0 +1,3 @@
|
||||
int(7)
|
||||
string(4) "e.g."
|
||||
bool(false)
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
class Example {
|
||||
public $property = 'prop:public';
|
||||
}
|
||||
$arrayobj = new ArrayObject(new Example());
|
||||
$arrayobj->offsetSet(4, 'four');
|
||||
$arrayobj->offsetSet('group', array('g1', 'g2'));
|
||||
var_dump($arrayobj);
|
||||
|
||||
$arrayobj = new ArrayObject(array('zero','one'));
|
||||
$arrayobj->offsetSet(null, 'last');
|
||||
var_dump($arrayobj);
|
||||
@@ -0,0 +1,35 @@
|
||||
object(ArrayObject)#1 (3) {
|
||||
["storage":"ArrayObject":private]=>
|
||||
object(Example)#2 (3) {
|
||||
["property"]=>
|
||||
string(11) "prop:public"
|
||||
["4"]=>
|
||||
string(4) "four"
|
||||
["group"]=>
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(2) "g1"
|
||||
[1]=>
|
||||
string(2) "g2"
|
||||
}
|
||||
}
|
||||
["flags":"ArrayObject":private]=>
|
||||
int(0)
|
||||
["iteratorClass":"ArrayObject":private]=>
|
||||
string(13) "ArrayIterator"
|
||||
}
|
||||
object(ArrayObject)#3 (3) {
|
||||
["storage":"ArrayObject":private]=>
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(4) "zero"
|
||||
[1]=>
|
||||
string(3) "one"
|
||||
[""]=>
|
||||
string(4) "last"
|
||||
}
|
||||
["flags":"ArrayObject":private]=>
|
||||
int(0)
|
||||
["iteratorClass":"ArrayObject":private]=>
|
||||
string(13) "ArrayIterator"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
$arrayobj = new ArrayObject(array(0=>'zero',2=>'two'));
|
||||
$arrayobj->offsetUnset(2);
|
||||
var_dump($arrayobj);
|
||||
@@ -0,0 +1,11 @@
|
||||
object(ArrayObject)#1 (3) {
|
||||
["storage":"ArrayObject":private]=>
|
||||
array(1) {
|
||||
[0]=>
|
||||
string(4) "zero"
|
||||
}
|
||||
["flags":"ArrayObject":private]=>
|
||||
int(0)
|
||||
["iteratorClass":"ArrayObject":private]=>
|
||||
string(13) "ArrayIterator"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
$o = new ArrayObject();
|
||||
|
||||
$s1 = serialize($o);
|
||||
$s2 = $o->serialize();
|
||||
|
||||
var_dump($s1);
|
||||
var_dump($s2);
|
||||
@@ -0,0 +1,2 @@
|
||||
string(108) "C:11:"ArrayObject":84:{a:3:{s:7:"storage";a:0:{}s:5:"flags";i:0;s:13:"iteratorClass";s:13:"ArrayIterator";}}"
|
||||
string(84) "a:3:{s:7:"storage";a:0:{}s:5:"flags";i:0;s:13:"iteratorClass";s:13:"ArrayIterator";}"
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
// Array of available fruits
|
||||
$fruits = array(
|
||||
"lemons" => 1,
|
||||
"oranges" => 4,
|
||||
"bananas" => 5,
|
||||
"apples" => 10
|
||||
);
|
||||
|
||||
$fruitsArrayObject = new ArrayObject($fruits);
|
||||
|
||||
// Try to use array key as property
|
||||
var_dump($fruitsArrayObject->lemons);
|
||||
// Set the flag so that the array keys can be used as properties
|
||||
$fruitsArrayObject->setFlags(ArrayObject::ARRAY_AS_PROPS);
|
||||
// Try it again
|
||||
var_dump($fruitsArrayObject->lemons);
|
||||
@@ -0,0 +1,2 @@
|
||||
NULL
|
||||
int(1)
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
// Custom ArrayIterator (inherits from ArrayIterator)
|
||||
class MyArrayIterator extends ArrayIterator {
|
||||
// custom implementation
|
||||
}
|
||||
|
||||
// Array of available fruits
|
||||
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);
|
||||
|
||||
$fruitsArrayObject = new ArrayObject($fruits);
|
||||
|
||||
// Set the iterator classname to the newly
|
||||
$fruitsArrayObject->setIteratorClass('MyArrayIterator');
|
||||
print_r($fruitsArrayObject->getIterator());
|
||||
@@ -0,0 +1,12 @@
|
||||
MyArrayIterator Object
|
||||
(
|
||||
[arr:protected] => Array
|
||||
(
|
||||
[lemons] => 1
|
||||
[oranges] => 4
|
||||
[bananas] => 5
|
||||
[apples] => 10
|
||||
)
|
||||
|
||||
[flags:protected] => 0
|
||||
)
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
// Comparison function
|
||||
function cmp($a, $b) {
|
||||
if ($a == $b) {
|
||||
return 0;
|
||||
}
|
||||
return ($a < $b) ? -1 : 1;
|
||||
}
|
||||
|
||||
// Array to be sorted
|
||||
$array = array(
|
||||
'a' => 4,
|
||||
'b' => 8,
|
||||
'c' => -1,
|
||||
'd' => -9,
|
||||
'e' => 2,
|
||||
'f' => 5,
|
||||
'g' => 3,
|
||||
'h' => -4,
|
||||
);
|
||||
$arrayObject = new ArrayObject($array);
|
||||
print_r($arrayObject);
|
||||
|
||||
// Sort and print the resulting array
|
||||
$arrayObject->uasort('cmp');
|
||||
print_r($arrayObject);
|
||||
@@ -0,0 +1,34 @@
|
||||
ArrayObject Object
|
||||
(
|
||||
[storage:ArrayObject:private] => Array
|
||||
(
|
||||
[a] => 4
|
||||
[b] => 8
|
||||
[c] => -1
|
||||
[d] => -9
|
||||
[e] => 2
|
||||
[f] => 5
|
||||
[g] => 3
|
||||
[h] => -4
|
||||
)
|
||||
|
||||
[flags:ArrayObject:private] => 0
|
||||
[iteratorClass:ArrayObject:private] => ArrayIterator
|
||||
)
|
||||
ArrayObject Object
|
||||
(
|
||||
[storage:ArrayObject:private] => Array
|
||||
(
|
||||
[d] => -9
|
||||
[h] => -4
|
||||
[c] => -1
|
||||
[e] => 2
|
||||
[g] => 3
|
||||
[a] => 4
|
||||
[f] => 5
|
||||
[b] => 8
|
||||
)
|
||||
|
||||
[flags:ArrayObject:private] => 0
|
||||
[iteratorClass:ArrayObject:private] => ArrayIterator
|
||||
)
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
function cmp($a, $b) {
|
||||
$a = preg_replace('@^(a|an|the) @', '', $a);
|
||||
$b = preg_replace('@^(a|an|the) @', '', $b);
|
||||
return strcasecmp($a, $b);
|
||||
}
|
||||
|
||||
$array = array("John" => 1, "the Earth" => 2, "an apple" => 3, "a banana" => 4);
|
||||
$arrayObject = new ArrayObject($array);
|
||||
$arrayObject->uksort('cmp');
|
||||
|
||||
foreach ($arrayObject as $key => $value) {
|
||||
echo "$key: $value\n";
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
an apple: 3
|
||||
a banana: 4
|
||||
the Earth: 2
|
||||
John: 1
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
$a = new ArrayObject(array(1,2,3));
|
||||
$b = $a->serialize();
|
||||
$c = new ArrayObject;
|
||||
$d = $c->unserialize($b);
|
||||
|
||||
var_dump($a);
|
||||
var_dump($c);
|
||||
var_dump($d);
|
||||
var_dump($a == $c);
|
||||
@@ -0,0 +1,32 @@
|
||||
object(ArrayObject)#1 (3) {
|
||||
["storage":"ArrayObject":private]=>
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(3)
|
||||
}
|
||||
["flags":"ArrayObject":private]=>
|
||||
int(0)
|
||||
["iteratorClass":"ArrayObject":private]=>
|
||||
string(13) "ArrayIterator"
|
||||
}
|
||||
object(ArrayObject)#2 (3) {
|
||||
["storage":"ArrayObject":private]=>
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(3)
|
||||
}
|
||||
["flags":"ArrayObject":private]=>
|
||||
int(0)
|
||||
["iteratorClass":"ArrayObject":private]=>
|
||||
string(13) "ArrayIterator"
|
||||
}
|
||||
NULL
|
||||
bool(true)
|
||||
@@ -331,7 +331,7 @@ parser.add_argument(
|
||||
"--only",
|
||||
type=str,
|
||||
action='append',
|
||||
help="only import tests whose path matches this regex."
|
||||
help="only import tests whose path contains this substring."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--dirty",
|
||||
@@ -592,11 +592,11 @@ if args.zend_path:
|
||||
for filename in files:
|
||||
full_file = os.path.join(root, filename)
|
||||
|
||||
def matches(regexes):
|
||||
if not regexes:
|
||||
def matches(patterns):
|
||||
if not patterns:
|
||||
return True
|
||||
for regex in regexes:
|
||||
if re.search(regex, full_file):
|
||||
for pattern in patterns:
|
||||
if pattern in full_file:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
Referência em uma Nova Issue
Bloquear um usuário