Skip to content

Commit 639676f

Browse files
committed
Merge pull request #229 from toin0u/MultipleResultFactory
Added: DefaultResultFactory and MultipleResultFactory classes - Fix #223
2 parents 17470bc + ca92c26 commit 639676f

File tree

8 files changed

+178
-25
lines changed

8 files changed

+178
-25
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,11 @@ You can provide your own `adapter`, you just need to create a new class which im
551551

552552
You can also write your own `provider` by implementing the `ProviderInterface`.
553553

554-
You can provide your own `result` by extending `ResultFactory` and implementing `ResultInterface` if your provider returns more informations than the default one.
554+
You can provide your own `result` by extending `DefaultResultFactory` or `MultipleResultFactory` and implementing
555+
`ResultInterface` if your provider returns one or multiple results and more informations than the default one.
556+
Please note that the method `createFromArray` is marked `final` in these factories.
557+
558+
If you need your own `ResultFactory`, just implement `ResultFactoryInterface`.
555559

556560
Note, `AbstractProvider` and `AbstractResult` classes can help you by providing useful features.
557561

src/Geocoder/Geocoder.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
namespace Geocoder;
1212

1313
use Geocoder\Provider\ProviderInterface;
14-
use Geocoder\Result\ResultFactory;
14+
use Geocoder\Result\ResultFactoryInterface;
15+
use Geocoder\Result\DefaultResultFactory;
1516

1617
/**
1718
* @author William Durand <william.durand1@gmail.com>
@@ -34,27 +35,27 @@ class Geocoder implements GeocoderInterface
3435
private $provider;
3536

3637
/**
37-
* @var ResultFactory
38+
* @var ResultFactoryInterface
3839
*/
3940
private $resultFactory;
4041

4142
/**
42-
* @param ProviderInterface $provider
43-
* @param ResultFactory $resultFactory
43+
* @param ProviderInterface $provider
44+
* @param ResultFactoryInterface $resultFactory
4445
*/
45-
public function __construct(ProviderInterface $provider = null, ResultFactory $resultFactory = null)
46+
public function __construct(ProviderInterface $provider = null, ResultFactoryInterface $resultFactory = null)
4647
{
4748
$this->provider = $provider;
4849

4950
$this->setResultFactory($resultFactory);
5051
}
5152

5253
/**
53-
* @param ResultFactory $resultFactory
54+
* @param ResultFactoryInterface $resultFactory
5455
*/
55-
public function setResultFactory(ResultFactory $resultFactory = null)
56+
public function setResultFactory(ResultFactoryInterface $resultFactory = null)
5657
{
57-
$this->resultFactory = $resultFactory ?: new ResultFactory();
58+
$this->resultFactory = $resultFactory ?: new DefaultResultFactory();
5859
}
5960

