-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add middleware for trimming/converting to null for cell values
- Loading branch information
1 parent
f6db323
commit 9a3ebc0
Showing
9 changed files
with
142 additions
and
16 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Maatwebsite\Excel\Middleware; | ||
|
||
use Maatwebsite\Excel\Cell; | ||
|
||
abstract class CellMiddleware | ||
{ | ||
/** | ||
* @param mixed $value | ||
* | ||
* @return mixed | ||
*/ | ||
abstract public function __invoke($value, callable $next); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Maatwebsite\Excel\Middleware; | ||
|
||
class ConvertEmptyCellValuesToNull extends CellMiddleware | ||
{ | ||
/** | ||
* @param mixed $value | ||
* | ||
* @return mixed | ||
*/ | ||
public function __invoke($value, callable $next) | ||
{ | ||
return $next( | ||
$value === '' ? null : $value | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Maatwebsite\Excel\Middleware; | ||
|
||
class TrimCellValue extends CellMiddleware | ||
{ | ||
|
||
/** | ||
* @param mixed $value | ||
* | ||
* @return mixed | ||
*/ | ||
public function __invoke($value, callable $next) | ||
{ | ||
if (!is_string($value)) { | ||
return $next($value); | ||
} | ||
|
||
// Remove whitespace, BOM and zero width spaces. | ||
$cleaned = preg_replace('~^[\s\x{FEFF}\x{200B}]+|[\s\x{FEFF}\x{200B}]+$~u', '', $value) ?? trim($value); | ||
|
||
return $next($cleaned); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Maatwebsite\Excel\Tests; | ||
|
||
use Maatwebsite\Excel\Cell; | ||
use Maatwebsite\Excel\Middleware\ConvertEmptyCellValuesToNull; | ||
use Maatwebsite\Excel\Middleware\TrimCellValue; | ||
|
||
class CellTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function can_get_cell_value() | ||
{ | ||
$worksheet = $this->read(__DIR__ . '/Data/Disks/Local/import-middleware.xlsx', 'Xlsx'); | ||
|
||
$this->assertEquals('test', Cell::make($worksheet->getActiveSheet(), 'A1')->getValue()); | ||
|
||
// By default spaces are not removed | ||
$this->assertEquals(' ', Cell::make($worksheet->getActiveSheet(), 'A2')->getValue()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_trim_empty_cells() | ||
{ | ||
config()->set('excel.imports.cells.middleware', [ | ||
TrimCellValue::class, | ||
]); | ||
|
||
$worksheet = $this->read(__DIR__ . '/Data/Disks/Local/import-middleware.xlsx', 'Xlsx'); | ||
|
||
$this->assertEquals('', Cell::make($worksheet->getActiveSheet(), 'A2')->getValue()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function convert_empty_cells_to_null() | ||
{ | ||
config()->set('excel.imports.cells.middleware', [ | ||
TrimCellValue::class, | ||
ConvertEmptyCellValuesToNull::class, | ||
]); | ||
|
||
$worksheet = $this->read(__DIR__ . '/Data/Disks/Local/import-middleware.xlsx', 'Xlsx'); | ||
|
||
$this->assertEquals(null, Cell::make($worksheet->getActiveSheet(), 'A2')->getValue()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.