1 | <?php |
||
11 | class RecentlyViewedBehavior extends Behavior |
||
12 | { |
||
13 | /** |
||
14 | * @param integer the limit to the number of items in the recently viewed list. |
||
15 | */ |
||
16 | public $limit = 8; // 0 = no limit. |
||
17 | |||
18 | /** |
||
19 | * Adds an item id to a 'recently viewed items' session object. |
||
20 | * @var string the modelClass of the item to store |
||
21 | * @param integer the id of the item to store |
||
22 | */ |
||
23 | |||
24 | 1 | public function setRecentlyViewed($modelClass, $id) |
|
25 | { |
||
26 | // Create the session index |
||
27 | 1 | $index = $modelClass . '_recently_viewed'; |
|
28 | // Check if the session index exists |
||
29 | 1 | if (!isset(Yii::$app->session[$index])) { |
|
30 | 1 | $recentlyViewed = []; |
|
31 | } else { |
||
32 | 1 | $recentlyViewed = Yii::$app->session[$index]; |
|
33 | // Remove the id if it is already in the list |
||
34 | 1 | if (($key = array_search($id, $recentlyViewed)) !== false) { |
|
35 | 1 | unset($recentlyViewed[$key]); |
|
36 | } |
||
37 | // If a limit is set, and the list is at (or over) the limit, remove oldest item(s) |
||
38 | 1 | if ($this->limit > 0 && count($recentlyViewed) >= $this->limit) { |
|
39 | $count = count($recentlyViewed) - $this->limit; |
||
40 | $recentlyViewed = array_slice($recentlyViewed, $count); |
||
41 | } |
||
42 | } |
||
43 | // Add the current item id to the end of the array |
||
44 | 1 | array_push($recentlyViewed, $id); |
|
45 | // Update the session |
||
46 | 1 | Yii::$app->getSession()->set($index, $recentlyViewed); |
|
|
|||
47 | 1 | } |
|
48 | |||
49 | /** |
||
50 | * Retrieves model records from a 'recently viewed items' session object. |
||
51 | * @param string $modelClass the modelClass of the items to retrieve |
||
52 | * @return array|null|static[] |
||
53 | */ |
||
54 | 1 | public function getRecentlyViewed($modelClass) |
|
79 | |||
80 | 1 | public function clearRecentlyViewed($modelClass) |
|
92 | } |
||
93 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: