helpers.php ➔ insert_if_exists()   C
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 15
c 2
b 0
f 0
nc 7
nop 3
dl 0
loc 22
rs 6.6037
1
<?php
2
3
if (! function_exists('insert_if_exists')) {
4
    function insert_if_exists ($insert, $template = null, $match = null) {
5
        if (!is_null($insert)) { // if item exists
6
            if (is_array($insert)) { // if more than one item is given
7
                $ret = ''; // total of all items to return
8
                foreach ($insert as $in) { // for each item
9
                    $ret .= insert_if_exists($in, $template, $match); // get the value if it exists
10
                }
11
                return $ret; // return total
12
            }
13
            if (!is_null($match) && !$match($insert)) { // if a match function is set and item doesn't match
14
                return ''; // return nothing
15
            }
16
            if (!is_null($template)) { // if a template is set
17
                if (is_callable($template)) { // if the template is a function
18
                    return $template($insert); // insert value into the function
19
                }
20
                return $template; // use the template as a replacement
21
            }
22
            return $insert; // else, return the value
23
        }
24
        return ''; // if item doesn't exist, return nothing
25
    }
26
}