Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'form/date-time-select' of git://github.com/bakura10/zf2…

… into feature/2947
  • Loading branch information...
commit 84064d200cf630950eb1777a67f5b6a4e33c9f8d 2 parents 6a0c35a + 3396f8b
@weierophinney weierophinney authored
View
99 library/Zend/Code/Scanner/ClassScanner.php
@@ -97,8 +97,6 @@ class ClassScanner implements ScannerInterface
protected $infos = array();
/**
- * Constructor
- *
* @param array $classTokens
* @param NameInformation|null $nameInformation
* @return ClassScanner
@@ -110,8 +108,11 @@ public function __construct(array $classTokens, NameInformation $nameInformation
}
/**
+<<<<<<< HEAD
+=======
* Get annotations
*
+>>>>>>> c7ab3b44304857c7271297becf651225c4bca6b4
* @param Annotation\AnnotationManager $annotationManager
* @return Annotation\AnnotationCollection
*/
@@ -124,22 +125,12 @@ public function getAnnotations(Annotation\AnnotationManager $annotationManager)
return new AnnotationScanner($annotationManager, $docComment, $this->nameInformation);
}
- /**
- * Get doc comment
- *
- * @return null|string
- */
public function getDocComment()
{
$this->scan();
return $this->docComment;
}
- /**
- * Get doc block
- *
- * @return false|DocBlockScanner
- */
public function getDocBlock()
{
if (!$docComment = $this->getDocComment()) {
@@ -148,132 +139,72 @@ public function getDocBlock()
return new DocBlockScanner($docComment);
}
- /**
- * Get name
- *
- * @return null|string
- */
public function getName()
{
$this->scan();
return $this->name;
}
- /**
- * Get short name
- *
- * @return null|string
- */
public function getShortName()
{
$this->scan();
return $this->shortName;
}
- /**
- * Get line start
- *
- * @return int|null
- */
public function getLineStart()
{
$this->scan();
return $this->lineStart;
}
- /**
- * Get line end
- *
- * @return int|null
- */
public function getLineEnd()
{
$this->scan();
return $this->lineEnd;
}
- /**
- * Check for final
- *
- * @return bool
- */
public function isFinal()
{
$this->scan();
return $this->isFinal;
}
- /**
- * Check for instantiable
- *
- * @return bool
- */
public function isInstantiable()
{
$this->scan();
return (!$this->isAbstract && !$this->isInterface);
}
- /**
- * Check for abstract
- *
- * @return bool
- */
public function isAbstract()
{
$this->scan();
return $this->isAbstract;
}
- /**
- * Check for interface
- *
- * @return bool
- */
public function isInterface()
{
$this->scan();
return $this->isInterface;
}
- /**
- * Has parent class
- *
- * @return bool
- */
public function hasParentClass()
{
$this->scan();
return ($this->parentClass != null);
}
- /**
- * Get parent class
- *
- * @return null|string
- */
public function getParentClass()
{
$this->scan();
return $this->parentClass;
}
- /**
- * Get interfaces
- *
- * @return string[]
- */
public function getInterfaces()
{
$this->scan();
return $this->interfaces;
}
- /**
- * Get constants
- *
- * @return array
- */
public function getConstants()
{
$this->scan();
@@ -389,8 +320,6 @@ public function getMethodNames()
}
/**
- * Get methods
- *
* @return MethodScanner[]
*/
public function getMethods()
@@ -410,10 +339,8 @@ public function getMethods()
}
/**
- * Get method
- *
* @param string|int $methodNameOrInfoIndex
- * @throws Exception\InvalidArgumentException
+ * @throws \Zend\Code\Exception\InvalidArgumentException
* @return MethodScanner
*/
public function getMethod($methodNameOrInfoIndex)
@@ -450,12 +377,6 @@ public function getMethod($methodNameOrInfoIndex)
return $m;
}
- /**
- * Has method
- *
- * @param string $name
- * @return bool
- */
public function hasMethod($name)
{
$this->scan();
@@ -468,9 +389,6 @@ public function hasMethod($name)
return false;
}
- /**
- * Export
- */
public static function export()
{
// @todo
@@ -481,12 +399,6 @@ public function __toString()
// @todo
}
- /**
- * Scan
- *
- * @return void
- * @throws Exception\RuntimeException
- */
protected function scan()
{
if ($this->isScanned) {
@@ -530,7 +442,7 @@ protected function scan()
$tokenType = null;
$tokenContent = $token;
$tokenLine = $tokenLine + substr_count($lastTokenArray[1],
- "\n"); // adjust token line by last known newline count
+ "\n"); // adjust token line by last known newline count
} else {
$lastTokenArray = $token;
list($tokenType, $tokenContent, $tokenLine) = $token;
@@ -830,3 +742,4 @@ protected function scan()
}
}
+
View
314 library/Zend/Form/Element/DateTimeSelect.php
@@ -0,0 +1,314 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Form
+ */
+
+namespace Zend\Form\Element;
+
+use DateTime;
+use Zend\Form\Form;
+use Zend\Validator\ValidatorInterface;
+use Zend\Validator\Date as DateValidator;
+
+class DateTimeSelect extends DateSelect
+{
+ /**
+ * Select form element that contains values for hour
+ *
+ * @var Select
+ */
+ protected $hourElement;
+
+ /**
+ * Select form element that contains values for minute
+ *
+ * @var Select
+ */
+ protected $minuteElement;
+
+ /**
+ * Select form element that contains values for second
+ *
+ * @var Select
+ */
+ protected $secondElement;
+
+ /**
+ * Is the seconds select shown when the element is rendered?
+ *
+ * @var bool
+ */
+ protected $shouldShowSeconds = false;
+
+
+ /**
+ * Constructor. Add the hour, minute and second select elements
+ *
+ * @param null|int|string $name Optional name for the element
+ * @param array $options Optional options for the element
+ */
+ public function __construct($name = null, $options = array())
+ {
+ parent::__construct($name, $options);
+
+ $this->hourElement = new Select('hour');
+ $this->minuteElement = new Select('minute');
+ $this->secondElement = new Select('second');
+ }
+
+ /**
+ * Accepted options for DateTimeSelect (plus the ones from DateSelect) :
+ * - hour_attributes: HTML attributes to be rendered with the hour element
+ * - minute_attributes: HTML attributes to be rendered with the minute element
+ * - second_attributes: HTML attributes to be rendered with the second element
+ * - should_show_seconds: if set to true, the seconds select is shown
+ *
+ * @param array|\Traversable $options
+ * @return DateSelect
+ */
+ public function setOptions($options)
+ {
+ parent::setOptions($options);
+
+ if (isset($options['hour_attributes'])) {
+ $this->setHourAttributes($options['hour_attributes']);
+ }
+
+ if (isset($options['minute_attributes'])) {
+ $this->setMinuteAttributes($options['minute_attributes']);
+ }
+
+ if (isset($options['second_attributes'])) {
+ $this->setSecondAttributes($options['second_attributes']);
+ }
+
+ if (isset($options['should_show_seconds'])) {
+ $this->setShouldShowSeconds($options['should_show_seconds']);
+ }
+
+ return $this;
+ }
+
+ /**
+ * @return Select
+ */
+ public function getHourElement()
+ {
+ return $this->hourElement;
+ }
+
+ /**
+ * @return Select
+ */
+ public function getMinuteElement()
+ {
+ return $this->minuteElement;
+ }
+
+ /**
+ * @return Select
+ */
+ public function getSecondElement()
+ {
+ return $this->secondElement;
+ }
+
+ /**
+ * Set the hour attributes
+ *
+ * @param array $hourAttributes
+ * @return DateSelect
+ */
+ public function setHourAttributes(array $hourAttributes)
+ {
+ $this->hourElement->setAttributes($hourAttributes);
+ return $this;
+ }
+
+ /**
+ * Get the hour attributes
+ *
+ * @return array
+ */
+ public function getHourAttributes()
+ {
+ return $this->hourElement->getAttributes();
+ }
+
+ /**
+ * Set the minute attributes
+ *
+ * @param array $minuteAttributes
+ * @return DateSelect
+ */
+ public function setMinuteAttributes(array $minuteAttributes)
+ {
+ $this->minuteElement->setAttributes($minuteAttributes);
+ return $this;
+ }
+
+ /**
+ * Get the minute attributes
+ *
+ * @return array
+ */
+ public function getMinuteAttributes()
+ {
+ return $this->minuteElement->getAttributes();
+ }
+
+ /**
+ * Set the second attributes
+ *
+ * @param array $secondAttributes
+ * @return DateSelect
+ */
+ public function setSecondAttributes(array $secondAttributes)
+ {
+ $this->secondElement->setAttributes($secondAttributes);
+ return $this;
+ }
+
+ /**
+ * Get the second attributes
+ *
+ * @return array
+ */
+ public function getSecondAttributes()
+ {
+ return $this->secondElement->getAttributes();
+ }
+
+ /**
+ * If set to true, this indicate that the second select is shown. If set to true, the seconds will be
+ * assumed to always be 00
+ *
+ * @param bool $shouldShowSeconds
+ * @return DateTimeSelect
+ */
+ public function setShouldShowSeconds($shouldShowSeconds)
+ {
+ $this->shouldShowSeconds = (bool) $shouldShowSeconds;
+ return $this;
+ }
+
+ /**
+ * @return bool
+ */
+ public function shouldShowSeconds()
+ {
+ return $this->shouldShowSeconds;
+ }
+
+ /**
+ * @param mixed $value
+ * @return void|\Zend\Form\Element
+ */
+ public function setValue($value)
+ {
+ if ($value instanceof DateTime) {
+ $value = array(
+ 'year' => $value->format('Y'),
+ 'month' => $value->format('m'),
+ 'day' => $value->format('d'),
+ 'hour' => $value->format('H'),
+ 'minute' => $value->format('i'),
+ 'second' => $value->format('s')
+ );
+ }
+
+ if (!isset($value['second'])) {
+ $value['second'] = '00';
+ }
+
+ $this->yearElement->setValue($value['year']);
+ $this->monthElement->setValue($value['month']);
+ $this->dayElement->setValue($value['day']);
+ $this->hourElement->setValue($value['hour']);
+ $this->minuteElement->setValue($value['minute']);
+ $this->secondElement->setValue($value['second']);
+ }
+
+ /**
+ * Prepare the form element (mostly used for rendering purposes)
+ *
+ * @param Form $form
+ * @return mixed
+ */
+ public function prepareElement(Form $form)
+ {
+ parent::prepareElement($form);
+
+ $name = $this->getName();
+ $this->hourElement->setName($name . '[hour]');
+ $this->minuteElement->setName($name . '[minute]');
+ $this->secondElement->setName($name . '[second]');
+ }
+
+ /**
+ * Get validator
+ *
+ * @return ValidatorInterface
+ */
+ protected function getValidator()
+ {
+ if (null === $this->validator) {
+ $this->validator = new DateValidator(array('format' => 'Y-m-d H:i:s'));
+ }
+
+ return $this->validator;
+ }
+
+ /**
+ * Should return an array specification compatible with
+ * {@link Zend\InputFilter\Factory::createInput()}.
+ *
+ * @return array
+ */
+ public function getInputSpecification()
+ {
+ return array(
+ 'name' => $this->getName(),
+ 'required' => false,
+ 'filters' => array(
+ array(
+ 'name' => 'Callback',
+ 'options' => array(
+ 'callback' => function($date) {
+ // Convert the date to a specific format
+ if (is_array($date)) {
+ $date = sprintf('%s-%s-%s %s:%s:%s',
+ $date['year'], $date['month'], $date['day'],
+ $date['hour'], $date['minute'], $date['second']
+ );
+ }
+
+ return $date;
+ }
+ )
+ )
+ ),
+ 'validators' => array(
+ $this->getValidator(),
+ )
+ );
+ }
+
+ /**
+ * Clone the element (this is needed by Collection element, as it needs different copies of the elements)
+ */
+ public function __clone()
+ {
+ $this->dayElement = clone $this->dayElement;
+ $this->monthElement = clone $this->monthElement;
+ $this->yearElement = clone $this->yearElement;
+ $this->hourElement = clone $this->monthElement;
+ $this->minuteElement = clone $this->minuteElement;
+ $this->secondElement = clone $this->secondElement;
+ }
+}
View
2  library/Zend/Form/View/Helper/FormDateSelect.php
@@ -36,7 +36,7 @@ public function render(ElementInterface $element)
{
if (!$element instanceof DateSelectElement) {
throw new Exception\InvalidArgumentException(sprintf(
- '%s requires that the element is of type Zend\Form\Element\Select',
+ '%s requires that the element is of type Zend\Form\Element\DateSelect',
__METHOD__
));
}
View
301 library/Zend/Form/View/Helper/FormDateTimeSelect.php
@@ -0,0 +1,301 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Form
+ */
+
+namespace Zend\Form\View\Helper;
+
+use DateTime;
+use IntlDateFormatter;
+use Zend\Form\ElementInterface;
+use Zend\Form\Element\DateTimeSelect as DateTimeSelectElement;
+use Zend\Form\Exception;
+use Zend\Form\View\Helper\FormDateSelect as FormDateSelectHelper;
+
+/**
+ * @category Zend
+ * @package Zend_Form
+ * @subpackage View
+ */
+class FormDateTimeSelect extends FormDateSelectHelper
+{
+ /**
+ * Time formatter to use
+ *
+ * @var int
+ */
+ protected $timeType = IntlDateFormatter::LONG;
+
+
+ /**
+ * Render a date element that is composed of six selects
+ *
+ * @param ElementInterface $element
+ * @throws \Zend\Form\Exception\InvalidArgumentException
+ * @throws \Zend\Form\Exception\DomainException
+ * @return string
+ */
+ public function render(ElementInterface $element)
+ {
+ if (!$element instanceof DateTimeSelectElement) {
+ throw new Exception\InvalidArgumentException(sprintf(
+ '%s requires that the element is of type Zend\Form\Element\DateTimeSelect',
+ __METHOD__
+ ));
+ }
+
+ $name = $element->getName();
+ if ($name === null || $name === '') {
+ throw new Exception\DomainException(sprintf(
+ '%s requires that the element has an assigned name; none discovered',
+ __METHOD__
+ ));
+ }
+
+ $selectHelper = $this->getSelectElementHelper();
+ $pattern = $this->parsePattern();
+
+ $daysOptions = $this->getDaysOptions($pattern['day']);
+ $monthsOptions = $this->getMonthsOptions($pattern['month']);
+ $yearOptions = $this->getYearsOptions($element->getMinYear(), $element->getMaxYear());
+ $hourOptions = $this->getHoursOptions($pattern['hour']);
+ $minuteOptions = $this->getMinutesOptions($pattern['minute']);
+ $secondOptions = $this->getSecondsOptions($pattern['second']);
+
+ $dayElement = $element->getDayElement()->setValueOptions($daysOptions);
+ $monthElement = $element->getMonthElement()->setValueOptions($monthsOptions);
+ $yearElement = $element->getYearElement()->setValueOptions($yearOptions);
+ $hourElement = $element->getHourElement()->setValueOptions($hourOptions);
+ $minuteElement = $element->getMinuteElement()->setValueOptions($minuteOptions);
+ $secondElement = $element->getSecondElement()->setValueOptions($secondOptions);
+
+ if ($element->shouldCreateEmptyOption()) {
+ $dayElement->setEmptyOption('');
+ $yearElement->setEmptyOption('');
+ $monthElement->setEmptyOption('');
+ $hourElement->setEmptyOption('');
+ $minuteElement->setEmptyOption('');
+ $secondElement->setEmptyOption('');
+ }
+
+ $markup = array();
+ $markup[$pattern['day']] = $selectHelper->render($dayElement);
+ $markup[$pattern['month']] = $selectHelper->render($monthElement);
+ $markup[$pattern['year']] = $selectHelper->render($yearElement);
+ $markup[$pattern['hour']] = $selectHelper->render($hourElement);
+ $markup[$pattern['minute']] = $selectHelper->render($minuteElement);
+
+ if ($element->shouldShowSeconds()) {
+ $markup[$pattern['second']] = $selectHelper->render($secondElement);
+
+ $markup = sprintf(
+ '%s %s %s %s %s %s %s %s %s %s %s',
+ $markup[array_shift($pattern)],
+ array_shift($pattern), // Delimiter
+ $markup[array_shift($pattern)],
+ array_shift($pattern), // Delimiter
+ $markup[array_shift($pattern)],
+ array_shift($pattern), // Delimiter
+ $markup[array_shift($pattern)],
+ array_shift($pattern), // Delimiter
+ $markup[array_shift($pattern)],
+ array_shift($pattern), // Delimiter
+ $markup[array_shift($pattern)]
+ );
+ } else {
+ $markup = sprintf(
+ '%s %s %s %s %s %s %s %s %s',
+ $markup[array_shift($pattern)],
+ array_shift($pattern), // Delimiter
+ $markup[array_shift($pattern)],
+ array_shift($pattern), // Delimiter
+ $markup[array_shift($pattern)],
+ array_shift($pattern), // Delimiter
+ $markup[array_shift($pattern)],
+ array_shift($pattern), // Delimiter
+ $markup[array_shift($pattern)]
+ );
+ }
+
+ return $markup;
+ }
+
+ /**
+ * Invoke helper as function
+ *
+ * Proxies to {@link render()}.
+ *
+ * @param \Zend\Form\ElementInterface $element
+ * @param int $dateType
+ * @param int|null|string $timeType
+ * @param null|string $locale
+ * @return string
+ */
+ public function __invoke(
+ ElementInterface $element = null,
+ $dateType = IntlDateFormatter::LONG,
+ $timeType = IntlDateFormatter::LONG,
+ $locale = null
+ ) {
+ if (!$element) {
+ return $this;
+ }
+
+ $this->setDateType($dateType);
+ $this->setTimeType($timeType);
+
+ if ($locale !== null) {
+ $this->setLocale($locale);
+ }
+
+ return $this->render($element);
+ }
+
+ /**
+ * @param int $timeType
+ * @return FormDateTimeSelect
+ */
+ public function setTimeType($timeType)
+ {
+ // The FULL format uses values that are not used
+ if ($timeType === IntlDateFormatter::FULL) {
+ $timeType = IntlDateFormatter::LONG;
+ }
+
+ $this->timeType = $timeType;
+
+ return $this;
+ }
+
+ /**
+ * @return int
+ */
+ public function getTimeType()
+ {
+ return $this->timeType;
+ }
+
+ /**
+ * Override to also get time part
+ *
+ * @return string
+ */
+ public function getPattern()
+ {
+ if ($this->pattern === null) {
+ $intl = new IntlDateFormatter($this->getLocale(), $this->dateType, $this->timeType);
+ $this->pattern = $intl->getPattern();
+ }
+
+ return $this->pattern;
+ }
+
+ /**
+ * Parse the pattern
+ *
+ * @return array
+ */
+ protected function parsePattern()
+ {
+ $pattern = $this->getPattern();
+ $pregResult = preg_split('/([ -,.:\/]+)/', $pattern, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
+
+ $result = array();
+ foreach ($pregResult as $value) {
+ if (stripos($value, 'd') !== false) {
+ $result['day'] = $value;
+ } elseif (strpos($value, 'M') !== false) {
+ $result['month'] = $value;
+ } elseif (stripos($value, 'y') !== false) {
+ $result['year'] = $value;
+ } elseif (stripos($value, 'h') !== false) {
+ $result['hour'] = $value;
+ } elseif (stripos($value, 'm') !== false) {
+ $result['minute'] = $value;
+ } elseif (strpos($value, 's') !== false) {
+ $result['second'] = $value;
+ } else {
+ $result[] = $value;
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * Create a key => value options for hours
+ *
+ * @param string $pattern Pattern to use for hours
+ * @return array
+ */
+ protected function getHoursOptions($pattern)
+ {
+ $keyFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'HH');
+ $valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
+ $date = new DateTime('1970-01-01 00:00:00');
+
+ $result = array();
+ for ($hour = 1; $hour <= 31; $hour++) {
+ $key = $keyFormatter->format($date);
+ $value = $valueFormatter->format($date);
+ $result[$key] = $value;
+
+ $date->modify('+1 hour');
+ }
+
+ return $result;
+ }
+
+ /**
+ * Create a key => value options for minutes
+ *
+ * @param string $pattern Pattern to use for minutes
+ * @return array
+ */
+ protected function getMinutesOptions($pattern)
+ {
+ $keyFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'mm');
+ $valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
+ $date = new DateTime('1970-01-01 00:00:00');
+
+ $result = array();
+ for ($hour = 1; $hour <= 31; $hour++) {
+ $key = $keyFormatter->format($date);
+ $value = $valueFormatter->format($date);
+ $result[$key] = $value;
+
+ $date->modify('+1 minute');
+ }
+
+ return $result;
+ }
+
+ /**
+ * Create a key => value options for seconds
+ *
+ * @param string $pattern Pattern to use for seconds
+ * @return array
+ */
+ protected function getSecondsOptions($pattern)
+ {
+ $keyFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'ss');
+ $valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
+ $date = new DateTime('1970-01-01 00:00:00');
+
+ $result = array();
+ for ($hour = 1; $hour <= 31; $hour++) {
+ $key = $keyFormatter->format($date);
+ $value = $valueFormatter->format($date);
+ $result[$key] = $value;
+
+ $date->modify('+1 second');
+ }
+
+ return $result;
+ }
+}
View
5 library/Zend/Form/View/Helper/FormElement.php
@@ -58,6 +58,11 @@ public function render(ElementInterface $element)
return $helper($element);
}
+ if ($element instanceof Element\DateTimeSelect) {
+ $helper = $renderer->plugin('form_date_time_select');
+ return $helper($element);
+ }
+
if ($element instanceof Element\DateSelect) {
$helper = $renderer->plugin('form_date_select');
return $helper($element);
View
2  library/Zend/Form/View/Helper/FormMonthSelect.php
@@ -65,7 +65,7 @@ public function render(ElementInterface $element)
{
if (!$element instanceof MonthSelectElement) {
throw new Exception\InvalidArgumentException(sprintf(
- '%s requires that the element is of type Zend\Form\Element\Select',
+ '%s requires that the element is of type Zend\Form\Element\MonthSelect',
__METHOD__
));
}
View
1  library/Zend/Form/View/HelperConfig.php
@@ -43,6 +43,7 @@ class HelperConfig implements ConfigInterface
'formdate' => 'Zend\Form\View\Helper\FormDate',
'formdatetime' => 'Zend\Form\View\Helper\FormDateTime',
'formdatetimelocal' => 'Zend\Form\View\Helper\FormDateTimeLocal',
+ 'formdatetimeselect' => 'Zend\Form\View\Helper\FormDateTimeSelect',
'formdateselect' => 'Zend\Form\View\Helper\FormDateSelect',
'formelement' => 'Zend\Form\View\Helper\FormElement',
'formelementerrors' => 'Zend\Form\View\Helper\FormElementErrors',
View
75 tests/ZendTest/Form/Element/DateTimeSelectTest.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Form
+ */
+
+namespace ZendTest\Form\Element;
+
+use DateTime;
+use PHPUnit_Framework_TestCase as TestCase;
+use Zend\Form\Element\DateTimeSelect as DateTimeSelectElement;
+use Zend\Form\Factory;
+
+class DateTimeSelectTest extends TestCase
+{
+ public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttributes()
+ {
+ $element = new DateTimeSelectElement();
+
+ $inputSpec = $element->getInputSpecification();
+ $this->assertArrayHasKey('validators', $inputSpec);
+ $this->assertInternalType('array', $inputSpec['validators']);
+
+ $expectedClasses = array(
+ 'Zend\Validator\Date'
+ );
+ foreach ($inputSpec['validators'] as $validator) {
+ $class = get_class($validator);
+ $this->assertTrue(in_array($class, $expectedClasses), $class);
+ switch ($class) {
+ case 'Zend\Validator\Date':
+ $this->assertEquals('Y-m-d H:i:s', $validator->getFormat());
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ public function testCanSetDateFromDateTime()
+ {
+ $element = new DateTimeSelectElement();
+ $element->setValue(new DateTime('2012-09-24 03:04:05'));
+
+ $this->assertEquals('2012', $element->getYearElement()->getValue());
+ $this->assertEquals('09', $element->getMonthElement()->getValue());
+ $this->assertEquals('24', $element->getDayElement()->getValue());
+ $this->assertEquals('03', $element->getHourElement()->getValue());
+ $this->assertEquals('04', $element->getMinuteElement()->getValue());
+ $this->assertEquals('05', $element->getSecondElement()->getValue());
+ }
+
+ public function testUseDefaultValueForSecondsIfNotProvided()
+ {
+ $element = new DateTimeSelectElement();
+ $element->setValue(array(
+ 'year' => '2012',
+ 'month' => '09',
+ 'day' => '24',
+ 'hour' => '03',
+ 'minute' => '04'
+ ));
+
+ $this->assertEquals('2012', $element->getYearElement()->getValue());
+ $this->assertEquals('09', $element->getMonthElement()->getValue());
+ $this->assertEquals('24', $element->getDayElement()->getValue());
+ $this->assertEquals('03', $element->getHourElement()->getValue());
+ $this->assertEquals('04', $element->getMinuteElement()->getValue());
+ $this->assertEquals('00', $element->getSecondElement()->getValue());
+ }
+}
View
89 tests/ZendTest/Form/View/Helper/FormDateTimeSelectTest.php
@@ -0,0 +1,89 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Form
+ */
+
+namespace ZendTest\Form\View\Helper;
+
+use Zend\Form\Element\DateTimeSelect;
+use Zend\Form\View\Helper\FormDateTimeSelect as FormDateTimeSelectHelper;
+
+/**
+ * @category Zend
+ * @package Zend_Form
+ * @subpackage UnitTest
+ */
+class FormDateTimeSelectTest extends CommonTestCase
+{
+ public function setUp()
+ {
+ $this->helper = new FormDateTimeSelectHelper();
+ parent::setUp();
+ }
+
+ public function testRaisesExceptionWhenNameIsNotPresentInElement()
+ {
+ $element = new DateTimeSelect();
+ $this->setExpectedException('Zend\Form\Exception\DomainException', 'name');
+ $this->helper->render($element);
+ }
+
+ public function testGeneratesFiveSelectsWithElementByDefault()
+ {
+ $element = new DateTimeSelect('foo');
+ $markup = $this->helper->render($element);
+ $this->assertContains('<select name="day"', $markup);
+ $this->assertContains('<select name="month"', $markup);
+ $this->assertContains('<select name="year"', $markup);
+ $this->assertContains('<select name="hour"', $markup);
+ $this->assertContains('<select name="minute"', $markup);
+ $this->assertNotContains('<select name="second"', $markup);
+ }
+
+ public function testGeneratesSecondSelectIfAskedByElement()
+ {
+ $element = new DateTimeSelect('foo');
+ $element->setShouldShowSeconds(true);
+ $markup = $this->helper->render($element);
+ $this->assertContains('<select name="day"', $markup);
+ $this->assertContains('<select name="month"', $markup);
+ $this->assertContains('<select name="year"', $markup);
+ $this->assertContains('<select name="hour"', $markup);
+ $this->assertContains('<select name="minute"', $markup);
+ $this->assertContains('<select name="second"', $markup);
+ }
+
+ public function testCanGenerateSelectsWithEmptyOption()
+ {
+ $element = new DateTimeSelect('foo');
+ $element->setShouldCreateEmptyOption(true);
+ $markup = $this->helper->render($element);
+ $this->assertContains('<select name="day"', $markup);
+ $this->assertContains('<select name="month"', $markup);
+ $this->assertContains('<select name="year"', $markup);
+ $this->assertContains('<select name="hour"', $markup);
+ $this->assertContains('<select name="minute"', $markup);
+ $this->assertContains('<option value=""></option>', $markup);
+ }
+
+ public function testInvokeProxiesToRender()
+ {
+ $element = new DateTimeSelect('foo');
+ $markup = $this->helper->__invoke($element);
+ $this->assertContains('<select name="day"', $markup);
+ $this->assertContains('<select name="month"', $markup);
+ $this->assertContains('<select name="year"', $markup);
+ $this->assertContains('<select name="hour"', $markup);
+ $this->assertContains('<select name="minute"', $markup);
+ }
+
+ public function testInvokeWithNoElementChainsHelper()
+ {
+ $this->assertSame($this->helper, $this->helper->__invoke());
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.