Passed
Push — dev ( 191906...4ff9c3 )
by James Ekow Abaka
02:16
created

PhpEngine::strip()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Ntentan Framework
5
 * Copyright (c) 2010-2015 James Ekow Abaka Ainooson
6
 * 
7
 * Permission is hereby granted, free of charge, to any person obtaining
8
 * a copy of this software and associated documentation files (the
9
 * "Software"), to deal in the Software without restriction, including
10
 * without limitation the rights to use, copy, modify, merge, publish,
11
 * distribute, sublicense, and/or sell copies of the Software, and to
12
 * permit persons to whom the Software is furnished to do so, subject to
13
 * the following conditions:
14
 * 
15
 * The above copyright notice and this permission notice shall be
16
 * included in all copies or substantial portions of the Software.
17
 * 
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
25
 */
26
27
namespace ntentan\honam\engines;
28
29
use Exception;
30
use ntentan\honam\engines\php\HelperVariable;
31
use ntentan\honam\engines\php\Janitor;
32
use ntentan\honam\engines\php\Variable;
33
use ntentan\honam\TemplateFileResolver;
34
use ntentan\honam\TemplateRenderer;
35
use ntentan\utils\StringStream;
36
37
/**
38
 * The PHP engine is a template engine built into honam which uses raw PHP as
39
 * the template language. By virtue of this, the PHP engine can boast of high
40
 * performance. Since this engine uses PHP, it has access to all the language's
41
 * features. This could sometimes create a problem since some of these features
42
 * are not intended for templating use.
43
 */
44
class PhpEngine extends AbstractEngine
45
{
46
    /**
47
     * @var HelperFactory
0 ignored issues
show
Bug introduced by
The type ntentan\honam\engines\HelperFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
48
     */
49
    private $helpersLoader;
50
51
    /**
52
     * @var Janitor
53
     */
54
    private $janitor;
55
    private $templateRenderer;
56
57 27
    public function __construct(TemplateRenderer $templateRenderer, HelperVariable $helperVariable, Janitor $janitor)
58
    {
59 27
        $this->helpersLoader = $helperVariable;
0 ignored issues
show
Documentation Bug introduced by
It seems like $helperVariable of type ntentan\honam\engines\php\HelperVariable is incompatible with the declared type ntentan\honam\engines\HelperFactory of property $helpersLoader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60 27
        $this->janitor = $janitor;
61 27
        $this->templateRenderer = $templateRenderer;
62 27
    }
63
64 16
    public function partial($template, $templateData = array())
65
    {
66 16
        return $this->templateRenderer->render($template, $templateData);
67
    }
68
69
    /**
70
     * @param mixed $item
71
     * @return mixed
72
     */
73
    public function unescape($item)
74
    {
75
        if($item instanceof Variable) {
76
            return $item->unescape();
77
        } else {
78
            return $item;
79
        }
80
    }
81
82
    /**
83
     * Passes the data to be rendered to the template engine instance.
84
     */
85 27
    public function renderFromFileTemplate(string $filePath, array $data) : string
86
    {
87
        // Escape each variable by passing it through the variable class.
88
        // Users would have to unescape them by calling the escape method directly
89
        // on the variable.
90 27
        foreach ($data as $_key => $_value) {
91 24
            $$_key = Variable::initialize($_value, $this->janitor);
92
        }
93
94
        // Expose helpers
95 27
        $helpers = $this->helpersLoader;
96
97
        // Start trapping the output buffer and include the PHP template for
98
        // execution.
99 27
        ob_start();
100
        try {
101 27
            include $filePath;
102
        } catch (Exception $e) {
103
            ob_get_flush();
104
            throw $e;
105
        }
106 27
        return ob_get_clean();
107
    }
108
109
    /**
110
     * Passes a template string and data to be rendered to the template engine
111
     * instance.
112
     */
113
    public function renderFromStringTemplate(string $string, array $data) : string
114
    {
115
        StringStream::register();
116
        file_put_contents('string://template', $string);
117
        return $this->renderFromFileTemplate('string://template', $data);
118
    }
119
120
    /**
121
     * A utility function to strip the text of all HTML code. This function
122
     * removes all HTML tags instead of escaping them.
123
     *
124
     * @param string $text
125
     * @return string
126
     */
127 1
    public function strip($text)
128
    {
129 1
        return $this->janitor->cleanHtml($text, true);
130
    }
131
132
    /**
133
     * A utility function to cut long pieces of text into meaningful short
134
     * chunks. The function is very good in cases where you want to show just
135
     * a short preview snippet of a long text. The function cuts the long string
136
     * without cutting through words and appends some sort of ellipsis
137
     * terminator to the text.
138
     *
139
     * @param string $text The text to be truncated.
140
     * @param string $length The maximum lenght of the truncated string. Might
141
     *      return a shorter string if the lenght ends in the middle of a word.
142
     * @param string $terminator The ellipsis terminator to use for the text.
143
     * @return string
144
     */
145 1
    public function truncate($text, $length, $terminator = ' ...')
146
    {
147 1
        while (mb_substr($text, $length, 1) != ' ' && $length > 0) {
0 ignored issues
show
Bug introduced by
$length of type string is incompatible with the type integer expected by parameter $start of mb_substr(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
        while (mb_substr($text, /** @scrutinizer ignore-type */ $length, 1) != ' ' && $length > 0) {
Loading history...
148 1
            $length--;
149
        }
150 1
        return mb_substr($text, 0, $length) . $terminator;
0 ignored issues
show
Bug introduced by
$length of type string is incompatible with the type integer expected by parameter $length of mb_substr(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
        return mb_substr($text, 0, /** @scrutinizer ignore-type */ $length) . $terminator;
Loading history...
151
    }
152
153
    public function __destruct()
154
    {
155
        StringStream::unregister();
156
    }
157
158
}
159