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
02dd8b1a
Commit
02dd8b1a
authored
Sep 29, 2007
by
romanb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validator refactoring. 2 new validators: past & future
parent
e647cdbb
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
391 additions
and
10 deletions
+391
-10
Validator.php
lib/Doctrine/Validator.php
+50
-5
Date.php
lib/Doctrine/Validator/Date.php
+1
-1
Email.php
lib/Doctrine/Validator/Email.php
+1
-1
Future.php
lib/Doctrine/Validator/Future.php
+79
-0
Past.php
lib/Doctrine/Validator/Past.php
+79
-0
ValidatorTest.php
models/ValidatorTest.php
+3
-3
ValidatorTest_DateModel.php
models/ValidatorTest_DateModel.php
+7
-0
FutureTestCase.php
tests/Validator/FutureTestCase.php
+84
-0
PastTestCase.php
tests/Validator/PastTestCase.php
+84
-0
run.php
tests/run.php
+3
-0
No files found.
lib/Doctrine/Validator.php
View file @
02dd8b1a
...
...
@@ -77,7 +77,7 @@ class Doctrine_Validator extends Doctrine_Object
foreach
(
$data
as
$key
=>
$value
)
{
if
(
$value
===
self
::
$_null
)
{
$value
=
null
;
}
elseif
(
$value
instanceof
Doctrine_Record
)
{
}
else
if
(
$value
instanceof
Doctrine_Record
)
{
$value
=
$value
->
getIncremented
();
}
...
...
@@ -191,7 +191,7 @@ class Doctrine_Validator extends Doctrine_Object
*
* @param $portableType portable doctrine type
* @return string
*/
*/
/*
public static function phpType($portableType)
{
switch ($portableType) {
...
...
@@ -208,7 +208,7 @@ class Doctrine_Validator extends Doctrine_Object
default:
return $portableType;
}
}
}
*/
/**
* returns whether or not the given variable is
* valid type
...
...
@@ -217,6 +217,7 @@ class Doctrine_Validator extends Doctrine_Object
* @param string $type
* @return boolean
*/
/*
public static function isValidType($var, $type)
{
if ($type == 'boolean') {
...
...
@@ -242,13 +243,57 @@ class Doctrine_Validator extends Doctrine_Object
return true;
break;
}
}*/
/**
* returns whether or not the given variable is
* valid type
*
* @param mixed $var
* @param string $type
* @return boolean
*/
public
static
function
isValidType
(
$var
,
$type
)
{
if
(
$var
===
null
)
{
return
true
;
}
else
if
(
is_object
(
$var
))
{
return
$type
==
'object'
;
}
switch
(
$type
)
{
case
'float'
:
case
'double'
:
return
(
String
)
$var
==
strval
(
floatval
(
$var
));
case
'integer'
:
return
(
String
)
$var
==
strval
(
intval
(
$var
));
case
'string'
:
return
is_string
(
$var
)
||
is_int
(
$var
)
||
is_float
(
$var
);
case
'array'
:
return
is_array
(
$var
);
case
'object'
:
return
is_object
(
$var
);
case
'boolean'
:
return
is_bool
(
$var
);
case
'timestamp'
:
// todo: validate the timestamp is in YYYY-MM-DD HH:MM:SS format
return
true
;
case
'date'
:
$validator
=
self
::
getValidator
(
'date'
);
return
$validator
->
validate
(
$var
);
default
:
return
false
;
}
}
/**
* returns the type of loosely typed variable
*
* @param mixed $var
* @return string
*/
*/
/*
public static function gettype($var)
{
$type = gettype($var);
...
...
@@ -265,5 +310,5 @@ class Doctrine_Validator extends Doctrine_Object
default:
return $type;
}
}
}
*/
}
lib/Doctrine/Validator/Date.php
View file @
02dd8b1a
...
...
@@ -40,7 +40,7 @@ class Doctrine_Validator_Date
*/
public
function
validate
(
$value
)
{
if
(
empty
(
$value
)
)
{
if
(
$value
===
null
)
{
return
true
;
}
$e
=
explode
(
'-'
,
$value
);
...
...
lib/Doctrine/Validator/Email.php
View file @
02dd8b1a
...
...
@@ -41,7 +41,7 @@ class Doctrine_Validator_Email
*/
public
function
validate
(
$value
)
{
if
(
empty
(
$value
)
)
{
if
(
$value
===
null
)
{
return
true
;
}
if
(
isset
(
$this
->
args
))
{
...
...
lib/Doctrine/Validator/Future.php
0 → 100644
View file @
02dd8b1a
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Validator_Future
*
* @package Doctrine
* @category Object Relational Mapping
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
* @author Roman Borschel <roman@code-factory.org>
*/
class
Doctrine_Validator_Future
{
/**
* checks if the given value is a valid date in the future.
*
* @param mixed $value
* @return boolean
*/
public
function
validate
(
$value
)
{
if
(
$value
===
null
)
{
return
true
;
}
$e
=
explode
(
'-'
,
$value
);
if
(
count
(
$e
)
!==
3
)
{
return
false
;
}
if
(
is_array
(
$this
->
args
)
&&
isset
(
$this
->
args
[
'timezone'
]))
{
switch
(
strtolower
(
$this
->
args
[
'timezone'
]))
{
case
'gmt'
:
$now
=
gmdate
(
"U"
)
-
date
(
"Z"
);
break
;
default
:
$now
=
getdate
();
break
;
}
}
else
{
$now
=
getdate
();
}
if
(
$now
[
'year'
]
>
$e
[
0
])
{
return
false
;
}
else
if
(
$now
[
'year'
]
==
$e
[
0
])
{
if
(
$now
[
'mon'
]
>
$e
[
1
])
{
return
false
;
}
else
if
(
$now
[
'mon'
]
==
$e
[
1
])
{
return
$now
[
'mday'
]
<
$e
[
2
];
}
else
{
return
true
;
}
}
else
{
return
true
;
}
}
}
\ No newline at end of file
lib/Doctrine/Validator/Past.php
0 → 100644
View file @
02dd8b1a
<?php
/*
* $Id: Date.php 2367 2007-09-02 20:00:27Z zYne $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Validator_Past
*
* @package Doctrine
* @category Object Relational Mapping
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
* @author Roman Borschel <roman@code-factory.org>
*/
class
Doctrine_Validator_Past
{
/**
* checks if the given value is a valid date in the past.
*
* @param mixed $value
* @return boolean
*/
public
function
validate
(
$value
)
{
if
(
$value
===
null
)
{
return
true
;
}
$e
=
explode
(
'-'
,
$value
);
if
(
count
(
$e
)
!==
3
)
{
return
false
;
}
if
(
is_array
(
$this
->
args
)
&&
isset
(
$this
->
args
[
'timezone'
]))
{
switch
(
strtolower
(
$this
->
args
[
'timezone'
]))
{
case
'gmt'
:
$now
=
gmdate
(
"U"
)
-
date
(
"Z"
);
break
;
default
:
$now
=
getdate
();
break
;
}
}
else
{
$now
=
getdate
();
}
if
(
$now
[
'year'
]
<
$e
[
0
])
{
return
false
;
}
else
if
(
$now
[
'year'
]
==
$e
[
0
])
{
if
(
$now
[
'mon'
]
<
$e
[
1
])
{
return
false
;
}
else
if
(
$now
[
'mon'
]
==
$e
[
1
])
{
return
$now
[
'mday'
]
>
$e
[
2
];
}
else
{
return
true
;
}
}
else
{
return
true
;
}
}
}
\ No newline at end of file
models/ValidatorTest.php
View file @
02dd8b1a
...
...
@@ -2,14 +2,14 @@
class
ValidatorTest
extends
Doctrine_Record
{
public
function
setTableDefinition
()
{
$this
->
hasColumn
(
'mymixed'
,
'string'
,
100
);
$this
->
hasColumn
(
'mystring'
,
'string'
,
100
,
'notnull|unique'
);
$this
->
hasColumn
(
'mystring'
,
'string'
,
100
,
array
(
'notnull'
,
'unique'
)
);
$this
->
hasColumn
(
'myarray'
,
'array'
,
1000
);
$this
->
hasColumn
(
'myobject'
,
'object'
,
1000
);
$this
->
hasColumn
(
'myinteger'
,
'integer'
,
11
);
$this
->
hasColumn
(
'myrange'
,
'integer'
,
11
,
array
(
'range'
=>
array
(
4
,
123
)));
$this
->
hasColumn
(
'myregexp'
,
'string'
,
5
,
array
(
'regexp'
=>
'/^[0-9]+$/'
));
$this
->
hasColumn
(
'myemail'
,
'string'
,
100
,
'email'
);
$this
->
hasColumn
(
'myemail2'
,
'string'
,
100
,
'email|notblank'
);
$this
->
hasColumn
(
'myemail'
,
'string'
,
100
,
array
(
'email'
)
);
$this
->
hasColumn
(
'myemail2'
,
'string'
,
100
,
array
(
'email'
,
'notblank'
)
);
}
}
models/ValidatorTest_DateModel.php
0 → 100644
View file @
02dd8b1a
<?php
class
ValidatorTest_DateModel
extends
Doctrine_Record
{
public
function
setTableDefinition
()
{
$this
->
hasColumn
(
'birthday'
,
'date'
,
null
,
array
(
'past'
));
$this
->
hasColumn
(
'death'
,
'date'
,
null
,
array
(
'future'
));
}
}
tests/Validator/FutureTestCase.php
0 → 100644
View file @
02dd8b1a
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Validator_FutureTestCase
*
* @package Doctrine
* @author Roman Borschel <roman@code-factory.org>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class
Doctrine_Validator_Future_TestCase
extends
Doctrine_UnitTestCase
{
public
function
prepareTables
()
{
$this
->
tables
[]
=
'ValidatorTest_DateModel'
;
parent
::
prepareTables
();
}
public
function
prepareData
()
{
}
public
function
testValidFutureDates
()
{
$this
->
manager
->
setAttribute
(
Doctrine
::
ATTR_VALIDATE
,
Doctrine
::
VALIDATE_ALL
);
// one year ahead
$user1
=
new
ValidatorTest_DateModel
();
$user1
->
death
=
date
(
'Y-m-d'
,
time
()
+
365
*
24
*
60
*
60
);
$this
->
assertTrue
(
$user1
->
trySave
());
// one month ahead
$user1
=
new
ValidatorTest_DateModel
();
$user1
->
death
=
date
(
'Y-m-d'
,
time
()
+
30
*
24
*
60
*
60
);
$this
->
assertTrue
(
$user1
->
trySave
());
// one day ahead
$user1
->
death
=
date
(
'Y-m-d'
,
time
()
+
24
*
60
*
60
);
$this
->
assertTrue
(
$user1
->
trySave
());
$this
->
manager
->
setAttribute
(
Doctrine
::
ATTR_VALIDATE
,
Doctrine
::
VALIDATE_NONE
);
}
public
function
testInvalidFutureDates
()
{
$this
->
manager
->
setAttribute
(
Doctrine
::
ATTR_VALIDATE
,
Doctrine
::
VALIDATE_ALL
);
$user1
=
new
ValidatorTest_DateModel
();
$user1
->
death
=
date
(
'Y-m-d'
,
42
);
$this
->
assertFalse
(
$user1
->
trySave
());
$user1
->
death
=
date
(
'Y-m-d'
,
time
());
$this
->
assertFalse
(
$user1
->
trySave
());
$user1
->
death
=
date
(
'Y-m-d'
,
time
()
+
60
);
$this
->
assertFalse
(
$user1
->
trySave
());
$this
->
manager
->
setAttribute
(
Doctrine
::
ATTR_VALIDATE
,
Doctrine
::
VALIDATE_NONE
);
}
}
tests/Validator/PastTestCase.php
0 → 100644
View file @
02dd8b1a
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Validator_FutureTestCase
*
* @package Doctrine
* @author Roman Borschel <roman@code-factory.org>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class
Doctrine_Validator_Past_TestCase
extends
Doctrine_UnitTestCase
{
public
function
prepareTables
()
{
$this
->
tables
[]
=
'ValidatorTest_DateModel'
;
parent
::
prepareTables
();
}
public
function
prepareData
()
{
}
public
function
testInvalidPastDates
()
{
$this
->
manager
->
setAttribute
(
Doctrine
::
ATTR_VALIDATE
,
Doctrine
::
VALIDATE_ALL
);
// one year ahead
$user1
=
new
ValidatorTest_DateModel
();
$user1
->
birthday
=
date
(
'Y-m-d'
,
time
()
+
365
*
24
*
60
*
60
);
$this
->
assertFalse
(
$user1
->
trySave
());
// one month ahead
$user1
=
new
ValidatorTest_DateModel
();
$user1
->
birthday
=
date
(
'Y-m-d'
,
time
()
+
30
*
24
*
60
*
60
);
$this
->
assertFalse
(
$user1
->
trySave
());
// one day ahead
$user1
->
birthday
=
date
(
'Y-m-d'
,
time
()
+
24
*
60
*
60
);
$this
->
assertFalse
(
$user1
->
trySave
());
$this
->
manager
->
setAttribute
(
Doctrine
::
ATTR_VALIDATE
,
Doctrine
::
VALIDATE_NONE
);
}
public
function
testValidPastDates
()
{
$this
->
manager
->
setAttribute
(
Doctrine
::
ATTR_VALIDATE
,
Doctrine
::
VALIDATE_ALL
);
$user1
=
new
ValidatorTest_DateModel
();
$user1
->
birthday
=
date
(
'Y-m-d'
,
42
);
$this
->
assertTrue
(
$user1
->
trySave
());
$user1
->
birthday
=
date
(
'Y-m-d'
,
mktime
(
0
,
0
,
0
,
6
,
3
,
1981
));
$this
->
assertTrue
(
$user1
->
trySave
());
$user1
->
birthday
=
date
(
'Y-m-d'
,
mktime
(
0
,
0
,
0
,
3
,
9
,
1983
));
$this
->
assertTrue
(
$user1
->
trySave
());
$this
->
manager
->
setAttribute
(
Doctrine
::
ATTR_VALIDATE
,
Doctrine
::
VALIDATE_NONE
);
}
}
tests/run.php
View file @
02dd8b1a
<?php
ini_set
(
'max_execution_time'
,
900
);
ini_set
(
"date.timezone"
,
"GMT+0"
);
function
parseOptions
(
$array
)
{
$currentName
=
''
;
...
...
@@ -223,6 +224,8 @@ $plugins = new GroupTest('Plugin tests: View, Validator, Hook');
//$utility->addTestCase(new Doctrine_PessimisticLocking_TestCase());
$plugins
->
addTestCase
(
new
Doctrine_View_TestCase
());
$plugins
->
addTestCase
(
new
Doctrine_Validator_TestCase
());
$plugins
->
addTestCase
(
new
Doctrine_Validator_Future_TestCase
());
$plugins
->
addTestCase
(
new
Doctrine_Validator_Past_TestCase
());
$plugins
->
addTestCase
(
new
Doctrine_Hook_TestCase
());
$plugins
->
addTestCase
(
new
Doctrine_I18n_TestCase
());
$test
->
addTestCase
(
$plugins
);
...
...
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