-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGitElephantRepositoryCollection.php
172 lines (154 loc) · 3.29 KB
/
GitElephantRepositoryCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Cypress\GitElephantBundle\Collection;
use GitElephant\Repository;
/**
* Class GitElephantRepositoryCollection
*
* @category Collection
* @package Cypress\GitElephantBundle\Collection
* @author Matteo Giachino <https://github.com/matteosister>
*/
class GitElephantRepositoryCollection implements \ArrayAccess, \Iterator, \Countable
{
/**
* the cursor position
*
* @var int
*/
private $position;
/**
* @var array
*/
private $repositories;
/**
* Class constructor
* Accept an array of repository in format:
* array(
* string $name => Repository $class
* )
*
* @param array $repositories an array of repository classes
* @param null $binary
*/
public function __construct($repositories, $binary = null)
{
$this->position = 0;
foreach ($repositories as $name => $path) {
$repository = new Repository($path, $binary);
$repository->setName($name);
$this->repositories[] = $repository;
}
}
/**
* Retrieve a repository by its name
*
* @param string $name the repository name
*
* @return Repository $repository
*/
public function get($name)
{
/** @var Repository $repository */
foreach ($this->repositories as $repository) {
if ($repository->getName() == $name) {
return $repository;
}
}
return null;
}
/**
* ArrayAccess interface
*
* @param int $offset offset
*
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->repositories[$offset]);
}
/**
* ArrayAccess interface
*
* @param int $offset offset
*
* @return null|mixed
*/
public function offsetGet($offset)
{
return isset($this->repositories[$offset]) ? $this->repositories[$offset] : null;
}
/**
* ArrayAccess interface
*
* @param int $offset offset
* @param mixed $value value
*/
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->repositories[] = $value;
} else {
$this->repositories[$offset] = $value;
}
}
/**
* ArrayAccess interface
*
* @param int $offset offset
*/
public function offsetUnset($offset)
{
unset($this->repositories[$offset]);
}
/**
* Countable interface
*
* @return int|void
*/
public function count()
{
return count($this->repositories);
}
/**
* Iterator interface
*
* @return mixed
*/
public function current()
{
return $this->repositories[$this->position];
}
/**
* Iterator interface
*/
public function next()
{
++$this->position;
}
/**
* Iterator interface
*
* @return int
*/
public function key()
{
return $this->position;
}
/**
* Iterator interface
*
* @return bool
*/
public function valid()
{
return isset($this->repositories[$this->position]);
}
/**
* Iterator interface
*/
public function rewind()
{
$this->position = 0;
}
}