1: <?php
2: /**
3: * Hard type support
4: * For when you absolutely want to know what you are getting
5: *
6: * @author Ashley Kitson <akitson@zf4.biz>
7: * @copyright Ashley Kitson, UK, 2014
8: * @licence GPL V3 or later : http://www.gnu.org/licenses/gpl.html
9: */
10:
11: namespace chippyash\Type\String;
12:
13: use chippyash\Type\AbstractType;
14: use chippyash\Zend\ErrorHandler;
15:
16: /**
17: * Numeric String Type
18: *
19: */
20: class DigitType extends AbstractType
21: {
22:
23: /**
24: * Is PCRE compiled with Unicode support?
25: *
26: * @var bool
27: **/
28: protected static $hasPcreUnicodeSupport = null;
29:
30: /**
31: * This will filter out any non numeric characters. You may potentially
32: * get an empty string
33: *
34: * @param mixed $value
35: * @return string
36: */
37: protected function typeOf($value)
38: {
39: return (string) $this->filter($value);
40: }
41:
42: /**
43: * Lifted entirely from the Zend framework so that we don't have to include
44: * the Zend\Filter package and all its dependencies.
45: *
46: * @param string $value
47: * @return string|mixed
48:
49: * zendframework/zend-filter/Zend/Filter/Digits.php
50: * Zend Framework (http://framework.zend.com/)
51: *
52: * @link http://github.com/zendframework/zf2 for the canonical source repository
53: * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
54: * @license http://framework.zend.com/license/new-bsd New BSD License
55: * Defined by Zend\Filter\FilterInterface
56: *
57: * Returns the string $value, removing all but digit characters
58: *
59: * If the value provided is non-scalar, the value will remain unfiltered
60: *
61: */
62: protected function filter($value)
63: {
64: if (!is_scalar($value)) {
65: return $value;
66: }
67: $value = (string) $value;
68:
69: if (!$this->hasPcreUnicodeSupport()) {
70: // POSIX named classes are not supported, use alternative 0-9 match
71: $pattern = '/[^0-9]/';
72: } elseif (extension_loaded('mbstring')) {
73: // Filter for the value with mbstring
74: $pattern = '/[^[:digit:]]/';
75: } else {
76: // Filter for the value without mbstring
77: $pattern = '/[\p{^N}]/';
78: }
79:
80: return preg_replace($pattern, '', $value);
81: }
82:
83: /**
84: * Lifted entirely from Zend Framework (http://framework.zend.com/) so we don't have
85: * to include Zend/Stdlib
86: *
87: * @link http://github.com/zendframework/zf2 for the canonical source repository
88: * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
89: * @license http://framework.zend.com/license/new-bsd New BSD License
90: * Is PCRE compiled with Unicode support?
91: *
92: * @return bool
93: */
94: protected function hasPcreUnicodeSupport()
95: {
96: if (static::$hasPcreUnicodeSupport === null) {
97: ErrorHandler::start();
98: static::$hasPcreUnicodeSupport =
99: defined('PREG_BAD_UTF8_OFFSET_ERROR') && preg_match('/\pL/u', 'a') == 1;
100: ErrorHandler::stop();
101: }
102: return static::$hasPcreUnicodeSupport;
103: }
104: }
105: