ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Number.php
Go to the documentation of this file.
1 <?php
2 
7 {
8 
12  protected $non_negative = false;
13 
17  public function __construct($non_negative = false) {
18  $this->non_negative = $non_negative;
19  }
20 
25  public function validate($number, $config, $context) {
26 
27  $number = $this->parseCDATA($number);
28 
29  if ($number === '') return false;
30  if ($number === '0') return '0';
31 
32  $sign = '';
33  switch ($number[0]) {
34  case '-':
35  if ($this->non_negative) return false;
36  $sign = '-';
37  case '+':
38  $number = substr($number, 1);
39  }
40 
41  if (ctype_digit($number)) {
42  $number = ltrim($number, '0');
43  return $number ? $sign . $number : '0';
44  }
45 
46  // Period is the only non-numeric character allowed
47  if (strpos($number, '.') === false) return false;
48 
49  list($left, $right) = explode('.', $number, 2);
50 
51  if ($left === '' && $right === '') return false;
52  if ($left !== '' && !ctype_digit($left)) return false;
53 
54  $left = ltrim($left, '0');
55  $right = rtrim($right, '0');
56 
57  if ($right === '') {
58  return $left ? $sign . $left : '0';
59  } elseif (!ctype_digit($right)) {
60  return false;
61  }
62 
63  return $sign . $left . '.' . $right;
64 
65  }
66 
67 }
68 
69 // vim: et sw=4 sts=4