[Locale] added missing docblocks

This commit is contained in:
Eriksen Costa 2011-03-01 01:59:14 -03:00
parent 51e9d8946b
commit 7ba71c0be1
18 changed files with 329 additions and 25 deletions

View File

@ -12,22 +12,31 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Parser and formatter for date formats
* Parser and formatter for AM/PM markers format
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class AmPmTransformer extends Transformer
{
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
return $dateTime->format('A');
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
return 'AM|PM';
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
return array(

View File

@ -12,12 +12,15 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Parser and formatter for date formats
* Parser and formatter for day of week format
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class DayOfWeekTransformer extends Transformer
{
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
$dayOfWeek = $dateTime->format('l');
@ -31,6 +34,9 @@ class DayOfWeekTransformer extends Transformer
}
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
$dayOfWeek = $dateTime->format('l');
@ -44,6 +50,9 @@ class DayOfWeekTransformer extends Transformer
}
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
return array(

View File

@ -12,23 +12,32 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Parser and formatter for date formats
* Parser and formatter for day of year format
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class DayOfYearTransformer extends Transformer
{
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
$dayOfYear = $dateTime->format('z') + 1;
return $this->padLeft($dayOfYear, $length);
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
return "\d{$length}";
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
return array(

View File

@ -12,17 +12,23 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Parser and formatter for date formats
* Parser and formatter for day format
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class DayTransformer extends Transformer
{
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
return $this->padLeft($dateTime->format('j'), $length);
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
if (1 == $length) {
@ -34,6 +40,9 @@ class DayTransformer extends Transformer
return $regExp;
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
return array(

View File

@ -30,6 +30,12 @@ class FullTransformer
private $pattern;
private $timezone;
/**
* Constructor
*
* @param string $pattern The pattern to be used to format and/or parse values
* @param string $timezone The timezone to perform the date/time calculations
*/
public function __construct($pattern, $timezone)
{
$this->pattern = $pattern;
@ -59,11 +65,22 @@ class FullTransformer
);
}
/**
* Return the array of Transformer objects
*
* @return array Associative array of Transformer objects (format char => Transformer)
*/
public function getTransformers()
{
return $this->transformers;
}
/**
* Format a DateTime using ICU dateformat pattern
*
* @param DateTime $dateTime A DateTime object to be used to generate the formatted value
* @return string The formatted value
*/
public function format(\DateTime $dateTime)
{
$that = $this;
@ -75,6 +92,14 @@ class FullTransformer
return $formatted;
}
/**
* Return the formatted ICU value for the matched date characters
*
* @param string $dateChars The date characters to be replaced with a formatted ICU value
* @param DateTime $dateTime A DateTime object to be used to generate the formatted value
* @return string The formatted value
* @throws NotImplementedException When it encounters a not implemented date character
*/
public function formatReplace($dateChars, $dateTime)
{
$length = strlen($dateChars);
@ -94,6 +119,13 @@ class FullTransformer
}
}
/**
* Retrieve a regular expression to match with a formatted value.
*
* @param string $pattern The pattern to create the reverse matching regular expression
* @return string The reverse matching regular expression with named captures being formed by the
* transformer index in the $transformer array
*/
public function getReverseMatchingRegExp($pattern)
{
$that = $this;
@ -121,6 +153,14 @@ class FullTransformer
return $reverseMatchingRegExp;
}
/**
* Parse a pattern based string to a timestamp value
*
* @param DateTime $dateTime A configured DateTime object to use to perform the date calculation
* @param string $value String to convert to a time value
* @return int The corresponding Unix timestamp
* @throws InvalidArgumentException When the value can not be matched with pattern
*/
public function parse(\DateTime $dateTime, $value)
{
$reverseMatchingRegExp = $this->getReverseMatchingRegExp($this->pattern);
@ -144,6 +184,12 @@ class FullTransformer
throw new \InvalidArgumentException(sprintf("Failed to match value '%s' with pattern '%s'", $value, $this->pattern));
}
/**
* Builds a chars match regular expression
*
* @param string $specialChars A string of chars to build the regular expression
* @return string The chars match regular expression
*/
protected function buildCharsMatch($specialChars)
{
$specialCharsArray = str_split($specialChars);
@ -155,6 +201,13 @@ class FullTransformer
return $specialCharsMatch;
}
/**
* Normalize a preg_replace match array, removing the numeric keys and returning an associative array
* with the value and pattern values for the matched Transformer
*
* @param array $data
* @return array
*/
protected function normalizeArray(array $data)
{
$ret = array();
@ -173,11 +226,23 @@ class FullTransformer
return $ret;
}
/**
* Check if the first char of a string is a single quote
*
* @param string $quoteMatch The string to check
* @return bool true if matches, false otherwise
*/
public function isQuoteMatch($quoteMatch)
{
return ("'" === $quoteMatch[0]);
}
/**
* Replaces single quotes at the start or end of a string with two single quotes
*
* @param string $quoteMatch The string to replace the quotes
* @return string A string with the single quotes replaced
*/
public function replaceQuoteMatch($quoteMatch)
{
if (preg_match("/^'+$/", $quoteMatch)) {
@ -186,7 +251,15 @@ class FullTransformer
return str_replace("''", "'", substr($quoteMatch, 1, -1));
}
private function calculateUnixTimestamp($dateTime, array $options)
/**
* Calculates the Unix timestamp based on the matched values by the reverse matching regular
* expression of parse()
*
* @param DateTime $dateTime The DateTime object to be used to calculate the timestamp
* @param array $options An array with the matched values to be used to calculate the timestamp
* @return bool|int The calculated timestamp or false if matched date is invalid
*/
private function calculateUnixTimestamp(\DateTime $dateTime, array $options)
{
$datetime = $this->extractDateTime($options);
@ -200,7 +273,7 @@ class FullTransformer
$hourInstance = $datetime['hourInstance'];
$timezone = $datetime['timezone'];
// If month is false, return immediately
// If month is false, return immediately (intl behavior)
if (false === $month) {
return false;
}
@ -221,6 +294,13 @@ class FullTransformer
return $dateTime->getTimestamp();
}
/**
* Extract date time data from an array and returns an associative array with sensible
* default values for the timestamp calculation of calculateUnixTimestamp()
*
* @param array $datetime
* @return array
*/
private function extractDateTime(array $datetime)
{
return array(

View File

@ -12,12 +12,15 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Parser and formatter for date formats
* Parser and formatter for 12 hour format (0-11)
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class Hour1200Transformer extends HourTransformer
{
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
$hourOfDay = $dateTime->format('g');
@ -25,6 +28,9 @@ class Hour1200Transformer extends HourTransformer
return $this->padLeft($hourOfDay, $length);
}
/**
* {@inheritDoc}
*/
public function getMktimeHour($hour, $marker = null)
{
if ('PM' === $marker) {
@ -34,11 +40,17 @@ class Hour1200Transformer extends HourTransformer
return $hour;
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
return '\d{1,2}';
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
return array(

View File

@ -12,17 +12,23 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Parser and formatter for date formats
* Parser and formatter for 12 hour format (1-12)
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class Hour1201Transformer extends HourTransformer
{
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
return $this->padLeft($dateTime->format('g'), $length);
}
/**
* {@inheritDoc}
*/
public function getMktimeHour($hour, $marker = null)
{
if ('PM' !== $marker && 12 === $hour) {
@ -35,11 +41,17 @@ class Hour1201Transformer extends HourTransformer
return $hour;
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
return '\d{1,2}';
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
return array(

View File

@ -12,17 +12,23 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Parser and formatter for date formats
* Parser and formatter for 24 hour format (0-23)
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class Hour2400Transformer extends HourTransformer
{
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
return $this->padLeft($dateTime->format('G'), $length);
}
/**
* {@inheritDoc}
*/
public function getMktimeHour($hour, $marker = null)
{
if (null !== $marker) {
@ -32,11 +38,17 @@ class Hour2400Transformer extends HourTransformer
return $hour;
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
return '\d{1,2}';
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
return array(

View File

@ -12,12 +12,15 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Parser and formatter for date formats
* Parser and formatter for 24 hour format (1-24)
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class Hour2401Transformer extends HourTransformer
{
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
$hourOfDay = $dateTime->format('G');
@ -25,6 +28,9 @@ class Hour2401Transformer extends HourTransformer
return $this->padLeft($hourOfDay, $length);
}
/**
* {@inheritDoc}
*/
public function getMktimeHour($hour, $marker = null)
{
if (null !== $marker || 24 === $hour) {
@ -34,11 +40,17 @@ class Hour2401Transformer extends HourTransformer
return $hour;
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
return '\d{1,2}';
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
return array(

View File

@ -12,9 +12,18 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Base class for hour transformers.
* Base class for hour transformers
*
* @author Eriksen Costa <eriksen.costa@infranology.com.br>
*/
abstract class HourTransformer extends Transformer
{
/**
* Returns an hour value suitable for mktime() usage
*
* @param int $hour The hour value
* @param string $marker An optional AM/PM marker
* @return int The hour value suitable for mktime() usage
*/
abstract public function getMktimeHour($hour, $marker = null);
}
}

View File

@ -12,18 +12,24 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Parser and formatter for date formats
* Parser and formatter for minute format
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class MinuteTransformer extends Transformer
{
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
$minuteOfHour = (int) $dateTime->format('i');
return $this->padLeft($minuteOfHour, $length);
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
if (1 == $length) {
@ -35,6 +41,9 @@ class MinuteTransformer extends Transformer
return $regExp;
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
return array(

View File

@ -14,13 +14,16 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException;
/**
* Parser and formatter for date formats
* Parser and formatter for month format
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class MonthTransformer extends Transformer
{
private static $months = array(
/**
* @var array
*/
static protected $months = array(
'January',
'February',
'March',
@ -35,12 +38,27 @@ class MonthTransformer extends Transformer
'December'
);
private static $shortMonths = array();
/**
* Short months names (first 3 letters)
* @var array
*/
static protected $shortMonths = array();
private static $flippedMonths = array();
/**
* Flipped $months array, $name => $index
* @var array
*/
static protected $flippedMonths = array();
private static $flippedShortMonths = array();
/**
* Flipped $shortMonths array, $name => $index
* @var array
*/
static protected $flippedShortMonths = array();
/**
* Constructor
*/
public function __construct()
{
if (0 == count(self::$shortMonths)) {
@ -53,6 +71,9 @@ class MonthTransformer extends Transformer
}
}
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
$matchLengthMap = array(
@ -71,6 +92,9 @@ class MonthTransformer extends Transformer
}
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
switch ($length) {
@ -94,6 +118,9 @@ class MonthTransformer extends Transformer
return $regExp;
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
if (!is_numeric($matched)) {

View File

@ -12,12 +12,15 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Parser and formatter for date formats
* Parser and formatter for quarter format
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class QuarterTransformer extends Transformer
{
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
$month = (int) $dateTime->format('n');
@ -34,6 +37,9 @@ class QuarterTransformer extends Transformer
}
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
switch ($length) {
@ -47,6 +53,9 @@ class QuarterTransformer extends Transformer
}
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
return array();

View File

@ -12,18 +12,24 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Parser and formatter for date formats
* Parser and formatter for the second format
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class SecondTransformer extends Transformer
{
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
$secondOfMinute = (int) $dateTime->format('s');
return $this->padLeft($secondOfMinute, $length);
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
if (1 == $length) {
@ -35,6 +41,9 @@ class SecondTransformer extends Transformer
return $regExp;
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
return array(

View File

@ -12,24 +12,37 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Parser and formatter for date formats
* Parser and formatter for time zone format
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class TimeZoneTransformer extends Transformer
{
/**
* Caches the matched timezones
* @var array
*/
static protected $timezonesId = array();
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
return $dateTime->format('\G\M\TP');
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
return 'GMT[+-]\d{2}:\d{2}';
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
return array(
@ -37,6 +50,16 @@ class TimeZoneTransformer extends Transformer
);
}
/**
* Get a Etc/GMT timezone identifier for the specified timezone
*
* @param string $timezone A GMT timezone string (GMT-03:00, e.g.)
* @return string A timezone identifier
* @see http://www.timezoneconverter.com/cgi-bin/zoneinfo.tzc?s=default&tz=GMT0
*
* TODO: refactor the timezone matching to match to non 0 minutes timezones (Pacific/Chatham, e.g.)
* http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
*/
protected function getTimezoneId($matched)
{
$offset = $this->getSecondsOffset($matched);
@ -61,6 +84,12 @@ class TimeZoneTransformer extends Transformer
return self::$timezonesId[$offset];
}
/**
* Calculates the seconds offset of a timezone
*
* @param string $timezone A GMT timezone string (GMT-03:00, e.g.)
* @return int The offset in seconds of the timezone
*/
protected function getSecondsOffset($timezone)
{
preg_match('/GMT(?P<signal>[+-])(?P<hours>\d{2}):(?P<minutes>\d{2})/', $timezone, $matches);

View File

@ -18,10 +18,41 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
*/
abstract class Transformer
{
/**
* Format a value using a configured DateTime as date/time source
*
*
* @param DateTime $dateTime A DateTime object to be used to generate the formatted value
* @param int $lenght The formatted value string lenght
* @return string The formatted value
*/
abstract public function format(\DateTime $dateTime, $length);
/**
* Returns a reverse matching regular expression of a string generated by format()
*
* @param int $lenght The lenght of the value to be reverse matched
* @return string The reverse matching regular expression
*/
abstract public function getReverseMatchingRegExp($length);
/**
* Extract date options from a matched value returned by the processing of the reverse matching
* regular expression
*
* @param string $matched The matched value
* @param int $lenght The lenght of the Transformer pattern string
* @return array An associative array
*/
abstract public function extractDateOptions($matched, $length);
/**
* Pad a string with zeros to the left
*
* @param string $value The string to be padded
* @param int $lenght The lenght to pad
* @return string The padded string
*/
protected function padLeft($value, $length)
{
return str_pad($value, $length, '0', STR_PAD_LEFT);

View File

@ -12,12 +12,15 @@
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Parser and formatter for date formats
* Parser and formatter for year format
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class YearTransformer extends Transformer
{
/**
* {@inheritDoc}
*/
public function format(\DateTime $dateTime, $length)
{
if (2 == $length) {
@ -27,20 +30,23 @@ class YearTransformer extends Transformer
}
}
/**
* {@inheritDoc}
*/
public function getReverseMatchingRegExp($length)
{
if (2 == $length) {
$regExp = '\d{2}';
} else {
$regExp = '\d{4}';
// The named capture in this case will be always y (that equals to yyyy)
//$length = 1;
}
return $regExp;
}
/**
* {@inheritDoc}
*/
public function extractDateOptions($matched, $length)
{
return array(

View File

@ -318,6 +318,13 @@ class StubIntlDateFormatter
}
}
/**
* Create and returns a DateTime object with the specified timestamp and with the
* current time zone
*
* @param int $timestamp
* @return DateTime
*/
protected function createDateTime($timestamp)
{
$dateTime = new \DateTime();
@ -327,6 +334,10 @@ class StubIntlDateFormatter
return $dateTime;
}
/**
* Returns a pattern string based in the datetype and timetype values
* @return string
*/
protected function getDefaultPattern()
{
$patternParts = array();