This repository has been archived by the owner on Oct 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataField.class.php
173 lines (156 loc) · 3.39 KB
/
DataField.class.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
** @file DataField.class.php
** @author immeëmosol (programmer dot willfris at nl)
** @date 2011-01-21
** Created: ven 2011-01-21, 10:33.38 CET
** Last modified: sab 2011-01-22, 21:32.09 CET
**/
/**
* @todo[~immeëmosol, ven 2011-01-21, 10:49.42 CET]
* make date , time & datetime be `bitmask-compatible`
**/
class DataField extends DataBase
{
const AUTO = 1;
const OPTIONAL = 2;
const PK = 4;
const FK = 8;
const INT = 16;
const BOOLEAN = 32;
const TINYINT = 64;
const POS_INT = 128;
const TEXT = 256;
const STRING = 512;
const DATETIME = 1024;
const DATE = 2048;
const TIME = 4096;
const OBJECT = 8192;
/**
* one of this class' constants.
**/
private $type;
/**
* collection of restrictions on the input-data
**/
private $restrictions = array();
/**
* collection of conditions that were not met by input.
**/
private $errors = array();
private $hint = NULL;
/**
* the default option for the data, if any
**/
private $auto_value;
const AV_DEFAULT = 'elbasseugnu_gnihtemos';
public function __construct (
$name ,
$type ,
$restrictions = array() ,
$auto_value = self::AV_DEFAULT ,
$hint = NULL
)
{
$this->type( $type );
$this->name( $name );
$this->hint( $hint );
$this->gsAutoValue( $auto_value );
if ( !is_array( $restrictions ) )
$restrictions = array( $restrictions );
foreach ( $restrictions as $restriction )
$this->restriction( $restriction );
}
public function autoValue ()
{
return $this->gsAutovalue();
}
private function gsAutoValue ( $auto_value = self::AV_DEFAULT )
{
//if ( 0 === func_num_args() )
if ( self::AV_DEFAULT === $auto_value )
return $this->auto_value;
$this->auto_value = $auto_value;
}
public function required ()
{
if (
self::AV_DEFAULT === $this->gsAutoValue()
|| DataField::OPTIONAL & $this->type()
)
return FALSE;
return TRUE;
}
public function check ( $value )
{
foreach ( $this->restrictions as $restriction )
{
$check = call_user_func_array(
array(
$this ,
'check_restriction'
) ,
array(
$restriction ,
$value ,
)
);
if ( FALSE === $check )
$this->errors[] = $restriction[ 'message' ];
}
if ( empty( $this->errors ) )
return TRUE;
return FALSE;
}
public function hint ( $hint = NULL )
{
if ( NULL === $hint )
return $this->hint;
$this->hint = $hint;
return;
return $this;
}
public function errors ()
{
return $this->errors;
}
public function type ( $type = NULL )
{
if ( NULL === $type )
return $this->type;
$this->type = $type;
return;
return $this;
}
protected function restriction ( $restriction )
{
if ( is_numeric( $restriction ) )
$restriction = array(
'function' => 'strlen' ,
'check' => array( 'lte' => $restriction ) ,
'message' => 'to many chars' ,
);
$this->restrictions[] = $restriction;
}
private function check_restriction ( $restriction , $value )
{
$check = TRUE;
if ( isset( $restriction[ 'function' ] ) )
$check = call_user_func( $restriction[ 'function' ] , $value );
if ( isset( $restriction[ 'check' ] ) )
{
$key = key( $restriction[ 'check' ] );
switch ( $key )
{
case 'lte' :
$check =
$check <= $restriction[ 'check' ][ 'lte' ]
? TRUE
: FALSE
;
break;
}
}
return $check;
}
}