DataHelpers   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 22
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A disambiguation() 0 14 4
1
<?php
2
/**
3
 * Livia
4
 * Copyright 2017-2019 Charlotte Dunois, All Rights Reserved
5
 *
6
 * Website: https://charuru.moe
7
 * License: https://github.com/CharlotteDunois/Livia/blob/master/LICENSE
8
*/
9
10
namespace CharlotteDunois\Livia\Utils;
11
12
/**
13
 * Data orientated helpers.
14
 */
15
class DataHelpers {
16
    /**
17
     * If a selection is ambiguous, this will make a list of selectable items.
18
     * @param array|\CharlotteDunois\Collect\Collection       $items
19
     * @param string                                          $label
20
     * @param string|null                                     $property
21
     * @return string
22
     */
23
    static function disambiguation($items, string $label, ?string $property = null) {
24
        if($items instanceof \CharlotteDunois\Collect\Collection) {
25
            $items = $items->all();
26
        }
27
        
28
        $itemList = \array_map(function ($item) use ($property) {
29
            if($property !== null) {
30
                $item = (\is_array($item) ? $item[$property] : $item->$property);
31
            }
32
            
33
            return '`'.\str_replace(' ', "\u{00A0}", \CharlotteDunois\Yasmin\Utils\MessageHelpers::escapeMarkdown($item)).'`';
34
        }, $items);
35
        
36
        return 'Multiple '.$label.' found, please be more specific: '.\implode(', ', $itemList);
37
    }
38
}
39