Date de publication: le lundi 26 mai 2008 à 02h35
Dernière modification: par Pascal BOYER le mercredi 6 octobre 2010 à 10h39
« Article précédent: Setting up files and configuration / Paramétrer les fichiers et configuration
» Article suivant: Extending eZDataType -- Etendre la classe eZDataType
We start our journey through the PHP code with the ymcDatatypeDatetime class, which holds a DateTime object.
Nous commençons notre travail par le code PHP de la classe ymcDatatypeDatetime contenant l'objet DateTime.
The first method is the constructor. It needs a string representing the date as input. The format of the string is the one accepted by the DateTime class respective to the GNU Date command. The second parameter represents the timezone and can be given either as a string or as a DateTimeZone object; it defaults to the PHP data.timezone INI setting.
La première méthode est le
constructeur
qui a besoin d'une chaîne de caractères représentant la date d'entrée (???). Le format de cette chaîne de caractères est celui accepté par la classe DateTime respectant la commande GNU
Date
. Le second paramètre représente le fuseau horaire pouvant être passé soit sous forme de chaîne de caractères soit en tant qu'objet DateTimeZone: la valeur par défaut correspond au paramètre data.timezone du fichier de configuration de PHP.
public function __construct( $dtString = NULL, $tz = NULL ) { if( is_string( $dtString ) ) { if( is_string( $tz ) ) { try{ $tz = new DateTimeZone( $tz ); } catch( Exception $e ) { throw new ymcDatatypeInvalidParams( $tz.' is no valid timezone identifier.' ); } } elseif( NULL === $tz ) { $tz = new DateTimeZone( date_default_timezone_get() ); } if( !$tz instanceOf DateTimeZone ) { throw new ymcDatatypeInvalidParams( 'The second parameter of the constructor needs to be either a ' .'valid Timezone string or an instance of DateTimeZone.' ); } try { $this->dateTime = new DateTime( $dtString, $tz ); } catch( Exception $e ) { throw new ymcDatatypeInvalidParams( $dtString.' is no valid DateTime string.'); } } }
What I want to point out here are the exceptions used in the constructor, as shown below:
Ici, je souhaite mettre l'accent sur les exceptions utilisées dans le constructeur:
try { $this->dateTime = new DateTime( $dtString, $tz ); } catch( Exception $e ) { throw new ymcDatatypeInvalidParams( $dtString.' is no valid DateTime string.'); }
If you try to instantiate a ymcDatatypeDateTime object with invalid data, an exception is thrown. This is no problem, since you can catch the exception and produce the appropriate feedback to the user. The advantage in the code is that the input validation can be reduced to just a few lines in the ymcDatatypeDateTimeType class, similar to the following simplified example:
Si vous tentez d'instancier un objet ymcDatatypeDateTime en saisissant des valeurs invalides alors une exception est lancée. Ceci n'est pas un problème puisque vous pouvez récupérer l'exception pour envoyer un message approprié à l'utilisateur. Au niveau du code de la classe ymcDatatypeDateTimeType, l'avantage est que la validation des entrées se résume à seulement quelques lignes similaire à celles de l'exemple simplifié ci-dessous:
try{ $this->content = new ymcDatatypeDateTime( $datestr, $tz ); } catch( ymcDatatypeInvalidParams $e ) { $this->content = NULL; return eZInputValidator::STATE_INVALID; } return eZInputValidator::STATE_ACCEPTED;
Inside the templates, we work with an instance of the ymcDatatypeDateTime class to show the content of the datatype. To get the data out of the object, eZ Publish needs two methods to access the data: attribute and hasAttribute. These two methods do essentially the same thing as the new PHP 5 magic methods __get and __isset. Therefore our methods simply forward the request to those magic methods.
Dans les templates, nous travaillons avec une instance de la classe ymcDatatypeDateTime pour afficher le contenu du datatype. Pour extraire les données de l'objet, eZ Publish utilise deux méthodes d'accès aux données: attribute et hasAttribute. Ces deux méthodes réalisent essentiellement la même chose que les nouvelles méthodes magiques
__get
et
__isset
de PHP5. Par conséquent, nos méthodes transmettent simplement la requête à ces méthodes magiques.
We will discuss the templates and how to retrieve the content later.
Nous reparlerons plus tard des templates et de la manière de retrouver le contenu.
The remaining __toString and createFromString methods are needed to serialize and unserialize the object to and from a string that can be saved in the database:
Les méthodes restantes __toString et createFromString sont nécessaires pour sérialiser et désérialiser l'objet vers et depuis une chaîne de caractères pouvant être sauvegardée dans la base de données:
public function __toString() { if( NULL === $this->dateTime ) { return ''; } return $this->dateTime->format( self::FORMAT_MYSQL_FULL ) .$this->dateTime->format( self::FORMAT_TIMEZONE_IDENTIFIER ); } public static function createFromString( $string ) { if( '' === $string ) { return new self; } return new self( substr( $string, 0, 19 ), substr( $string, 19 ) ); }
Commentaires














