Completed
Push — feature/code-analysis ( a26fec...c314d5 )
by Jonathan
07:11
created

JsonController::encode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * webtrees-lib: MyArtJaub library for webtrees
4
 *
5
 * @package MyArtJaub\Webtrees
6
 * @subpackage Controller
7
 * @author Jonathan Jaubart <[email protected]>
8
 * @copyright Copyright (c) 2012, Jonathan Jaubart
9
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
10
 */
11
 namespace MyArtJaub\Webtrees\Controller;
12
13
use Fisharebest\Webtrees\Controller\BaseController;
14
use MyArtJaub\Webtrees\Mvc\MvcException;
15
16
/**
17
 * Base controller for all Json pages
18
 */
19
class JsonController extends BaseController {
20
    
21
    /**
22
     * {@inheritDoc}
23
     * @see \Fisharebest\Webtrees\Controller\BaseController::pageHeader()
24
     */
25
    public function pageHeader() {        
26
        header('Content-Type: application/json');
27
        header('Cache-Control: no-cache, must-revalidate');
28
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
29
        // We've displayed the header - display the footer automatically
30
        register_shutdown_function(array($this, 'pageFooter'));
31
        
32
        return $this;
33
    }
34
    
35
    /**
36
     * {@inheritDoc}
37
     * @see \Fisharebest\Webtrees\Controller\BaseController::pageFooter()
38
     */
39
    public function pageFooter() {
40
        return $this;
41
    }
42
    
43
    /**
44
     * Restrict access.
45
     *
46
     * @param bool $condition
47
     *
48
     * @return $this
49
     */
50
    public function restrictAccess($condition) {
51
        if ($condition !== true) {
52
            throw new MvcException(403);
53
        }
54
    
55
        return $this;
56
    }
57
    
58
    /**
59
     * Encode the data to JSON format.
60
     * 
61
     * @param array $data Data to encode
62
     * @param number $options JSON options mask. See http://php.net/manual/fr/json.constants.php
63
     */
64
    public function encode(array $data, $options = 0) {
65
        echo json_encode($data, $options);
66
    }
67
}
68