Skip to content

Commit b76e502

Browse files
committed
Add factories with variable drivers
1 parent 368d8af commit b76e502

File tree

6 files changed

+144
-0
lines changed

6 files changed

+144
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Driver swapping via factories
810

911

1012
## [0.2.0] - 2024-05-27

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ SpatiaLite\Sfc::makePoint(1, 3)->setSRID(3059);
8686
// Produces SetSRID(MakePoint(?, ?), ?) with bindings [1, 3, 3059]
8787
```
8888

89+
Use factories with drivers set on them:
90+
91+
```php
92+
// simple feature classes
93+
$sf = new Sf('PostGIS');
94+
$sf->point('mycol'); // a PostGIS/Point wrapping the `mycol`
95+
96+
// simple feature constructors
97+
$sfc = new Sfc('PostGIS');
98+
$sfc->makePoint(1, 3); // a PostGIS/Point with ST_MakePoint(?, ?) and [1, 3] bindings
99+
```
100+
89101
## Usage examples
90102

91103
This section presents some plain examples in plain PDO.

src/Sf.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace GlaivePro\SF;
4+
5+
use GlaivePro\SF\OGC\Sfc as OGCSfc;
6+
7+
/**
8+
* Creating simple feature classes of the selected driver.
9+
*/
10+
class Sf
11+
{
12+
protected string $namespace;
13+
14+
public function __construct(string $driver = 'OGC')
15+
{
16+
$this->namespace = '\\GlaivePro\\SF\\'.$driver.'\\';
17+
}
18+
19+
public function __call($method, $args)
20+
{
21+
$class = $this->namespace.ucfirst($method);
22+
23+
return new $class(...$args);
24+
}
25+
}

src/Sfc.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace GlaivePro\SF;
4+
5+
use GlaivePro\SF\OGC\Sfc as OGCSfc;
6+
7+
/**
8+
* Simple feature constructors for the selected driver.
9+
*/
10+
class Sfc
11+
{
12+
protected OGCSfc $driver;
13+
14+
public function __construct(string $driver = 'OGC')
15+
{
16+
$driverClass = '\\GlaivePro\\SF\\'.$driver.'\\Sfc';
17+
18+
$this->driver = new $driverClass;
19+
}
20+
21+
public function __call($method, $args)
22+
{
23+
return $this->driver->$method(...$args);
24+
}
25+
}

tests/Feature/SfTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace GlaivePro\SF\Tests\Feature;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use GlaivePro\SF\OGC\Contracts;
7+
use GlaivePro\SF\OGC;
8+
use GlaivePro\SF\PostGIS;
9+
use GlaivePro\SF\Sf;
10+
11+
class SfTest extends TestCase
12+
{
13+
public function testDefaultDriver(): void
14+
{
15+
$sf = new Sf;
16+
17+
$point = $sf->point('somecol');
18+
19+
$this->assertEquals('somecol', (string) $point);
20+
$this->assertInstanceOf(Contracts\Point::class, $point);
21+
$this->assertInstanceOf(OGC\Point::class, $point);
22+
$this->assertNotInstanceOf(PostGIS\Point::class, $point);
23+
}
24+
25+
public function testPostgisDriver(): void
26+
{
27+
$sf = new Sf('PostGIS');
28+
29+
$point = $sf->point('somecol');
30+
31+
$this->assertEquals('somecol', (string) $point);
32+
$this->assertInstanceOf(Contracts\Point::class, $point);
33+
$this->assertInstanceOf(PostGIS\Point::class, $point);
34+
$this->assertNotInstanceOf(OGC\Point::class, $point);
35+
}
36+
}

tests/Feature/SfcTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace GlaivePro\SF\Tests\Feature;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use GlaivePro\SF\OGC\Contracts;
7+
use GlaivePro\SF\OGC;
8+
use GlaivePro\SF\PostGIS;
9+
use GlaivePro\SF\Sfc;
10+
11+
class SfcTest extends TestCase
12+
{
13+
public function testDefaultDriver(): void
14+
{
15+
$sfc = new Sfc;
16+
17+
$point = $sfc->pointFromWkb('binary');
18+
19+
$this->assertEquals(
20+
'ST_PointFromWKB(?)',
21+
(string) $point,
22+
);
23+
$this->assertEquals(['binary'], $point->bindings);
24+
$this->assertInstanceOf(Contracts\Point::class, $point);
25+
$this->assertInstanceOf(OGC\Point::class, $point);
26+
$this->assertNotInstanceOf(PostGIS\Point::class, $point);
27+
}
28+
29+
public function testPostgisDriver(): void
30+
{
31+
$sfc = new Sfc('PostGIS');
32+
33+
$point = $sfc->pointFromWkb('binary');
34+
35+
$this->assertEquals(
36+
'ST_PointFromWKB(?)',
37+
(string) $point,
38+
);
39+
$this->assertEquals(['binary'], $point->bindings);
40+
$this->assertInstanceOf(Contracts\Point::class, $point);
41+
$this->assertInstanceOf(PostGIS\Point::class, $point);
42+
$this->assertNotInstanceOf(OGC\Point::class, $point);
43+
}
44+
}

0 commit comments

Comments
 (0)