-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for using a custom Date class (#532)
If your project uses a date library that doesn't extend \DateTime (e.g. Zend_Date), then you currently have to convert to and from ActiveRecord\DateTime when interacting with models, which is a pain and error prone. We've had to fix quite a number of bugs due to missing conversions. This commit removes the need for that by adding support for using a custom date class. The custom class is set in ActiveRecord\Config, which uses similar logic to the logger methods for the getter and setter. The ActiveRecord\Config->set_date_class() method checks if the provided class has "format" and "createFromFormat" methods, which are all that are required. I changed everything that created new date objects to use "createFromFormat()" instead of the constructor, because many date libraries don't accept a date string in the constructor. Besides, I think createFromFormat() is better in general because it's more explicit. For the "attribute_of" method, I made that an optional feature by introducing an interface that defines it, which ActiveRecord\Model->assign_attribute() checks for. The reason I made this optional is because if you use an immutable date class (e.g. PHP's DateTimeImmutable), you don't need to worry about the dirty flagging, so there's no need to have that method.
- Loading branch information
Showing
13 changed files
with
180 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* @package ActiveRecord | ||
*/ | ||
namespace ActiveRecord; | ||
|
||
/** | ||
* Interface for the ActiveRecord\DateTime class so that ActiveRecord\Model->assign_attribute() will | ||
* know to call attribute_of() on passed values. This is so the DateTime object can flag the model | ||
* as dirty via $model->flag_dirty() when one of its setters is called. | ||
* | ||
* @package ActiveRecord | ||
* @see http://php.net/manual/en/class.datetime.php | ||
*/ | ||
interface DateTimeInterface | ||
{ | ||
/** | ||
* Indicates this object is an attribute of the specified model, with the given attribute name. | ||
* | ||
* @param Model $model The model this object is an attribute of | ||
* @param string $attribute_name The attribute name | ||
* @return void | ||
*/ | ||
public function attribute_of($model, $attribute_name); | ||
|
||
/** | ||
* Formats the DateTime to the specified format. | ||
*/ | ||
public function format($format=null); | ||
|
||
/** | ||
* See http://php.net/manual/en/datetime.createfromformat.php | ||
*/ | ||
public static function createFromFormat($format, $time, $tz = null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters