Completed
Push — master ( ece391...331600 )
by Cheren
03:20
created

DocumentHelper::meta()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 3
eloc 9
nc 4
nop 2
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\View\Helper;
17
18
use JBZoo\Utils\Str;
19
use Cake\Core\Configure;
20
21
/**
22
 * Class DocumentHelper
23
 *
24
 * @package Core\View\Helper
25
 * @property \Core\View\Helper\HtmlHelper $Html
26
 */
27
class DocumentHelper extends AppHelper
28
{
29
30
    /**
31
     * Init vars.
32
     *
33
     * @var string
34
     */
35
    public $dir;
36
    public $eol;
37
    public $tab;
38
    public $locale;
39
    public $charset;
40
41
    /**
42
     * Uses helpers.
43
     *
44
     * @var array
45
     */
46
    public $helpers = [
47
        'Core.Html',
48
    ];
49
50
    /**
51
     * Constructor hook method.
52
     *
53
     * @param array $config
54
     */
55
    public function initialize(array $config)
56
    {
57
        parent::initialize($config);
58
59
        $this->dir     = Configure::read('Cms.docDir');
60
        $this->locale  = Configure::read('App.defaultLocale');
61
        $this->charset = Str::low(Configure::read('App.encoding'));
62
        $this->eol     = (Configure::read('debug')) ? PHP_EOL : '';
63
        $this->tab     = (Configure::read('debug')) ? Configure::read('Cms.lineTab') : '';
64
    }
65
66
    /**
67
     * Site language.
68
     *
69
     * @param bool|true $isLang
70
     * @return string
71
     * @throws \Exception
72
     */
73
    public function lang($isLang = true)
74
    {
75
        list($lang, $region) = explode('_', $this->locale);
76
        return ($isLang) ? Str::low($lang) : Str::low($region);
77
    }
78
79
    /**
80
     * Creates a link to an external resource and handles basic meta tags.
81
     *
82
     * @param array $rows
83
     * @param null $block
84
     * @return null|string
85
     */
86
    public function meta(array $rows, $block = null)
87
    {
88
        $output = [];
89
        foreach ($rows as $row) {
90
            $output[] = trim($row);
91
        }
92
93
        $output = implode($this->eol, $output) . $this->eol;
94
95
        if ($block !== null) {
96
            $this->_View->append($block, $output);
97
            return null;
98
        }
99
100
        return $output;
101
    }
102
103
    /**
104
     * Create html 5 document type.
105
     *
106
     * @return string
107
     * @throws \Exception
108
     */
109
    public function type()
110
    {
111
        $lang = $this->lang();
112
        $html = [
113
            '<!doctype html>',
114
            '<!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7 ie6" '
115
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
116
            '<!--[if IE 7]><html class="no-js lt-ie9 lt-ie8 ie7" '
117
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
118
            '<!--[if IE 8]><html class="no-js lt-ie9 ie8" '
119
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
120
            '<!--[if gt IE 8]><!--><html class="no-js" xmlns="http://www.w3.org/1999/xhtml" '
121
            . 'lang="' . $lang . '" dir="' . $this->dir . '" '
122
            . 'prefix="og: http://ogp.me/ns#" '
123
            . '> <!--<![endif]-->',
124
        ];
125
        return implode($this->eol, $html) . $this->eol;
126
    }
127
}
128