Skip to content

Commit

Permalink
fix fields sometimes being the wrong datatype
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Dec 11, 2014
1 parent 9053003 commit 991458b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Sulphur changelog

## v1.1.1

* Fixed field values sometimes being the wrong datatype

## v1.1.0

* Added `passes` and `doesNotPass` for filtering with custom callbacks
Expand Down
24 changes: 22 additions & 2 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct($response) {

/**
* Parses the passed response and passes the key-value pairs to references.
* @param type $response the response to parse
* @param string $response the response to parse
*/
protected function parse($response) {

Expand Down Expand Up @@ -47,7 +47,8 @@ protected function parse($response) {
} else if(preg_match('/^(.*)=(.*)$/', $line, $matches)) {
// key-value pair, like "State=Lobby"
if(!$heading) {
$reference[trim($matches[1])] = $matches[2];
$value = self::cast($matches[2]);
$reference[trim($matches[1])] = $value;
}
}
}
Expand Down Expand Up @@ -81,4 +82,23 @@ public function all() {
return $this->where(null);
}

/**
* Casts the value to the correct datatype.
* @param string $value the value to cast
* @return any
*/
public static function cast($value) {
$matches = array();
if(strtolower($value) === 'true') {
$value = (bool) true;
} else if(strtolower($value) === 'false') {
$value = (bool) false;
} else if(is_numeric($value)) {
$value = (int) $value;
} else if(preg_match('/^"(.*)"$/', $value, $matches)) {
$value = $matches[1];
}
return $value;
}

}
38 changes: 36 additions & 2 deletions tests/src/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,52 @@ protected function setUp() {
$this->response = $factory->fromString(file_get_contents(__DIR__.'/data/reference.ini'));
}

/**
* @covers Sulphur\Response::parse
* @depends testAll
*/
public function testParse() {
$references = $this->response->all();
$reference = $references[0];
$this->assertEquals(12, $reference->Icon);
$this->assertEquals('Minor Melee', $reference->Title);
$this->assertEquals(true, $reference->IsNetworkGame);
}

/**
* @covers Sulphur\Response::where
*/
public function testWhere() {
$this->assertInstanceOf('Sulphur\FilterableList', $this->response->where('foo'));
$where = $this->response->where('foo');
$this->assertInstanceOf('Sulphur\FilterableList', $where);
$this->assertEquals(1, count($where));
}

/**
* @covers Sulphur\Response::all
*/
public function testAll() {
$this->assertInstanceOf('Sulphur\FilterableList', $this->response->all());
$all = $this->response->all();
$this->assertInstanceOf('Sulphur\FilterableList', $all);
$this->assertEquals(1, count($all));
}

/**
* @covers Sulphur\Response::all
*/
public function testCast() {
$this->assertSame(true, Response::cast('true'));
$this->assertSame(true, Response::cast('TRUE'));
$this->assertSame(false, Response::cast('false'));
$this->assertSame(false, Response::cast('FALSE'));
$this->assertSame('String', Response::cast('"String"'));
$this->assertSame('true', Response::cast('"true"'));
$this->assertSame('TRUE', Response::cast('"TRUE"'));
$this->assertSame('false', Response::cast('"false"'));
$this->assertSame('FALSE', Response::cast('"FALSE"'));
$this->assertSame('42', Response::cast('"42"'));
$this->assertSame(42, Response::cast('42'));
$this->assertNotSame(42, Response::cast('"42"'));
}

}

0 comments on commit 991458b

Please sign in to comment.