-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCargoFieldDescription.php
113 lines (104 loc) · 3.31 KB
/
CargoFieldDescription.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* CargoFieldDescription - holds the attributes of a single field as defined
* in the #cargo_declare parser function.
*
* @author Yaron Koren
* @ingroup Cargo
*/
class CargoFieldDescription {
var $mType;
var $mSize;
var $mIsList = false;
var $mDelimiter;
var $mAllowedValues = null;
var $mIsHidden = false;
var $mOtherParams = array();
/**
* Initializes from a string within the #cargo_declare function.
*/
static function newFromString( $fieldDescriptionStr ) {
$fieldDescription = new CargoFieldDescription();
if ( strpos( $fieldDescriptionStr, 'List') === 0 ) {
$matches = array();
$foundMatch = preg_match( '/List \((.*)\) of (.*)/', $fieldDescriptionStr, $matches);
if (! $foundMatch) {
// Return a true error message here?
return null;
}
$fieldDescription->mIsList = true;
$fieldDescription->mDelimiter = $matches[1];
$fieldDescriptionStr = $matches[2];
}
// There may be additional parameters, in/ parentheses.
$matches = array();
$foundMatch2 = preg_match( '/(.*)\s*\((.*)\)/', $fieldDescriptionStr, $matches);
if ( $foundMatch2 ) {
$fieldDescriptionStr = trim( $matches[1] );
$extraParamsString = $matches[2];
$extraParams = explode( ';', $extraParamsString );
foreach ( $extraParams as $extraParam ) {
$extraParamParts = explode( '=', $extraParam, 2 );
if ( count( $extraParamParts ) == 1 ) {
$paramKey = trim( $extraParamParts[0] );
$fieldDescription->mOtherParams[$paramKey] = true;
} else {
$paramKey = trim( $extraParamParts[0] );
$paramValue = trim( $extraParamParts[1] );
if ( $paramKey == 'allowed values' ) {
$fieldDescription->mAllowedValues = array_map( 'trim', explode( ',', $paramValue ) );
} elseif ( $paramKey == 'size' ) {
$fieldDescription->mSize = $paramValue;
} else {
$fieldDescription->mOtherParams[$paramKey] = $paramValue;
}
}
}
}
// What's left will be the type, hopefully.
$fieldDescription->mType = $fieldDescriptionStr;
return $fieldDescription;
}
static function newFromDBArray( $descriptionData ) {
$fieldDescription = new CargoFieldDescription();
foreach ( $descriptionData as $param => $value ) {
if ( $param == 'type' ) {
$fieldDescription->mType = $value;
} elseif ( $param == 'size' ) {
$fieldDescription->mSize = $value;
} elseif ( $param == 'isList' ) {
$fieldDescription->mIsList = true;
} elseif ( $param == 'delimiter' ) {
$fieldDescription->mDelimiter = $value;
} elseif ( $param == 'allowedValues' ) {
$fieldDescription->mAllowedValues = $value;
} elseif ( $param == 'hidden' ) {
$fieldDescription->mIsHidden = true;
}
}
return $fieldDescription;
}
function toDBArray() {
$descriptionData = array();
$descriptionData['type'] = $this->mType;
if ( $this->mSize != null ) {
$descriptionData['size'] = $this->mSize;
}
if ( $this->mIsList ) {
$descriptionData['isList'] = true;
}
if ( $this->mDelimiter != null ) {
$descriptionData['delimiter'] = $this->mDelimiter;
}
if ( $this->mAllowedValues != null ) {
$descriptionData['allowedValues'] = $this->mAllowedValues;
}
if ( $this->mIsHidden ) {
$descriptionData['hidden'] = true;
}
foreach ( $this->mOtherParams as $otherParam => $value ) {
$descriptionData[$otherParam] = $value;
}
return $descriptionData;
}
}