factory() public method

Create an instance of the format class e.g: echo $this->format->factory(['foo' => 'bar'])->to_csv();
public factory ( mixed $data, string $from_type = NULL ) : object
$data mixed Data to convert/parse
$from_type string Type to convert from e.g. json, csv, html
return object Instance of the format class
 public function testMethod()
 {
     // Check if everything is ok
     $original_array = array('test', 'test2');
     $xml = Format::factory($original_array)->to_json();
     $this->assertEquals('["test","test2"]', $xml);
 }
Example #2
0
	/**
	 * Test for Format::factory($foo)->to_csv()
	 *
	 * @test
	 */
	public function test_to_csv()
	{
		$csv = 'field1,field2,field3
"Value 1","35","123123"
"Value 1","Value
line 2","Value 3"';
		
		$this->assertEquals($csv, Format::factory(static::$data)->to_csv());
	}
Example #3
0
 /**
  * Response
  *
  * Takes pure data and optionally a status code, then creates the response
  *
  * @param  array
  * @param  int
  */
 protected function response($data = array(), $http_code = 200)
 {
     if (empty($data)) {
         $this->response->status = 404;
         return;
     }
     $this->response->status = $http_code;
     // If the format method exists, call and return the output in that format
     if (method_exists('Format', 'to_' . $this->request->format)) {
         // Set the correct format header
         $this->response->set_header('Content-Type', $this->_supported_formats[$this->request->format]);
         $this->response->body(Format::factory($data)->{'to_' . $this->request->format}());
     } else {
         $this->response->body((string) $data);
     }
 }
Example #4
0
 /**
  * Test for Format::factory($foo)->to_csv()
  *
  * @test
  * @dataProvider array_provider
  */
 public function test_to_csv($array, $csv)
 {
     $this->assertEquals($csv, Format::factory($array)->to_csv());
 }