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\Number;
12:
13: use chippyash\Type\Exceptions\InvalidTypeException;
14:
15: /**
16: * Natural Integer Type
17: */
18: class NaturalIntType extends IntType
19: {
20:
21: /**
22: * Negates the number
23: *
24: * @throws \BadMethodCallException
25: */
26: public function negate()
27: {
28: throw new \BadMethodCallException('Negate not supported for Natural Int Types');
29: }
30:
31: /**
32: * Return correctly typed value for this type
33: *
34: * @param mixed $value
35: *
36: * @return int
37: *
38: * @throws InvalidTypeException
39: */
40: protected function typeOf($value)
41: {
42: $val = intval($value);
43: if ($val > 0) {
44: return $val;
45: } else {
46: throw new InvalidTypeException("{$val} < 1 for natural integer type");
47: }
48: }
49: }
50: