Utf8mapHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
require_once \dirname(__DIR__) . '/include/songlist.object.php';
4
require_once \dirname(__DIR__) . '/include/songlist.form.php';
5
6
/**
7
 * Class Utf8map
8
 */
9
class Utf8map extends \XoopsObject
10
{
11
    /**
12
     * Utf8map constructor.
13
     * @param null $fid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fid is correct as it would always require null to be passed?
Loading history...
14
     */
15
    public function __construct($fid = null)
0 ignored issues
show
Unused Code introduced by
The parameter $fid is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

15
    public function __construct(/** @scrutinizer ignore-unused */ $fid = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
16
    {
17
        $this->initVar('utfid', XOBJ_DTYPE_INT, 0, false);
18
        $this->initVar('from', XOBJ_DTYPE_TXTBOX, null, false, 2);
19
        $this->initVar('to', XOBJ_DTYPE_TXTBOX, null, false, 2);
20
        $this->initVar('created', XOBJ_DTYPE_INT, 0, false);
21
        $this->initVar('updated', XOBJ_DTYPE_INT, 0, false);
22
    }
23
24
    /**
25
     * @param bool $as_array
26
     * @return array|string
27
     */
28
    public function getForm($as_array = false)
29
    {
30
        return songlist_utf8map_get_form($this, $as_array);
0 ignored issues
show
Bug introduced by
The function songlist_utf8map_get_form was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        return /** @scrutinizer ignore-call */ songlist_utf8map_get_form($this, $as_array);
Loading history...
31
    }
32
33
    /**
34
     * @return array
35
     */
36
    public function toArray(): array
37
    {
38
        $ret  = parent::toArray();
39
        $form = $this->getForm(true);
40
        foreach ($form as $key => $element) {
41
            $ret['form'][$key] = $element->render();
42
        }
43
        foreach (['created', 'updated'] as $key) {
44
            if ($this->getVar($key) > 0) {
45
                $ret['form'][$key] = date(_DATESTRING, $this->getVar($key));
46
                $ret[$key]         = date(_DATESTRING, $this->getVar($key));
47
            }
48
        }
49
50
        return $ret;
51
    }
52
}
53
54
/**
55
 * Class Utf8mapHandler
56
 */
57
class Utf8mapHandler extends \XoopsPersistableObjectHandler
58
{
59
    /**
60
     * Utf8mapHandler constructor.
61
     * @param \XoopsDatabase $db
62
     */
63
    public function __construct(\XoopsDatabase $db)
64
    {
65
        parent::__construct($db, 'songlist_utf8map', Utf8map::class, 'utfid', 'from');
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function filterFields(): array
72
    {
73
        return ['utfid', 'from', 'to', 'created', 'updated'];
74
    }
75
76
    /**
77
     * @param $filter
78
     * @return \CriteriaCompo
79
     */
80
    public function getFilterCriteria($filter): \CriteriaCompo
81
    {
82
        $parts    = explode('|', $filter);
83
        $criteria = new \CriteriaCompo();
84
        foreach ($parts as $part) {
85
            $var = explode(',', $part);
86
            if (!empty($var[1]) && !is_numeric($var[0])) {
87
                $object = $this->create();
88
                if (XOBJ_DTYPE_TXTBOX == $object->vars[$var[0]]['data_type']
89
                    || XOBJ_DTYPE_TXTAREA == $object->vars[$var[0]]['data_type']) {
90
                    $criteria->add(new \Criteria('`' . $var[0] . '`', '%' . $var[1] . '%', ($var[2] ?? 'LIKE')));
91
                } elseif (in_array($object->vars[$var[0]]['data_type'], [XOBJ_DTYPE_INT, XOBJ_DTYPE_DECIMAL, XOBJ_DTYPE_FLOAT])) {
92
                    $criteria->add(new \Criteria('`' . $var[0] . '`', $var[1], ($var[2] ?? '=')));
93
                } elseif (XOBJ_DTYPE_ENUM == $object->vars[$var[0]]['data_type']) {
94
                    $criteria->add(new \Criteria('`' . $var[0] . '`', $var[1], ($var[2] ?? '=')));
95
                } elseif (XOBJ_DTYPE_ARRAY == $object->vars[$var[0]]['data_type']) {
96
                    $criteria->add(new \Criteria('`' . $var[0] . '`', '%"' . $var[1] . '";%', ($var[2] ?? 'LIKE')));
97
                }
98
            } elseif (!empty($var[1]) && is_numeric($var[0])) {
99
                $criteria->add(new \Criteria($var[0], $var[1]));
100
            }
101
        }
102
103
        return $criteria;
104
    }
105
106
    /**
107
     * @param        $filter
108
     * @param        $field
109
     * @param string $sort
110
     * @param string $op
111
     * @param string $fct
112
     * @return string
113
     */
114
    public function getFilterForm($filter, $field, $sort = 'created', $op = 'dashboard', $fct = 'list'): string
115
    {
116
        $ele = songlist_getFilterElement($filter, $field, $sort, $op, $fct);
117
        if (is_object($ele)) {
118
            return $ele->render();
119
        }
120
121
        return '&nbsp;';
122
    }
123
124
    /**
125
     * @param bool $force
126
     * @return mixed
127
     */
128
    public function insert(\XoopsObject $obj, $force = true)
129
    {
130
        if ($obj->isNew()) {
131
            $obj->setVar('created', time());
132
        } else {
133
            $obj->setVar('updated', time());
134
        }
135
136
        return parent::insert($obj, $force);
137
    }
138
139
    /**
140
     * @param string $phrase
141
     * @param null   $criteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $criteria is correct as it would always require null to be passed?
Loading history...
142
     * @return string|string[]
143
     */
144
    public function convert($phrase = '', $criteria = null)
145
    {
146
        foreach ($this->getObjects($criteria, true) as $utfid => $utf8) {
147
            $phrase = str_replace(mb_strtolower($utf8->getVar('from')), \mb_strtolower($utf8->getVar('to')), $phrase);
148
            $phrase = str_replace(mb_strtoupper($utf8->getVar('from')), \mb_strtoupper($utf8->getVar('to')), $phrase);
149
        }
150
151
        return $phrase;
152
    }
153
}
154