|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace League\Emoji\Dataset; |
|
6
|
|
|
|
|
7
|
|
|
use League\Emoji\Util\ImmutableArrayIterator; |
|
8
|
|
|
use League\Emoji\Util\Normalize; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @method Emoji|null current() |
|
12
|
|
|
* @method Emoji[] getArrayCopy() |
|
13
|
|
|
*/ |
|
14
|
|
|
final class Dataset extends ImmutableArrayIterator implements \ArrayAccess, \Countable, \SeekableIterator, \Serializable |
|
15
|
|
|
{ |
|
16
|
|
|
public const DIRECTORY = __DIR__ . '/../../datasets'; |
|
17
|
|
|
|
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
private $index; |
|
20
|
|
|
|
|
21
|
|
|
/** @var Dataset[] */ |
|
22
|
|
|
private $indices = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param mixed $emojis |
|
26
|
|
|
*/ |
|
27
|
315 |
|
public function __construct($emojis = [], string $index = 'hexcode') |
|
28
|
|
|
{ |
|
29
|
315 |
|
$this->index = $index; |
|
30
|
315 |
|
$normalized = Normalize::dataset($emojis, $index); |
|
31
|
315 |
|
parent::__construct($normalized, \ArrayIterator::ARRAY_AS_PROPS | \ArrayIterator::STD_PROP_LIST); |
|
32
|
315 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param callable(Emoji):bool $callback |
|
36
|
|
|
*/ |
|
37
|
18 |
|
public function filter(callable $callback): Dataset |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var \Iterator $iterator */ |
|
40
|
18 |
|
$iterator = $this; |
|
41
|
18 |
|
$filter = new \CallbackFilterIterator($iterator, $callback); |
|
42
|
|
|
|
|
43
|
18 |
|
return new self($filter); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
258 |
|
public function indexBy(string $index = 'hexcode'): Dataset |
|
47
|
|
|
{ |
|
48
|
258 |
|
if (! isset($this->indices[$index])) { |
|
49
|
258 |
|
$this->indices[$index] = new self($this, $index); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
258 |
|
return $this->indices[$index]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $key |
|
57
|
|
|
*/ |
|
58
|
105 |
|
public function offsetGet($key): ?Emoji // phpcs:ignore |
|
59
|
|
|
{ |
|
60
|
|
|
// Normalize shortcodes to match index. |
|
61
|
105 |
|
if (\strpos($this->index, 'shortcode') !== false) { |
|
62
|
75 |
|
$key = (string) \current(Normalize::shortcodes($key)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** @var ?Emoji $emoji */ |
|
66
|
105 |
|
$emoji = parent::offsetGet($key); |
|
67
|
|
|
|
|
68
|
102 |
|
return $emoji; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|