1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: VITALYIEGOROV |
5
|
|
|
* Date: 08.12.15 |
6
|
|
|
* Time: 22:14 |
7
|
|
|
*/ |
8
|
|
|
namespace samsoncms\api; |
9
|
|
|
|
10
|
|
|
use samsonframework\orm\QueryInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Generic SamsonCMS entity which has Navigation relation. |
14
|
|
|
* @package samsoncms\api |
15
|
|
|
*/ |
16
|
|
|
class Entity |
17
|
|
|
{ |
18
|
|
|
/** Deletion flag field name */ |
19
|
|
|
const DELETE_FLAG_FIELD = 'Active'; |
20
|
|
|
|
21
|
|
|
/** @var QueryInterface Database query instance */ |
22
|
|
|
protected $query; |
23
|
|
|
|
24
|
|
|
/** @var string Entity identifier */ |
25
|
|
|
protected $identifier; |
26
|
|
|
|
27
|
|
|
/** @var string Entity primary field name */ |
28
|
|
|
protected $primaryField; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Entity constructor. |
32
|
|
|
* @param QueryInterface $query Database query instance |
33
|
|
|
* @param string $identifier Entity identifier |
34
|
|
|
*/ |
35
|
|
|
public function __construct(QueryInterface $query, $identifier) |
36
|
|
|
{ |
37
|
|
|
$this->query = $query; |
38
|
|
|
$this->identifier = $identifier; |
39
|
|
|
$this->primaryField = $identifier::$_primary; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get current entity identifiers collection by navigation identifier. |
44
|
|
|
* |
45
|
|
|
* @param string $navigationID Navigation identifier |
46
|
|
|
* @param array $filteringIDs Collection of entity identifiers for filtering query |
47
|
|
|
* @return array Collection of entity identifiers filtered by navigation identifier. |
48
|
|
|
*/ |
49
|
|
|
public function idsByNavigationID($navigationID, $filteringIDs = null) |
50
|
|
|
{ |
51
|
|
|
// Prepare query |
52
|
|
|
$this->query->where(Navigation::$_primary, $navigationID)->where(self::DELETE_FLAG_FIELD, 1); |
53
|
|
|
|
54
|
|
|
// Add material identifier filter if passed |
55
|
|
|
if (isset($filteringIDs)) { |
56
|
|
|
$this->query->where($this->primaryField, $filteringIDs); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Perform database query and get only material identifiers collection |
60
|
|
|
return $this->query->fields($this->primaryField); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get current entity instances collection by their identifiers. |
65
|
|
|
* Method can accept different query executors. |
66
|
|
|
* |
67
|
|
|
* @param string|array $entityIDs Entity identifier or their collection |
68
|
|
|
* @param string $executor Method name for query execution |
69
|
|
|
* @return self[] Collection of entity instances |
70
|
|
|
*/ |
71
|
|
|
public function byIDs($entityIDs, $executor = 'exec') |
72
|
|
|
{ |
73
|
|
|
return $this->query |
74
|
|
|
->where($this->primaryField, $entityIDs) |
|
|
|
|
75
|
|
|
->where(self::DELETE_FLAG_FIELD, 1) |
76
|
|
|
->$executor(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get current entity instances collection by navigation identifier. |
81
|
|
|
* |
82
|
|
|
* @param string $navigationID Navigation identifier |
83
|
|
|
* @return self[] Collection of entity instances |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function byNavigationID($navigationID) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$return = array(); |
88
|
|
|
/** @var array $materialIds Collection of entity identifiers filtered by additional field */ |
89
|
|
|
if (sizeof($materialIds = $this->idsByNavigationID($navigationID))) { |
90
|
|
|
$return = $this->byIDs($materialIds); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get current entity instances amount by navigation identifier. |
98
|
|
|
* |
99
|
|
|
* @param string $navigationID Navigation identifier |
100
|
|
|
* @return integer Amount of entities related to Navigation identifier |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function amountByNavigationID($navigationID) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$return =0; |
105
|
|
|
/** @var array $materialIds Collection of entity identifiers filtered by additional field */ |
106
|
|
|
if (sizeof($materialIds = $this->idsByNavigationID($navigationID))) { |
107
|
|
|
$return = $this->byIDs($materialIds, 'count'); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $return; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: