From 95f7b556816f3dc3c6f24e952f2138d89561ec56 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Thu, 25 Oct 2018 08:32:58 +0000 Subject: [PATCH] dblayeroo : Add normalize method by default git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4631 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- Tests/dblayerooComplet.php | 19 ++++++++++--------- dblayeroo.php | 22 +++++++++++++++++++++- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/Tests/dblayerooComplet.php b/Tests/dblayerooComplet.php index f967112..36213e6 100644 --- a/Tests/dblayerooComplet.php +++ b/Tests/dblayerooComplet.php @@ -177,7 +177,8 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase { // Insert : first row inserted $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", "object"=>"object"))->execute (); $tbl1->disconnect (); @@ -1295,35 +1296,35 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase } //// TEST REAL TYPES //// - public function test_checkRealType_1 () + public function test_checkRealTypes_1 () { $tbl1 = $this->tbl1 (); - $res = $tbl1->checkRealType (array ("group" => "notempty")); + $res = $tbl1->checkRealTypes (array ("group" => "notempty")); $this->assertSame (array ( "object" => "The field can not be empty", "where" => "The field can not be empty", ), $res); } - public function test_checkRealType_2 () + public function test_checkRealTypes_2 () { $tbl1 = $this->tbl1 (); - $res = $tbl1->checkRealType (array ("group" => "not valid")); + $res = $tbl1->checkRealTypes (array ("group" => "not valid")); $this->assertSame (array ( "group" => "Invalid value provided : do not match the regex", "object" => "The field can not be empty", "where" => "The field can not be empty", ), $res); } - public function test_checkRealType_3 () + public function test_checkRealTypes_3 () { $tbl1 = $this->tbl1 (); - $res = $tbl1->checkRealType (array ("group" => "notempty"), true); + $res = $tbl1->checkRealTypes (array ("group" => "notempty"), true); $this->assertSame (array (), $res); } - public function test_checkRealType_4 () + public function test_checkRealTypes_4 () { $tbl1 = $this->tbl1 (); - $res = $tbl1->checkRealType (array ("group" => "not valid"), true); + $res = $tbl1->checkRealTypes (array ("group" => "not valid"), true); $this->assertSame (array ( "group" => "Invalid value provided : do not match the regex", ), $res); diff --git a/dblayeroo.php b/dblayeroo.php index 6def20d..a3fc6a4 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -1434,7 +1434,7 @@ class dblayeroo */ 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 () /* {{{ */ @@ -2343,6 +2343,7 @@ class dblayeroo $this->debugLog ("Entering setValues (",$values,")"); if (! is_array ($values)) $this->DBException ("Invalid values to setValues : not an array"); + $values = $this->normalize ($values); $associative = null; $tmpValues = 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 * database structure. * In update, do not forget to define the whereAdd parameters ! @@ -2614,6 +2632,7 @@ class dblayeroo if ($this->primary === null) throw new \Exception ("No primary key defined for table '$this->table'", 500); + $values = $this->normalize ($values); if ($update === false) { // INSERT mode @@ -2915,6 +2934,7 @@ class dblayeroo if (! is_array ($values)) $this->DBException ("Invalid checkRealTypes provided : not an array", 500); + $values = $this->normalize ($values); $errors = array (); foreach ($this->fields as $field => $params) {