DirEmpty   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 48
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C validate() 0 23 7
1
<?php
2
/**
3
 * Jaeger
4
 *
5
 * @copyright	Copyright (c) 2015-2016, mithra62
6
 * @link		http://jaeger-app.com
7
 * @version		1.0
8
 * @filesource 	./Validate/Filesystem/DirEmpty.php
9
 */
10
namespace JaegerApp\Validate\Rules\Filesystem;
11
12
use JaegerApp\Validate\AbstractRule;
13
if (! class_exists('\\JaegerApp\\Validate\\Rules\\Filesystem\\DirEmpty')) {
14
15
    /**
16
     * Jaeger - Empty Directory Validation Rule
17
     *
18
     * Validates that a given input is an empty directory
19
     *
20
     * @package Validate\Rules\Filesystem
21
     * @author Eric Lamb <[email protected]>
22
     */
23
    class DirEmpty extends AbstractRule
24
    {
25
26
        /**
27
         * The Rule shortname
28
         * 
29
         * @var string
30
         */
31
        protected $name = 'dir_empty';
32
33
        /**
34
         * The error template
35
         * 
36
         * @var string
37
         */
38
        protected $error_message = '{field} has to be an empty directory';
39
40
        /**
41
         * (non-PHPdoc)
42
         * 
43
         * @see \mithra62\Validate\RuleInterface::validate()
44
         * @ignore
45
         *
46
         */
47
        public function validate($field, $input, array $params = array())
48
        {
49
            if ($input instanceof \SplFileInfo) {
50
                if ($input->isDir()) {
51
                    $input = $input->getBasename();
52
                } else {
53
                    return false;
54
                }
55
            } else {
56
                if (! is_dir($input)) {
57
                    return false;
58
                }
59
            }
60
            
61
            $d = dir($input);
62
            while (false !== ($entry = $d->read())) {
63
                if ($entry != '.' && $entry != '..') {
64
                    return false;
65
                }
66
            }
67
            
68
            return true;
69
        }
70
    }
71
}