Skip to content

Commit d6c77ab

Browse files
committed
add getInstance mehtod like in UploadedFile
1 parent 4f21774 commit d6c77ab

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,31 @@ Usage
3232
**Example 1**
3333
```php
3434
$model = new Post();
35+
$model->load(Yii::$app->request->post());
36+
37+
$file = UploadFromUrl::getInstance( $model, 'image' );
38+
39+
//if second parameter is TRUE it writes uploaded file path to this model property
40+
$file->saveAs( 'uploads/yii.png', true );
41+
42+
echo $model->image; // uploads/yii.png
43+
```
44+
45+
**Example 2**
46+
```php
47+
$model = new Post();
3548
$model->image = 'http://static.yiiframework.com/files/logo/yii.png';
3649

3750
$file = UploadFromUrl::initWithModel( $model, 'image' );
3851

39-
//if second parameter is TRUE it writes uploaded file path to this model property
52+
//if second parameter is TRUE it writes uploaded file path to this model property
4053
$file->saveAs( 'uploads/yii.png', true );
4154

4255
echo $model->image; // uploads/yii.png
4356
```
4457

4558

46-
**Example 2**
59+
**Example 3**
4760
```php
4861
$url = 'http://static.yiiframework.com/files/logo/yii.png' ;
4962
$path = 'uploads/yii.png';
@@ -57,7 +70,7 @@ $model->image = $path;
5770
```
5871

5972

60-
**Example 3**
73+
**Example 4**
6174
```php
6275
$url = 'http://static.yiiframework.com/files/logo/yii.png' ;
6376
$path = 'uploads/yii.png';

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"autoload": {
2424
"psr-4": {
25-
"igogo5yo\\uploadfromurl\\": ""
25+
"igogo5yo\\uploadfromurl\\": "src"
2626
}
2727
}
28-
}
28+
}
File renamed without changes.

UploadFromUrl.php renamed to src/UploadFromUrl.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function initWithUrl($url)
7777
]);
7878
}
7979

80-
public static function initWithModel(ActiveRecord $model, $attribute)
80+
public static function initWithModel($model, $attribute)
8181
{
8282
return self::createInstance([
8383
'url' => $model->{$attribute},
@@ -87,7 +87,7 @@ public static function initWithModel(ActiveRecord $model, $attribute)
8787
]);
8888
}
8989

90-
public static function initWithUrlAndModel($url, ActiveRecord $model, $attribute)
90+
public static function initWithUrlAndModel($url, $model, $attribute)
9191
{
9292
return self::createInstance([
9393
'url' => $url,
@@ -97,6 +97,11 @@ public static function initWithUrlAndModel($url, ActiveRecord $model, $attribute
9797
]);
9898
}
9999

100+
public static function getInstance($model, $attribute)
101+
{
102+
return self::initWithModel($model, $attribute);
103+
}
104+
100105
public function saveAs($file, $saveToModel = false)
101106
{
102107
if ($saveToModel && $this->isWithModel) {

0 commit comments

Comments
 (0)