Skip to content

Commit

Permalink
Moving data normalization into separate method.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Apr 20, 2015
1 parent 40c10c6 commit df4d03e
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions src/View/Widget/DateTimeWidget.php
Expand Up @@ -123,30 +123,10 @@ public function __construct(StringTemplate $templates, SelectBoxWidget $selectBo
*/
public function render(array $data, ContextInterface $context)
{
$data += [
'name' => '',
'empty' => false,
'disabled' => null,
'val' => null,
'year' => [],
'month' => [],
'day' => [],
'hour' => [],
'minute' => [],
'second' => [],
'meridian' => null,
];
$data = $this->_normalizeData($data);

$selected = $this->_deconstructDate($data['val'], $data);

$timeFormat = isset($data['hour']['format']) ? $data['hour']['format'] : null;
if ($timeFormat === 12 && !isset($data['meridian'])) {
$data['meridian'] = [];
}
if ($timeFormat === 24) {
$data['meridian'] = false;
}

$templateOptions = [];
foreach ($this->_selects as $select) {
if ($data[$select] === false || $data[$select] === null) {
Expand Down Expand Up @@ -178,6 +158,39 @@ public function render(array $data, ContextInterface $context)
return $this->_templates->format('dateWidget', $templateOptions);
}

/**
* Normalize data
*
* @param $data Data to normalize.
* @return array
*/
protected function _normalizeData($data)
{
$data += [
'name' => '',
'empty' => false,
'disabled' => null,
'val' => null,
'year' => [],
'month' => [],
'day' => [],
'hour' => [],
'minute' => [],
'second' => [],
'meridian' => null,
];

$timeFormat = isset($data['hour']['format']) ? $data['hour']['format'] : null;
if ($timeFormat === 12 && !isset($data['meridian'])) {
$data['meridian'] = [];
}
if ($timeFormat === 24) {
$data['meridian'] = false;
}

return $data;
}

/**
* Deconstructs the passed date value into all time units
*
Expand Down Expand Up @@ -569,15 +582,15 @@ protected function _generateNumbers($start, $end, $options = [])
*/
public function secureFields(array $data)
{
$data = $this->_normalizeData($data);

$fields = [];
$hourFormat = isset($data['hour']['format']) ? $data['hour']['format'] : null;
foreach ($this->_selects as $type) {
if ($type === 'meridian' && ($hourFormat === null || $hourFormat === 24)) {
foreach ($this->_selects as $select) {
if ($data[$select] === false || $data[$select] === null) {
continue;
}
if (!isset($data[$type]) || $data[$type] !== false) {
$fields[] = $data['name'] . '[' . $type . ']';
}

$fields[] = $data['name'] . '[' . $select . ']';
}
return $fields;
}
Expand Down

0 comments on commit df4d03e

Please sign in to comment.