Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
IntegerParser
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 setMsgContent
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6
7declare(strict_types=1);
8
9namespace pvc\parser\numeric;
10
11use NumberFormatter;
12use pvc\interfaces\intl\LocaleInterface;
13use pvc\interfaces\msg\MsgInterface;
14
15/**
16 * Class IntegerParser
17 * @extends NumericParser<int>
18 * Parses a string of numbers into an integer.  Does not accept grouping separators by default.
19 */
20class IntegerParser extends NumericParser
21{
22    /**
23     * IntegerParser constructor.
24     */
25    public function __construct(MsgInterface $msg, LocaleInterface $locale)
26    {
27        $frmtr = new NumberFormatter((string)$locale, NumberFormatter::DECIMAL);
28        parent::__construct($msg, $locale, $frmtr);
29
30        /**
31         * pattern does not have a decimal separator or any fractional digits
32         */
33        $this->getFrmtr()->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 0);
34
35        /**
36         * without this flag, the parser parses a number like 123.456, return 123 as the parsed value but the pos
37         * offset is at the end of the string, even if the pattern has no decimal separator and fractional digits
38         */
39        $this->getFrmtr()->setAttribute(NumberFormatter::PARSE_INT_ONLY, 1);
40    }
41
42
43    protected function setMsgContent(MsgInterface $msg): void
44    {
45        $msgId = 'not_integer';
46        $msgParameters = [];
47        $msg->setContent($this->getMsgDomain(), $msgId, $msgParameters);
48    }
49}