Total Complexity | 10 |
Total Lines | 41 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
15 | class FileSaveBehavior extends \yii\base\Behavior |
||
16 | { |
||
17 | protected $_file; |
||
18 | public $attribute; |
||
19 | public $field; |
||
20 | /** |
||
21 | * @var FileUploadTrait |
||
22 | */ |
||
23 | public $fileClass; |
||
24 | |||
25 | public function canSetProperty($name, $checkVars = true) |
||
26 | { |
||
27 | return $name == $this->attribute; |
||
28 | } |
||
29 | |||
30 | public function __set($name, $value) |
||
31 | { |
||
32 | if ($this->canSetProperty($name)) { |
||
33 | $this->setFile($value); |
||
34 | } else { |
||
35 | parent::__set($name, $value); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | protected function setFile($value) |
||
40 | { |
||
41 | if ($value instanceof UploadedFile) { |
||
42 | $this->_file = $value; |
||
43 | } else { |
||
44 | $this->_file = UploadedFile::getInstance($this->owner, $this->attribute); |
||
45 | } |
||
46 | $eventName = $this->owner->isNewRecord ? ActiveRecord::EVENT_BEFORE_INSERT : ActiveRecord::EVENT_BEFORE_UPDATE; |
||
47 | $this->owner->on($eventName, [$this, 'saveFile']); |
||
48 | } |
||
49 | |||
50 | public function saveFile() |
||
56 | } |
||
57 | } |
||
58 | } |