Column::validateMandatoryColumns()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
ccs 12
cts 13
cp 0.9231
rs 8.8571
cc 5
eloc 10
nc 5
nop 0
crap 5.0113
1
<?php
2
namespace JsonTable\Analyse;
3
4
/**
5
 * Perform analysis on the CSV columns.
6
 *
7
 * @package JsonTable
8
 */
9
class Column extends Analyse implements AnalyseInterface
10
{
11
    /**
12
     * @var string The description for missing mandatory columns.
13
     */
14
    const ERROR_REQUIRED_COLUMN_MISSING = '<strong>%d</strong> required column(s) missing:';
15
16
    /**
17
     * @var string The description for CSV columns that are not in the schema.
18
     */
19
    const ERROR_UNSPECIFIED_COLUMN = '<strong>%d</strong> unexpected column(s):';
20
21
    
22
    /**
23
     * Validate that all fields are of the correct type, format and pattern.
24
     * This also checks that each CSV row has the expected number of columns.
25
     *
26
     * @return  boolean Is all data lexically valid.
27
     */
28 75
    public function validate()
29
    {
30 75
        if (!$this->validateMandatoryColumns()) {
31 2
            return false;
32
        }
33
34 73
        if (!$this->validateUnspecifiedColumns() && $this->stopIfInvalid) {
35
            return false;
36
        }
37
38 73
        return true;
39
    }
40
    
41
    
42
    /**
43
     * Validate that all mandatory columns are present.
44
     *
45
     * @return boolean Are all mandatory columns present.
46
     */
47 75
    private function validateMandatoryColumns()
48
    {
49 75
        $validMandatoryColumns = true;
50
51 75
        foreach (parent::$schemaJson->fields as $field) {
52 75
            if ($this->isColumnMandatory($field)) {
53 74
                if (!in_array($field->name, parent::$headerColumns)) {
54 2
                    $this->error->setError(self::ERROR_REQUIRED_COLUMN_MISSING, $field->name);
55 2
                    $validMandatoryColumns = false;
56
57 2
                    if ($this->stopIfInvalid) {
58
                        return false;
59
                    }
60 2
                }
61 74
            }
62 75
        }
63
64 75
        return $validMandatoryColumns;
65
    }
66
67
68
    /**
69
     * Check that there are no columns in the CSV that are not specified in the schema.
70
     *
71
     * @return boolean Are all the CSV columns specified in the schema.
72
     */
73 73
    private function validateUnspecifiedColumns()
74
    {
75 73
        $validUnspecifiedColumns = true;
76
77 73
        foreach (parent::$headerColumns as $csvColumnName) {
78 73
            if (false === $this->getSchemaKeyFromName($csvColumnName)) {
79
                $this->error->setError(self::ERROR_UNSPECIFIED_COLUMN, $csvColumnName);
80
                $validUnspecifiedColumns = false;
81
82
                if ($this->stopIfInvalid) {
83
                    return false;
84
                }
85
            }
86 73
        }
87
88 73
        return $validUnspecifiedColumns;
89
    }
90
}