Author: ts Date: Thu Dec 20 12:26:54 2007 New Revision: 7033 Log: - Added ezcPersistentObjectPropertyDateTimeConversion class.
Added: trunk/PersistentObject/src/object/property_conversions/ trunk/PersistentObject/src/object/property_conversions/date.php (with props) trunk/PersistentObject/tests/date_time_conversion_test.php (with props) Modified: trunk/PersistentObject/design/class_diagram.png trunk/PersistentObject/src/persistent_autoload.php trunk/PersistentObject/tests/suite.php Modified: trunk/PersistentObject/design/class_diagram.png ============================================================================== Binary files - no diff available. Added: trunk/PersistentObject/src/object/property_conversions/date.php ============================================================================== --- trunk/PersistentObject/src/object/property_conversions/date.php (added) +++ trunk/PersistentObject/src/object/property_conversions/date.php [iso-8859-1] Thu Dec 20 12:26:54 2007 @@ -1,0 +1,77 @@ +<?php +/** + * File containing the ezcPersistentObjectPropertyDateTimeConversion class. + * + * @version //autogen// + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + * @package PersistentObject + */ + +/** + * Property conversion class for date/time values. + * + * An instance of this class can be used with [EMAIL PROTECTED] + * ezcPersistentObjectProperty} in a [EMAIL PROTECTED] ezcPersistentObjectDefinition} to + * indicate, that a database date/time value (represented by a unix timestamp + * integer value) should be converted to a PHP DateTime object. + * + * @package PersistentObject + * @version //autogen// + */ +class ezcPersistentObjectPropertyDateTimeConversion +{ + /** + * Converts unix timestamp to DateTime instance. + * + * This method is called right after a column value has been read from the + * database, given the $databaseValue. The value returned by this method is + * then assigned to the persistent objects property. + * + * The given integer value $databaseValue is handled as a date/time value + * in unix timestamp representation. A corresponding DateTime object is + * returned, representing the same date/time value. + * + * @param int $databaseValue + * @return DateTime + * + * @throws ezcBaseValueException + * if the given $databaseValue is not an integer. + */ + public function fromDatabase( $databaseValue ) + { + if ( !is_int( $databaseValue ) && !is_numeric( $databaseValue ) ) + { + throw new ezcBaseValueException( 'databaseValue', $databaseValue, 'int' ); + } + return new DateTime( '@' . (int) $databaseValue ); + } + + /** + * Converts a DateTime object into a unix timestamp. + * + * This method is called right before a property value is written to the + * database, given the $propertyValue. The value returned by this method is + * then written back to the database. + * + * The method expects a DateTime object in $propertyValue and returns the + * date/time value represented by it as an integer value in unix timestamp + * format. + * + * @param DateTime $propertyValue + * @return int + * + * @throws ezcBaseValueException + * if the given $propertyValue is not an instance of DateTime. + */ + public function toDatabase( $propertyValue ) + { + if ( !( $propertyValue instanceof DateTime ) ) + { + throw new ezcBaseValueException( 'propertyValue', $propertyValue, 'DateTime' ); + } + return $propertyValue->format( 'U' ); + } +} + +?> Propchange: trunk/PersistentObject/src/object/property_conversions/date.php ------------------------------------------------------------------------------ svn:eol-style = native Modified: trunk/PersistentObject/src/persistent_autoload.php ============================================================================== --- trunk/PersistentObject/src/persistent_autoload.php [iso-8859-1] (original) +++ trunk/PersistentObject/src/persistent_autoload.php [iso-8859-1] Thu Dec 20 12:26:54 2007 @@ -41,6 +41,8 @@ 'ezcPersistentObjectIdProperty' => 'PersistentObject/object/persistent_object_id_property.php', 'ezcPersistentObjectProperties' => 'PersistentObject/object/persistent_object_properties.php', 'ezcPersistentObjectProperty' => 'PersistentObject/object/persistent_object_property.php', + 'ezcPersistentObjectPropertyConversion' => 'PersistentObject/interfaces/property_conversion.php', + 'ezcPersistentObjectPropertyDateTimeConversion' => 'PersistentObject/object/property_conversions/date.php', 'ezcPersistentObjectRelations' => 'PersistentObject/object/persistent_object_relations.php', 'ezcPersistentOneToManyRelation' => 'PersistentObject/relations/one_to_many.php', 'ezcPersistentOneToOneRelation' => 'PersistentObject/relations/one_to_one.php', Added: trunk/PersistentObject/tests/date_time_conversion_test.php ============================================================================== --- trunk/PersistentObject/tests/date_time_conversion_test.php (added) +++ trunk/PersistentObject/tests/date_time_conversion_test.php [iso-8859-1] Thu Dec 20 12:26:54 2007 @@ -1,0 +1,115 @@ +<?php +/** + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + * @version //autogentag// + * @filesource + * @package PersistentObject + * @subpackage Tests + */ + +/** + * Tests the ezcPersistentObjectPropertyDateTimeConversion class. + * + * @package PersistentObject + * @subpackage Tests + */ +class ezcPersistentObjectPropertyDateTimeConversionTest extends ezcTestCase +{ + public static function suite() + { + return new PHPUnit_Framework_TestSuite( 'ezcPersistentObjectPropertyDateTimeConversionTest' ); + } + + public function testFromDatabaseSuccess() + { + $conv = new ezcPersistentObjectPropertyDateTimeConversion(); + + $this->assertEquals( + new DateTime( '@327535200' ), + $conv->fromDatabase( 327535200 ), + 'Conversion of positive time stamp from database failed.' + ); + + $this->assertEquals( + new DateTime( '@-1000' ), + $conv->fromDatabase( -1000 ), + 'Conversion of positive time stamp from database failed.' + ); + + $this->assertEquals( + new DateTime( '@327535200' ), + $conv->fromDatabase( '327535200' ), + 'Conversion of positive time stamp as string from database failed.' + ); + } + + public function testFromDatabaseFailure() + { + $conv = new ezcPersistentObjectPropertyDateTimeConversion(); + + $values = array( + 'Test string', + true, + false, + new stdClass(), + array( 23, 42 ) + ); + + foreach ( $values as $value ) + { + try + { + $res = $conv->fromDatabase( $value ); + $this->fail( 'Exception not thrown on illegal conversion of type ' . gettype( $value ) ); + } + catch ( ezcBaseValueException $e ) {} + } + } + + public function testToDatabaseSuccess() + { + $conv = new ezcPersistentObjectPropertyDateTimeConversion(); + + $this->assertEquals( + 327535200, + $conv->toDatabase( new DateTime( '@327535200' ) ), + 'Conversion of positive time stamp to database failed.' + ); + + $this->assertEquals( + -1000, + $conv->toDatabase( new DateTime( '@-1000' ) ), + 'Conversion of positive time stamp to database failed.' + ); + } + + public function testToDatabaseFailure() + { + $conv = new ezcPersistentObjectPropertyDateTimeConversion(); + + $values = array( + 23, + 23.42, + 'Test string', + true, + false, + new stdClass(), + array( 23, 42 ) + ); + + foreach ( $values as $value ) + { + try + { + $res = $conv->toDatabase( $value ); + $this->fail( 'Exception not thrown on illegal conversion of type ' . gettype( $value ) ); + } + catch ( ezcBaseValueException $e ) {} + } + } + +} + + +?> Propchange: trunk/PersistentObject/tests/date_time_conversion_test.php ------------------------------------------------------------------------------ svn:eol-style = native Modified: trunk/PersistentObject/tests/suite.php ============================================================================== --- trunk/PersistentObject/tests/suite.php [iso-8859-1] (original) +++ trunk/PersistentObject/tests/suite.php [iso-8859-1] Thu Dec 20 12:26:54 2007 @@ -31,6 +31,7 @@ require_once( 'object_relations_test.php' ); require_once( 'object_properties_test.php' ); require_once( 'object_columns_test.php' ); +require_once 'date_time_conversion_test.php'; /** * @package PersistentObject @@ -63,6 +64,7 @@ $this->addTest( ezcPersistentObjectRelationsTest::suite() ); $this->addTest( ezcPersistentObjectPropertiesTest::suite() ); $this->addTest( ezcPersistentObjectColumnsTest::suite() ); + $this->addTest( ezcPersistentObjectPropertyDateTimeConversionTest::suite() ); } public static function suite() -- svn-components mailing list svn-components@lists.ez.no http://lists.ez.no/mailman/listinfo/svn-components