Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cache Successful Result For Future Use
Per suggestion from @MarkBaker
  • Loading branch information
oleibman committed Jul 11, 2021
commit 6536281479dcc06f963b221035e69c483365b0c4
13 changes: 8 additions & 5 deletions src/PhpSpreadsheet/Shared/CodePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class CodePage
{
public const DEFAULT_CODE_PAGE = 'CP1252';

private const PAGE_ARRAY = [
/** @var array */
private static $pageArray = [
0 => 'CP1252', // CodePage is not always correctly set when the xls file was saved by Apple's Numbers program
367 => 'ASCII', // ASCII
437 => 'CP437', // OEM US
Expand Down Expand Up @@ -70,7 +71,7 @@ class CodePage

public static function validate(string $codePage): bool
{
return in_array($codePage, self::PAGE_ARRAY, true);
return in_array($codePage, self::$pageArray, true);
}

/**
Expand All @@ -83,11 +84,13 @@ public static function validate(string $codePage): bool
*/
public static function numberToName(int $codePage): string
{
if (array_key_exists($codePage, self::PAGE_ARRAY)) {
$value = self::PAGE_ARRAY[$codePage];
if (array_key_exists($codePage, self::$pageArray)) {
$value = self::$pageArray[$codePage];
if (is_array($value)) {
foreach ($value as $encoding) {
if (@iconv('UTF-8', $encoding, ' ') !== false) {
self::$pageArray[$codePage] = $encoding;

return $encoding;
}
}
Expand All @@ -106,6 +109,6 @@ public static function numberToName(int $codePage): string

public static function getEncodings(): array
{
return self::PAGE_ARRAY;
return self::$pageArray;
}
}
26 changes: 26 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xls/XlsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Shared\CodePage;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class XlsTest extends AbstractFunctional
Expand All @@ -17,6 +18,7 @@ public function testLoadXlsSample(): void
$reader = new Xls();
$spreadsheet = $reader->load($filename);
self::assertEquals('Title', $spreadsheet->getSheet(0)->getCell('A1')->getValue());
$spreadsheet->disconnectWorksheets();
}

/**
Expand All @@ -43,6 +45,8 @@ public function testLoadXlsBug1505(): void
self::assertEquals($row, $newrow);
self::assertEquals($sheet->getCell('A1')->getFormattedValue(), $newsheet->getCell('A1')->getFormattedValue());
self::assertEquals($sheet->getCell("$col$row")->getFormattedValue(), $newsheet->getCell("$col$row")->getFormattedValue());
$spreadsheet->disconnectWorksheets();
$newspreadsheet->disconnectWorksheets();
}

/**
Expand Down Expand Up @@ -80,6 +84,8 @@ public function testLoadXlsBug1592(): void
self::assertEquals($valOld, $valNew);
}
}
$spreadsheet->disconnectWorksheets();
$newspreadsheet->disconnectWorksheets();
}

/**
Expand All @@ -88,11 +94,31 @@ public function testLoadXlsBug1592(): void
*/
public function testLoadMacCentralEurope(): void
{
$codePages = CodePage::getEncodings();
self::assertIsArray($codePages[10029]);
$filename = 'tests/data/Reader/XLS/maccentraleurope.xls';
$reader = new Xls();
// When no fix applied, spreadsheet fails to load on some systems
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('Ładowność', $sheet->getCell('I1')->getValue());
$spreadsheet->disconnectWorksheets();
}

/**
* First test changes array entry in CodePage.
* This test confirms new that new entry is okay.
*/
public function testLoadMacCentralEurope2(): void
{
$codePages = CodePage::getEncodings();
self::assertIsString($codePages[10029]);
$filename = 'tests/data/Reader/XLS/maccentraleurope.xls';
$reader = new Xls();
// When no fix applied, spreadsheet fails to load on some systems
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('Ładowność', $sheet->getCell('I1')->getValue());
$spreadsheet->disconnectWorksheets();
}
}