navcon / utility
此包提供各种PHP接口和类,用于结构化和组织数据以及对象组
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: 4.8.*
This package is auto-updated.
Last update: 2024-08-29 03:36:04 UTC
README
关于
此包提供各种PHP接口和类,用于结构化和组织数据以及对象组。
功能
- 迭代器
- 迭代器感知
- 迭代器反转
- 迭代器限制
- 分页器
- 迭代器装饰器抽象类
- 迭代器装饰器反转
- 迭代器装饰器限制
- 分页器装饰器
- 集合
- 集合装饰器
- 数组列表
- 映射
- 哈希映射
- 树映射
- 集合
- 哈希集合
- 树
- 枚举器
- 组合
致谢
此包受到java.util包的启发。这些概念已经调整以适应PHP世界。
兼容性
- PHP5.3
安装
使用composer安装最新版本
composer require naucon/utility
示例
启动内置的Web服务器以查看示例
cd examples
php -S 127.0.0.1:3000
在浏览器中打开URL
http://127.0.0.1:3000/index.html
基本用法
迭代器
Iterator
扩展 IteratorAbstract
IteratorAbstract
实现 IteratorInterface
IteratorInterface
扩展 Iterator
,Countable
Iterator
类在内部数组中持有数据。可以使用 foreach()
命令迭代该类来从数组中检索数据。它还提供了以下方法来循环和计数数据: isFirst()
,isLast()
,current()
,next()
,hasNext()
,previous()
,hasPrevious()
,first()
,last()
,rewind()
,key()
,indexOf()
,hasIndex()
,setItemPosition()
,count()
。该类实现了PHP的 Iterator
和 Countable
接口。
与 Collection
或 ArrayList
相比,Iterator
只能检索数据,不能添加数据。它也没有对其索引的控制。
$array = array();
$array[] = 'foo';
$array[] = 'bar';
// create instance
use Naucon\Utility\Iterator;
$iteratorObject = new Iterator($array);
// count
echo count($iteratorObject); // output: 2
// iterate
foreach ($iteratorObject as $key => $value) {
echo $value . ' ';
}
// output: foo bar
迭代器感知
IteratorAware
扩展 IteratorAwareAbstract
IteratorAwareAbstract
实现 IteratorAwareInterface
IteratorAwareInterface
扩展 IteratorAggregate
,Countable
IteratorAware
类与 Iterator
类类似。它不是数组,而是在内部 Iterator
实例中持有数据。没有循环数据的方法,可以使用 foreach()
命令迭代该类。要计数数据,它提供了 count()
方法。可以通过 getIterator()
方法访问内部 Iterator
。该类实现了 IteratorAggregate
和 Countable
接口。
// create instance
use Naucon\Utility\IteratorAware;
use Naucon\Utility\Iterator;
$iteratorAwareObject = new IteratorAware(new Iterator(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)));
// count
echo count($iteratorAwareObject); // output: 10
// iterate
foreach ($iteratorAwareObject as $key => $value) {
echo $value . '';
}
// output: 1 2 3 4 5 6 7 8 9 10
迭代器反转
IteratorReverse
扩展 IteratorReverseAbstract
IteratorReverseAbstract
扩展 IteratorAbstract
IteratorAbstract
实现 IteratorInterface
IteratorInterface
扩展 Iterator
,Countable
基于 Iterator
的 IteratorReverse
类反转返回项的顺序。
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// create instance
use Naucon\Utility\IteratorReverse;
$iteratorObject = new IteratorReverse($array);
// count
echo count($iteratorObject); // Output: 10
// iterate
foreach ($iteratorObject as $key => $value) {
echo $value . ' ';
}
// Output: 10 9 8 7 6 5 4 3 2 1
迭代器限制
IteratorLimit
扩展 IteratorLimitAbstract
IteratorLimitAbstract
扩展 IteratorAbstract
实现 IteratorLimitInterface
IteratorAbstract
实现 IteratorInterface
IteratorInterface
扩展 Iterator
,Countable
基于 Iterator
的 IteratorLimit
类通过给定的 offset
和 count
参数检索数据的子集。
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21);
// create instance
use Naucon\Utility\IteratorLimit;
$iteratorObject = new IteratorLimit($array, 10, 10); // offset=10, count=10
// count
echo count($iteratorObject); // Output: 21
// iterate
foreach ($iteratorObject as $key => $value) {
echo $value . ' ';
}
// Output: 11 12 13 14 15 16 17 18 19 20
分页器
分页器
类继承自PaginatorAbstract
类,而PaginatorAbstract
类继承自IteratorLimitAbstract
类,并实现了PaginatorInterface
接口。IteratorLimitAbstract
类继承自IteratorAbstract
类,并实现了IteratorLimitInterface
接口。IteratorAbstract
类实现了IteratorInterface
接口。IteratorInterface
接口继承自Iterator
接口,并实现了Countable
接口。
基于IteratorLimit
类,Paginator
类通过给定的itemsPerPage
参数将数据分页。它还提供了以下方法来控制页面:getCurrentPageNumber()
、setPage()
、nextPage()
、countPages()
、isFirstPage()
、isLastPage()
、hasNextPage()
、hasPreviousPage()
、getNextPageNumber()
和getPreviousPageNumber()
。
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21);
// create instance
use Naucon\Utility\Paginator;
$paginatorObject = new Paginator($array, 10);
// count
echo count($paginatorObject); // Output: 21
// iterate page 1
foreach ($paginatorObject as $key => $value) {
echo $value . ' ';
}
// Output: 1 2 3 4 5 6 7 8 9 10
// next page
$paginatorObject->nextPage();
// iterate page 2
foreach ($paginatorObject as $key => $value) {
echo $value . ' ';
}
// Output: 11 12 13 14 15 16 17 18 19 20
// set a certain page
$paginatorObject->setPage(3);
// iterate page 3
foreach ($paginatorObject as $key => $value) {
echo $value . ' ';
}
// Output: 21
迭代器装饰器抽象类
IteratorDecoratorAbstract
实现了IteratorInterface
接口。IteratorInterface
接口继承自Iterator
接口,并实现了Countable
接口。
IteratorDecoratorAbstract
是一个抽象类。与IteratorAware
类类似,它在内部持有Iterator
实例的数据。与IteratorAware
类不同,它实现了IteratorInterface
接口的每个方法,以确保与普通Iterator
的完全兼容性。
它的目的是在Iterator
和个别实现之间创建一个抽象层。
例如,Paginator
和IteratorLimit
不能修改相同的数据或实例。因为它们是IteratorInterface
接口的个别实现。而装饰器可以修改任何Iterator
实例,次数不限。
正如你所猜到的,它是IteratorDecoratorReverse
、IteratorDecoratorLimit
和PaginatorDecorator
类的基础。
迭代器装饰器反转
IteratorDecoratorReverse
继承自IteratorDecoratorAbstract
类。IteratorDecoratorAbstract
实现了IteratorInterface
接口。IteratorInterface
接口继承自Iterator
接口,并实现了Countable
接口。
IteratorDecoratorReverse
类是任何IteratorInterface
实例的装饰器。装饰器反转返回项的顺序。它具有与IteratorLimit
类相同的方法。
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// create instance
use Naucon\Utility\Iterator;
use Naucon\Utility\IteratorDecoratorReverse;
$iteratorLimitObject = new IteratorDecoratorReverse(new Iterator($array));
迭代器装饰器限制
IteratorDecoratorLimit
继承自IteratorDecoratorAbstract
类。IteratorDecoratorAbstract
实现了IteratorInterface
接口。IteratorInterface
接口继承自Iterator
接口,并实现了Countable
接口。
IteratorDecoratorLimit
类是任何IteratorInterface
实例的装饰器。装饰器通过给定的offset
和count
参数检索数据子集。它具有与IteratorLimit
类相同的方法。
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21);
// create instance
use Naucon\Utility\Iterator;
use Naucon\Utility\IteratorDecoratorLimit;
$iteratorLimitObject = new IteratorDecoratorLimit(new Iterator($array), 0, 10);
分页器装饰器
PaginatorDecorator
继承自IteratorDecoratorLimit
类,并实现了PaginatorInterface
接口。IteratorDecoratorLimit
继承自IteratorDecoratorAbstract
类,而IteratorDecoratorAbstract
实现了IteratorInterface
接口。IteratorInterface
接口继承自Iterator
接口,并实现了Countable
接口。
PaginatorDecorator
类是任何IteratorInterface
实例的装饰器。装饰器通过给定的itemsPerPage
参数将数据分页。它具有与Paginator
类相同的方法。
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21);
// create instance
use Naucon\Utility\Iterator;
use Naucon\Utility\PaginatorDecorator;
$paginatorObject = new PaginatorDecorator(new Iterator($array), 10);
集合
Collection
类继承自CollectionAbstract
类,而CollectionAbstract
类继承自IteratorAwareAbstract
类,并实现了CollectionInterface
接口。IteratorAwareAbstract
实现了IteratorAwareInterface
接口,而IteratorAwareInterface
继承自IteratorAggregate
接口,并实现了Countable
接口。
Collection
类在内部数组中存储、添加和删除数据。它还实现了IteratorAwareInterface
接口,以便通过foreach()
命令迭代数据。
与Iterator
或IteratorAware
不同,Collection
可以检索、添加和删除数据,但它也没有索引控制。
它提供了以下方法来添加和计数数据:add()
、addAll()
、clear()
、contains()
、isEmpty()
、getIterator()
、remove()
、count()
和toArray()
。该类实现了PHP的IteratorAggregate
和Countable
接口。
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// create instance
use Naucon\Utility\Collection;
$collectionObject = new Collection($array);
// count
echo count($collectionObject); // Output: 10
// iterate
foreach ($collectionObject as $key => $value) {
echo $value . ' ';
}
// Output: 1 2 3 4 5 6 7 8 9 10
// dump data to a array
var_dump($collectionObject->toArray());
// Output: array(10) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) [8]=> int(9) [9]=> int(10) }
// contains value of 10
if ($collectionObject->contains(10)) {
echo 'TRUE';
} else {
echo 'FALSE';
}
// Output: TRUE
// contains value of 40
if ($collectionObject->contains(40)) {
echo 'TRUE';
} else {
echo 'FALSE';
}
// Output: FALSE
// add data
$collectionObject->add(11);
// add array of data
$collectionObject->addAll(array(12, 13, 14, 15, 16));
// remove data
$collectionObject->remove(4);
// remove all data
$collectionObject->clear();
集合装饰器
CollectionDecorator
继承自CollectionDecoratorAbstract
类。CollectionDecoratorAbstract
实现了CollectionInterface
接口。CollectionInterface
接口继承自IteratorAwareInterface
接口,而IteratorAwareInterface
继承自IteratorAggregate
接口,并实现了Countable
接口。
CollectionDecorator
是一个抽象类。与IteratorDecoratorAbstract
类类似,它在内部持有Collection
实例的数据。它实现了CollectionInterface
接口的每个方法,以确保与普通Collection
的完全兼容性。
其目的是在Collection
和单个实现之间创建一个抽象层。
数组列表
ArrayList
扩展了ListAbstract
,ListAbstract
扩展了CollectionAbstract
并实现了ListInterface
,CollectionAbstract
扩展了IteratorAwareAbstract
并实现了CollectionInterface
,IteratorAwareAbstract
实现了IteratorAwareInterface
,IteratorAwareInterface
扩展了IteratorAggregate
和Countable
。
ArrayList
类在内部数组中以单独的索引持有、添加和删除数据。此外,它实现了CollectionInterface
,因此也实现了IteratorAwareInterface
,可以通过foreach()
命令迭代数据。
与Collection
不同,它控制索引。
它提供了以下添加和计数数据的方法:add()
、addWithIndex()
、addAll()
、get()
、hasIndex()
、removeIndex()
、set()
、clear()
、contains()
、isEmpty()
、getIterator()
、remove()
、count()
、toArray()
。该类实现了PHP的IteratorAggregate
和Countable
接口。
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// create instance
use Naucon\Utility\ArrayList;
$listObject = new ArrayList($array);
// count
echo count($listObject); // Output: 10
// iterate
foreach ($listObject as $key => $value) {
echo $value . ' ';
}
// Output: 1 2 3 4 5 6 7 8 9 10
// dump data to a array
var_dump($listObject->toArray());
// Output: array(10) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) [8]=> int(9) [9]=> int(10) }
// get entry by index
echo $listObject->get(0); // Output: 1
echo $listObject->get(9); // Output: 10
// add data
$listObject->add(11);
// add array of data
$listObject->addAll(array(12, 13, 14, 15, 16));
// add data with index
$listObject->addWithIndex(16, 17);
$listObject->addWithIndex(17, 18);
$listObject->set(18, 19);
$listObject->set(10, 99);
// remove data by value
$listObject->remove(4);
// remove data by index
$listObject->removeIndex(0);
// remove all data
$listObject->clear();
映射
Map
扩展了MapAbstract
,MapAbstract
实现了MapInterface
,MapInterface
扩展了Countable
。
Map
类在内部数组中持有、添加和删除键值对数据。与Iterator
和Collection
类不同,它不能使用foreach()
命令迭代数据。
Map
中的键只能使用一次。当添加具有现有键的键值对时,它将覆盖现有的键值对。这是与Set
类的主要区别。
它提供了以下添加和删除键值对的方法:hasKey()
、hasValue()
、get()
、getAll()
、set()
、setAll()
、remove()
、clear()
、count()
。该类实现了PHP的Countable
接口。
// create instance
use Naucon\Utility\Map;
$mapObject = new Map();
// key-value pair 1
$mapKey[] = 'KeyA';
$mapValue[] = 'Value A';
// key-value pair 2
$mapKey[] = 'KeyB';
$mapValue[] = 'Value B';
// key-value pair 3
$mapKey[] = 'KeyC';
$mapValue[] = 'Value C';
// add key-value-pairs to map
$mapObject->set($mapKey[0], $mapValue[0]);
$mapObject->set($mapKey[1], $mapValue[1]);
$mapObject->set($mapKey[2], $mapValue[2]);
// get key-value-pairs from map
echo $mapObject->get($mapKey[0]); // Output: Value A
echo $mapObject->get($mapKey[1]); // Output: Value B
echo $mapObject->get($mapKey[2]); // Output: Value C
// count key-value-pairs
echo count($mapObject); // Output: 3
// remove key-value pair with 'Value B'
$mapObject->remove($mapKey[1]);
哈希映射
HashMap
扩展了HashMapAbstract
,HashMapAbstract
扩展了MapAbstract
并实现了HashMapInterface
,MapAbstract
实现了MapInterface
,MapInterface
扩展了Countable
。
基于Map
,HashMap
以与Map
类相同的方式添加和删除键值对。与Map
类不同,HashMap
中的键也可以是对象的实例。HashMap
使用spl_object_hash()
函数将实例转换为哈希键。由于键被转换为哈希,HashMap
不能返回其原始形式(对象)。但可以通过给定的实例添加、删除和返回值。
它提供了以下添加和删除键值对的方法:hasKey()
、hasValue()
、get()
、getAll()
、set()
、setAll()
、remove()
、clear()
、count()
。该类实现了PHP的Countable
接口。
树映射
TreeMap
扩展了TreeMapAbstract
,TreeMapAbstract
扩展了Map
并实现了MapInterface
,Map
扩展了MapAbstract
,MapAbstract
实现了MapInterface
,MapInterface
扩展了Countable
。
TreeMap
类与Map
类类似。它在内部数组中持有、添加和删除键值对数据。
与Map
不同,TreeMap
可以向一个键添加多个值。
它提供了以下添加和删除键值对的方法:hasKey()
、hasValue()
、get()
、getAll()
、set()
、setAll()
、remove()
、clear()
、count()
。该类实现了PHP的Countable
接口。
// create instance
use Naucon\Utility\TreeMap;
$mapObject = new TreeMap();
// key-value pair 1
$mapKey[0] = 'KeyA';
$mapValue[0] = 'Value A';
// key-value pair 2
$mapKey[1] = 'KeyB';
$mapValue[1] = 'Value B';
// key-value pair 3
$mapKey[2] = 'KeyC';
$mapValue[2][0] = 'Value C1';
$mapValue[2][1] = 'Value C2';
$mapValue[2][2] = 'Value C3';
// add key-value pairs to map
$mapObject->set($mapKey[0], $mapValue[0]);
$mapObject->set($mapKey[1], $mapValue[1]);
$mapObject->set($mapKey[2], $mapValue[2][0]);
$mapObject->set($mapKey[2], $mapValue[2][1]);
$mapObject->set($mapKey[2], $mapValue[2][2]);
// get key-value pairs from map
echo $mapObject->get($mapKey[0]); // Output: Value A
echo $mapObject->get($mapKey[1]); // Output: Value B
foreach ($mapObject->get($mapKey[2]) as $value) {
echo $value . ' ';
}
// Output: Value C1 Value C2 Value C3'
// count key-value-pairs
echo count($mapObject); // Output: 3
// remove key-value with 'Value B'
$mapObject->remove($mapKey[1]);
集合
Set
扩展了SetAbstract
,SetAbstract
扩展了CollectionAbstract
,CollectionAbstract
扩展了IteratorAwareAbstract
并实现了CollectionInterface
,IteratorAwareAbstract
实现了IteratorAwareInterface
。
Set
类与Collection
类类似。它在内部数组中持有、添加和删除数据。现有值只能添加一次。数据也可以通过foreach()
命令进行迭代。
它提供了以下检索、添加和删除数据的方法:add()
、addAll()
、clear()
、contains()
、isEmpty()
、getIterator()
、remove()
、count()
、toArray()
。该类实现了PHP的IteratorAggregate
和Countable
接口。
// create instance
use Naucon\Utility\Set;
$setObject = new Set();
$setObject->addAll(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
// count
echo count($setObject); // Output: 10
// iterate
foreach ($setObject as $key => $value) {
echo $value . ' ';
}
// Output: 1 2 3 4 5 6 7 8 9 10
// dump data to a array
var_dump($setObject->toArray());
// Output: array(10) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) [8]=> int(9) [9]=> int(10) }
// contains value of 10
if ($setObject->contains(10)) {
echo 'TRUE';
} else {
echo 'FALSE';
}
// Output: TRUE
// contains value of 40
if ($setObject->contains(40)) {
echo 'TRUE';
} else {
echo 'FALSE';
}
// Output: FALSE
// add data
$setObject->add(11); // return true
// add array of data
$setObject->addAll(array(12, 13, 14, 15, 16));
// add duplicate data // return false
$setObject->add(11);
// remove data
$setObject->remove(4);
// remove all data
$setObject->clear();
哈希集合
HashSet
扩展了HashSetAbstract
,HashSetAbstract
扩展了SetAbstract
并实现了HashSetInterface
,SetAbstract
扩展了CollectionAbstract
,CollectionAbstract
扩展了IteratorAwareAbstract
并实现了CollectionInterface
,IteratorAwareAbstract
实现了IteratorAwareInterface
。
基于Set
所持有的,HashSet
以与Set
类相同的方式添加和删除数据。与Set
类相比,HashSet
中的键是其值的哈希。使用spl_object_hash()
函数将给定值的实例转换为哈希键。
与Set
类一样,现有值只能添加一次。还可以通过foreach()
命令迭代数据。
它提供了以下方法来添加和删除键值对:add()
、addAll()
、clear()
、contains()
、isEmpty()
、getIterator()
、remove()
、count()
、toArray()
。该类实现了PHP的IteratorAggregate
和Countable
接口。
树
Tree
扩展自TreeAbstract
,TreeAbstract
扩展自TreeNodeAbstract
,并实现了TreeInterface
接口。TreeNodeAbstract
实现了TreeNodeInterface
接口。TreeInterface
扩展自TreeNodeInterface
和IteratorAwareInterface
接口,而IteratorAwareInterface
扩展自IteratorAggregate
和Countable
。
Tree
类在内部使用HashSet
存储层次结构数据。可以使用foreach()
命令迭代该类以检索HashSet
中的数据。它还提供了以下方法来检索、添加和删除数据:getIterator()
、hasChilds()
、count()
、add()
、addChild()
、removeChild()
、hasParent()
、getParent()
、removeNode()
、rewind()
、key()
、indexOf()
、hasIndex()
、setItemPosition()
、count()
。该类实现了PHP的IteratorAggregate
和Countable
接口。
与IteratorAware
、Iterator
或Collection
不同,Tree
只与TreeNodeInterface
的实例一起工作。
// create instance
use Naucon\Utility\TreeNodeAbstract;
use Naucon\Utility\TreeInterface;
use Naucon\Utility\TreeAbstract;
use Naucon\Utility\Tree;
$treeRootObject = new Tree();
// declare a simple tree node class
class FooTree extends TreeNodeAbstract
{
protected $value = '';
public function __construct($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
}
// declare another simple tree node class
class BarTree extends TreeAbstract
{
protected $value = '';
public function __construct($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
}
// create instance of tree nodes
$fooObj1 = new \FooTree('A');
$fooObj2 = new \FooTree('B');
// add a nodes A and B to tree
$treeRootObject->add($fooObj1);
$treeRootObject->add($fooObj2);
// add a tree C to tree
$treeChildLevel1Object = $treeRootObject->add(new \BarTree('C')); // add() returns a instance of the `TreeNode`
// add a node C1 to node C
$treeChildLevel1Object->add(new \FooTree('C1'));
// add a node C2 to node C
$treeChildLevel2Object = $treeChildLevel1Object->add(new \BarTree('C2'));
// add a node C21 to node C2
$treeChildLevel2Object->add(new \FooTree('C21'));
// add a node C3 to node C
$treeChildLevel1Object->add(new \FooTree('C3'));
// add a node D to tree
$treeRootObject->add(new \FooTree('D'));
// count
echo count($treeRootObject); // Output: 4
// iterate
foreach ($treeRootObject as $treeNodeObject) {
echo $treeNodeObject->getValue();
echo '<br/>';
if ($treeNodeObject instanceof TreeInterface) {
foreach ($treeNodeObject as $treeChildNodeObject) {
echo $treeNodeObject->getValue() . ' - ' . $treeChildNodeObject->getValue();
echo '<br/>';
if ($treeChildNodeObject instanceof TreeInterface) {
foreach ($treeChildNodeObject as $treeChild2NodeObject) {
echo $treeNodeObject->getValue() . ' - ' . $treeChildNodeObject->getValue() . ' - ' . $treeChild2NodeObject->getValue();
echo '<br/>';
}
}
}
}
}
// Output:
// A
// B
// C
// C - C1
// C - C2
// C - C2 - C21
// C - C3
// D
// remove tree node from tree
$treeRootObject->remove($fooObj2);
枚举器
Enumerator
扩展自EnumeratorAbstract
,EnumeratorAbstract
扩展自IteratorAbstract
,并实现了EnumeratorInterface
接口。IteratorAbstract
实现了IteratorInterface
接口,而IteratorInterface
扩展自Iterator
和Countable
。
Enumerator
类提供了一种简单的方法来检索、添加和迭代数据的键值对。可以使用foreach()
命令迭代该类。它还提供了以下方法来检索、添加和删除数据的键值对:set()
、remove()
、isFirst()
、isLast()
、current()
、next()
、hasNext()
、previous()
、hasPrevious()
、first()
、last()
、key()
、valid()
、rewind()
、hasIndex()
、indexOf()
、setItemPosition()
、count()
。该类实现了Iterator
和Countable
接口。
// create instance
use Naucon\Utility\Enumerator;
$enumeratorObject1 = new Enumerator('RED', 'BLUE', 'GREEN', 'YELLOW', 'BLACK');
echo 'My favorite color is ' . $enumeratorObject1->RED;
// Output: My favorite color is RED
// iterate
foreach ($enumeratorObject1 as $key => $value) {
echo $value . ' ';
}
// Output: RED BLUE GREEN YELLOW BLACK
// create another instance
$enumeratorObject2 = new Enumerator();
// add enumeration
$enumeratorObject2->set('FF0000', 'RED');
$enumeratorObject2->set('0000FF', 'BLUE');
$enumeratorObject2->set('00FF00', 'GREEN');
$enumeratorObject2->set('FFFF00', 'YELLOW');
$enumeratorObject2->set('000000', 'BLACK');
echo 'HEX color code of RED is #' . $enumeratorObject2->RED;
// Output: HEX color code of RED is #FF0000
// create another instance
$enumeratorObject3 = new Enumerator();
// add enumeration
$enumeratorObject3->RED = 'FF0000';
$enumeratorObject3->BLUE = '0000FF';
$enumeratorObject3->GREEN = '00FF00';
$enumeratorObject3->YELLOW = 'FFFF00';
$enumeratorObject3->BLACK = '000000';
echo 'HEX color code of RED is #' . $enumeratorObject3->RED;
// Output: HEX color code of RED is #FF0000
// create another instance
$enumeratorObjectParent = new Enumerator();
// add enumeration
$enumeratorObjectParent->COLOR = $enumeratorObject3; // add a enumeration instance
$enumeratorObjectParent->FOO = 'bar';
echo 'HEX color code of RED is #' . $enumeratorObjectParent->COLOR->RED . '<br/>';
// Output: HEX color code of RED is #FF0000
// iterate
foreach ($enumeratorObjectParent as $key => $value) {
if ($value instanceof Enumerator) {
echo $key . ' ';
foreach ($value as $value2) {
echo $value2 . ' ';
}
} else {
echo (string)$value . ' ';
}
}
// Output: COLOR FF0000 0000FF 00FF00 FFFF00 000000 bar
组合
Composite
扩展自CompositeAbstract
,CompositeAbstract
扩展自IteratorAbstract
,并实现了CompositeElementInterface
接口。IteratorAbstract
实现了IteratorInterface
接口,而IteratorInterface
扩展自Iterator
和Countable
。
Composite
类在内部使用数组存储层次结构数据,类似于Tree
类。可以使用foreach()
命令迭代该类以检索子元素。它还提供了以下方法来检索、添加和删除数据:add()
、remove()
、isFirst()
、isLast()
、current()
、next()
、hasNext()
、previous()
、hasPrevious()
、first()
、last()
、key()
、valid()
、rewind()
、indexOf()
、setItemPosition()
、count()
。该类实现了PHP的Iterator
和Countable
接口。
与Tree
类不同,组合没有根元素。每个元素都是相等的。
元素必须是CompositeElementInterface
的实例。
use Naucon\Utility\CompositeAbstract;
// declare simple composite element class
class CompositeElement extends CompositeAbstract
{
protected $state = null;
public function __construct($state)
{
$this->state = $state;
}
public function __toString()
{
return $this->state;
}
}
// create element instances
$elementAObject = new \CompositeElement('A');
$elementBObject = new \CompositeElement('B');
$elementCObject = new \CompositeElement('C');
$elementDObject = new \CompositeElement('D');
$elementEObject = new \CompositeElement('E');
// add elements
$elementAObject->add($elementBObject); // B to A
$elementAObject->add($elementCObject); // C to A
$elementBObject->add($elementDObject); // D to B
$elementAObject->add($elementEObject); // E to A
// iterate
foreach ($elementAObject as $elementChildObject) {
echo (string)$elementChildObject . ' '; // call __toString() method
}
// Output: B C E
// remove element
//$elementAObject->remove($elementBObject);
许可证
MIT许可证(MIT)
版权所有(c)2015 Sven Sanzenbacher
在此特此授予任何获得此软件和相关文档副本(“软件”)的人免费使用该软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许获得该软件的人这样做,但受以下条件的约束
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途适用性和非侵权性。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论这些责任是否源于合同行为、侵权行为或其他,无论是否与软件有关、使用软件或其他与软件有关的行为。