Completed
Push — master ( a31037...83afbc )
by WEBEWEB
02:24
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
rs 9.552
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-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\BootstrapBundle\Helper\Model;
13
14
use WBW\Bundle\BootstrapBundle\Model\Select2\Select2DataFormatInterface;
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\BootstrapBundle\Helper\Model
22
 */
23
class Select2Helper {
24
25
    /**
26
     * Convert items into a "results" array.
27
     *
28
     * @param Select2DataFormatInterface[] $items The items.
29
     * @return array Returns the "results" array.
30
     * @throws IllegalArgumentException Throws an illegal argument exceptino if an item does not implement Select2DataFormatInterface.
31
     */
32
    public static function toResults($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 Select2DataFormatInterface)) {
42
                throw new IllegalArgumentException("The item must implements Select2DataFormatInterface");
43
            }
44
45
            // Add the item.
46
            $output[] = [
47
                "id"   => $current->getSelect2DataFormatId(),
48
                "text" => $current->getSelect2DataFormatText(),
49
            ];
50
        }
51
52
        // Return the output.
53
        return $output;
54
    }
55
56
}
57