Skip to content

Commit d36fa07

Browse files
committed
Merge branch 'develop' into wip/1.2
* develop: Apply fix from 1.0 Add test case for trimStringAttributes flag Make string attribute value auto-trimming optional (#97) # Conflicts: # src/Database/Attach/File.php
2 parents 768c2c4 + ee7bba6 commit d36fa07

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/Database/Attach/File.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ public function fromPost($uploadedFile)
128128
* @param string $filePath The path to the file.
129129
* @return static
130130
*/
131-
public function fromFile($filePath)
131+
public function fromFile($filePath, $filename = null)
132132
{
133133
$file = new FileObj($filePath);
134-
$this->file_name = $file->getFilename();
134+
$this->file_name = empty($filename) ? $file->getFilename() : $filename;
135135
$this->file_size = $file->getSize();
136136
$this->content_type = $file->getMimeType();
137137
$this->disk_name = $this->getDiskName();
@@ -150,10 +150,11 @@ public function fromFile($filePath)
150150
*/
151151
public function fromData($data, $filename)
152152
{
153-
$tempPath = temp_path($filename);
153+
$tempName = str_replace('.', '', uniqid('', true)) . '.tmp';
154+
$tempPath = temp_path($tempName);
154155
FileHelper::put($tempPath, $data);
155156

156-
$file = $this->fromFile($tempPath);
157+
$file = $this->fromFile($tempPath, basename($filename));
157158
FileHelper::delete($tempPath);
158159

159160
return $file;

src/Database/Model.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class Model extends EloquentModel implements ModelInterface
5252
*/
5353
public $duplicateCache = true;
5454

55+
/**
56+
* @var bool Indicates if all string model attributes will be trimmed prior to saving.
57+
*/
58+
public $trimStringAttributes = true;
59+
5560
/**
5661
* @var array The array of models booted events.
5762
*/
@@ -1243,7 +1248,7 @@ public function setAttribute($key, $value)
12431248
/*
12441249
* Trim strings
12451250
*/
1246-
if (is_string($value)) {
1251+
if ($this->trimStringAttributes && is_string($value)) {
12471252
$value = trim($value);
12481253
}
12491254

tests/Database/ModelTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,28 @@ public function testAddCasts()
2222
$this->assertEquals(['id' => 'int', 'foo' => 'int'], $model->getCasts());
2323
}
2424

25+
public function testStringIsTrimmed()
26+
{
27+
$name = "Name";
28+
$nameWithSpace = " ${name} ";
29+
$model = new TestModelGuarded();
30+
31+
$model->name = $nameWithSpace;
32+
$model->save();
33+
34+
// Make sure we load the database saved model
35+
$model->refresh();
36+
$this->assertEquals($name, $model->name);
37+
38+
$model->trimStringAttributes = false;
39+
$model->name = $nameWithSpace;
40+
$model->save();
41+
42+
// Refresh the model from the database
43+
$model->refresh();
44+
$this->assertEquals($nameWithSpace, $model->name);
45+
}
46+
2547
public function testIsGuarded()
2648
{
2749
$model = new TestModelGuarded();

0 commit comments

Comments
 (0)