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

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