6061
/**

src/Geocoder/Result/ResultFactory.php renamed to src/Geocoder/Result/DefaultResultFactory.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
/**
1414
* @author William Durand <william.durand1@gmail.com>
1515
*/
16-
class ResultFactory
16+
class DefaultResultFactory implements ResultFactoryInterface
1717
{
1818
/**
19-
* @param array $data An array of data.
20-
*
21-
* @return ResultInterface
19+
* {@inheritDoc}
2220
*/
2321
final public function createFromArray(array $data)
2422
{
@@ -29,7 +27,7 @@ final public function createFromArray(array $data)
2927
}
3028

3129
/**
32-
* @return ResultInterface
30+
* {@inheritDoc}
3331
*/
3432
public function newInstance()
3533
{
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder\Result;
12+
13+
/**
14+
* @author Markus Bachmann <markus.bachmann@bachi.biz>
15+
*/
16+
class MultipleResultFactory implements ResultFactoryInterface
17+
{
18+
/**
19+
* {@inheritDoc}
20+
*/
21+
final public function createFromArray(array $data)
22+
{
23+
$result = new \SplObjectStorage();
24+
foreach ($data as $row) {
25+
$instance = $this->newInstance();
26+
$instance->fromArray($row);
27+
$result->attach($instance);
28+
}
29+
30+
return $result;
31+
}
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public function newInstance()
37+
{
38+
return new Geocoded();
39+
}
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder\Result;
12+
13+
/**
14+
* @author Antoine Corcy <contact@sbin.dk>
15+
*/
16+
interface ResultFactoryInterface
17+
{
18+
/**
19+
* @param array $data An array of data.
20+
*
21+
* @return ResultInterface
22+
*/
23+
public function createFromArray(array $data);
24+
25+
/**
26+
* @return ResultInterface
27+
*/
28+
public function newInstance();
29+
}

tests/Geocoder/Tests/GeocoderTest.php

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ public function testReverseEmpty()
130130
$this->assertEquals(0, $this->geocoder->getProvider('test2')->geocodeCount);
131131
}
132132

133-
public function testUseCustomResultFactory()
133+
public function testUseCustomDefaultResultFactory()
134134
{
135-
$factoryMock = $this->getMock('Geocoder\Result\ResultFactory');
135+
$factoryMock = $this->getMock('Geocoder\Result\DefaultResultFactory');
136136
$factoryMock
137137
->expects($this->once())
138138
->method('newInstance')
@@ -142,19 +142,64 @@ public function testUseCustomResultFactory()
142142
$this->assertInstanceOf('Geocoder\Tests\DummyResult', $geocoder->returnResult(array()));
143143
}
144144

145-
public function testSetAndUseCustomResultFactory()
145+
public function testSetAndUseCustomDefaultResultFactory()
146146
{
147-
$factoryMock = $this->getMock('Geocoder\Result\ResultFactory');
147+
$factoryMock = $this->getMock('Geocoder\Result\DefaultResultFactory');
148148
$factoryMock
149149
->expects($this->once())
150150
->method('newInstance')
151151
->will($this->returnValue(new DummyResult()));
152152

153-
$geocoder = new TestableGeocoder(null);
153+
$geocoder = new TestableGeocoder();
154154
$geocoder->setResultFactory($factoryMock);
155155
$this->assertInstanceOf('Geocoder\Tests\DummyResult', $geocoder->returnResult(array()));
156156
}
157157

158+
public function testUseCustomMultipleResultFactory()
159+
{
160+
$factoryMock = $this->getMock('Geocoder\Result\MultipleResultFactory');
161+
$factoryMock->expects($this->at(0))->method('newInstance')->will($this->returnValue(new DummyResult()));
162+
$factoryMock->expects($this->at(1))->method('newInstance')->will($this->returnValue(new DummyResult()));
163+
$factoryMock->expects($this->at(2))->method('newInstance')->will($this->returnValue(new DummyResult()));
164+
165+
$geocoder = new TestableGeocoder(null, $factoryMock);
166+
$results = $geocoder->returnResult(array(
167+
array(),
168+
array(),
169+
array(),
170+
));
171+
172+
$this->assertInstanceOf('\SplObjectStorage', $results);
173+
$this->assertCount(3, $results);
174+
foreach ($results as $result) {
175+
$this->assertInstanceOf('Geocoder\Tests\DummyResult', $result);
176+
$this->assertInstanceOf('Geocoder\Result\ResultInterface', $result);
177+
}
178+
}
179+
180+
public function testSetAndUseCustomMultipleResultFactory()
181+
{
182+
$factoryMock = $this->getMock('Geocoder\Result\MultipleResultFactory');
183+
$factoryMock->expects($this->at(0))->method('newInstance')->will($this->returnValue(new DummyResult()));
184+
$factoryMock->expects($this->at(1))->method('newInstance')->will($this->returnValue(new DummyResult()));
185+
$factoryMock->expects($this->at(2))->method('newInstance')->will($this->returnValue(new DummyResult()));
186+
187+
$geocoder = new TestableGeocoder();
188+
$geocoder->setResultFactory($factoryMock);
189+
$results = $geocoder->returnResult(array(
190+
array(),
191+
array(),
192+
array(),
193+
));
194+
195+
$this->assertInstanceOf('\SplObjectStorage', $results);
196+
$this->assertCount(3, $results);
197+
foreach ($results as $result) {
198+
$this->assertInstanceOf('Geocoder\Tests\DummyResult', $result);
199+
$this->assertInstanceOf('Geocoder\Result\ResultInterface', $result);
200+
}
201+
}
202+
158203
protected function assertEmptyResult($result)
159204
{
160205
$this->assertEquals(0, $result->getLatitude());

tests/Geocoder/Tests/Result/ResultFactoryTest.php renamed to tests/Geocoder/Tests/Result/DefaultResultFactoryTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,31 @@
22

33
namespace Geocoder\Tests\Result;
44

5-
use Geocoder\Result\ResultFactory;
5+
use Geocoder\Result\DefaultResultFactory;
66
use Geocoder\Tests\TestCase;
77

88
/**
99
* @author William Durand <william.durand1@gmail.com>
1010
*/
11-
class ResultFactoryTest extends TestCase
11+
class DefaultResultFactoryTest extends TestCase
1212
{
1313
public function testNewInstance()
1414
{
15-
$factory = new ResultFactory();
15+
$factory = new DefaultResultFactory();
1616
$result = $factory->newInstance();
1717

18-
$this->assertTrue(is_object($result));
1918
$this->assertInstanceOf('Geocoder\Result\Geocoded', $result);
2019
$this->assertInstanceOf('Geocoder\Result\ResultInterface', $result);
2120
}
2221

2322
public function testCreateFromArray()
2423
{
25-
$factory = new ResultFactory();
24+
$factory = new DefaultResultFactory();
2625
$result = $factory->createFromArray(array(
2726
'latitude' => 123,
2827
'longitude' => 456,
2928
));
3029

31-
$this->assertTrue(is_object($result));
3230
$this->assertInstanceOf('Geocoder\Result\Geocoded', $result);
3331
$this->assertInstanceOf('Geocoder\Result\ResultInterface', $result);
3432
$this->assertEquals(123, $result->getLatitude());
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Geocoder\Tests\Result;
4+
5+
use Geocoder\Result\MultipleResultFactory;
6+
use Geocoder\Tests\TestCase;
7+
8+
/**
9+
* @author Antoine Corcy <contact@sbin.dk>
10+
*/
11+
class MultipleResultFactoryTest extends TestCase
12+
{
13+
public function testNewInstance()
14+
{
15+
$factory = new MultipleResultFactory();
16+
$result = $factory->newInstance();
17+
18+
$this->assertInstanceOf('Geocoder\Result\Geocoded', $result);
19+
$this->assertInstanceOf('Geocoder\Result\ResultInterface', $result);
20+
}
21+
22+
public function testCreateFromArray()
23+
{
24+
$factory = new MultipleResultFactory();
25+
$results = $factory->createFromArray(array(
26+
array(),
27+
array(),
28+
array(),
29+
));
30+
31+
$this->assertInstanceOf('\SplObjectStorage', $results);
32+
$this->assertCount(3, $results);
33+
foreach ($results as $result) {
34+
$this->assertInstanceOf('Geocoder\Result\Geocoded', $result);
35+
$this->assertInstanceOf('Geocoder\Result\ResultInterface', $result);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)