Skip to content

Commit 14fbd73

Browse files
committed
Merge pull request #13 from creof/release-2.1
Release 2.1
2 parents 8d7f336 + 0e71a9d commit 14fbd73

File tree

9 files changed

+472
-139
lines changed

9 files changed

+472
-139
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,27 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased]
66
### Added
7+
78
### Changed
89

10+
### Removed
11+
12+
## [2.1.0] - 2016-05-03
13+
### Added
14+
- Support for numbers in scientific notation.
15+
16+
### Changed
17+
- Parser constructor no longer requires a value, enabling instance reuse.
18+
- Lexer constructor no longer requires a value, enabling instance reuse.
19+
- Tests now use Composer autoload.
20+
- PHPUnit config XML now conforms to XSD.
21+
- Documentation updated with new usage pattern.
22+
- Move non-value comparison into if statement in Lexer::getType().
23+
- Test case and test data cleanup.
24+
25+
### Removed
26+
- TestInit no longer needed
27+
928
## [2.0.0] - 2015-11-18
1029
### Added
1130
- Change base namespace to CrEOF\Geo\String to avoid class collision with other CrEOF packages.

README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,31 @@
44
[![Test Coverage](https://codeclimate.com/github/creof/geo-parser/badges/coverage.svg)](https://codeclimate.com/github/creof/geo-parser/coverage)
55
[![Build Status](https://travis-ci.org/creof/geo-parser.svg)](https://travis-ci.org/creof/geo-parser)
66

7-
Lexer and parser library for geometric and geographic string values.
7+
Lexer and parser library for geometric and geographic point string values.
88

99
## Usage
10-
Pass value to be parsed in the constructor, then call parse() on the created object.
10+
11+
There are two use patterns for the parser. The value to be parsed can be passed into the constructor, then parse()
12+
called on the returned ```Parser``` object:
1113

1214
```php
1315
$input = '79°56′55″W, 40°26′46″N';
16+
1417
$parser = new Parser($input);
15-
$value = $parser->parse();
18+
19+
$value = $parser->parse();
20+
```
21+
22+
If many values need to be parsed, a single ```Parser``` instance can be used:
23+
24+
```php
25+
$input1 = '56.242 E';
26+
$input2 = '40:26:46 S';
27+
28+
$parser = new Parser();
29+
30+
$value1 = $parser->parse($input1);
31+
$value2 = $parser->parse($input2);
1632
```
1733

1834
## Supported Formats
@@ -68,10 +84,14 @@ Both single values and pairs are supported. Some samples of supported formats ar
6884
* 99:58:56 W
6985
* 44:58:53.9 N
7086

71-
8. Two of any one format separated by space(s)
87+
8. Two of any one format separated by whitespace
7288

7389
9. Two of any one format separated by a comma
7490

7591
## Return
7692

7793
The parser will return a integer/float or an array containing a pair of these values.
94+
95+
## Exceptions
96+
97+
The ```Lexer``` and ```Parser``` will throw exceptions implementing interface ```CrEOF\Geo\String\Exception\ExceptionInterface```.

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@
2323
"psr-0": {
2424
"CrEOF\\Geo\\String": "lib/"
2525
}
26+
},
27+
"autoload-dev": {
28+
"psr-0": {
29+
"CrEOF\\Geo\\String\\Tests": "tests/"
30+
}
2631
}
2732
}

lib/CrEOF/Geo/String/Lexer.php

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright (C) 2015 Derek J. Lambert
3+
* Copyright (C) 2016 Derek J. Lambert
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
66
* of this software and associated documentation files (the "Software"), to deal
@@ -48,11 +48,13 @@ class Lexer extends AbstractLexer
4848
const T_DEGREE = 14;
4949

5050
/**
51-
* @param string $input
51+
* @param string|null $input
5252
*/
53-
public function __construct($input)
53+
public function __construct($input = null)
5454
{
55-
$this->setInput($input);
55+
if (null !== $input) {
56+
$this->setInput($input);
57+
}
5658
}
5759

5860
/**
@@ -62,38 +64,38 @@ public function __construct($input)
6264
*/
6365
protected function getType(&$value)
6466
{
65-
switch (true) {
66-
case (is_numeric($value)):
67-
if (false !== strpos($value, '.')) {
68-
$value = (float) $value;
67+
if (is_numeric($value)) {
68+
$value += 0;
6969

70-
return self::T_FLOAT;
71-
}
70+
if (is_int($value)) {
71+
return self::T_INTEGER;
72+
}
7273

73-
$value = (int) $value;
74+
return self::T_FLOAT;
75+
}
7476

75-
return self::T_INTEGER;
76-
case (':' === $value):
77+
switch ($value) {
78+
case ':':
7779
return self::T_COLON;
78-
case ('\'' === $value):
79-
case ("\xe2\x80\xb2" === $value): // prime
80+
case '\'':
81+
case "\xe2\x80\xb2": // prime
8082
return self::T_APOSTROPHE;
81-
case ('"' === $value):
82-
case ("\xe2\x80\xb3" === $value): // double prime
83+
case '"':
84+
case "\xe2\x80\xb3": // double prime
8385
return self::T_QUOTE;
84-
case (',' === $value):
86+
case ',':
8587
return self::T_COMMA;
86-
case ('-' === $value):
88+
case '-':
8789
return self::T_MINUS;
88-
case ('+' === $value):
90+
case '+':
8991
return self::T_PLUS;
90-
case ('°' === $value):
92+
case '°':
9193
return self::T_DEGREE;
92-
case ('N' === strtoupper($value)):
93-
case ('S' === strtoupper($value)):
94+
case 'N':
95+
case 'S':
9496
return self::T_CARDINAL_LAT;
95-
case ('E' === strtoupper($value)):
96-
case ('W' === strtoupper($value)):
97+
case 'E':
98+
case 'W':
9799
return self::T_CARDINAL_LON;
98100
default:
99101
return self::T_NONE;

lib/CrEOF/Geo/String/Parser.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright (C) 2015 Derek J. Lambert
3+
* Copyright (C) 2016 Derek J. Lambert
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
66
* of this software and associated documentation files (the "Software"), to deal
@@ -41,11 +41,6 @@ class Parser
4141
*/
4242
private $input;
4343

44-
/**
45-
* @var Lexer
46-
*/
47-
private $lexer;
48-
4944
/**
5045
* @var int
5146
*/
@@ -56,28 +51,45 @@ class Parser
5651
*/
5752
private $nextSymbol;
5853

54+
/**
55+
* @var Lexer
56+
*/
57+
private $lexer;
58+
5959
/**
6060
* Constructor
6161
*
6262
* Setup up instance properties
6363
*
64-
* @param string $input
64+
* @param string|null $input
6565
*/
66-
public function __construct($input)
66+
public function __construct($input = null)
6767
{
68-
// Save input string for use in messages
69-
$this->input = $input;
70-
// Create new Lexer and tokenize input string
71-
$this->lexer = new Lexer($input);
68+
$this->lexer = new Lexer();
69+
70+
if (null !== $input) {
71+
$this->input = $input;
72+
}
7273
}
7374

7475
/**
7576
* Parse input string
7677
*
78+
* @param string|null $input
79+
*
7780
* @return float|int|array
7881
*/
79-
public function parse()
82+
public function parse($input = null)
8083
{
84+
if (null !== $input) {
85+
$this->input = $input;
86+
}
87+
88+
$this->nextCardinal = null;
89+
$this->nextSymbol = null;
90+
91+
$this->lexer->setInput($this->input);
92+
8193
// Move Lexer to first token
8294
$this->lexer->moveNext();
8395

@@ -315,7 +327,7 @@ private function minutes()
315327
}
316328

317329
// Get fractional minutes
318-
$minutes = $minutes / 60;
330+
$minutes /= 60;
319331

320332
// Match minutes symbol
321333
$this->symbol();
@@ -346,7 +358,7 @@ private function seconds()
346358
}
347359

348360
// Get fractional seconds
349-
$seconds = $seconds / 3600;
361+
$seconds /= 3600;
350362

351363
// Match seconds symbol if requirement not colon
352364
if (Lexer::T_COLON !== $this->nextSymbol) {
@@ -430,7 +442,7 @@ private function cardinal($value)
430442

431443
// Throw exception if value is out of range
432444
if ($value > $range) {
433-
throw $this->rangeError('Degrees', $range, (-1 * $range));
445+
throw $this->rangeError('Degrees', $range, -1 * $range);
434446
}
435447

436448
// Return value with sign

phpunit.xml.dist

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<phpunit backupGlobals="false"
3-
colors="true"
4-
bootstrap="./tests/TestInit.php"
5-
>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="./vendor/autoload.php"
8+
>
69

710
<testsuites>
8-
<testsuite>
11+
<testsuite name="Tests">
912
<directory>./tests/CrEOF/Geo/String/Tests</directory>
1013
</testsuite>
1114
</testsuites>
1215

1316
<filter>
14-
<blacklist>
15-
<directory suffix=".php">./vendor</directory>
16-
</blacklist>
17+
<whitelist>
18+
<directory suffix=".php">lib</directory>
19+
<exclude>
20+
<directory>tests</directory>
21+
<directory>vendor</directory>
22+
</exclude>
23+
</whitelist>
1724
</filter>
1825
</phpunit>

0 commit comments

Comments
 (0)