Test Failed
Push — resize ( fbaf05...026e01 )
by Mikael
02:27
created

CWhitelist::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 2
1
<?php
2
3
namespace Mos\CImage;
4
5
/**
6
 * Act as whitelist (or blacklist).
7
 *
8
 */
9
class CWhitelist
10
{
11
    /**
12
     * Array to contain the whitelist options.
13
     */
14
    private $whitelist = array();
15
16
17
18
    /**
19
     * Set the whitelist from an array of strings, each item in the
20
     * whitelist should be a regexp without the surrounding / or #.
21
     *
22
     * @param array $whitelist with all valid options,
23
     *                         default is to clear the whitelist.
24 6
     *
25
     * @return $this
26 6
     */
27 1
    public function set($whitelist = array())
28
    {
29
        if (!is_array($whitelist)) {
30 5
            throw new Exception("Whitelist is not of a supported format.");
31 5
        }
32
33
        $this->whitelist = $whitelist;
34
        return $this;
35
    }
36
37
38
39
    /**
40
     * Check if item exists in the whitelist.
41
     *
42
     * @param string $item      string to check.
43
     * @param array  $whitelist optional with all valid options, default is null.
44 5
     *
45
     * @return boolean true if item is in whitelist, else false.
46 5
     */
47 2
    public function check($item, $whitelist = null)
48 2
    {
49
        if ($whitelist !== null) {
50 5
            $this->set($whitelist);
51 1
        }
52
        
53
        if (empty($item) or empty($this->whitelist)) {
54 4
            return false;
55 4
        }
56 2
        
57
        foreach ($this->whitelist as $regexp) {
58 2
            if (preg_match("#$regexp#", $item)) {
59
                return true;
60 2
            }
61
        }
62
63
        return false;
64
    }
65
}
66