1 | <?php |
||||||
2 | |||||||
3 | |||||||
4 | namespace carono\yii2file\behaviors; |
||||||
5 | |||||||
6 | |||||||
7 | use carono\yii2file\FileUploadTrait; |
||||||
8 | use carono\yii2migrate\traits\PivotTrait; |
||||||
9 | use yii\db\ActiveRecord; |
||||||
10 | use yii\web\UploadedFile; |
||||||
11 | |||||||
12 | /** |
||||||
13 | * Class PivotFileSaveBehavior |
||||||
14 | * |
||||||
15 | * @package backend\behaviors |
||||||
16 | * @property PivotTrait|ActiveRecord $owner |
||||||
17 | */ |
||||||
18 | class PivotFileSaveBehavior extends \yii\base\Behavior |
||||||
19 | { |
||||||
20 | protected $_pivots; |
||||||
21 | public $attribute; |
||||||
22 | public $pivotClass; |
||||||
23 | public $fileProcess; |
||||||
24 | public $fileClass; |
||||||
25 | |||||||
26 | public function canSetProperty($name, $checkVars = true) |
||||||
27 | { |
||||||
28 | return $name === $this->attribute; |
||||||
29 | } |
||||||
30 | |||||||
31 | public function __set($name, $value) |
||||||
32 | { |
||||||
33 | if ($this->canSetProperty($name)) { |
||||||
34 | $this->setPivots($value); |
||||||
35 | } else { |
||||||
36 | parent::__set($name, $value); |
||||||
37 | } |
||||||
38 | } |
||||||
39 | |||||||
40 | public function getStoredPivots($attribute) |
||||||
41 | { |
||||||
42 | if ($attribute === $this->attribute) { |
||||||
43 | return $this->_pivots; |
||||||
44 | } |
||||||
45 | |||||||
46 | return null; |
||||||
47 | } |
||||||
48 | |||||||
49 | /** |
||||||
50 | * @return ActiveRecord|FileUploadTrait |
||||||
51 | */ |
||||||
52 | protected function getModelClass() |
||||||
53 | { |
||||||
54 | return $this->fileClass; |
||||||
55 | } |
||||||
56 | |||||||
57 | protected function setPivots($values) |
||||||
58 | { |
||||||
59 | if (empty(array_filter((array)$values))) { |
||||||
60 | $values = UploadedFile::getInstances($this->owner, $this->attribute); |
||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
61 | } |
||||||
62 | $class = $this->getModelClass(); |
||||||
63 | $this->_pivots = []; |
||||||
64 | foreach ((array)$values as $value) { |
||||||
65 | if (is_numeric($value)) { |
||||||
66 | $this->_pivots[] = $class::findOne($value); |
||||||
67 | } elseif ($this->fileProcess instanceof \Closure) { |
||||||
68 | $this->_pivots[] = call_user_func_array($this->fileProcess, [$value, $class]); |
||||||
69 | } elseif ($value instanceof UploadedFile || is_string($value)) { |
||||||
70 | $this->_pivots[] = $class::startUpload($value)->process(); |
||||||
0 ignored issues
–
show
The method
startUpload() does not exist on yii\db\ActiveRecord . Are you sure you never get this type here, but always one of the subclasses?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
71 | } |
||||||
72 | } |
||||||
73 | $eventName = $this->owner->isNewRecord ? ActiveRecord::EVENT_AFTER_INSERT : ActiveRecord::EVENT_AFTER_UPDATE; |
||||||
74 | $this->owner->on($eventName, [$this, 'savePivots']); |
||||||
0 ignored issues
–
show
It seems like
on() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
75 | } |
||||||
76 | |||||||
77 | public function savePivots() |
||||||
78 | { |
||||||
79 | foreach ((array)$this->_pivots as $pv) { |
||||||
80 | $this->owner->addPivot($pv, $this->pivotClass); |
||||||
81 | } |
||||||
82 | } |
||||||
83 | } |