@@ -24,90 +24,102 @@ class Collection implements CollectionInterface
24
24
private $ search ;
25
25
private $ entries ;
26
26
27
- public function __construct (Connection $ connection , Query $ search, array $ entries = array () )
27
+ public function __construct (Connection $ connection , Query $ search )
28
28
{
29
29
$ this ->connection = $ connection ;
30
30
$ this ->search = $ search ;
31
- $ this ->entries = array ();
32
31
}
33
32
34
33
/**
35
34
* {@inheritdoc}
36
35
*/
37
36
public function toArray ()
38
37
{
39
- $ this ->initialize ();
38
+ if (null === $ this ->entries ) {
39
+ $ this ->entries = iterator_to_array ($ this ->getIterator (), false );
40
+ }
40
41
41
42
return $ this ->entries ;
42
43
}
43
44
44
45
public function count ()
45
46
{
46
- $ this ->initialize ();
47
+ if (false !== $ count = ldap_count_entries ($ this ->connection ->getResource (), $ this ->search ->getResource ())) {
48
+ return $ count ;
49
+ }
47
50
48
- return count ($ this ->entries );
51
+ throw new LdapException ( sprintf ( ' Error while retrieving entry count: %s ' , ldap_error ($ this ->connection -> getResource ())) );
49
52
}
50
53
51
54
public function getIterator ()
52
55
{
53
- return new ResultIterator ($ this ->connection , $ this ->search );
56
+ $ con = $ this ->connection ->getResource ();
57
+ $ search = $ this ->search ->getResource ();
58
+ $ current = ldap_first_entry ($ con , $ search );
59
+
60
+ if (0 === $ this ->count ()) {
61
+ return ;
62
+ }
63
+
64
+ if (false === $ current ) {
65
+ throw new LdapException (sprintf ('Could not rewind entries array: %s ' , ldap_error ($ con )));
66
+ }
67
+
68
+ yield $ this ->getSingleEntry ($ con , $ current );
69
+
70
+ while (false !== $ current = ldap_next_entry ($ con , $ current )) {
71
+ yield $ this ->getSingleEntry ($ con , $ current );
72
+ }
54
73
}
55
74
56
75
public function offsetExists ($ offset )
57
76
{
58
- $ this ->initialize ();
77
+ $ this ->toArray ();
59
78
60
79
return isset ($ this ->entries [$ offset ]);
61
80
}
62
81
63
82
public function offsetGet ($ offset )
64
83
{
84
+ $ this ->toArray ();
85
+
65
86
return isset ($ this ->entries [$ offset ]) ? $ this ->entries [$ offset ] : null ;
66
87
}
67
88
68
89
public function offsetSet ($ offset , $ value )
69
90
{
70
- $ this ->initialize ();
91
+ $ this ->toArray ();
71
92
72
93
$ this ->entries [$ offset ] = $ value ;
73
94
}
74
95
75
96
public function offsetUnset ($ offset )
76
97
{
77
- $ this ->initialize ();
98
+ $ this ->toArray ();
78
99
79
100
unset($ this ->entries [$ offset ]);
80
101
}
81
102
82
- private function initialize ( )
103
+ private function getSingleEntry ( $ con , $ current )
83
104
{
84
- if (null === $ this ->entries ) {
85
- return ;
86
- }
105
+ $ attributes = ldap_get_attributes ($ con , $ current );
87
106
88
- $ con = $ this ->connection ->getResource ();
89
-
90
- $ entries = ldap_get_entries ($ con , $ this ->search ->getResource ());
91
-
92
- if (false === $ entries ) {
93
- throw new LdapException (sprintf ('Could not load entries: %s ' , ldap_error ($ con )));
107
+ if (false === $ attributes ) {
108
+ throw new LdapException (sprintf ('Could not fetch attributes: %s ' , ldap_error ($ con )));
94
109
}
95
110
96
- if (0 === $ entries ['count ' ]) {
97
- return array ();
98
- }
111
+ $ attributes = $ this ->cleanupAttributes ($ attributes );
99
112
100
- unset( $ entries [ ' count ' ] );
113
+ $ dn = ldap_get_dn ( $ con , $ current );
101
114
102
- $ this -> entries = array_map ( function ( array $ entry ) {
103
- $ dn = $ entry [ ' dn ' ] ;
104
- $ attributes = $ this -> cleanupAttributes ( $ entry );
115
+ if ( false === $ dn ) {
116
+ throw new LdapException ( sprintf ( ' Could not fetch DN: %s ' , ldap_error ( $ con ))) ;
117
+ }
105
118
106
- return new Entry ($ dn , $ attributes );
107
- }, $ entries );
119
+ return new Entry ($ dn , $ attributes );
108
120
}
109
121
110
- private function cleanupAttributes (array $ entry = array () )
122
+ private function cleanupAttributes (array $ entry )
111
123
{
112
124
$ attributes = array_diff_key ($ entry , array_flip (range (0 , $ entry ['count ' ] - 1 )) + array (
113
125
'count ' => null ,
0 commit comments