FRED™  3.0
FRED™: Framework for Rapid and Easy Development
View.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred\Controller;
4 
5 class View extends \Rsi\Fred\Component{
6 
7  public $convert = false;
8  public $minifiedPath = null;
9  public $numericCheck = false; //!< Convert numeric strings to numbers.
10  public $maxInt = 0x7fffffff; //!< Higher numbers (integers) will not be converted (from string).
11 
12  protected $_title = null;
13 
14  protected $_controller = null;
15 
16  protected function init(){
17  parent::init();
18  $this->publish('title');
19  }
20 
21  public function jsonEncode($data){
22  if($this->numericCheck) array_walk_recursive($data,function(&$value){ //JSON_NUMERIC_CHECK removes leading zeros and plus signs
23  if(\Rsi\Str::numeric($value) && (strpos('.',$value) || (abs($value) < $this->maxInt))) $value += 0;
24  });
25  return json_encode($data,$this->_fred->debug ? JSON_PRETTY_PRINT : null);
26  }
27 
28  public function render(){
29  if(!headers_sent()) header('Content-type: application/json; charset=utf-8');
30  print($this->jsonEncode($data = $this->data));
31  if($error = json_last_error())
32  $this->_fred->log->error('JSON encoding error: ' . json_last_error_msg(),__FILE__,__LINE__,compact('data','error'));
33  }
34 
35  protected function getData(){
36  $request = $this->_fred->request;
37  $data = [
38  'data' => $request->data,
39  'result' => $request->result,
40  ];
41  if(!$request->action) $data['config'] = array_merge(
42  $this->_fred->clientConfig(),
43  ['controller' => $this->_controller->clientConfig()]
44  );
45  if($request->errors) $data['errors'] = $request->errors;
46  if($request->redir) $data['redir'] = $request->redir;
47  elseif($messages = $this->component('message')->retrieve()) $data['messages'] = $messages;
48  return $data;
49  }
50 
51 }
$maxInt
Higher numbers (integers) will not be converted (from string).
Definition: View.php:10
publish($property, $visibility=self::READABLE)
Publish a property (or hide it again).
Definition: Thing.php:27
component($name)
Get a component (local or default).
Definition: Component.php:80
$numericCheck
Convert numeric strings to numbers.
Definition: View.php:9