Skip to content

Commit

Permalink
Use native Enums
Browse files Browse the repository at this point in the history
Bumps minimum required PHP version to 8.1.
  • Loading branch information
jsor committed Jan 17, 2022
1 parent ba6f060 commit 43d909e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 141 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ jobs:
strategy:
matrix:
include:
- php: '8.0'
- php: '8.1'
code-coverage: 'yes'

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"require": {
"php": "^8.0"
"php": "^8.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.1",
Expand Down
45 changes: 9 additions & 36 deletions src/Dimension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,22 @@

namespace GeoIO;

use TypeError;
use ValueError;
use function is_string;

/**
* @psalm-immutable
*/
final class Dimension
enum Dimension: string
{
/**
/*
* Two-Dimension Geometry, e.g. POINT(x, y).
*/
public const DIMENSION_2D = '2D';

/**
case DIMENSION_2D = '2D';
/*
* Three-Dimension Geometry, e.g. POINT(x, y, z).
*/
public const DIMENSION_3DZ = '3DZ';

/**
case DIMENSION_3DZ = '3DZ';
/*
* Two-Dimension Measured Geometry, e.g. POINT(x, y, m).
*/
public const DIMENSION_3DM = '3DM';

/**
case DIMENSION_3DM = '3DM';
/*
* Three-Dimension Measured Geometry, e.g. POINT(x, y, z, m).
*/
public const DIMENSION_4D = '4D';

public static function assert(mixed $value): void
{
if (!is_string($value)) {
throw new TypeError('Expected type string, got ' . get_debug_type($value) . '.');
}

switch ($value) {
case self::DIMENSION_2D:
case self::DIMENSION_3DZ:
case self::DIMENSION_3DM:
case self::DIMENSION_4D:
return;
}

throw new ValueError('Invalid value ' . $value . '.');
}
case DIMENSION_4D = '4D';
}
40 changes: 8 additions & 32 deletions src/GeometryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,13 @@

namespace GeoIO;

use TypeError;
use ValueError;
use function is_string;

final class GeometryType
enum GeometryType: string
{
public const POINT = 'Point';
public const LINESTRING = 'LineString';
public const POLYGON = 'Polygon';
public const MULTIPOINT = 'MultiPoint';
public const MULTILINESTRING = 'MultiLineString';
public const MULTIPOLYGON = 'MultiPolygon';
public const GEOMETRYCOLLECTION = 'GeometryCollection';

public static function assert(mixed $value): void
{
if (!is_string($value)) {
throw new TypeError('Expected type string, got ' . get_debug_type($value) . '.');
}

switch ($value) {
case self::POINT:
case self::LINESTRING:
case self::POLYGON:
case self::MULTIPOINT:
case self::MULTILINESTRING:
case self::MULTIPOLYGON:
case self::GEOMETRYCOLLECTION:
return;
}

throw new ValueError('Invalid value ' . $value . '.');
}
case POINT = 'Point';
case LINESTRING = 'LineString';
case POLYGON = 'Polygon';
case MULTIPOINT = 'MultiPoint';
case MULTILINESTRING = 'MultiLineString';
case MULTIPOLYGON = 'MultiPolygon';
case GEOMETRYCOLLECTION = 'GeometryCollection';
}
40 changes: 6 additions & 34 deletions tests/DimensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,24 @@
namespace GeoIO;

use PHPUnit\Framework\TestCase;
use stdClass;

class DimensionTest extends TestCase
{
/**
* @dataProvider provideDimensions
*/
public function testAssert(string $dimension): void
public function testValid(string $dimension): void
{
Dimension::assert($dimension);
Dimension::from($dimension);

$this->assertTrue(true);
}

/**
* @dataProvider provideInvalidTypes
*/
public function testAssertThrowsForInvalidType($value): void
{
$this->expectError();
$this->expectErrorMessageMatches('/^Expected type string, got .+\.$/');

Dimension::assert($value);
}

public function testAssertThrowsForInvalidValue(): void
{
$this->expectError();
$this->expectErrorMessageMatches('/^Invalid value foo\.$/');

Dimension::assert('foo');
}

public function provideDimensions(): iterable
{
yield [Dimension::DIMENSION_2D];
yield [Dimension::DIMENSION_3DZ];
yield [Dimension::DIMENSION_3DM];
yield [Dimension::DIMENSION_4D];
}

public function provideInvalidTypes(): iterable
{
yield [1];
yield [1.0];
yield [['array']];
yield [new stdClass()];
yield ['2D'];
yield ['3DZ'];
yield ['3DM'];
yield ['4D'];
}
}
46 changes: 9 additions & 37 deletions tests/GeometryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,27 @@
namespace GeoIO;

use PHPUnit\Framework\TestCase;
use stdClass;

class GeometryTypeTest extends TestCase
{
/**
* @dataProvider provideGeometryTypes
*/
public function testAssert(string $dimension): void
public function testValid(string $type): void
{
GeometryType::assert($dimension);
GeometryType::from($type);

$this->assertTrue(true);
}

/**
* @dataProvider provideInvalidTypes
*/
public function testAssertThrowsForInvalidType($value): void
{
$this->expectError();
$this->expectErrorMessageMatches('/^Expected type string, got .+\.$/');

GeometryType::assert($value);
}

public function testAssertThrowsForInvalidValue(): void
{
$this->expectError();
$this->expectErrorMessageMatches('/^Invalid value foo\.$/');

GeometryType::assert('foo');
}

public function provideGeometryTypes(): iterable
{
yield [GeometryType::POINT];
yield [GeometryType::LINESTRING];
yield [GeometryType::POLYGON];
yield [GeometryType::MULTIPOINT];
yield [GeometryType::MULTILINESTRING];
yield [GeometryType::MULTIPOLYGON];
yield [GeometryType::GEOMETRYCOLLECTION];
}

public function provideInvalidTypes(): iterable
{
yield [1];
yield [1.0];
yield [['array']];
yield [new stdClass()];
yield ['Point'];
yield ['LineString'];
yield ['Polygon'];
yield ['MultiPoint'];
yield ['MultiLineString'];
yield ['MultiPolygon'];
yield ['GeometryCollection'];
}
}

0 comments on commit 43d909e

Please sign in to comment.