Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
9c16da05
Commit
9c16da05
authored
Jun 11, 2006
by
doctrine
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validator type bug fix
parent
2d5e8c4a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
105 additions
and
6 deletions
+105
-6
Validator.php
Doctrine/Validator.php
+40
-6
ValidatorTestCase.php
tests/ValidatorTestCase.php
+65
-0
No files found.
Doctrine/Validator.php
View file @
9c16da05
...
...
@@ -154,11 +154,16 @@ class Doctrine_Validator {
break
;
}
}
/**
if(self::gettype($value) !== $column[0] && self::gettype($value) != 'NULL') {
$err[$key] = Doctrine_Validator::ERR_TYPE;
continue;
}
*/
if
(
!
self
::
isValidType
(
$value
,
$column
[
0
]))
{
$err
[
$key
]
=
Doctrine_Validator
::
ERR_TYPE
;
continue
;
}
}
if
(
!
empty
(
$err
))
{
...
...
@@ -184,6 +189,32 @@ class Doctrine_Validator {
public
function
getErrorStack
()
{
return
$this
->
stack
;
}
/**
* returns whether or not the given variable is
* valid type
*
* @param mixed $var
* @param string $type
* @return boolean
*/
public
static
function
isValidType
(
$var
,
$type
)
{
$looseType
=
self
::
gettype
(
$var
);
switch
(
$looseType
)
:
case
'float'
:
case
'double'
:
case
'integer'
:
if
(
$type
==
'string'
||
$type
==
'float'
)
return
true
;
case
'string'
:
case
'array'
:
case
'object'
:
return
(
$type
===
$looseType
);
break
;
case
'NULL'
:
return
true
;
break
;
endswitch
;
}
/**
* returns the type of loosely typed variable
*
...
...
@@ -193,13 +224,16 @@ class Doctrine_Validator {
public
static
function
gettype
(
$var
)
{
$type
=
gettype
(
$var
);
switch
(
$type
)
:
case
"string"
:
if
(
preg_match
(
"/^[0-9]+$/"
,
$var
))
return
"integer"
;
elseif
(
is_numeric
(
$var
))
return
"float"
;
else
return
$type
;
case
'string'
:
if
(
preg_match
(
"/^[0-9]+$/"
,
$var
))
return
'integer'
;
elseif
(
is_numeric
(
$var
))
return
'float'
;
else
return
$type
;
break
;
default
:
return
$type
;
return
$type
;
endswitch
;
}
}
...
...
tests/ValidatorTestCase.php
View file @
9c16da05
...
...
@@ -4,6 +4,71 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase {
$this
->
tables
[]
=
"Validator_Test"
;
parent
::
prepareTables
();
}
public
function
testIsValidType
()
{
$var
=
"123"
;
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"string"
));
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"integer"
));
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"float"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"array"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"object"
));
$var
=
123
;
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"string"
));
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"integer"
));
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"float"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"array"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"object"
));
$var
=
123.12
;
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"string"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"integer"
));
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"float"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"array"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"object"
));
$var
=
'123.12'
;
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"string"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"integer"
));
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"float"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"array"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"object"
));
$var
=
''
;
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"string"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"integer"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"float"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"array"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"object"
));
$var
=
null
;
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"string"
));
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"integer"
));
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"float"
));
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"array"
));
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"object"
));
$var
=
'str'
;
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"string"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"integer"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"float"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"array"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"object"
));
$var
=
array
();
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"string"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"integer"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"float"
));
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"array"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"object"
));
$var
=
new
Exception
();
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"string"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"integer"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"float"
));
$this
->
assertFalse
(
Doctrine_Validator
::
isValidType
(
$var
,
"array"
));
$this
->
assertTrue
(
Doctrine_Validator
::
isValidType
(
$var
,
"object"
));
}
public
function
testValidate2
()
{
$test
=
new
Validator_Test
();
$test
->
mymixed
=
"message"
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment