Completed
Push — master ( 6994e0...9e8e5d )
by WEBEWEB
01:34
created

Select2Helper::toResults()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
rs 9.552
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
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 WBW\Bundle\CoreBundle\Helper;
13
14
use WBW\Bundle\CoreBundle\Entity\Select2ItemInterface;
15
use WBW\Library\Core\Exception\Argument\IllegalArgumentException;
16
17
/**
18
 * Select2 helper.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Bundle\CoreBundle\Helper
22
 */
23
class Select2Helper {
24
25
    /**
26
     * Convert items into a "results" array.
27
     *
28
     * @param Select2ItemInterface[] $items The items.
29
     * @return array Returns the "results" array.
30
     * @throws IllegalArgumentException Throws an illegal argument exception if an item does not implement Select2ItemInterface.
31
     */
32
    public static function toResults(array $items) {
33
34
        // Initialize the output.
35
        $output = [];
36
37
        // Handle each item.
38
        foreach ($items as $current) {
39
40
            // Check the item.
41
            if (false === ($current instanceof Select2ItemInterface)) {
42
                throw new IllegalArgumentException("The item must implements Select2ItemInterface");
43
            }
44
45
            // Add the item.
46
            $output[] = [
47
                "id"   => $current->getSelect2ItemId(),
48
                "text" => $current->getSelect2ItemText(),
49
            ];
50
        }
51
52
        // Return the output.
53
        return $output;
54
    }
55
56
}
57