Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
15 / 15
AttributeSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
4 / 4
9
100.00% covered (success)
100.00%
15 / 15
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 getRequired
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 getMulti
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
 get
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
4 / 4
1<?php
2
3namespace Proton\IosReceiptParser\Attribute;
4
5use Proton\IosReceiptParser\Exception\AttributeMissingException;
6
7/**
8 * Set of attributes without assigned semantics.
9 *
10 * @internal
11 * @psalm-type AttributeSequence = array{type: \phpseclib\Math\BigInteger, value: string}
12 */
13final class AttributeSet
14{
15    /**
16     * @var array
17     * @psalm-var list<AttributeSequence>
18     */
19    private $data;
20
21    /** @var string|null */
22    private $context;
23
24    /**
25     * @psalm-param list<AttributeSequence> $data
26     * @param string $context Used to provide context for better error messages.
27     */
28    public function __construct(array $data, string $context = null)
29    {
30        $this->data = $data;
31        $this->context = $context;
32    }
33
34    /**
35     * @throws AttributeMissingException
36     */
37    public function getRequired(int $type): string
38    {
39        if (($value = $this->get($type)) !== null) {
40            return $value;
41        }
42
43        throw new AttributeMissingException($type, $this->context);
44    }
45
46    /**
47     * @psalm-return list<string>
48     */
49    public function getMulti(int $type): array
50    {
51        $return = [];
52        foreach ($this->data as $attribute) {
53            if ((int) (string) $attribute['type'] === $type) {
54                $return[] = $attribute['value'];
55            }
56        }
57
58        return $return;
59    }
60
61    public function get(int $type): ?string
62    {
63        foreach ($this->data as $attribute) {
64            if ((int) (string) $attribute['type'] === $type) {
65                return $attribute['value'];
66            }
67        }
68
69        return null;
70    }
71}