/** * Generate HTML table * * $headers is an associative array with header labels. Its keys are used to * match corresponding fields in the other arguments. For each header, a * corresponding field must be set in the table data or in $renderCallbacks. * * $data is an array of row objects. Row objects are typically associative * arrays or objects implementing the \ArrayAccess interface. A default * rendering method is available for these types. For any other type, all * columns must be rendered by a callback. If no rows are present, an * empty string is returned. * * By default, cell data is retrieved from $data and escaped automatically. * \DateTime objects are rendered as short timestamps (yy-mm-dd hh:mm). The * application's default locale controls the date/time format. * Alternatively, a callback can be provided in the $renderCallbacks array. * If a callback is defined for a column, the callback is responsible for * escaping cell data. It gets called with the following arguments: * * 1. The view renderer * 2. The row object * 3. The key of the column to be rendered. This is useful for callbacks * that render more than 1 column. * * The optional $columnClasses array may contain values for a "class" * attribute which gets applied to all cells of a specified column. The * $columnClasses keys are matched against the keys of each row. * * $rowClassCallback, if given, is called for every non-header row. It * receives the unprocessed column data for each row and delivers a string * that is set as the row's class attribute if it is not empty. * * If the optional $sorting array contains the "order" and "direction" * elements (other elements are ignored), headers are generated as * hyperlinks with "order" and "direction" parameters set to the * corresponding column. The values denote the sorting in effect for the * current request - the header will be marked with an arrow indicating the * current sorting. The controller action should evaluate these parameters, * sort the data and provide the sorting to the view renderer. The * \Console\Mvc\Controller\Plugin\GetOrder controller plugin simplifies * these tasks. * * @param array|\Traversable $data * @param array $headers * @param array $sorting * @param array $renderCallbacks * @param string[] $columnClasses Optional class attributes to apply to columns (keys are matched against $row) * @param callable $rowClassCallback Optional callback to provide row class attributes * @return string HTML table */ public function __invoke($data, array $headers, $sorting = array(), $renderCallbacks = array(), $columnClasses = array(), $rowClassCallback = null) { if (count($data) == 0) { return ''; } $table = "<table class='alternating'>\n"; // Generate header row if (isset($sorting['order']) and isset($sorting['direction'])) { $row = array(); foreach ($headers as $key => $label) { $row[$key] = $this->sortableHeader($label, $key, $sorting['order'], $sorting['direction']); } $table .= $this->row($row, true, $columnClasses); } else { $table .= $this->row($headers, true, $columnClasses); } // Generate data rows $keys = array_keys($headers); foreach ($data as $rowData) { $row = array(); foreach ($keys as $key) { if (isset($renderCallbacks[$key])) { $row[$key] = $renderCallbacks[$key]($this->view, $rowData, $key); } elseif ($rowData[$key] instanceof \DateTime) { $row[$key] = $this->_escapeHtml->__invoke($this->_dateFormat->__invoke($rowData[$key], \IntlDateFormatter::SHORT, \IntlDateFormatter::SHORT)); } else { $row[$key] = $this->_escapeHtml->__invoke($rowData[$key]); } } $table .= $this->row($row, false, $columnClasses, $rowClassCallback ? $rowClassCallback($rowData) : null); } $table .= "</table>\n"; return $table; }
/** * Retorna data e hora * * @param string|null $date * @return string|Plugin\Formatar */ public function dataHora($date) { $this->setData($date); if (!$this->data instanceof \DateTime) { return 'data inválida!'; } return parent::__invoke($this->data, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM, 'pt_BR', null); }
public function __invoke($dateObject, $dateFormat = null, $timeFormat = null, $locale = null, $pattern = null) { if (!$dateObject instanceof \DateTime) { $dateObject = new \DateTime($dateObject); } $dateString = parent::__invoke($dateObject, $dateFormat, $timeFormat, $locale, $pattern); return $dateString; }
/** * Formats a date. * * Pass <b>$dateTime</b> and/or <b>$timeType</b> as string or constant. * Sets <b>$timeType</b> to same format as <b>$dateType</b> if not given. * Proxies to parent method for rendering. * * @see \Zend\I18n\View\Helper\DateFormat::__invoke() * * @param \DateTime $data|string * @param string $dateType * @return string */ public function __invoke($date, $dateType = self::SHORT, $timeType = null, $locale = null, $pattern = null) { if (is_string($dateType)) { $dateType = $this->detectType($dateType); if (null === $timeType) { $timeType = $dateType; } } if (is_string($date)) { $date = date_create($date); } if (is_string($timeType)) { $timeType = $this->detectType($timeType); } return parent::__invoke($date, $dateType, $timeType, $locale, $pattern); }
public function testBugTwoPatternOnSameHelperInstance() { $date = new DateTime('2012-07-02T22:44:03Z'); $helper = new DateFormatHelper(); $helper->setTimezone('Europe/Berlin'); $this->assertEquals('03/2012', $helper->__invoke($date, null, null, 'it_IT', 'dd/Y')); $this->assertEquals('03-2012', $helper->__invoke($date, null, null, 'it_IT', 'dd-Y')); }
/** * Rendering single cell * * @return string */ public function render($type = 'html') { $row = $this->getTable()->getRow()->getActualRow(); $value = ''; if (is_array($row) || $row instanceof \ArrayAccess) { $value = isset($row[$this->getHeader()->getName()]) ? $row[$this->getHeader()->getName()] : ''; } elseif (is_object($row)) { $headerName = $this->getHeader()->getName(); $methodName = 'get' . ucfirst($headerName); if (method_exists($row, $methodName)) { $value = $row->{$methodName}(); } else { $value = property_exists($row, $headerName) ? $row->{$headerName} : ''; } } foreach ($this->decorators as $decorator) { if ($decorator->validConditions()) { $value = $decorator->render($value); } } if ($type == 'html') { switch (true) { case $value instanceof \Datetime: $dateFormat = new DateFormat(); $value = $dateFormat->__invoke($value, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM, \Locale::getDefault()); break; } $ret = sprintf("<td %s>%s</td>", $this->getAttributes(), $value); $this->clearVar(); return $ret; } else { return $value; } }
/** * @dataProvider currencyTestsDataProvider */ public function testSettersProvideDefaults($locale, $timezone, $timeType, $dateType, $date, $expected) { $this->helper->setTimezone($timezone)->setLocale($locale); $this->assertMbStringEquals($expected, $this->helper->__invoke($date, $dateType, $timeType)); }