aboutsummaryrefslogtreecommitdiff
path: root/formats/JsonFormat.php
blob: a60601a0a0e2c6b0c19e3ac9c7de99ea5bbf5be5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
/**
* Json
* Builds a JSON string from $this->items and return it to browser.
*/
class JsonFormat extends FormatAbstract {

	public function stringify(){
		$items = $this->getItems();
		$toReturn = json_encode($items, JSON_PRETTY_PRINT);

		// Remove invalid non-UTF8 characters
		ini_set('mbstring.substitute_character', 'none');
		$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
		return $toReturn;
	}

	public function display(){
		$this
			->setContentType('application/json; charset=' . $this->getCharset())
			->callContentType();

		return parent::display();
	}
}