1
|
|
|
<?php namespace XoopsModules\Smartobject; |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* You may not change or alter any portion of this comment or credits |
5
|
|
|
* of supporting developers from this source code or any supporting source code |
6
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
7
|
|
|
* |
8
|
|
|
* This program is distributed in the hope that it will be useful, |
9
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @copyright XOOPS Project https://xoops.org/ |
15
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
16
|
|
|
* @package |
17
|
|
|
* @since |
18
|
|
|
* @author XOOPS Development Team |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use XoopsModules\Smartobject; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class SmartPlugin |
25
|
|
|
*/ |
26
|
|
|
class Plugin |
27
|
|
|
{ |
28
|
|
|
public $_infoArray; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* SmartPlugin constructor. |
32
|
|
|
* @param $array |
33
|
|
|
*/ |
34
|
|
|
public function __construct($array) |
35
|
|
|
{ |
36
|
|
|
$this->_infoArray = $array; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param $item |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
public function getItemInfo($item) |
44
|
|
|
{ |
45
|
|
|
if (isset($this->_infoArray['items'][$item])) { |
46
|
|
|
return $this->_infoArray['items'][$item]; |
47
|
|
|
} else { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function getItemList() |
56
|
|
|
{ |
57
|
|
|
$itemsArray = $this->_infoArray['items']; |
58
|
|
|
foreach ($itemsArray as $k => $v) { |
59
|
|
|
$ret[$k] = $v['caption']; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $ret; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return bool|int|string |
67
|
|
|
*/ |
68
|
|
|
public function getItem() |
69
|
|
|
{ |
70
|
|
|
$ret = false; |
71
|
|
|
foreach ($this->_infoArray['items'] as $k => $v) { |
72
|
|
|
$search_str = str_replace('%u', '', $v['url']); |
73
|
|
|
if (strpos($_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'], $search_str) > 0) { |
74
|
|
|
$ret = $k; |
75
|
|
|
break; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $ret; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $item |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function getItemIdForItem($item) |
87
|
|
|
{ |
88
|
|
|
return $_REQUEST[$this->_infoArray['items'][$item]['request']]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.