Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

includes/Model/Comment.php (1 issue)

Check for loose comparison of integers.

Best Practice Bug Major

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\Model;
3
4
use function array_filter;
5
use function implode;
6
use function is_array;
7
8
/**
9
 * parent class to provide the comment model
10
 *
11
 * @since 3.3.0
12
 *
13
 * @package Redaxscript
14
 * @category Model
15
 * @author Henry Ruhs
16
 */
17
18
class Comment extends ContentAbstract
19
{
20
	/**
21
	 * name of the table
22
	 *
23
	 * @var string
24
	 */
25
26
	protected $_table = 'comments';
27
28
	/**
29
	 * count the comments by article and language
30
	 *
31
	 * @since 4.0.0
32
	 *
33
	 * @param int $articleId identifier of the article
34
	 * @param string $language
35
	 *
36
	 * @return int|null
37
	 */
38
39
	public function countByArticleAndLanguage(int $articleId = null, string $language = null) : ?int
40
	{
41
		return $this
42
			->query()
43
			->where(
44
			[
45
				'article' => $articleId,
46
				'status' => 1
47
			])
48
			->whereLanguageIs($language)
49
			->count() ? : null;
50
	}
51
52
	/**
53
	 * get the articles by category and language and order
54
	 *
55
	 * @since 4.0.0
56
	 *
57
	 * @param int $articleId identifier of the article
58
	 * @param string $language
59
	 * @param string $orderColumn
60
	 *
61
	 * @return object|null
62
	 */
63
64
	public function getByArticleAndLanguageAndOrder(int $articleId = null, string $language = null, string $orderColumn = null) : ?object
65
	{
66
		return $this
67
			->query()
68
			->where(
69
			[
70
				'article' => $articleId,
71
				'status' => 1
72
			])
73
			->whereLanguageIs($language)
74
			->orderBySetting($orderColumn)
75
			->findMany() ? : null;
76
	}
77
78
	/**
79
	 * get the articles by category and language and order and step
80
	 *
81
	 * @since 4.0.0
82
	 *
83
	 * @param int $articleId identifier of the article
84
	 * @param string $language
85
	 * @param string $orderColumn
86
	 * @param int $limitStep
87
	 *
88
	 * @return object|null
89
	 */
90
91
	public function getByArticleAndLanguageAndOrderAndStep(int $articleId = null, string $language = null, string $orderColumn = null, int $limitStep = null) : ?object
92
	{
93
		return $this
94
			->query()
95
			->where(
96
			[
97
				'article' => $articleId,
98
				'status' => 1
99
			])
100
			->whereLanguageIs($language)
101
			->orderBySetting($orderColumn)
102
			->limitBySetting($limitStep)
103
			->findMany() ? : null;
104
	}
105
106
	/**
107
	 * get the comment route by id
108
	 *
109
	 * @since 3.3.0
110
	 *
111
	 * @param int $commentId identifier of the comment
112
	 *
113
	 * @return string|null
114
	 */
115
116 4
	public function getRouteById(int $commentId = null) : ?string
117
	{
118 4
		if ($commentId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $commentId 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...
119
		{
120
			$routeArray = $this
121 3
				->query()
122 3
				->tableAlias('comment')
123 3
				->leftJoinPrefix('articles', 'comment.article = article.id', 'article')
124 3
				->leftJoinPrefix('categories', 'article.category = category.id', 'category')
125 3
				->leftJoinPrefix('categories', 'category.parent = parent.id', 'parent')
126 3
				->select('parent.alias', 'parentAlias')
127 3
				->select('category.alias', 'categoryAlias')
128 3
				->select('article.alias', 'articleAlias')
129 3
				->where('comment.id', $commentId)
130 3
				->findArray();
131
132
			/* handle route */
133
134 3
			if (is_array($routeArray[0]))
135
			{
136 3
				return implode('/', array_filter($routeArray[0])) . '#comment-' . $commentId;
137
			}
138
		}
139 1
		return null;
140
	}
141
142
	/**
143
	 * create the comment by array
144
	 *
145
	 * @since 3.3.0
146
	 *
147
	 * @param array $createArray array of the create
148
	 *
149
	 * @return bool
150
	 */
151
152
	public function createByArray(array $createArray = []) : bool
153
	{
154
		return $this
155
			->query()
156
			->create()
157
			->set($createArray)
158
			->save();
159
	}
160
}
161