Skip to content

Commit 6e6caa9

Browse files
authored
Merge pull request #148 from zobzn/fix-php-71-validator-schema-get-bytes
fix notice "A non well formed numeric value encountered in ... sfValidatorSchema::getBytes" (php 7.1)
2 parents 3ed7f99 + b0e8d46 commit 6e6caa9

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

lib/validator/sfValidatorSchema.class.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,21 @@ public function __clone()
390390

391391
protected function getBytes($value)
392392
{
393-
$value = trim($value);
394-
switch (strtolower($value[strlen($value) - 1]))
395-
{
396-
// The 'G' modifier is available since PHP 5.1.0
397-
case 'g':
398-
$value *= 1024;
399-
case 'm':
400-
$value *= 1024;
401-
case 'k':
402-
$value *= 1024;
393+
$value = trim($value);
394+
$number = (float) $value;
395+
$modifier = strtolower($value[strlen($value) - 1]);
396+
397+
$exp_by_modifier = array(
398+
'k' => 1,
399+
'm' => 2,
400+
'g' => 3,
401+
);
402+
403+
if (array_key_exists($modifier, $exp_by_modifier)) {
404+
$exp = $exp_by_modifier[$modifier];
405+
$number = $number * pow(1024, $exp);
403406
}
404407

405-
return $value;
408+
return $number;
406409
}
407410
}

test/unit/validator/sfValidatorSchemaTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
require_once(__DIR__.'/../../bootstrap/unit.php');
1212

13-
$t = new lime_test(85);
13+
$t = new lime_test(94);
1414

1515
class PreValidator extends sfValidatorBase
1616
{
@@ -48,6 +48,14 @@ protected function doClean($values)
4848
}
4949
}
5050

51+
class BytesValidatorSchema extends sfValidatorSchema
52+
{
53+
public function getBytes($value)
54+
{
55+
return parent::getBytes($value);
56+
}
57+
}
58+
5159
$v1 = new sfValidatorString(array('max_length' => 3));
5260
$v2 = new sfValidatorString(array('min_length' => 3));
5361

@@ -401,3 +409,15 @@ protected function doClean($values)
401409
$t->ok($v1->getPreValidator() == $v->getPreValidator(), '__clone() clones the pre validator');
402410
$t->ok($v1->getPostValidator() !== $v->getPostValidator(), '__clone() clones the post validator');
403411
$t->ok($v1->getPostValidator() == $v->getPostValidator(), '__clone() clones the post validator');
412+
413+
$t->diag('convert post_max_size to bytes');
414+
$v = new BytesValidatorSchema();
415+
$t->is($v->getBytes(null), 0, 'empty string considered as 0 bytes');
416+
$t->is($v->getBytes(''), 0, 'empty string considered as 0 bytes');
417+
$t->is($v->getBytes('0'), 0, 'simple bytes');
418+
$t->is($v->getBytes('1'), 1, 'simple bytes');
419+
$t->is($v->getBytes('1B'), 1, 'simple bytes');
420+
$t->is($v->getBytes('1K'), 1024, 'kilobytes');
421+
$t->is($v->getBytes('1M'), 1024 * 1024, 'megabytes short syntax');
422+
$t->is($v->getBytes('0.5M'), 1024 * 1024 / 2, 'fractional megabytes');
423+
$t->is($v->getBytes('1G'), 1024 * 1024 * 1024, 'gigabytes');

0 commit comments

Comments
 (0)