Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
IdTester
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setIdType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 testIdType
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace pvc\struct\types\id;
4
5use pvc\interfaces\struct\types\id\IdTesterInterface;
6use pvc\interfaces\struct\types\id\IdType;
7
8class IdTester implements IdTesterInterface
9{
10    public function __construct(protected IdType $idType = IdType::Integer) {}
11
12    public function setIdType(IdType $idType): void
13    {
14        $this->idType = $idType;
15    }
16
17    /**
18     * testIdType
19     * @param  mixed  $id
20     *
21     * @return bool
22     *
23     * This method also encapsulates the restriction that if typed as integers,
24     * ids must be non-negative.
25     */
26    public function testIdType(mixed $id) : bool
27    {
28        return match (gettype($id)) {
29            'string' => $this->idType == IdType::String,
30            'integer' => ($this->idType == IdType::Integer) && ($id >= 0),
31            default => false,
32        };
33    }
34}