Skip to content

Commit

Permalink
adding support for making har objects from arrays and 'object'
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgoucher committed Jun 29, 2012
1 parent 0c1e90a commit 42fc8d0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
26 changes: 19 additions & 7 deletions PHPHARchive/HAR.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,27 @@ class PHPHARchive_HAR {
public $pages = array();
public $entries = array();

// http://ca2.php.net/manual/en/function.get-object-vars.php#93416
private function toArray($data) {
if (is_object($data)) $data = get_object_vars($data);
return is_array($data) ? array_map(__METHOD__, $data) : $data;
}

function __construct($h) {
if (is_file($h)) {
$this->raw = json_decode(file_get_contents($h), True);
} elseif (substr($h, 0, 1) == "{") {
$this->raw = json_decode($h, True);
} else {
throw new PHPHARchive_MissingHARException($h . " does not exist on disk or is not valid JSON");
if (is_string($h)) {
if (is_file($h)) {
$this->raw = json_decode(file_get_contents($h), True);
} elseif (substr($h, 0, 1) == "{") {
$this->raw = json_decode($h, True);
} else {
throw new PHPHARchive_MissingHARException($h . " does not exist on disk or is not valid JSON");
}
} elseif(is_object($h)) {
$this->raw = $this->toArray($h);
} elseif(is_array($h)) {
$this->raw = $h;
}

// version; mandatory, but can be empty
if (array_key_exists("version", $this->raw["log"])) {
if (strlen($this->raw["log"]["version"]) != 0) {
Expand Down
4 changes: 2 additions & 2 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</lead>
<date>2012-06-28</date>
<version>
<release>1.1.0</release>
<api>1.1.0</api>
<release>1.2.0</release>
<api>1.2.0</api>
</version>
<stability>
<release>stable</release>
Expand Down
10 changes: 10 additions & 0 deletions tests/HarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class HarTest extends PHPUnit_Framework_TestCase {
/**
* @group har
* @group file
* @expectedException PHPHARchive_MissingHARException
* @expectedExceptionMessage foo.har does not exist on disk or is not valid JSON
*/
Expand All @@ -20,6 +21,15 @@ public function test_parsing_of_a_string() {
$h = new PHPHARchive_HAR('{"log": {"version": "1.2"}}');
}

/**
* @group har
* @group array
*/
public function test_parsing_of_an_array() {
$a = json_decode('{"log": {"version": "1.2"}}');
$h = new PHPHARchive_HAR($a);
}

/**
* @group interesting
*/
Expand Down

0 comments on commit 42fc8d0

Please sign in to comment.