Completed
Push — master ( 4eb4a8...a52438 )
by Henry
07:48
created

includes/View/Helper/Byline.php (4 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\Helper;
3
4
use Redaxscript\Dater;
5
use Redaxscript\Html;
6
use Redaxscript\Module;
7
use Redaxscript\View\ViewAbstract;
8
use function array_replace_recursive;
9
10
/**
11
 * helper class to create the byline
12
 *
13
 * @since 4.0.0
14
 *
15
 * @package Redaxscript
16
 * @category View
17
 * @author Henry Ruhs
18
 */
19
20
class Byline extends ViewAbstract
21
{
22
	/**
23
	 * options of the byline
24
	 *
25
	 * @var array
26
	 */
27
28
	protected $_optionArray =
29
	[
30
		'className' =>
31
		[
32
			'box' => 'rs-box-byline',
33
			'text' =>
34
			[
35
				'by' => 'rs-text-by',
36
				'author' => 'rs-text-author',
37
				'on' => 'rs-text-on',
38
				'date' => 'rs-text-date',
39
				'at' => 'rs-text-at',
40
				'time' => 'rs-text-time',
41
			]
42
		]
43
	];
44
45
	/**
46
	 * init the class
47
	 *
48
	 * @since 4.0.0
49
	 *
50
	 * @param array $optionArray options of the pagination
51
	 *
52
	 * @return self
53
	 */
54
55 2
	public function init(array $optionArray = []) : self
56
	{
57 2
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
58 2
		return $this;
59
	}
60
61
	/**
62
	 * render the view
63
	 *
64
	 * @since 4.0.0
65
	 *
66
	 * @param int $date timestamp of the date
67
	 * @param string $author name of the author
68
	 *
69
	 * @return string
70
	 */
71
72 2
	public function render(int $date = null, string $author = null) : string
73
	{
74 2
		$output = Module\Hook::trigger('bylineStart');
75 2
		$dater = new Dater();
76 2
		$dater->init($date);
77
78
		/* html element */
79
80 2
		$element = new Html\Element();
81
		$boxElement = $element
82 2
			->copy()
83 2
			->init('div',
84
			[
85 2
				'class' => $this->_optionArray['className']['box']
86
			]);
87
		$textElement = $element
88 2
			->copy()
89 2
			->init('span');
90
91
		/* collect output */
92
93 2
		if ($author)
0 ignored issues
show
Bug Best Practice introduced by
The expression $author of type null|string 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...
94
		{
95 1
			$boxElement->html(
96
				$textElement
97 1
					->copy()
98 1
					->addClass($this->_optionArray['className']['text']['by'])
99 1
					->text($this->_language->get('posted_by')) .
100
				$textElement
101 1
					->copy()
102 1
					->addClass($this->_optionArray['className']['text']['author'])
103 1
					->text($author)
104
			);
105
		}
106 2
		if ($author && $date)
0 ignored issues
show
Bug Best Practice introduced by
The expression $author of type null|string 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...
Bug Best Practice introduced by
The expression $date 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...
107
		{
108 1
			$boxElement->append(
109
				$textElement
110 1
					->copy()
111 1
					->addClass($this->_optionArray['className']['text']['on'])
112 1
					->text($this->_language->get('on'))
113
			);
114
		}
115 2
		if ($date)
0 ignored issues
show
Bug Best Practice introduced by
The expression $date 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...
116
		{
117 2
			$boxElement->append(
118
				$textElement
119 2
					->copy()
120 2
					->addClass($this->_optionArray['className']['text']['date'])
121 2
					->text($dater->formatDate()) .
122
				$textElement
123 2
					->copy()
124 2
					->addClass($this->_optionArray['className']['text']['at'])
125 2
					->text($this->_language->get('at')) .
126
				$textElement
127 2
					->copy()
128 2
					->addClass($this->_optionArray['className']['text']['time'])
129 2
					->text($dater->formatTime())
130
			);
131
		}
132 2
		$output .= $boxElement . Module\Hook::trigger('bylineEnd');
133 2
		return $output;
134
	}
135
}
136