xref: /PHP-5.3/ext/spl/tests/array_005.phpt (revision 610c7fbe)
1--TEST--
2SPL: ArrayObject/Iterator interaction
3--FILE--
4<?php
5
6class Student
7{
8	private $id;
9	private $name;
10
11    public function __construct($id, $name)
12    {
13    	$this->id = $id;
14    	$this->name = $name;
15    }
16
17	public function __toString()
18	{
19		return $this->id . ', ' . $this->name;
20	}
21
22	public function getId()
23	{
24		return $this->id;
25	}
26}
27
28class StudentIdFilter extends FilterIterator
29{
30	private $id;
31
32	public function __construct(ArrayObject $students, Student $other)
33	{
34		FilterIterator::__construct($students->getIterator());
35		$this->id = $other->getId();
36	}
37
38	public function accept()
39	{
40		echo "ACCEPT ".$this->current()->getId()." == ".$this->id."\n";
41		return $this->current()->getId() == $this->id;
42	}
43}
44
45class StudentList implements IteratorAggregate
46{
47	private $students;
48
49	public function __construct()
50	{
51		$this->students = new ArrayObject(array());
52	}
53
54	public function add(Student $student)
55	{
56		if (!$this->contains($student)) {
57			$this->students[] = $student;
58		}
59	}
60
61	public function contains(Student $student)
62	{
63		foreach ($this->students as $s)
64		{
65			if ($s->getId() == $student->getId()) {
66				return true;
67			}
68		}
69		return false;
70	}
71
72	public function getIterator() {
73		return $this->students->getIterator();
74	}
75}
76
77$students = new StudentList();
78$students->add(new Student('01234123', 'Joe'));
79$students->add(new Student('00000014', 'Bob'));
80$students->add(new Student('00000014', 'Foo'));
81
82foreach ($students as $student) {
83	echo $student, "\n";
84}
85?>
86===DONE===
87<?php exit(0); ?>
88--EXPECT--
8901234123, Joe
9000000014, Bob
91===DONE===
92