Emoji::findByName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Laravel Emoji package.
5
 *
6
 * (c) Prosper Otemuyiwa <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Unicodeveloper\Emoji;
13
14
use Unicodeveloper\Emoji\Exceptions\{ UnknownMethod, UnknownEmoji, UnknownUnicode, IsNull };
15
16
class Emoji {
17
18
    /**
19
     * Get the Emojis from emoji.php
20
     * @return array
21
     */
22
    private function getEmojis() : array
23
    {
24
        return require("Emojis/emoji.php");
25
    }
26
27
    /**
28
     * Get the emoji  by passing the commonly used emoji name
29
     * @param  string $emojiName
30
     * @return unicode
31
     * @throws \Unicodeveloper\Emoji\Exceptions\IsNull
32
     * @throws \Unicodeveloper\Emoji\Exceptions\UnknownUnicode
33
     */
34 View Code Duplication
    public function findByAlias($emojiName = null) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        if (is_null($emojiName)) {
37
            throw IsNull::create("Please provide the name of the emoji you are looking for");
38
        }
39
40
        $emoji = strtolower($emojiName);
41
42
        if (! array_key_exists($emoji, $this->getEmojis())) {
43
            throw UnknownEmoji::create($emoji);
44
        }
45
46
        return $this->getEmojis()[$emoji];
47
    }
48
49
    /**
50
     * Get the emoji  by passing the commonly used emoji name
51
     * Alias for findByAlias
52
     * @param  string $emojiName
53
     * @return unicode
54
     */
55
    public function findByName($emojiName = null) : string
56
    {
57
        return $this->findByAlias($emojiName);
58
    }
59
60
    /**
61
     * Get the emoji name by passing the unicode value
62
     * @param  string $unicode
63
     * @return string
64
     * @throws \Unicodeveloper\Emoji\Exceptions\IsNull
65
     * @throws \Unicodeveloper\Emoji\Exceptions\UnknownUnicode
66
     */
67 View Code Duplication
    public function findByUnicode($unicode = null) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        if (is_null($unicode)) {
70
            throw IsNull::create("Please provide a valid UTF-8 Unicode value");
71
        }
72
73
        $emojis = array_flip($this->getEmojis());
74
75
        if (! array_key_exists($unicode, $emojis)) {
76
            throw UnknownUnicode::create($unicode);
77
        }
78
79
        return $emojis[$unicode];
80
    }
81
82
    /**
83
     * Ensure that a proper exception is thrown for methods that do not exist
84
     * @param  string $method
85
     * @param  array $parameters
86
     * @throws \Unicodeveloper\Emoji\Exceptions\UnknownMethod
87
     */
88
    public function __call($method, $parameters)
89
    {
90
        if (! method_exists(new static, $method)) {
91
            throw UnknownMethod::create($method);
92
        }
93
    }
94
95
}