Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
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
53 2
	public function init(array $optionArray = []) : void
54
	{
55 2
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
56 2
	}
57
58
	/**
59
	 * render the view
60
	 *
61
	 * @since 4.0.0
62
	 *
63
	 * @param int $date timestamp of the date
64
	 * @param string $author name of the author
65
	 *
66
	 * @return string
67
	 */
68
69 2
	public function render(int $date = null, string $author = null) : string
70
	{
71 2
		$output = Module\Hook::trigger('bylineStart');
72 2
		$dater = new Dater();
73 2
		$dater->init($date);
74
75
		/* html element */
76
77 2
		$element = new Html\Element();
78
		$boxElement = $element
79 2
			->copy()
80 2
			->init('div',
81
			[
82 2
				'class' => $this->_optionArray['className']['box']
83
			]);
84
		$textElement = $element
85 2
			->copy()
86 2
			->init('span');
87
88
		/* collect output */
89
90 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...
91
		{
92 1
			$boxElement->html(
93
				$textElement
94 1
					->copy()
95 1
					->addClass($this->_optionArray['className']['text']['by'])
96 1
					->text($this->_language->get('posted_by')) .
97
				$textElement
98 1
					->copy()
99 1
					->addClass($this->_optionArray['className']['text']['author'])
100 1
					->text($author)
101
			);
102
		}
103 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...
104
		{
105 1
			$boxElement->append(
106
				$textElement
107 1
					->copy()
108 1
					->addClass($this->_optionArray['className']['text']['on'])
109 1
					->text($this->_language->get('on'))
110
			);
111
		}
112 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...
113
		{
114 2
			$boxElement->append(
115
				$textElement
116 2
					->copy()
117 2
					->addClass($this->_optionArray['className']['text']['date'])
118 2
					->text($dater->formatDate()) .
119
				$textElement
120 2
					->copy()
121 2
					->addClass($this->_optionArray['className']['text']['at'])
122 2
					->text($this->_language->get('at')) .
123
				$textElement
124 2
					->copy()
125 2
					->addClass($this->_optionArray['className']['text']['time'])
126 2
					->text($dater->formatTime())
127
			);
128
		}
129 2
		$output .= $boxElement . Module\Hook::trigger('bylineEnd');
130 2
		return $output;
131
	}
132
}
133