Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Http\Testing;
4
 
5
use Illuminate\Http\UploadedFile;
6
 
7
class File extends UploadedFile
8
{
9
    /**
10
     * The name of the file.
11
     *
12
     * @var string
13
     */
14
    public $name;
15
 
16
    /**
17
     * The temporary file resource.
18
     *
19
     * @var resource
20
     */
21
    public $tempFile;
22
 
23
    /**
24
     * The "size" to report.
25
     *
26
     * @var int
27
     */
28
    public $sizeToReport;
29
 
30
    /**
31
     * The MIME type to report.
32
     *
33
     * @var string|null
34
     */
35
    public $mimeTypeToReport;
36
 
37
    /**
38
     * Create a new file instance.
39
     *
40
     * @param  string  $name
41
     * @param  resource  $tempFile
42
     * @return void
43
     */
44
    public function __construct($name, $tempFile)
45
    {
46
        $this->name = $name;
47
        $this->tempFile = $tempFile;
48
 
49
        parent::__construct(
50
            $this->tempFilePath(), $name, $this->getMimeType(),
51
            null, true
52
        );
53
    }
54
 
55
    /**
56
     * Create a new fake file.
57
     *
58
     * @param  string  $name
59
     * @param  string|int  $kilobytes
60
     * @return \Illuminate\Http\Testing\File
61
     */
62
    public static function create($name, $kilobytes = 0)
63
    {
64
        return (new FileFactory)->create($name, $kilobytes);
65
    }
66
 
67
    /**
68
     * Create a new fake file with content.
69
     *
70
     * @param  string  $name
71
     * @param  string  $content
72
     * @return \Illuminate\Http\Testing\File
73
     */
74
    public static function createWithContent($name, $content)
75
    {
76
        return (new FileFactory)->createWithContent($name, $content);
77
    }
78
 
79
    /**
80
     * Create a new fake image.
81
     *
82
     * @param  string  $name
83
     * @param  int  $width
84
     * @param  int  $height
85
     * @return \Illuminate\Http\Testing\File
86
     */
87
    public static function image($name, $width = 10, $height = 10)
88
    {
89
        return (new FileFactory)->image($name, $width, $height);
90
    }
91
 
92
    /**
93
     * Set the "size" of the file in kilobytes.
94
     *
95
     * @param  int  $kilobytes
96
     * @return $this
97
     */
98
    public function size($kilobytes)
99
    {
100
        $this->sizeToReport = $kilobytes * 1024;
101
 
102
        return $this;
103
    }
104
 
105
    /**
106
     * Get the size of the file.
107
     *
108
     * @return int
109
     */
110
    public function getSize(): int
111
    {
112
        return $this->sizeToReport ?: parent::getSize();
113
    }
114
 
115
    /**
116
     * Set the "MIME type" for the file.
117
     *
118
     * @param  string  $mimeType
119
     * @return $this
120
     */
121
    public function mimeType($mimeType)
122
    {
123
        $this->mimeTypeToReport = $mimeType;
124
 
125
        return $this;
126
    }
127
 
128
    /**
129
     * Get the MIME type of the file.
130
     *
131
     * @return string
132
     */
133
    public function getMimeType(): string
134
    {
135
        return $this->mimeTypeToReport ?: MimeType::from($this->name);
136
    }
137
 
138
    /**
139
     * Get the path to the temporary file.
140
     *
141
     * @return string
142
     */
143
    protected function tempFilePath()
144
    {
145
        return stream_get_meta_data($this->tempFile)['uri'];
146
    }
147
}