Skip to content

Commit

Permalink
Merge pull request #13 from creof/release-2.1
Browse files Browse the repository at this point in the history
Release 2.1
  • Loading branch information
sadortun committed May 3, 2016
2 parents 8d7f336 + 0e71a9d commit 14fbd73
Show file tree
Hide file tree
Showing 9 changed files with 472 additions and 139 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added

### Changed

### Removed

## [2.1.0] - 2016-05-03
### Added
- Support for numbers in scientific notation.

### Changed
- Parser constructor no longer requires a value, enabling instance reuse.
- Lexer constructor no longer requires a value, enabling instance reuse.
- Tests now use Composer autoload.
- PHPUnit config XML now conforms to XSD.
- Documentation updated with new usage pattern.
- Move non-value comparison into if statement in Lexer::getType().
- Test case and test data cleanup.

### Removed
- TestInit no longer needed

## [2.0.0] - 2015-11-18
### Added
- Change base namespace to CrEOF\Geo\String to avoid class collision with other CrEOF packages.
Expand Down
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,31 @@
[![Test Coverage](https://codeclimate.com/github/creof/geo-parser/badges/coverage.svg)](https://codeclimate.com/github/creof/geo-parser/coverage)
[![Build Status](https://travis-ci.org/creof/geo-parser.svg)](https://travis-ci.org/creof/geo-parser)

Lexer and parser library for geometric and geographic string values.
Lexer and parser library for geometric and geographic point string values.

## Usage
Pass value to be parsed in the constructor, then call parse() on the created object.

There are two use patterns for the parser. The value to be parsed can be passed into the constructor, then parse()
called on the returned ```Parser``` object:

```php
$input = '79°56′55″W, 40°26′46″N';

$parser = new Parser($input);
$value = $parser->parse();

$value = $parser->parse();
```

If many values need to be parsed, a single ```Parser``` instance can be used:

```php
$input1 = '56.242 E';
$input2 = '40:26:46 S';

$parser = new Parser();

$value1 = $parser->parse($input1);
$value2 = $parser->parse($input2);
```

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

8. Two of any one format separated by space(s)
8. Two of any one format separated by whitespace

9. Two of any one format separated by a comma

## Return

The parser will return a integer/float or an array containing a pair of these values.

## Exceptions

The ```Lexer``` and ```Parser``` will throw exceptions implementing interface ```CrEOF\Geo\String\Exception\ExceptionInterface```.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@
"psr-0": {
"CrEOF\\Geo\\String": "lib/"
}
},
"autoload-dev": {
"psr-0": {
"CrEOF\\Geo\\String\\Tests": "tests/"
}
}
}
52 changes: 27 additions & 25 deletions lib/CrEOF/Geo/String/Lexer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2015 Derek J. Lambert
* Copyright (C) 2016 Derek J. Lambert
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -48,11 +48,13 @@ class Lexer extends AbstractLexer
const T_DEGREE = 14;

/**
* @param string $input
* @param string|null $input
*/
public function __construct($input)
public function __construct($input = null)
{
$this->setInput($input);
if (null !== $input) {
$this->setInput($input);
}
}

/**
Expand All @@ -62,38 +64,38 @@ public function __construct($input)
*/
protected function getType(&$value)
{
switch (true) {
case (is_numeric($value)):
if (false !== strpos($value, '.')) {
$value = (float) $value;
if (is_numeric($value)) {
$value += 0;

return self::T_FLOAT;
}
if (is_int($value)) {
return self::T_INTEGER;
}

$value = (int) $value;
return self::T_FLOAT;
}

return self::T_INTEGER;
case (':' === $value):
switch ($value) {
case ':':
return self::T_COLON;
case ('\'' === $value):
case ("\xe2\x80\xb2" === $value): // prime
case '\'':
case "\xe2\x80\xb2": // prime
return self::T_APOSTROPHE;
case ('"' === $value):
case ("\xe2\x80\xb3" === $value): // double prime
case '"':
case "\xe2\x80\xb3": // double prime
return self::T_QUOTE;
case (',' === $value):
case ',':
return self::T_COMMA;
case ('-' === $value):
case '-':
return self::T_MINUS;
case ('+' === $value):
case '+':
return self::T_PLUS;
case ('°' === $value):
case '°':
return self::T_DEGREE;
case ('N' === strtoupper($value)):
case ('S' === strtoupper($value)):
case 'N':
case 'S':
return self::T_CARDINAL_LAT;
case ('E' === strtoupper($value)):
case ('W' === strtoupper($value)):
case 'E':
case 'W':
return self::T_CARDINAL_LON;
default:
return self::T_NONE;
Expand Down
44 changes: 28 additions & 16 deletions lib/CrEOF/Geo/String/Parser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2015 Derek J. Lambert
* Copyright (C) 2016 Derek J. Lambert
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -41,11 +41,6 @@ class Parser
*/
private $input;

/**
* @var Lexer
*/
private $lexer;

/**
* @var int
*/
Expand All @@ -56,28 +51,45 @@ class Parser
*/
private $nextSymbol;

/**
* @var Lexer
*/
private $lexer;

/**
* Constructor
*
* Setup up instance properties
*
* @param string $input
* @param string|null $input
*/
public function __construct($input)
public function __construct($input = null)
{
// Save input string for use in messages
$this->input = $input;
// Create new Lexer and tokenize input string
$this->lexer = new Lexer($input);
$this->lexer = new Lexer();

if (null !== $input) {
$this->input = $input;
}
}

/**
* Parse input string
*
* @param string|null $input
*
* @return float|int|array
*/
public function parse()
public function parse($input = null)
{
if (null !== $input) {
$this->input = $input;
}

$this->nextCardinal = null;
$this->nextSymbol = null;

$this->lexer->setInput($this->input);

// Move Lexer to first token
$this->lexer->moveNext();

Expand Down Expand Up @@ -315,7 +327,7 @@ private function minutes()
}

// Get fractional minutes
$minutes = $minutes / 60;
$minutes /= 60;

// Match minutes symbol
$this->symbol();
Expand Down Expand Up @@ -346,7 +358,7 @@ private function seconds()
}

// Get fractional seconds
$seconds = $seconds / 3600;
$seconds /= 3600;

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

// Throw exception if value is out of range
if ($value > $range) {
throw $this->rangeError('Degrees', $range, (-1 * $range));
throw $this->rangeError('Degrees', $range, -1 * $range);
}

// Return value with sign
Expand Down
23 changes: 15 additions & 8 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit backupGlobals="false"
colors="true"
bootstrap="./tests/TestInit.php"
>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="./vendor/autoload.php"
>

<testsuites>
<testsuite>
<testsuite name="Tests">
<directory>./tests/CrEOF/Geo/String/Tests</directory>
</testsuite>
</testsuites>

<filter>
<blacklist>
<directory suffix=".php">./vendor</directory>
</blacklist>
<whitelist>
<directory suffix=".php">lib</directory>
<exclude>
<directory>tests</directory>
<directory>vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Loading

0 comments on commit 14fbd73

Please sign in to comment.