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

CWhitelist   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 2 Features 2
Metric Value
c 4
b 2
f 2
dl 0
loc 57
rs 10
ccs 16
cts 16
cp 1
wmc 8
lcom 1
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A set() 0 9 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