Completed
Push — master ( 03fc7e...44f12a )
by Henry
15:26
created

includes/View/Article.php (7 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\View;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Config;
6
use Redaxscript\Content;
7
use Redaxscript\Html;
8
use Redaxscript\Language;
9
use Redaxscript\Model;
10
use Redaxscript\Module;
11
use Redaxscript\Registry;
12
use Redaxscript\Request;
13
use Redaxscript\Validator;
14
15
/**
16
 * children class to create the article
17
 *
18
 * @since 4.0.0
19
 *
20
 * @package Redaxscript
21
 * @category View
22
 * @author Henry Ruhs
23
 */
24
25
class Article extends ViewAbstract
26
{
27
	/**
28
	 * instance of the request class
29
	 *
30
	 * @var Request
31
	 */
32
33
	protected $_request;
34
35
	/**
36
	 * instance of the config class
37
	 *
38
	 * @var Config
39
	 */
40
41
	protected $_config;
42
43
	/**
44
	 * options of the article
45
	 *
46
	 * @var array
47
	 */
48
49
	protected $_optionArray =
50
	[
51
		'tag' =>
52
		[
53
			'title' => 'h2',
54
			'box' => 'div'
55
		],
56
		'className' =>
57
		[
58
			'title' => 'rs-title-content',
59
			'box' => 'rs-box-content'
60
		],
61
		'orderColumn' => 'rank'
62
	];
63
64
	/**
65
	 * constructor of the class
66
	 *
67
	 * @since 4.0.0
68
	 *
69
	 * @param Registry $registry instance of the registry class
70
	 * @param Request $request instance of the request class
71
	 * @param Language $language instance of the language class
72
	 * @param Config $config instance of the config class
73
	 */
74
75
	public function __construct(Registry $registry, Request $request, Language $language, Config $config)
76
	{
77
		parent::__construct($registry, $language);
78
		$this->_request = $request;
79
		$this->_config = $config;
80
	}
81
82
	/**
83
	 * stringify the article
84
	 *
85
	 * @since 4.0.0
86
	 *
87
	 * @return string
88
	 */
89
90
	public function __toString() : string
91
	{
92
		return $this->render();
93
	}
94
95
	/**
96
	 * init the class
97
	 *
98
	 * @since 4.0.0
99
	 *
100
	 * @param array $optionArray options of the article
101
	 */
102
103
	public function init(array $optionArray = [])
104
	{
105
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
106
	}
107
108
	/**
109
	 * render the view
110
	 *
111
	 * @since 4.0.0
112
	 *
113
	 * @param int $categoryId identifier of the category
114
	 * @param int $articleId identifier of the article
115
	 *
116
	 * @return string
117
	 */
118
119
	public function render(int $categoryId = null, int $articleId = null) : string
120
	{
121
		if ($this->_registry->get('articleReplace'))
122
		{
123
			return Module\Hook::trigger('articleReplace');
124
		}
125
		$output = Module\Hook::trigger('articleStart');
126
		$accessValidator = new Validator\Access();
127
		$settingModel = new Model\Setting();
128
		$articleModel = new Model\Article();
129
		$articles = null;
130
		$contentParser = new Content\Parser($this->_registry, $this->_request, $this->_language, $this->_config);
131
		$byline = new Helper\Byline($this->_registry, $this->_language);
132
		$byline->init();
133
		$adminDock = new Admin\View\Helper\Dock($this->_registry, $this->_language);
134
		$adminDock->init();
135
		$language = $this->_registry->get('language');
136
		$loggedIn = $this->_registry->get('loggedIn');
137
		$token = $this->_registry->get('token');
138
		$firstParameter = $this->_registry->get('firstParameter');
139
		$lastSubParameter = $this->_registry->get('lastSubParameter');
140
		$lastTable = $this->_registry->get('lastTable');
141
		$parameterRoute = $this->_registry->get('parameterRoute');
142
		$myGroups = $this->_registry->get('myGroups');
143
144
		/* html element */
145
146
		$element = new Html\Element();
147
		$titleElement = $element
148
			->copy()
149
			->init($this->_optionArray['tag']['title'],
150
			[
151
				'class' => $this->_optionArray['className']['title']
152
			]);
153
		$linkElement = $element->copy()->init('a');
154
		$boxElement = $element
155
			->copy()
156
			->init($this->_optionArray['tag']['box'],
157
			[
158
				'class' => $this->_optionArray['className']['box']
159
			]);
160
161
		/* query articles */
162
163
		if ($categoryId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $categoryId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
164
		{
165
			if ($settingModel->get('pagination'))
0 ignored issues
show
Bug Best Practice introduced by
The expression $settingModel->get('pagination') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
166
			{
167
				$articles = $articleModel->getByCategoryAndLanguageAndOrderAndStep($categoryId, $language, $this->_optionArray['orderColumn'], $lastSubParameter - 1);
0 ignored issues
show
It seems like $language defined by $this->_registry->get('language') on line 135 can also be of type array; however, Redaxscript\Model\Articl...nguageAndOrderAndStep() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
168
			}
169
			else
170
			{
171
				$articles = $articleModel->getByCategoryAndLanguageAndOrder($categoryId, $language, $this->_optionArray['orderColumn']);
0 ignored issues
show
It seems like $language defined by $this->_registry->get('language') on line 135 can also be of type array; however, Redaxscript\Model\Articl...ryAndLanguageAndOrder() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
172
			}
173
		}
174
		else if ($articleId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $articleId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
175
		{
176
			$articles = $articleModel->getByIdAndLanguageAndOrder($articleId, $language, $this->_optionArray['orderColumn']);
0 ignored issues
show
It seems like $language defined by $this->_registry->get('language') on line 135 can also be of type array; however, Redaxscript\Model\Conten...IdAndLanguageAndOrder() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
177
		}
178
179
		/* process articles */
180
181
		if ($articles)
182
		{
183
			foreach ($articles as $value)
184
			{
185
				if ($accessValidator->validate($value->access, $myGroups))
0 ignored issues
show
It seems like $myGroups defined by $this->_registry->get('myGroups') on line 142 can also be of type array; however, Redaxscript\Validator\Access::validate() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
186
				{
187
					$output .= Module\Hook::trigger('articleFragmentStart', (array)$value);
188
					if ($value->headline)
189
					{
190
						$output .= $titleElement
191
							->attr('id', 'article-' . $value->alias)
192
							->html($lastTable === 'categories' ? $linkElement
193
								->attr('href', $parameterRoute . $articleModel->getRouteById($value->id))
194
								->text($value->title) : $value->title
195
							);
196
					}
197
					$contentParser->process($value->text);
198
					$output .= $boxElement->html($contentParser->getOutput()) . $byline->render($value->date, $value->author) . Module\Hook::trigger('articleFragmentEnd', (array)$value);
199
200
					/* admin dock */
201
202
					if ($loggedIn === $token && $firstParameter !== 'logout')
203
					{
204
						$output .= $adminDock->render('articles', $value->id);
205
					}
206
				}
207
			}
208
		}
209
		$output .= Module\Hook::trigger('articleEnd');
210
		return $output;
211
	}
212
}
213