Skip to content

Commit

Permalink
New methods for generating common structures (CSV, JSON, etc.) from c…
Browse files Browse the repository at this point in the history
…omplex Results objects
  • Loading branch information
troydavisson committed Apr 26, 2015
1 parent e11c4e8 commit ac4be12
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require": {
"guzzlehttp/guzzle": ">=4.0,<6.0",
"illuminate/container": ">=4.2.0",
"illuminate/support": ">=4.2.0"
"illuminate/support": ">=4.2.0",
"league/csv": ">=6.0"
},
"require-dev": {
"phpunit/phpunit": "4.1.*",
Expand Down
56 changes: 55 additions & 1 deletion src/Models/Search/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Countable;
use ArrayAccess;
use IteratorAggregate;
use League\Csv\Writer;
use SplTempFileObject;

class Results implements Countable, ArrayAccess, IteratorAggregate
{
Expand All @@ -16,7 +18,7 @@ class Results implements Countable, ArrayAccess, IteratorAggregate
protected $total_results_count = 0;
protected $returned_results_count = 0;
protected $error = null;
/** @var \Illuminate\Support\Collection */
/** @var \Illuminate\Support\Collection|\PHRETS\Models\Search\Record[] */
protected $results;
protected $headers = [];
protected $restricted_indicator = '****';
Expand Down Expand Up @@ -342,4 +344,56 @@ public function lists($field)
}
return $l;
}

/**
* Return results as a large prepared CSV string
*
* @return string
*/
public function toCSV()
{
// create a temporary file so we can write the CSV out
$writer = Writer::createFromFileObject(new SplTempFileObject);

// add the header line
$writer->insertOne($this->getHeaders());

// go through each record
foreach ($this->results as $r) {
$record = [];

// go through each field and ensure that each record is prepared in an order consistent with the headers
foreach ($this->getHeaders() as $h) {
$record[] = $r->get($h);
}
$writer->insertOne($record);
}

// return as a string
return (string) $writer;
}

/**
* Return results as a JSON string
*
* @return string
*/
public function toJSON()
{
return json_encode($this->toArray());
}

/**
* Return results as a simple array
*
* @return array
*/
public function toArray()
{
$result = [];
foreach ($this->results as $r) {
$result[] = $r->toArray();
}
return $result;
}
}
26 changes: 26 additions & 0 deletions tests/Models/Search/ResultsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public function setUp()
{
$this->rs = new Results;

$this->rs->setHeaders(['id', 'name', 'value']);

$rc = new Record;
$rc->set('id', 1);
$rc->set('name', 'left');
Expand Down Expand Up @@ -194,4 +196,28 @@ public function it_gives_a_list_excluding_restricted_values()

$this->assertSame(['extra', 'bonus'], $rs->lists('id'));
}

/** @test **/
public function it_converts_object_to_CSV()
{
$expected = "id,name,value\n1,left,up\n2,right,down\n";
$this->assertSame($expected, $this->rs->toCSV());
}

/** @test **/
public function it_converts_object_to_JSON()
{
$expected = '[{"id":1,"name":"left","value":"up"},{"id":2,"name":"right","value":"down"}]';
$this->assertSame($expected, $this->rs->toJSON());
}

/** @test **/
public function it_converts_object_to_array()
{
$expected = [
['id' => 1, 'name' => 'left', 'value' => 'up'],
['id' => 2, 'name' => 'right', 'value' => 'down'],
];
$this->assertSame($expected, $this->rs->toArray());
}
}

0 comments on commit ac4be12

Please sign in to comment.