dblayeroo : Add normalize method by default

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4631 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2018-10-25 08:32:58 +00:00
parent 681863f187
commit 95f7b55681
2 changed files with 31 additions and 10 deletions

View File

@@ -177,7 +177,8 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase
{ {
// Insert : first row inserted // Insert : first row inserted
$tbl1 = $this->tbl1 (); $tbl1 = $this->tbl1 ();
$res = $tbl1->insert()->setValues(array ("group"=>"group1", // Normalize the value to be inserted too
$res = $tbl1->insert()->setValues(array ("group"=>" group1 ",
"where"=>"where", "where"=>"where",
"object"=>"object"))->execute (); "object"=>"object"))->execute ();
$tbl1->disconnect (); $tbl1->disconnect ();
@@ -1295,35 +1296,35 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase
} }
//// TEST REAL TYPES //// //// TEST REAL TYPES ////
public function test_checkRealType_1 () public function test_checkRealTypes_1 ()
{ {
$tbl1 = $this->tbl1 (); $tbl1 = $this->tbl1 ();
$res = $tbl1->checkRealType (array ("group" => "notempty")); $res = $tbl1->checkRealTypes (array ("group" => "notempty"));
$this->assertSame (array ( $this->assertSame (array (
"object" => "The field can not be empty", "object" => "The field can not be empty",
"where" => "The field can not be empty", "where" => "The field can not be empty",
), $res); ), $res);
} }
public function test_checkRealType_2 () public function test_checkRealTypes_2 ()
{ {
$tbl1 = $this->tbl1 (); $tbl1 = $this->tbl1 ();
$res = $tbl1->checkRealType (array ("group" => "not valid")); $res = $tbl1->checkRealTypes (array ("group" => "not valid"));
$this->assertSame (array ( $this->assertSame (array (
"group" => "Invalid value provided : do not match the regex", "group" => "Invalid value provided : do not match the regex",
"object" => "The field can not be empty", "object" => "The field can not be empty",
"where" => "The field can not be empty", "where" => "The field can not be empty",
), $res); ), $res);
} }
public function test_checkRealType_3 () public function test_checkRealTypes_3 ()
{ {
$tbl1 = $this->tbl1 (); $tbl1 = $this->tbl1 ();
$res = $tbl1->checkRealType (array ("group" => "notempty"), true); $res = $tbl1->checkRealTypes (array ("group" => "notempty"), true);
$this->assertSame (array (), $res); $this->assertSame (array (), $res);
} }
public function test_checkRealType_4 () public function test_checkRealTypes_4 ()
{ {
$tbl1 = $this->tbl1 (); $tbl1 = $this->tbl1 ();
$res = $tbl1->checkRealType (array ("group" => "not valid"), true); $res = $tbl1->checkRealTypes (array ("group" => "not valid"), true);
$this->assertSame (array ( $this->assertSame (array (
"group" => "Invalid value provided : do not match the regex", "group" => "Invalid value provided : do not match the regex",
), $res); ), $res);

View File

@@ -1434,7 +1434,7 @@ class dblayeroo
*/ */
private static $sortOrder = 0; private static $sortOrder = 0;
/** The method to get a new sorti order acrossed the differents objects /** The method to get a new sort order acrossed the differents objects
*/ */
public function getSortOrder () public function getSortOrder ()
/* {{{ */ /* {{{ */
@@ -2343,6 +2343,7 @@ class dblayeroo
$this->debugLog ("Entering setValues (",$values,")"); $this->debugLog ("Entering setValues (",$values,")");
if (! is_array ($values)) if (! is_array ($values))
$this->DBException ("Invalid values to setValues : not an array"); $this->DBException ("Invalid values to setValues : not an array");
$values = $this->normalize ($values);
$associative = null; $associative = null;
$tmpValues = array (); $tmpValues = array ();
$tmpType = array (); $tmpType = array ();
@@ -2597,6 +2598,23 @@ class dblayeroo
} }
/* }}} */ /* }}} */
/** Normalize the values before using them.
* The normalize is called by methods : verify, checkRealTypes and setValues
* By default, remove the spaces (trim) at begin and end.
* This method can be overloaded by extending the class
* @param array $values The values to test or INSERT or UPDATE
* @return array the updated values
*/
public function normalize ($values)
// {{{
{
if (! is_array ($values))
$this->DBException ("Invalid values to normalize : not an array");
$values = array_map ("trim", $values);
return $values;
}
// }}}
/** Check the provided values which will be inserted or updated against the /** Check the provided values which will be inserted or updated against the
* database structure. * database structure.
* In update, do not forget to define the whereAdd parameters ! * In update, do not forget to define the whereAdd parameters !
@@ -2614,6 +2632,7 @@ class dblayeroo
if ($this->primary === null) if ($this->primary === null)
throw new \Exception ("No primary key defined for table '$this->table'", throw new \Exception ("No primary key defined for table '$this->table'",
500); 500);
$values = $this->normalize ($values);
if ($update === false) if ($update === false)
{ {
// INSERT mode // INSERT mode
@@ -2915,6 +2934,7 @@ class dblayeroo
if (! is_array ($values)) if (! is_array ($values))
$this->DBException ("Invalid checkRealTypes provided : not an array", $this->DBException ("Invalid checkRealTypes provided : not an array",
500); 500);
$values = $this->normalize ($values);
$errors = array (); $errors = array ();
foreach ($this->fields as $field => $params) foreach ($this->fields as $field => $params)
{ {