forked from zendframework/zf1
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathFormat.php
1342 lines (1219 loc) · 54.6 KB
/
Format.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Locale
* @subpackage Format
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @version $Id$
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* include needed classes
*/
#require_once 'Zend/Locale/Data.php';
/**
* @category Zend
* @package Zend_Locale
* @subpackage Format
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Locale_Format
{
const STANDARD = 'auto';
private static $_options = array('date_format' => null,
'number_format' => null,
'format_type' => 'iso',
'fix_date' => false,
'locale' => null,
'cache' => null,
'disableCache' => null,
'precision' => null);
/**
* Sets class wide options, if no option was given, the actual set options will be returned
* The 'precision' option of a value is used to truncate or stretch extra digits. -1 means not to touch the extra digits.
* The 'locale' option helps when parsing numbers and dates using separators and month names.
* The date format 'format_type' option selects between CLDR/ISO date format specifier tokens and PHP's date() tokens.
* The 'fix_date' option enables or disables heuristics that attempt to correct invalid dates.
* The 'number_format' option can be used to specify a default number format string
* The 'date_format' option can be used to specify a default date format string, but beware of using getDate(),
* checkDateFormat() and getTime() after using setOptions() with a 'format'. To use these four methods
* with the default date format for a locale, use array('date_format' => null, 'locale' => $locale) for their options.
*
* @param array $options Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false,
* locale = Zend_Locale | locale string, precision = whole number between -1 and 30
* @throws Zend_Locale_Exception
* @return array if no option was given
*/
public static function setOptions(array $options = array())
{
self::$_options = self::_checkOptions($options) + self::$_options;
return self::$_options;
}
/**
* Internal function for checking the options array of proper input values
* See {@link setOptions()} for details.
*
* @param array $options Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false,
* locale = Zend_Locale | locale string, precision = whole number between -1 and 30
* @throws Zend_Locale_Exception
* @return array if no option was given
*/
private static function _checkOptions(array $options = array())
{
if (count($options) == 0) {
return self::$_options;
}
foreach ($options as $name => $value) {
$name = strtolower($name);
if ($name !== 'locale') {
if (gettype($value) === 'string') {
$value = strtolower($value);
}
}
switch($name) {
case 'number_format' :
if ($value == Zend_Locale_Format::STANDARD) {
$locale = self::$_options['locale'];
if (isset($options['locale'])) {
$locale = $options['locale'];
}
$options['number_format'] = Zend_Locale_Data::getContent($locale, 'decimalnumber');
} else if ((gettype($value) !== 'string') and ($value !== NULL)) {
#require_once 'Zend/Locale/Exception.php';
$stringValue = (string)(is_array($value) ? implode(' ', $value) : $value);
throw new Zend_Locale_Exception("Unknown number format type '" . gettype($value) . "'. "
. "Format '$stringValue' must be a valid number format string.");
}
break;
case 'date_format' :
if ($value == Zend_Locale_Format::STANDARD) {
$locale = self::$_options['locale'];
if (isset($options['locale'])) {
$locale = $options['locale'];
}
$options['date_format'] = Zend_Locale_Format::getDateFormat($locale);
} else if ((gettype($value) !== 'string') and ($value !== NULL)) {
#require_once 'Zend/Locale/Exception.php';
$stringValue = (string)(is_array($value) ? implode(' ', $value) : $value);
throw new Zend_Locale_Exception("Unknown dateformat type '" . gettype($value) . "'. "
. "Format '$stringValue' must be a valid ISO or PHP date format string.");
} else {
if (((isset($options['format_type']) === true) and ($options['format_type'] == 'php')) or
((isset($options['format_type']) === false) and (self::$_options['format_type'] == 'php'))) {
$options['date_format'] = Zend_Locale_Format::convertPhpToIsoFormat($value);
}
}
break;
case 'format_type' :
if (($value != 'php') && ($value != 'iso')) {
#require_once 'Zend/Locale/Exception.php';
throw new Zend_Locale_Exception("Unknown date format type '$value'. Only 'iso' and 'php'"
. " are supported.");
}
break;
case 'fix_date' :
if (($value !== true) && ($value !== false)) {
#require_once 'Zend/Locale/Exception.php';
throw new Zend_Locale_Exception("Enabling correction of dates must be either true or false"
. "(fix_date='$value').");
}
break;
case 'locale' :
$options['locale'] = Zend_Locale::findLocale($value);
break;
case 'cache' :
if ($value instanceof Zend_Cache_Core) {
Zend_Locale_Data::setCache($value);
}
break;
case 'disablecache' :
if (null !== $value) {
Zend_Locale_Data::disableCache($value);
}
break;
case 'precision' :
if ($value === NULL) {
$value = -1;
}
if (($value < -1) || ($value > 30)) {
#require_once 'Zend/Locale/Exception.php';
throw new Zend_Locale_Exception("'$value' precision is not a whole number less than 30.");
}
break;
default:
#require_once 'Zend/Locale/Exception.php';
throw new Zend_Locale_Exception("Unknown option: '$name' = '$value'");
break;
}
}
return $options;
}
/**
* Changes the numbers/digits within a given string from one script to another
* 'Decimal' representated the stardard numbers 0-9, if a script does not exist
* an exception will be thrown.
*
* Examples for conversion from Arabic to Latin numerals:
* convertNumerals('١١٠ Tests', 'Arab'); -> returns '100 Tests'
* Example for conversion from Latin to Arabic numerals:
* convertNumerals('100 Tests', 'Latn', 'Arab'); -> returns '١١٠ Tests'
*
* @param string $input String to convert
* @param string $from Script to parse, see {@link Zend_Locale::getScriptList()} for details.
* @param string $to OPTIONAL Script to convert to
* @return string Returns the converted input
* @throws Zend_Locale_Exception
*/
public static function convertNumerals($input, $from, $to = null)
{
if (!self::_getUniCodeSupport()) {
trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
}
$from = strtolower($from);
$source = Zend_Locale_Data::getContent('en', 'numberingsystem', $from);
if (empty($source)) {
#require_once 'Zend/Locale/Exception.php';
throw new Zend_Locale_Exception("Unknown script '$from'. Use 'Latn' for digits 0,1,2,3,4,5,6,7,8,9.");
}
if ($to !== null) {
$to = strtolower($to);
$target = Zend_Locale_Data::getContent('en', 'numberingsystem', $to);
if (empty($target)) {
#require_once 'Zend/Locale/Exception.php';
throw new Zend_Locale_Exception("Unknown script '$to'. Use 'Latn' for digits 0,1,2,3,4,5,6,7,8,9.");
}
} else {
$target = '0123456789';
}
for ($x = 0; $x < 10; ++$x) {
$asource[$x] = "/" . iconv_substr($source, $x, 1, 'UTF-8') . "/u";
$atarget[$x] = iconv_substr($target, $x, 1, 'UTF-8');
}
return preg_replace($asource, $atarget, $input);
}
/**
* Returns the normalized number from a localized one
* Parsing depends on given locale (grouping and decimal)
*
* Examples for input:
* '2345.4356,1234' = 23455456.1234
* '+23,3452.123' = 233452.123
* '12343 ' = 12343
* '-9456' = -9456
* '0' = 0
*
* @param string $input Input string to parse for numbers
* @param array $options Options: locale, precision. See {@link setOptions()} for details.
* @return string Returns the extracted number
* @throws Zend_Locale_Exception
*/
public static function getNumber($input, array $options = array())
{
$options = self::_checkOptions($options) + self::$_options;
if (!is_string($input)) {
return $input;
}
if (!self::isNumber($input, $options)) {
#require_once 'Zend/Locale/Exception.php';
throw new Zend_Locale_Exception('No localized value in ' . $input . ' found, or the given number does not match the localized format');
}
// Get correct signs for this locale
$symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
// Change locale input to be default number
if (($input[0] == $symbols['minus']) && ('-' != $input[0])) {
$input = '-' . substr($input, 1);
}
$input = str_replace($symbols['group'],'', $input);
if (strpos($input, $symbols['decimal']) !== false) {
if ($symbols['decimal'] != '.') {
$input = str_replace($symbols['decimal'], ".", $input);
}
$pre = substr($input, strpos($input, '.') + 1);
if ($options['precision'] === null) {
$options['precision'] = strlen($pre);
}
if (strlen($pre) >= $options['precision']) {
$input = substr($input, 0, strlen($input) - strlen($pre) + $options['precision']);
}
if (($options['precision'] == 0) && ($input[strlen($input) - 1] == '.')) {
$input = substr($input, 0, -1);
}
}
return $input;
}
/**
* Returns a locale formatted number depending on the given options.
* The seperation and fraction sign is used from the set locale.
* ##0.# -> 12345.12345 -> 12345.12345
* ##0.00 -> 12345.12345 -> 12345.12
* ##,##0.00 -> 12345.12345 -> 12,345.12
*
* @param string $value Localized number string
* @param array $options Options: number_format, locale, precision. See {@link setOptions()} for details.
* @return string locale formatted number
* @throws Zend_Locale_Exception
*/
public static function toNumber($value, array $options = array())
{
// load class within method for speed
#require_once 'Zend/Locale/Math.php';
$value = Zend_Locale_Math::floatalize($value);
$value = Zend_Locale_Math::normalize($value);
$options = self::_checkOptions($options) + self::$_options;
$options['locale'] = (string) $options['locale'];
// Get correct signs for this locale
$symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
$oenc = self::_getEncoding();
self::_setEncoding('UTF-8');
// Get format
$format = $options['number_format'];
if ($format === null) {
$format = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
$format = self::_seperateFormat($format, $value, $options['precision']);
if ($options['precision'] !== null) {
$value = Zend_Locale_Math::normalize(Zend_Locale_Math::round($value, $options['precision']));
}
} else {
// seperate negative format pattern when available
$format = self::_seperateFormat($format, $value, $options['precision']);
if (strpos($format, '.')) {
if (is_numeric($options['precision'])) {
$value = Zend_Locale_Math::round($value, $options['precision']);
} else {
if (substr($format, iconv_strpos($format, '.') + 1, 3) == '###') {
$options['precision'] = null;
} else {
$options['precision'] = iconv_strlen(iconv_substr($format, iconv_strpos($format, '.') + 1,
iconv_strrpos($format, '0') - iconv_strpos($format, '.')));
$format = iconv_substr($format, 0, iconv_strpos($format, '.') + 1) . '###'
. iconv_substr($format, iconv_strrpos($format, '0') + 1);
}
}
} else {
$value = Zend_Locale_Math::round($value, 0);
$options['precision'] = 0;
}
$value = Zend_Locale_Math::normalize($value);
}
if (iconv_strpos($format, '0') === false) {
self::_setEncoding($oenc);
#require_once 'Zend/Locale/Exception.php';
throw new Zend_Locale_Exception('Wrong format... missing 0');
}
// get number parts
$pos = iconv_strpos($value, '.');
if ($pos !== false) {
if ($options['precision'] === null) {
$precstr = iconv_substr($value, $pos + 1);
} else {
$precstr = iconv_substr($value, $pos + 1, $options['precision']);
if (iconv_strlen($precstr) < $options['precision']) {
$precstr = $precstr . str_pad("0", ($options['precision'] - iconv_strlen($precstr)), "0");
}
}
} else {
if ($options['precision'] > 0) {
$precstr = str_pad("0", ($options['precision']), "0");
}
}
if ($options['precision'] === null) {
if (isset($precstr)) {
$options['precision'] = iconv_strlen($precstr);
} else {
$options['precision'] = 0;
}
}
// get fraction and format lengths
if (strpos($value, '.') !== false) {
$number = substr((string) $value, 0, strpos($value, '.'));
} else {
$number = $value;
}
$prec = call_user_func(Zend_Locale_Math::$sub, $value, $number, $options['precision']);
$prec = Zend_Locale_Math::floatalize($prec);
$prec = Zend_Locale_Math::normalize($prec);
if (iconv_strpos($prec, '-') !== false) {
$prec = iconv_substr($prec, 1);
}
if (($prec == 0) and ($options['precision'] > 0)) {
$prec = "0.0";
}
if (($options['precision'] + 2) > iconv_strlen($prec)) {
$prec = str_pad((string) $prec, $options['precision'] + 2, "0", STR_PAD_RIGHT);
}
if (iconv_strpos($number, '-') !== false) {
$number = iconv_substr($number, 1);
}
$group = iconv_strrpos($format, ',');
$group2 = iconv_strpos ($format, ',');
$point = iconv_strpos ($format, '0');
// Add fraction
$rest = "";
if (iconv_strpos($format, '.')) {
$rest = iconv_substr($format, iconv_strpos($format, '.') + 1);
$length = iconv_strlen($rest);
for($x = 0; $x < $length; ++$x) {
if (($rest[0] == '0') || ($rest[0] == '#')) {
$rest = iconv_substr($rest, 1);
}
}
$format = iconv_substr($format, 0, iconv_strlen($format) - iconv_strlen($rest));
}
if ($options['precision'] == '0') {
if (iconv_strrpos($format, '-') != 0) {
$format = iconv_substr($format, 0, $point)
. iconv_substr($format, iconv_strrpos($format, '#') + 2);
} else {
$format = iconv_substr($format, 0, $point);
}
} else {
$format = iconv_substr($format, 0, $point) . $symbols['decimal']
. iconv_substr($prec, 2);
}
$format .= $rest;
// Add seperation
if ($group == 0) {
// no seperation
$format = $number . iconv_substr($format, $point);
} else if ($group == $group2) {
// only 1 seperation
$seperation = ($point - $group);
for ($x = iconv_strlen($number); $x > $seperation; $x -= $seperation) {
if (iconv_substr($number, 0, $x - $seperation) !== "") {
$number = iconv_substr($number, 0, $x - $seperation) . $symbols['group']
. iconv_substr($number, $x - $seperation);
}
}
$format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
} else {
// 2 seperations
if (iconv_strlen($number) > ($point - $group)) {
$seperation = ($point - $group);
$number = iconv_substr($number, 0, iconv_strlen($number) - $seperation) . $symbols['group']
. iconv_substr($number, iconv_strlen($number) - $seperation);
if ((iconv_strlen($number) - 1) > ($point - $group + 1)) {
$seperation2 = ($group - $group2 - 1);
for ($x = iconv_strlen($number) - $seperation2 - 2; $x > $seperation2; $x -= $seperation2) {
$number = iconv_substr($number, 0, $x - $seperation2) . $symbols['group']
. iconv_substr($number, $x - $seperation2);
}
}
}
$format = iconv_substr($format, 0, iconv_strpos($format, '#')) . $number . iconv_substr($format, $point);
}
// set negative sign
if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $options['precision']) < 0) {
if (iconv_strpos($format, '-') === false) {
$format = $symbols['minus'] . $format;
} else {
$format = str_replace('-', $symbols['minus'], $format);
}
}
self::_setEncoding($oenc);
return (string) $format;
}
/**
* @param string $format
* @param string $value
* @param int $precision
* @return string
*/
private static function _seperateFormat($format, $value, $precision)
{
if (iconv_strpos($format, ';') !== false) {
if (call_user_func(Zend_Locale_Math::$comp, $value, 0, $precision) < 0) {
$tmpformat = iconv_substr($format, iconv_strpos($format, ';') + 1);
if ($tmpformat[0] == '(') {
$format = iconv_substr($format, 0, iconv_strpos($format, ';'));
} else {
$format = $tmpformat;
}
} else {
$format = iconv_substr($format, 0, iconv_strpos($format, ';'));
}
}
return $format;
}
/**
* Checks if the input contains a normalized or localized number
*
* @param string $input Localized number string
* @param array $options Options: locale. See {@link setOptions()} for details.
* @return boolean Returns true if a number was found
*/
public static function isNumber($input, array $options = array())
{
if (!self::_getUniCodeSupport()) {
trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
}
$options = self::_checkOptions($options) + self::$_options;
// Get correct signs for this locale
$symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
$regexs = Zend_Locale_Format::_getRegexForType('decimalnumber', $options);
$regexs = array_merge($regexs, Zend_Locale_Format::_getRegexForType('scientificnumber', $options));
if (!empty($input) && ($input[0] == $symbols['decimal'])) {
$input = 0 . $input;
}
foreach ($regexs as $regex) {
preg_match($regex, $input, $found);
if (isset($found[0])) {
return true;
}
}
return false;
}
/**
* Internal method to convert cldr number syntax into regex
*
* @param string $type
* @param array $options Options: locale. See {@link setOptions()} for details.
* @return string
* @throws Zend_Locale_Exception
*/
private static function _getRegexForType($type, $options)
{
$decimal = Zend_Locale_Data::getContent($options['locale'], $type);
$decimal = preg_replace('/[^#0,;\.\-Ee]/u', '',$decimal);
$patterns = explode(';', $decimal);
if (count($patterns) == 1) {
$patterns[1] = '-' . $patterns[0];
}
$symbols = Zend_Locale_Data::getList($options['locale'],'symbols');
foreach($patterns as $pkey => $pattern) {
$regex[$pkey] = '/^';
$rest = 0;
$end = null;
if (strpos($pattern, '.') !== false) {
$end = substr($pattern, strpos($pattern, '.') + 1);
$pattern = substr($pattern, 0, -strlen($end) - 1);
}
if (strpos($pattern, ',') !== false) {
$parts = explode(',', $pattern);
$count = count($parts);
foreach($parts as $key => $part) {
switch ($part) {
case '#':
case '-#':
if ($part[0] == '-') {
$regex[$pkey] .= '[' . preg_quote($symbols['minus']) . '-]{0,1}';
} else {
$regex[$pkey] .= '[' . $symbols['plus'] . '+]{0,1}';
}
if (($parts[$key + 1]) == '##0') {
$regex[$pkey] .= '[0-9]{1,3}';
} else if (($parts[$key + 1]) == '##') {
$regex[$pkey] .= '[0-9]{1,2}';
} else {
throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 1):"' . $pattern . '"');
}
break;
case '##':
if ($parts[$key + 1] == '##0') {
$regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{2})*';
} else {
throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 2):"' . $pattern . '"');
}
break;
case '##0':
if ($parts[$key - 1] == '##') {
$regex[$pkey] .= '[0-9]';
} else if (($parts[$key - 1] == '#') || ($parts[$key - 1] == '-#')) {
$regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{3})*';
} else {
throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 3):"' . $pattern . '"');
}
break;
case '#0':
if ($key == 0) {
$regex[$pkey] .= '[0-9]*';
} else {
throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 4):"' . $pattern . '"');
}
break;
}
}
}
if (strpos($pattern, 'E') !== false) {
if (($pattern == '#E0') || ($pattern == '#E00')) {
$regex[$pkey] .= '[' . $symbols['plus']. '+]{0,1}[0-9]{1,}(\\' . $symbols['decimal'] . '[0-9]{1,})*[eE][' . $symbols['plus']. '+]{0,1}[0-9]{1,}';
} else if (($pattern == '-#E0') || ($pattern == '-#E00')) {
$regex[$pkey] .= '[' . preg_quote($symbols['minus']) . '-]{0,1}[0-9]{1,}(\\' . $symbols['decimal'] . '[0-9]{1,})*[eE][' . preg_quote($symbols['minus']) . '-]{0,1}[0-9]{1,}';
} else {
throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 5):"' . $pattern . '"');
}
}
if (!empty($end)) {
if ($end == '###') {
$regex[$pkey] .= '(\\' . $symbols['decimal'] . '{1}[0-9]{1,}){0,1}';
} else if ($end == '###-') {
$regex[$pkey] .= '(\\' . $symbols['decimal'] . '{1}[0-9]{1,}){0,1}[' . preg_quote($symbols['minus']) . '-]';
} else {
throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 6):"' . $pattern . '"');
}
}
$regex[$pkey] .= '$/u';
}
return $regex;
}
/**
* Alias for getNumber
*
* @param string $input Number to localize
* @param array $options Options: locale, precision. See {@link setOptions()} for details.
* @return float
*/
public static function getFloat($input, array $options = array())
{
return floatval(self::getNumber($input, $options));
}
/**
* Returns a locale formatted integer number
* Alias for toNumber()
*
* @param string $value Number to normalize
* @param array $options Options: locale, precision. See {@link setOptions()} for details.
* @return string Locale formatted number
*/
public static function toFloat($value, array $options = array())
{
$options['number_format'] = Zend_Locale_Format::STANDARD;
return self::toNumber($value, $options);
}
/**
* Returns if a float was found
* Alias for isNumber()
*
* @param string $value Localized number string
* @param array $options Options: locale. See {@link setOptions()} for details.
* @return boolean Returns true if a number was found
*/
public static function isFloat($value, array $options = array())
{
return self::isNumber($value, $options);
}
/**
* Returns the first found integer from an string
* Parsing depends on given locale (grouping and decimal)
*
* Examples for input:
* ' 2345.4356,1234' = 23455456
* '+23,3452.123' = 233452
* ' 12343 ' = 12343
* '-9456km' = -9456
* '0' = 0
* '(-){0,1}(\d+(\.){0,1})*(\,){0,1})\d+'
*
* @param string $input Input string to parse for numbers
* @param array $options Options: locale. See {@link setOptions()} for details.
* @return integer Returns the extracted number
*/
public static function getInteger($input, array $options = array())
{
$options['precision'] = 0;
return intval(self::getFloat($input, $options));
}
/**
* Returns a localized number
*
* @param string $value Number to normalize
* @param array $options Options: locale. See {@link setOptions()} for details.
* @return string Locale formatted number
*/
public static function toInteger($value, array $options = array())
{
$options['precision'] = 0;
$options['number_format'] = Zend_Locale_Format::STANDARD;
return self::toNumber($value, $options);
}
/**
* Returns if a integer was found
*
* @param string $value Localized number string
* @param array $options Options: locale. See {@link setOptions()} for details.
* @return boolean Returns true if a integer was found
*/
public static function isInteger($value, array $options = array())
{
if (!self::isNumber($value, $options)) {
return false;
}
if (self::getInteger($value, $options) == self::getFloat($value, $options)) {
return true;
}
return false;
}
/**
* Converts a format string from PHP's date format to ISO format
* Remember that Zend Date always returns localized string, so a month name which returns the english
* month in php's date() will return the translated month name with this function... use 'en' as locale
* if you are in need of the original english names
*
* The conversion has the following restrictions:
* 'a', 'A' - Meridiem is not explicit upper/lowercase, you have to upper/lowercase the translated value yourself
*
* @param string $format Format string in PHP's date format
* @return string Format string in ISO format
*/
public static function convertPhpToIsoFormat($format)
{
if ($format === null) {
return null;
}
$convert = array(
'd' => 'dd' , 'D' => 'EE' , 'j' => 'd' , 'l' => 'EEEE',
'N' => 'eee' , 'S' => 'SS' , 'w' => 'e' , 'z' => 'D' ,
'W' => 'ww' , 'F' => 'MMMM', 'm' => 'MM' , 'M' => 'MMM' ,
'n' => 'M' , 't' => 'ddd' , 'L' => 'l' , 'o' => 'YYYY',
'Y' => 'yyyy', 'y' => 'yy' , 'a' => 'a' , 'A' => 'a' ,
'B' => 'B' , 'g' => 'h' , 'G' => 'H' , 'h' => 'hh' ,
'H' => 'HH' , 'i' => 'mm' , 's' => 'ss' , 'e' => 'zzzz',
'I' => 'I' , 'O' => 'Z' , 'P' => 'ZZZZ', 'T' => 'z' ,
'Z' => 'X' , 'c' => 'yyyy-MM-ddTHH:mm:ssZZZZ', 'r' => 'r',
'U' => 'U',
);
$escaped = false;
$inEscapedString = false;
$converted = array();
foreach (str_split($format) as $char) {
if (!$escaped && $char == '\\') {
// Next char will be escaped: let's remember it
$escaped = true;
} elseif ($escaped) {
if (!$inEscapedString) {
// First escaped string: start the quoted chunk
$converted[] = "'";
$inEscapedString = true;
}
// Since the previous char was a \ and we are in the quoted
// chunk, let's simply add $char as it is
$converted[] = $char;
$escaped = false;
} elseif ($char == "'") {
// Single quotes need to be escaped like this
$converted[] = "''";
} else {
if ($inEscapedString) {
// Close the single-quoted chunk
$converted[] = "'";
$inEscapedString = false;
}
// Convert the unescaped char if needed
if (isset($convert[$char])) {
$converted[] = $convert[$char];
} else {
$converted[] = $char;
}
}
}
return implode($converted);
}
/**
* Parse date and split in named array fields
*
* @param string $date Date string to parse
* @param array $options Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
* @return array Possible array members: day, month, year, hour, minute, second, fixed, format
* @throws Zend_Locale_Exception
*/
private static function _parseDate($date, $options)
{
if (!self::_getUniCodeSupport()) {
trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
}
$options = self::_checkOptions($options) + self::$_options;
$test = array('h', 'H', 'm', 's', 'y', 'Y', 'M', 'd', 'D', 'E', 'S', 'l', 'B', 'I',
'X', 'r', 'U', 'G', 'w', 'e', 'a', 'A', 'Z', 'z', 'v');
$format = $options['date_format'];
$number = $date; // working copy
$result['date_format'] = $format; // save the format used to normalize $number (convenience)
$result['locale'] = $options['locale']; // save the locale used to normalize $number (convenience)
$oenc = self::_getEncoding();
self::_setEncoding('UTF-8');
$day = iconv_strpos($format, 'd');
$month = iconv_strpos($format, 'M');
$year = iconv_strpos($format, 'y');
$hour = iconv_strpos($format, 'H');
$min = iconv_strpos($format, 'm');
$sec = iconv_strpos($format, 's');
$am = null;
if ($hour === false) {
$hour = iconv_strpos($format, 'h');
}
if ($year === false) {
$year = iconv_strpos($format, 'Y');
}
if ($day === false) {
$day = iconv_strpos($format, 'E');
if ($day === false) {
$day = iconv_strpos($format, 'D');
}
}
if ($day !== false) {
$parse[$day] = 'd';
if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
(!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
// erase day string
$daylist = Zend_Locale_Data::getList($options['locale'], 'day');
foreach($daylist as $key => $name) {
if (iconv_strpos($number, $name) !== false) {
$number = str_replace($name, "EEEE", $number);
break;
}
}
}
}
$position = false;
if ($month !== false) {
$parse[$month] = 'M';
if (!empty($options['locale']) && ($options['locale'] !== 'root') &&
(!is_object($options['locale']) || ((string) $options['locale'] !== 'root'))) {
// prepare to convert month name to their numeric equivalents, if requested,
// and we have a $options['locale']
$position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'],
'month'));
if ($position === false) {
$position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'],
'month', array('gregorian', 'format', 'abbreviated')));
}
}
}
if ($year !== false) {
$parse[$year] = 'y';
}
if ($hour !== false) {
$parse[$hour] = 'H';
}
if ($min !== false) {
$parse[$min] = 'm';
}
if ($sec !== false) {
$parse[$sec] = 's';
}
if (empty($parse)) {
self::_setEncoding($oenc);
#require_once 'Zend/Locale/Exception.php';
throw new Zend_Locale_Exception("Unknown date format, neither date nor time in '" . $format . "' found");
}
ksort($parse);
// get daytime
if (iconv_strpos($format, 'a') !== false) {
if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'am'))) !== false) {
$am = true;
} else if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'pm'))) !== false) {
$am = false;
}
}
// split number parts
$split = false;
preg_match_all('/\d+/u', $number, $splitted);
if (count($splitted[0]) == 0) {
self::_setEncoding($oenc);
#require_once 'Zend/Locale/Exception.php';
throw new Zend_Locale_Exception("No date part in '$date' found.");
}
if (count($splitted[0]) == 1) {
$split = 0;
}
$cnt = 0;
foreach($parse as $key => $value) {
switch($value) {
case 'd':
if ($split === false) {
if (count($splitted[0]) > $cnt) {
$result['day'] = $splitted[0][$cnt];
}
} else {
$result['day'] = iconv_substr($splitted[0][0], $split, 2);
$split += 2;
}
++$cnt;
break;
case 'M':
if ($split === false) {
if (count($splitted[0]) > $cnt) {
$result['month'] = $splitted[0][$cnt];
}
} else {
$result['month'] = iconv_substr($splitted[0][0], $split, 2);
$split += 2;
}
++$cnt;
break;
case 'y':
$length = 2;
if ((iconv_substr($format, $year, 4) == 'yyyy')
|| (iconv_substr($format, $year, 4) == 'YYYY')) {
$length = 4;
}
if ($split === false) {
if (count($splitted[0]) > $cnt) {
$result['year'] = $splitted[0][$cnt];
}
} else {
$result['year'] = iconv_substr($splitted[0][0], $split, $length);
$split += $length;
}
++$cnt;
break;
case 'H':
if ($split === false) {
if (count($splitted[0]) > $cnt) {
$result['hour'] = $splitted[0][$cnt];
}
} else {
$result['hour'] = iconv_substr($splitted[0][0], $split, 2);
$split += 2;
}
++$cnt;
break;
case 'm':
if ($split === false) {
if (count($splitted[0]) > $cnt) {
$result['minute'] = $splitted[0][$cnt];
}
} else {
$result['minute'] = iconv_substr($splitted[0][0], $split, 2);
$split += 2;
}
++$cnt;
break;
case 's':
if ($split === false) {
if (count($splitted[0]) > $cnt) {
$result['second'] = $splitted[0][$cnt];
}
} else {
$result['second'] = iconv_substr($splitted[0][0], $split, 2);
$split += 2;
}
++$cnt;
break;
}
}
// AM/PM correction
if ($hour !== false) {
if (($am === true) and ($result['hour'] == 12)){