Skip to content

Commit 700a803

Browse files
authored
Merge commit from fork
* Validate Post Item in Convert-Online.php * Correct Samples - Currency, Accounting, and Downloader * Smarter Color Formatting for Currency * Use helper->log Rather than echo in Convert-Online Responding to comment from @PowerKiKi about use of echo for error messages. I do not believe that an exception is warranted, but other scripts use helper->log for error messages, and now so will this one.
1 parent 687d87c commit 700a803

File tree

6 files changed

+63
-33
lines changed

6 files changed

+63
-33
lines changed

samples/Engineering/Convert-Online.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,16 @@
7878
$quantity = $_POST['quantity'];
7979
$fromUnit = $_POST['fromUnit'];
8080
$toUnit = $_POST['toUnit'];
81-
if (isset($units[$_POST['category']][$fromUnit], $units[$_POST['category']][$toUnit])) {
81+
if (!is_numeric($quantity)) {
82+
$helper->log('Quantity is not numeric');
83+
} elseif (isset($units[$_POST['category']][$fromUnit], $units[$_POST['category']][$toUnit])) {
8284
/** @var float|string */
8385
$result = ConvertUOM::CONVERT($quantity, $fromUnit, $toUnit);
8486

85-
echo "{$quantity} {$units[$_POST['category']][$fromUnit]} is {$result} {$units[$_POST['category']][$toUnit]}", PHP_EOL;
87+
$helper->log("{$quantity} {$units[$_POST['category']][$fromUnit]} is {$result} {$units[$_POST['category']][$toUnit]}");
8688
} else {
87-
echo 'Please enter quantity and select From Unit and To Unit', PHP_EOL;
89+
$helper->log('Please enter quantity and select From Unit and To Unit');
8890
}
8991
} else {
90-
echo 'Please enter quantity and select From Unit and To Unit', PHP_EOL;
92+
$helper->log('Please enter quantity and select From Unit and To Unit');
9193
}

