Skip to content

Commit a241792

Browse files
authored
Merge pull request #3376 from PHPOffice/AdvancedValueBinder-Localisation
Advanced value binder localisation and Improvements
2 parents 5072ffb + 22aa65d commit a241792

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1919

2020
- Improved handling for @ in Number Format Masks [PR #3344](https://github.com/PHPOffice/PhpSpreadsheet/pull/3344)
2121
- Improved support for locale settings and currency codes when matching formatted strings to numerics in the Calculation Engine [PR #3373](https://github.com/PHPOffice/PhpSpreadsheet/pull/3373) and [PR #3374](https://github.com/PHPOffice/PhpSpreadsheet/pull/3374)
22-
- Improved support for locale settings and currency identification in the Advanced Value Binder [PR #3375](https://github.com/PHPOffice/PhpSpreadsheet/pull/3375)
22+
- Improved support for locale settings and matching in the Advanced Value Binder [PR #3376](https://github.com/PHPOffice/PhpSpreadsheet/pull/3376)
2323

2424
### Deprecated
2525

src/PhpSpreadsheet/Cell/AdvancedValueBinder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public function bindValue(Cell $cell, $value = null)
3434
// Style logic - strings
3535
if ($dataType === DataType::TYPE_STRING && !$value instanceof RichText) {
3636
// Test for booleans using locale-setting
37-
if ($value == Calculation::getTRUE()) {
37+
if (StringHelper::strToUpper($value) === Calculation::getTRUE()) {
3838
$cell->setValueExplicit(true, DataType::TYPE_BOOL);
3939

4040
return true;
41-
} elseif ($value == Calculation::getFALSE()) {
41+
} elseif (StringHelper::strToUpper($value) === Calculation::getFALSE()) {
4242
$cell->setValueExplicit(false, DataType::TYPE_BOOL);
4343

4444
return true;

tests/PhpSpreadsheetTests/Cell/AdvancedValueBinderTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder;
66
use PhpOffice\PhpSpreadsheet\Cell\Cell;
77
use PhpOffice\PhpSpreadsheet\Cell\IValueBinder;
8+
use PhpOffice\PhpSpreadsheet\Settings;
89
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
910
use PhpOffice\PhpSpreadsheet\Spreadsheet;
1011
use PHPUnit\Framework\TestCase;
@@ -35,6 +36,7 @@ class AdvancedValueBinderTest extends TestCase
3536

3637
protected function setUp(): void
3738
{
39+
Settings::setLocale('en_US');
3840
$this->currencyCode = StringHelper::getCurrencyCode();
3941
$this->decimalSeparator = StringHelper::getDecimalSeparator();
4042
$this->thousandsSeparator = StringHelper::getThousandsSeparator();
@@ -72,6 +74,27 @@ public function testBoolean(): void
7274
$sheet->getCell('A2')->setValue(false);
7375
self::assertFalse($sheet->getCell('A2')->getValue());
7476

77+
$sheet->getCell('A3')->setValue('true');
78+
self::assertTrue($sheet->getCell('A3')->getValue());
79+
80+
$sheet->getCell('A4')->setValue('false');
81+
self::assertFalse($sheet->getCell('A4')->getValue());
82+
83+
$spreadsheet->disconnectWorksheets();
84+
}
85+
86+
public function testBooleanLocale(): void
87+
{
88+
$spreadsheet = new Spreadsheet();
89+
$sheet = $spreadsheet->getActiveSheet();
90+
Settings::setLocale('nl_NL');
91+
92+
$sheet->getCell('A1')->setValue('Waar');
93+
self::assertTrue($sheet->getCell('A1')->getValue());
94+
95+
$sheet->getCell('A2')->setValue('OnWaar');
96+
self::assertFalse($sheet->getCell('A2')->getValue());
97+
7598
$spreadsheet->disconnectWorksheets();
7699
}
77100

@@ -111,6 +134,8 @@ public function currencyProvider(): array
111134
['€2,020.22', 2020.22, ',', '.', ''],
112135
['$10.11', 10.11, ',', '.', ''],
113136
['€2,020.20', 2020.2, ',', '.', '$'],
137+
['-2,020.20€', -2020.2, ',', '.', '$'],
138+
['- 2,020.20 € ', -2020.2, ',', '.', '$'],
114139
];
115140
}
116141

@@ -136,6 +161,7 @@ public function fractionProvider(): array
136161
return [
137162
['1/5', 0.2],
138163
['-1/5', -0.2],
164+
['- 1/5', -0.2],
139165
['12/5', 2.4],
140166
['2/100', 0.02],
141167
['15/12', 1.25],
@@ -145,6 +171,7 @@ public function fractionProvider(): array
145171
['1 4/20', 1.2],
146172
['1 16/20', 1.8],
147173
['12 20/100', 12.2],
174+
['-1 4/20', -1.2],
148175
];
149176
}
150177

@@ -172,6 +199,9 @@ public function percentageProvider(): array
172199
['-12%', -0.12],
173200
['120%', 1.2],
174201
['12.5%', 0.125],
202+
['-12.5%', -0.125],
203+
['12,345%', 123.45],
204+
['12,345.67%', 123.4567],
175205
];
176206
}
177207

0 commit comments

Comments
 (0)