Permalink
Browse files

Plug date_with_time and date_with_time_and_timezone into Date object

Added helper class for salted hashed passwords
Update tests to work with Date class
Various fixes
  • Loading branch information...
1 parent bb45614 commit 99190e04d56c3d7a9580b3485c68b1a5b843db63 @pokeb committed Jan 18, 2009
View
@@ -41,10 +41,8 @@ h3. Coming soon
* Better documentation
* Write more tests
* Hashed password fields
-* File fields (for local file storage)
* Many to many relationships without a joining table model
* Full-text searching
-* Date handling - these are left up the the db at the moment
* Support for multiple find clauses with the same operator + field (eg: find_all_by_email_like_and_email_like())
* Postgres support, other databases (SQLite?)
@@ -3,10 +3,11 @@
define("SITE_PATH","/Users/ben/Sites/fuzzyrecord");
define("DB_SERVER","127.0.0.1");
-define("DB_USER","root");
+define("DB_USER","");
define("DB_PASSWORD","");
define("DB_DATABASE","fuzzy-record-test");
-define("DB_TYPE","pdo-mysql");
+define("DB_TYPE","pdo-mysql"); // valid options are 'pdo-mysql' and 'pdo-pgsql'
+
MemoryStore::$use_in_memory_store = true;
MemoryStore::$use_memcache_store = false;
View
@@ -87,36 +87,6 @@ static public function commit_implicit_transaction() {
DBBase::commit();
}
}
- /*
- static protected function db_field_type_for_field($object,$field) {
- $class = get_class($object);
- if (!key_exists($field,$class::$properties)) {
- throw new FuzzyRecordException("Property '$field' not found");
- }
- $options = $class::$properties[$field];
- $field_types = array("integer","boolean","date","time","varchar","text","enum");
- foreach ($field_types as $type) {
- if (in_array($type, $options) || key_exists($type,$options)) {
- return $type;
- }
- }
- if (in_array("bool",$options) || in_array("sorter",$options) || in_array("integer",$options)) {
- return "int";
- } elseif (in_array("file",$options) || in_array("email_address",$options)) {
- return "varchar";
- } elseif (in_array("date_with_time",$options)) {
- return "datetime";
- } elseif (in_array("time_with_timezone",$options) || in_array("date_with_time_and_timezone",$options)) {
- return "varchar";
- } elseif (in_array("auto_increment",$options)) {
- return "integer";
- } elseif (key_exists("max_length", $options) && $options['max_length'] < 256) {
- return "varchar";
- } else {
- return "text";
- }
- }
- */
static public function create_table_sql_for_class($class) {
$sql = "create table if not exists ".$class::table_name()." (\r\n";
View
@@ -26,7 +26,6 @@ public function execute($values=NULL) {
public function fetch_assoc() {
$result = $this->statement->fetch(PDO::FETCH_ASSOC);
-
return $result;
}
View
@@ -22,26 +22,6 @@ static public function last_insert_id($sequence="") {
return static::$db_connection->lastInsertId($sequence);
}
- /*
- static protected function db_field_type_for_field($object,$field) {
- $class = get_class($object);
- if (!key_exists($field,$class::$properties)) {
- throw new FuzzyRecordException("Property '$field' not found");
- }
- $options = $class::$properties[$field];
- if (in_array("bool",$options)) {
- return "boolean";
- } elseif (in_array("date_with_time",$options)) {
- return "timestamp without timezone";
- } elseif (in_array("time_with_timezone",$options)) {
- return "time with timezone";
- } elseif (in_array("date_with_time_and_timezone",$options)) {
- return "timestamp with timezone";
- }
- return DBBase::db_field_type_for_field($object,$field);
- }
- */
-
static public function create_table_sql_for_class($class) {
$index_sql = "";
$sql = "create table ".$class::table_name()." (\r\n";
@@ -62,9 +42,6 @@ static public function create_table_sql_for_class($class) {
case "integer":
$sql .= "integer ";
if (in_array("auto_increment",$options)) {
-
-
-
$index_sql .= "CREATE SEQUENCE ".$class::table_name()."_".$name."_seq INCREMENT BY 1 NO MAXVALUE NO MINVALUE CACHE 1;\r\n";
$index_sql .= "alter SEQUENCE ".$class::table_name()."_".$name."_seq owned by ".$class::table_name().".$name;\r\n";
$index_sql .= "alter table ".$class::table_name()." alter column $name set default nextval('".$class::table_name()."_".$name."_seq'::regclass);\r\n";
View
@@ -51,6 +51,14 @@ public function __construct($date="") {
}
}
+ public function db_date_with_time() {
+ return $this->year."-".$this->month."-".$this->day." ".$this->hour.":".$this->minute.":".$this->second;
+ }
+
+ public function db_date_with_time_and_timezone() {
+ return $this->db_date_with_time().$this->timezone_offset;
+ }
+
public static function now() {
return new Date("Y-m-d H:i:s");
}
View
@@ -1,13 +0,0 @@
-<?php
-
-class DateHelper {
-
- public static function now() {
- return date("Y-m-d H:i:s");
- }
-
- public static function db_date($date) {
- return date("Y-m-d H:i:s",$date);
- }
-
-}
View
@@ -200,11 +200,9 @@ public function write_new() {
}
-
$this->reset_sorters();
-
- //Cache this stuff somewhere!!
+ //Cache this stuff somewhere?
$properties_sql = "";
$values_sql = "";
foreach (static::$properties as $name => $info) {
@@ -488,7 +486,11 @@ static protected function value_from_database($field,$value) {
return NULL;
}
return new File($file_properties['save_path']."/".$value);
-
+
+ case "date_with_time":
+ case "date_with_time_and_timezone":
+ return new Date($value);
+
case "boolean":
if (empty($value) || $value === "0" || $value === "f") {
@@ -502,6 +504,18 @@ static protected function value_from_database($field,$value) {
static protected function value_for_database($field,$value) {
switch (static::field_type($field)) {
+ case "date_with_time":
+ if (!$value) {
+ return NULL;
+ } elseif (!is_object($value) || get_class($value) != "Date") {
+ throw new FuzzyRecordException("The value of '$field' must be a date object");
+ }
+ return $value->db_date_with_time();
+ case "date_with_time_and_timezone":
+ if (!$value) {
+ return NULL;
+ }
+ return $value->db_date_with_time_and_timezone();
case "boolean":
if ($value === false) {
if (DB_TYPE == "postgresql") {
@@ -542,7 +556,7 @@ static public function field_type($field) {
throw new FuzzyRecordException("Property '$field' not found");
}
$options = static::$properties[$field];
- $field_types = array("file","integer","sorter","boolean","date_with_time","date","time","email_address","password","varchar","text","enum");
+ $field_types = array("file","integer","sorter","boolean","date_with_time","date_with_time_and_timezone","date","time","email_address","password","varchar","text","enum");
foreach ($field_types as $type) {
if (in_array($type, $options) || key_exists($type,$options)) {
return $type;
View
@@ -0,0 +1,13 @@
+<?php
+
+class Password {
+
+ static function generate_salt() {
+ return substr(md5(uniqid(rand(),true)),0,32);
+ }
+
+ static function hash($password,$salt) {
+ return sha1($password.$salt);
+ }
+
+}
@@ -13,7 +13,7 @@ class CustomDocument extends FuzzyRecord {
'id' => array('primary_key','auto_increment'),
'author_id' => array('integer'),
'file' => array('file','save_path' => DOCUMENT_SAVE_PATH),
- 'last_modified' => array('date_with_time','order_by_default')
+ 'last_modified' => array('date_with_time_and_timezone','order_by_default')
);
static protected $relationships = array(
View
@@ -19,7 +19,7 @@ public function __construct() {
$user->email = "ben@allseeing-i.com";
$user->password = "secret";
$user->accepted_terms_and_conditions = true;
- $user->registration_date = "2008-12-12";
+ $user->registration_date = new Date();
$user->first_name = "Ben";
$user->last_name = "Copsey";
$user->save();
@@ -62,7 +62,7 @@ public function test_file_field_read_and_write() {
$user = User::find_by_email("ben@allseeing-i.com");
$document = new Document();
$document->user = $user;
- $document->last_modified = DateHelper::now();
+ $document->last_modified = new Date();
$document->file = $file;
$document->save();
FuzzyTest::assert_true($file->exists(),"File not written");
@@ -85,7 +85,7 @@ public function test_custom_file_name() {
$user = User::find_by_email("ben@allseeing-i.com");
$document = new CustomDocument();
$document->user = $user;
- $document->last_modified = DateHelper::now();
+ $document->last_modified = new Date();
$document->file = $file;
$document->save();
$result = file_exists(DOCUMENT_SAVE_PATH."/".$document->id.".info");
View
@@ -10,22 +10,22 @@ public function __construct() {
$page1 = new Page();
$page1->title = "This is page 1";
- $page1->last_modified = DateHelper::now();
+ $page1->last_modified = new Date();
$page1->body = "This is the content";
$page1->url = "page-1";
$page1->save();
$page2 = new Page();
$page2->title = "This is page 2";
- $page2->last_modified = DateHelper::now();
+ $page2->last_modified = new Date();
$page2->body = "This is the content for page 2";
$page2->url = "page-2";
$page2->save();
$page3 = new Page();
$page3->title = "This is page 3";
- $page3->last_modified = DateHelper::now();
+ $page3->last_modified = new Date();
$page3->body = "This is the content for page 3";
$page3->url = "page-3";
$page3->save();
View
@@ -14,14 +14,14 @@ public function test_belongs_to() {
$user->last_name = "Copsey";
$user->password = "secret";
$user->accepted_terms_and_conditions = true;
- $user->registration_date = "2008-12-12";
+ $user->registration_date = new Date();
$user->first_name = "Ben";
$user->save();
$page1 = new Page();
$page1->title = "This is page 1";
- $page1->last_modified = DateHelper::now();
+ $page1->last_modified = new Date();
$page1->body = "This is the content";
$page1->url = "page-1";
$page1->author = $user;
View
@@ -12,7 +12,7 @@ public function __construct() {
$user->email = "ben@allseeing-i.com";
$user->password = "secret";
$user->accepted_terms_and_conditions = true;
- $user->registration_date = "2008-12-12";
+ $user->registration_date = new Date();
$user->first_name = "Ben";
$user->last_name = "Copsey";
$user->save();
@@ -21,7 +21,7 @@ public function __construct() {
$user->email = "ben@acknet.co.uk";
$user->password = "secret";
$user->accepted_terms_and_conditions = true;
- $user->registration_date = DateHelper::now();
+ $user->registration_date = new Date();
$user->first_name = "Ben";
$user->last_name = "Copsey";
$user->save();
@@ -30,7 +30,7 @@ public function __construct() {
$user->email = "ben@shared-space.net";
$user->password = "secret";
$user->accepted_terms_and_conditions = true;
- $user->registration_date = DateHelper::now();
+ $user->registration_date = new Date();
$user->first_name = "Benjamus";
$user->last_name = "Copso";
$user->save();
View
@@ -11,7 +11,7 @@ public function test_has_many() {
$user->email = "ben@allseeing-i.com";
$user->password = "secret";
$user->accepted_terms_and_conditions = true;
- $user->registration_date = "2008-12-12";
+ $user->registration_date = new Date();
$user->first_name = "Ben";
$user->last_name = "Copsey";
$user->save();
@@ -20,7 +20,7 @@ public function test_has_many() {
$user->email = "ben@acknet.co.uk";
$user->password = "secret";
$user->accepted_terms_and_conditions = true;
- $user->registration_date = DateHelper::now();
+ $user->registration_date = new Date();
$user->first_name = "Ben";
$user->last_name = "Copsey";
$user->save();
@@ -29,7 +29,7 @@ public function test_has_many() {
$user_login = new UserLogin();
$user_login->user_id = $user->id;
- $user_login->date = DateHelper::now();
+ $user_login->date = new Date();
$user_login->ip_address = "127.0.0.1";
$user_login->save();
@@ -40,7 +40,7 @@ public function test_has_many() {
$user_login = new UserLogin();
$user_login->user_id = $user->id;
- $user_login->date = DateHelper::now();
+ $user_login->date = new Date();
$user_login->ip_address = "127.0.0.1";
$user_login->save();
@@ -81,7 +81,7 @@ public function test_has_many() {
$user_login = new UserLogin();
$user_login->user = $user;
- $user_login->date = DateHelper::now();
+ $user_login->date = new Date();
$user_login->ip_address = "127.0.0.1";
$user_login->save();
@@ -99,11 +99,11 @@ public function test_has_many() {
UserLogin::delete_all();
$user_login1 = new UserLogin();
- $user_login1->date = DateHelper::now();
+ $user_login1->date = new Date();
$user_login1->ip_address = "127.0.0.1";
$user_login2 = new UserLogin();
- $user_login2->date = DateHelper::now();
+ $user_login2->date = new Date();
$user_login2->ip_address = "127.0.0.1";
View
@@ -28,7 +28,7 @@ public function test_validation() {
$user->validate();
FuzzyTest::assert_false(key_exists("password",$user->validation_errors),"Password validation error erroneously set");
- $user->registration_date = DateHelper::now();
+ $user->registration_date =new Date();
$user->validate();
FuzzyTest::assert_false(key_exists("registration_date",$user->validation_errors),"Required validation error erroneously set");
Oops, something went wrong.

0 comments on commit 99190e0

Please sign in to comment.