|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hiqdev\billing\hiapi\models\relations; |
|
4
|
|
|
|
|
5
|
|
|
use hiqdev\php\billing\EntityInterface; |
|
6
|
|
|
use yii\base\InvalidParamException; |
|
7
|
|
|
use yii\helpers\ArrayHelper; |
|
8
|
|
|
|
|
9
|
|
|
class Bucket |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $items = []; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $sourceKey; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Bucket constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $sourceKey |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($sourceKey) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->sourceKey = $sourceKey; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param $rows |
|
32
|
|
|
* @param $sourceKey |
|
33
|
|
|
* @return static |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function fromRows($rows, $sourceKey) |
|
36
|
|
|
{ |
|
37
|
|
|
$bucket = new static($sourceKey); |
|
38
|
|
|
$bucket->initialize($rows); |
|
39
|
|
|
|
|
40
|
|
|
return $bucket; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function initialize($rows) |
|
44
|
|
|
{ |
|
45
|
|
|
$result = []; |
|
46
|
|
|
foreach ($rows as $row) { |
|
47
|
|
|
$key = $row[$this->sourceKey]; |
|
48
|
|
|
$result[$key] = []; |
|
49
|
|
|
} |
|
50
|
|
|
$this->items = $result; |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getKeys() |
|
56
|
|
|
{ |
|
57
|
|
|
return array_keys($this->items); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param EntityInterface[] $entities |
|
62
|
|
|
* @param string $key the attribute name in $entity that represent this relation |
|
63
|
|
|
* @param string $entityKey |
|
|
|
|
|
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
|
|
public function fill($entities, $key, $entityIdKey) |
|
67
|
|
|
{ |
|
68
|
|
|
foreach ($entities as $entity) { |
|
69
|
|
|
$itemKey = ArrayHelper::getValue($entity, $key); |
|
70
|
|
|
if (!isset($this->items[$itemKey])) { |
|
71
|
|
|
throw new InvalidParamException('This should not happen. Result was not requested in bucket.'); |
|
72
|
|
|
} |
|
73
|
|
|
$entityKey = ArrayHelper::getValue($entity, $entityIdKey); |
|
74
|
|
|
$this->items[$itemKey][$entityKey] = $entity; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function pour(&$rows, $relationName) |
|
81
|
|
|
{ |
|
82
|
|
|
foreach ($rows as &$row) { |
|
83
|
|
|
$keyName = $this->sourceKey; |
|
84
|
|
|
$key = $row[$keyName]; |
|
85
|
|
|
$row[$relationName] = $this->items[$key]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.