Passed
Push — latest ( 8e76fa...96be75 )
by Mark
02:56
created

Dataset::offsetGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji;
6
7
use UnicornFail\Emoji\Exception\FileNotFoundException;
8
use UnicornFail\Emoji\Exception\MalformedArchiveException;
9
use UnicornFail\Emoji\Exception\UnarchiveException;
10
use UnicornFail\Emoji\Util\ImmutableArrayIterator;
11
use UnicornFail\Emoji\Util\Normalize;
12
13
/**
14
 * @method Emoji[] getArrayCopy()
15
 */
16
final class Dataset extends ImmutableArrayIterator
17
{
18
    public const DIRECTORY = __DIR__ . '/../datasets';
19
20
    /** @var string */
21
    private $index;
22
23
    /** @var Dataset[] */
24
    private $indices = [];
25
26
    /**
27
     * @param mixed $emojis
28
     */
29 33
    public function __construct($emojis = [], string $index = 'hexcode')
30
    {
31 33
        $this->index = $index;
32 33
        parent::__construct(Normalize::dataset($emojis, $index), \ArrayIterator::ARRAY_AS_PROPS | \ArrayIterator::STD_PROP_LIST);
33 33
    }
34
35 621
    public static function unarchive(string $filename): self
36
    {
37 621
        if (! \file_exists($filename)) {
38 417
            throw new FileNotFoundException($filename);
39
        }
40
41
        if (
42 204
            ! ($contents = \file_get_contents($filename)) ||
43 204
            ! ($decoded = \gzdecode($contents))
44
        ) {
45 3
            throw new UnarchiveException($filename);
46
        }
47
48
        try {
49
            /** @var ?Dataset $dataset */
50 201
            $dataset = \unserialize((string) $decoded);
51 3
        } catch (\Throwable $throwable) {
52 3
            throw new MalformedArchiveException($filename, $throwable);
53
        }
54
55 198
        if (! $dataset instanceof Dataset) {
56 3
            throw new MalformedArchiveException($filename);
57
        }
58
59 195
        return $dataset;
60
    }
61
62
    /**
63
     * @param string[] $indices
64
     *
65
     * @return false|string
66
     */
67 3
    public function archive(array $indices = Parser::INDICES)
68
    {
69 3
        foreach ($indices as $index) {
70 3
            $this->indexBy($index);
71
        }
72
73 3
        $serialize = \serialize($this);
74
75 3
        return \gzencode($serialize, 9);
76
    }
77
78
    /**
79
     * @param callable(Emoji):bool $callback
80
     */
81 9
    public function filter(callable $callback): Dataset
82
    {
83 9
        return new self(new \CallbackFilterIterator($this, $callback));
84
    }
85
86 87
    public function indexBy(string $index = 'hexcode'): Dataset
87
    {
88 87
        if (! isset($this->indices[$index])) {
89 6
            $this->indices[$index] = new self($this, $index);
90
        }
91
92 87
        return $this->indices[$index];
93
    }
94
95
    /**
96
     * @param string $key
97
     */
98 84
    public function offsetGet($key): ?Emoji // phpcs:ignore
99
    {
100
        // Normalize shortcodes to match index.
101 84
        if (\strpos($this->index, 'shortcode') !== false) {
102 54
            $key = \current(Normalize::shortcodes($key));
103
        }
104
105
        /** @var ?Emoji $emoji */
106 84
        $emoji = parent::offsetGet($key);
107
108 84
        return $emoji;
109
    }
110
}
111