samples/Wizards/NumberFormat/Accounting.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@
6464
<input name="position" type="radio" value="0" <?php echo (isset($_POST['position']) && $_POST['position'] === '0') ? 'checked' : ''; ?>>Trailing
6565
</div>
6666
</div>
67-
<div class="mb-3 row">
68-
<label for="spacing" class="col-sm-2 col-form-label">Currency Spacing</label>
69-
<div class="col-sm-10">
70-
<input name="spacing" type="radio" value="1" <?php echo (isset($_POST['spacing']) && $_POST['spacing'] === '1') ? 'checked' : ''; ?>>Yes
71-
<input name="spacing" type="radio" value="0" <?php echo ((isset($_POST['spacing']) === false) || (isset($_POST['spacing']) && $_POST['spacing'] === '0')) ? 'checked' : ''; ?>>No
72-
</div>
73-
</div>
7467
<div class="mb-3 row">
7568
<div class="col-sm-10">
7669
<input class="btn btn-primary" name="submit" type="submit" value="Display Mask"><br />
@@ -85,21 +78,23 @@
8578
$helper->log('The Sample Number Value must be numeric');
8679
} elseif (!is_numeric($_POST['decimals']) || str_contains((string) $_POST['decimals'], '.') || (int) $_POST['decimals'] < 0) {
8780
$helper->log('The Decimal Places value must be positive integer');
81+
} elseif (!in_array($_POST['currency'], array_keys($currencies), true)) {
82+
$helper->log('Unrecognized currency symbol');
8883
} else {
8984
try {
90-
$wizard = new Wizard\Accounting($_POST['currency'], (int) $_POST['decimals'], isset($_POST['thousands']), (bool) $_POST['position'], (bool) $_POST['spacing']);
85+
$wizard = new Wizard\Accounting($_POST['currency'], (int) $_POST['decimals'], isset($_POST['thousands']), (bool) $_POST['position']);
9186
$mask = $wizard->format();
9287
$example = (string) NumberFormat::toFormattedString((float) $_POST['number'], $mask);
9388
$helper->log('<hr /><b>Code:</b><br />');
9489
$helper->log('use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;');
9590
$helper->log(
96-
"\$mask = Wizard\\Accounting('{$_POST['currency']}', {$_POST['decimals']}, Wizard\\Number::"
91+
"\$wizard = new Wizard\\Accounting('{$_POST['currency']}', {$_POST['decimals']}, Wizard\\Number::"
9792
. (isset($_POST['thousands']) ? 'WITH_THOUSANDS_SEPARATOR' : 'WITHOUT_THOUSANDS_SEPARATOR')
9893
. ', Wizard\Currency::' . (((bool) $_POST['position']) ? 'LEADING_SYMBOL' : 'TRAILING_SYMBOL')
99-
. ', Wizard\Currency::' . (((bool) $_POST['spacing']) ? 'SYMBOL_WITH_SPACING' : 'SYMBOL_WITHOUT_SPACING')
100-
. ');<br />'
94+
. ');'
10195
);
102-
$helper->log('echo (string) $mask;');
96+
$helper->log('$mask = $wizard->format();');
97+
$helper->log('<br />echo (string) $mask;');
10398
$helper->log('<hr /><b>Mask:</b><br />');
10499
$helper->log($mask . '<br />');
105100
$helper->log('<br /><b>Example:</b><br />');

samples/Wizards/NumberFormat/Currency.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use PhpOffice\PhpSpreadsheet\Settings;
66
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
77
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
8+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard\CurrencyNegative;
9+
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter;
810

911
require __DIR__ . '/../Header.php';
1012

@@ -15,6 +17,19 @@
1517
return;
1618
}
1719

20+
$negatives = [
21+
CurrencyNegative::minus,
22+
CurrencyNegative::redMinus,
23+
CurrencyNegative::parentheses,
24+
CurrencyNegative::redParentheses,
25+
];
26+
$negativesString = [
27+
'CurrencyNegative::minus',
28+
'CurrencyNegative::redMinus',
29+
'CurrencyNegative::parentheses',
30+
'CurrencyNegative::redParentheses',
31+
];
32+
1833
$currencies = [
1934
'$' => 'US Dollars ($)',
2035
'' => 'Euro (€)',
@@ -65,10 +80,12 @@
6580
</div>
6681
</div>
6782
<div class="mb-3 row">
68-
<label for="spacing" class="col-sm-2 col-form-label">Currency Spacing</label>
83+
<label for="negative" class="col-sm-2 col-form-label">Negative Numbers</label>
6984
<div class="col-sm-10">
70-
<input name="spacing" type="radio" value="1" <?php echo (isset($_POST['spacing']) && $_POST['spacing'] === '1') ? 'checked' : ''; ?>>Yes
71-
<input name="spacing" type="radio" value="0" <?php echo ((isset($_POST['spacing']) === false) || (isset($_POST['spacing']) && $_POST['spacing'] === '0')) ? 'checked' : ''; ?>>No
85+
<input name="negative" type="radio" value="0" <?php echo (!isset($_POST['negative']) || $_POST['negative'] === '0') ? 'checked' : ''; ?>>Minus Sign
86+
<input name="negative" type="radio" value="1" <?php echo (isset($_POST['negative']) && $_POST['negative'] === '1') ? 'checked' : ''; ?>>Red Minus Sign
87+
<input name="negative" type="radio" value="2" <?php echo (isset($_POST['negative']) && $_POST['negative'] === '2') ? 'checked' : ''; ?>>Parentheses
88+
<input name="negative" type="radio" value="3" <?php echo (isset($_POST['negative']) && $_POST['negative'] === '3') ? 'checked' : ''; ?>>Red Parentheses
7289
</div>
7390
</div>
7491
<div class="mb-3 row">
@@ -85,21 +102,27 @@
85102
$helper->log('The Sample Number Value must be numeric');
86103
} elseif (!is_numeric($_POST['decimals']) || str_contains((string) $_POST['decimals'], '.') || (int) $_POST['decimals'] < 0) {
87104
$helper->log('The Decimal Places value must be positive integer');
105+
} elseif (!in_array($_POST['currency'], array_keys($currencies), true)) {
106+
$helper->log('Unrecognized currency symbol');
88107
} else {
89108
try {
90-
$wizard = new Wizard\Currency($_POST['currency'], (int) $_POST['decimals'], isset($_POST['thousands']), (bool) $_POST['position'], (bool) $_POST['spacing']);
109+
$negative = $negatives[$_POST['negative']] ?? CurrencyNegative::minus;
110+
$wizard = new Wizard\Currency($_POST['currency'], (int) $_POST['decimals'], isset($_POST['thousands']), (bool) $_POST['position']);
111+
$wizard->setNegative($negative);
91112
$mask = $wizard->format();
92-
$example = (string) NumberFormat::toFormattedString((float) $_POST['number'], $mask);
113+
$example = (string) NumberFormat::toFormattedString((float) $_POST['number'], $mask, [HtmlWriter::class, 'formatColorStatic']);
93114
$helper->log('<hr /><b>Code:</b><br />');
94115
$helper->log('use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;');
116+
$helper->log('use PhpOffice\PhpSpreadsheet\Style\NumberFormat\CurrencyNegative;');
95117
$helper->log(
96-
"\$mask = Wizard\\Currency('{$_POST['currency']}', {$_POST['decimals']}, Wizard\\Number::"
118+
"\$wizard = new Wizard\\Currency('{$_POST['currency']}', {$_POST['decimals']}, Wizard\\Number::"
97119
. (isset($_POST['thousands']) ? 'WITH_THOUSANDS_SEPARATOR' : 'WITHOUT_THOUSANDS_SEPARATOR')
98120
. ', Wizard\Currency::' . (((bool) $_POST['position']) ? 'LEADING_SYMBOL' : 'TRAILING_SYMBOL')
99-
. ', Wizard\Currency::' . (((bool) $_POST['spacing']) ? 'SYMBOL_WITH_SPACING' : 'SYMBOL_WITHOUT_SPACING')
100-
. ');<br />'
121+
. ');'
101122
);
102-
$helper->log('echo (string) $mask;');
123+
$helper->log('$wizard->setNegative(' . $negativesString[$_POST['negative']] . ');');
124+
$helper->log('$mask = $wizard->format();');
125+
$helper->log('<br />echo (string) $mask;');
103126
$helper->log('<hr /><b>Mask:</b><br />');
104127
$helper->log($mask . '<br />');
105128
$helper->log('<br /><b>Example:</b><br />');

src/PhpSpreadsheet/Helper/Downloader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ class Downloader
3030
public function __construct(string $folder, string $filename, ?string $filetype = null)
3131
{
3232
if ((is_dir($folder) === false) || (is_readable($folder) === false)) {
33-
throw new Exception("Folder {$folder} is not accessable");
33+
throw new Exception('Folder is not accessible');
3434
}
3535
$filepath = "{$folder}/{$filename}";
3636
$this->filepath = (string) realpath($filepath);
3737
$this->filename = basename($filepath);
3838
if ((file_exists($this->filepath) === false) || (is_readable($this->filepath) === false)) {
39-
throw new Exception("{$this->filename} not found, or cannot be read");
39+
throw new Exception('File not found, or cannot be read');
4040
}
4141

4242
$filetype ??= pathinfo($filename, PATHINFO_EXTENSION);
4343
if (array_key_exists(strtolower($filetype), self::CONTENT_TYPES) === false) {
44-
throw new Exception("Invalid filetype: {$filetype} cannot be downloaded");
44+
throw new Exception('Invalid filetype: file cannot be downloaded');
4545
}
4646
$this->filetype = strtolower($filetype);
4747
}

src/PhpSpreadsheet/Style/NumberFormat/Formatter.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ private static function splitFormatForSectionSelection(array $sections, mixed $v
112112
* @param null|array|bool|float|int|RichText|string $value Value to format
113113
* @param string $format Format code: see = self::FORMAT_* for predefined values;
114114
* or can be any valid MS Excel custom format string
115-
* @param ?array $callBack Callback function for additional formatting of string
115+
* @param null|array|callable $callBack Callback function for additional formatting of string
116116
*
117117
* @return string Formatted string
118118
*/
119-
public static function toFormattedString($value, string $format, ?array $callBack = null): string
119+
public static function toFormattedString($value, string $format, null|array|callable $callBack = null): string
120120
{
121121
while (is_array($value)) {
122122
$value = array_shift($value);
@@ -200,9 +200,8 @@ public static function toFormattedString($value, string $format, ?array $callBac
200200
}
201201

202202
// Additional formatting provided by callback function
203-
if ($callBack !== null) {
204-
[$writerInstance, $function] = $callBack;
205-
$value = $writerInstance->$function($value, $colors);
203+
if (is_callable($callBack)) {
204+
$value = $callBack($value, $colors);
206205
}
207206

208207
return str_replace(chr(0x00), '.', $value);

src/PhpSpreadsheet/Writer/Html.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,17 @@ public function setUseInlineCss(bool $useInlineCss): static
17201720
* @param string $format Format code
17211721
*/
17221722
public function formatColor(string $value, string $format): string
1723+
{
1724+
return self::formatColorStatic($value, $format);
1725+
}
1726+
1727+
/**
1728+
* Add color to formatted string as inline style.
1729+
*
1730+
* @param string $value Plain formatted value without color
1731+
* @param string $format Format code
1732+
*/
1733+
public static function formatColorStatic(string $value, string $format): string
17231734
{
17241735
// Color information, e.g. [Red] is always at the beginning
17251736
$color = null; // initialize

0 commit comments

Comments
 (0)