Skip to content

Commit

Permalink
Change file property visibility to protected (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohorev authored May 15, 2020
1 parent 946439c commit ebc687a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ php:
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4

sudo: false

Expand All @@ -25,7 +27,7 @@ before_script:
fi
script:
- phpunit --verbose $PHPUNIT_FLAGS
- ./vendor/bin/phpunit --verbose $PHPUNIT_FLAGS

after_script:
- |
Expand Down
24 changes: 12 additions & 12 deletions src/UploadBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class UploadBehavior extends Behavior
/**
* @var UploadedFile the uploaded file instance.
*/
private $_file;
protected $file;


/**
Expand Down Expand Up @@ -132,17 +132,17 @@ public function beforeValidate()
$model = $this->owner;
if (in_array($model->scenario, $this->scenarios)) {
if (($file = $model->getAttribute($this->attribute)) instanceof UploadedFile) {
$this->_file = $file;
$this->file = $file;
} else {
if ($this->instanceByName === true) {
$this->_file = UploadedFile::getInstanceByName($this->attribute);
$this->file = UploadedFile::getInstanceByName($this->attribute);
} else {
$this->_file = UploadedFile::getInstance($model, $this->attribute);
$this->file = UploadedFile::getInstance($model, $this->attribute);
}
}
if ($this->_file instanceof UploadedFile) {
$this->_file->name = $this->getFileName($this->_file);
$model->setAttribute($this->attribute, $this->_file);
if ($this->file instanceof UploadedFile) {
$this->file->name = $this->getFileName($this->file);
$model->setAttribute($this->attribute, $this->file);
}
}
}
Expand All @@ -155,13 +155,13 @@ public function beforeSave()
/** @var BaseActiveRecord $model */
$model = $this->owner;
if (in_array($model->scenario, $this->scenarios)) {
if ($this->_file instanceof UploadedFile) {
if ($this->file instanceof UploadedFile) {
if (!$model->getIsNewRecord() && $model->isAttributeChanged($this->attribute)) {
if ($this->unlinkOnSave === true) {
$this->delete($this->attribute, true);
}
}
$model->setAttribute($this->attribute, $this->_file->name);
$model->setAttribute($this->attribute, $this->file->name);
} else {
// Protect attribute
unset($model->{$this->attribute});
Expand All @@ -181,10 +181,10 @@ public function beforeSave()
*/
public function afterSave()
{
if ($this->_file instanceof UploadedFile) {
if ($this->file instanceof UploadedFile) {
$path = $this->getUploadPath($this->attribute);
if (is_string($path) && FileHelper::createDirectory(dirname($path))) {
$this->save($this->_file, $path);
$this->save($this->file, $path);
$this->afterUpload();
} else {
throw new InvalidArgumentException(
Expand Down Expand Up @@ -242,7 +242,7 @@ public function getUploadUrl($attribute)
*/
protected function getUploadedFile()
{
return $this->_file;
return $this->file;
}

/**
Expand Down

0 comments on commit ebc687a

Please sign in to comment.