ListToolbox   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 72
rs 10
wmc 11
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 8 2
D formatter() 0 37 9
1
<?php
2
/**
3
 * Licensed under The GPL-3.0 License
4
 * For full copyright and license information, please see the LICENSE.txt
5
 * Redistributions of files must retain the above copyright notice.
6
 *
7
 * @since    2.0.0
8
 * @author   Christopher Castro <[email protected]>
9
 * @link     http://www.quickappscms.org
10
 * @license  http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License
11
 */
12
namespace Field\Utility;
13
14
use CMS\Shortcode\ShortcodeTrait;
15
use Field\Model\Entity\Field;
16
17
/**
18
 * List utility class.
19
 *
20
 * Utility methods used by ListField Handler.
21
 */
22
class ListToolbox
23
{
24
25
    use ShortcodeTrait;
26
27
    /**
28
     * Holds an instance of this class.
29
     *
30
     * @var \Field\Utility\ListToolbox
31
     */
32
    protected static $_instance = null;
33
34
    /**
35
     * Returns an instance of this class.
36
     *
37
     * Useful when we need to use some of the trait methods.
38
     *
39
     * @return \Field\Utility\ListToolbox
40
     */
41
    public static function getInstance()
42
    {
43
        if (!static::$_instance) {
44
            static::$_instance = new ListToolbox();
45
        }
46
47
        return static::$_instance;
48
    }
49
50
    /**
51
     * Formats the given field.
52
     *
53
     * @param \Field\Model\Entity\Field $field The field being rendered
54
     * @return string
55
     */
56
    public static function formatter(Field $field)
57
    {
58
        $result = '';
59
        $options = [];
60
61
        if (!empty($field->metadata->settings['options'])) {
62
            foreach (explode("\n", $field->metadata->settings['options']) as $option) {
63
                $option = explode('|', $option);
64
                $value = $option[0];
65
                $label = isset($option[1]) ? $option[1] : $option[0];
66
                $options[$value] = $label;
67
            }
68
        }
69
70
        if (is_string($field->extra)) {
71
            $selectedOptions = [$field->extra];
72
        } else {
73
            $selectedOptions = (array)$field->extra;
74
        }
75
76
        foreach ($selectedOptions as $key) {
77
            switch ($field->viewModeSettings['formatter']) {
78
                case 'key':
79
                    $result .= "{$key}<br />";
80
                    break;
81
82
                case 'default':
83
                default:
84
                    if (!empty($options[$key])) {
85
                        $result .= "{$options[$key]}<br />";
86
                    }
87
                    break;
88
            }
89
        }
90
91
        return $result;
92
    }
93
}
94