Exemple #1
0
 /**
  * Escape a string
  *
  * @param  string $string
  *
  * @return string
  */
 protected function escape($string)
 {
     if (!$this->escaper) {
         $this->escaper = $this->getView()->plugin('escapeHtml');
     }
     return $this->escaper->__invoke((string) $string);
 }
Exemple #2
0
 /**
  * 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;
 }
 /**
  * escape html
  *
  * @author VanCK
  * @return string
  */
 public function escapeHtml($value)
 {
     if (!$this->escapeHtml) {
         $this->escapeHtml = new EscapeHtml();
     }
     return $this->escapeHtml->__invoke($value);
 }
Exemple #4
0
 /**
  * Retrieve the escapeHtml helper
  *
  * @return EscapeHtml
  */
 protected function getEscapeHtmlHelper()
 {
     if ($this->escapeHtmlHelper) {
         return $this->escapeHtmlHelper;
     }
     if (method_exists($this->view, 'plugin')) {
         $this->escapeHtmlHelper = $this->view->plugin('escapehtml');
     }
     if (!$this->escapeHtmlHelper instanceof EscapeHtml) {
         $this->escapeHtmlHelper = new EscapeHtml();
         $this->escapeHtmlHelper->setView($this->getView());
     }
     return $this->escapeHtmlHelper;
 }
 public function testInvokeWithRowClassCallback()
 {
     $rowClassCallback = function ($columns) {
         static $counter = 0;
         if ($counter++) {
             return "{$columns['column1']}+{$columns['column2']}";
         } else {
             return '';
         }
     };
     $this->_escapeHtml->expects($this->exactly(4))->method('__invoke')->will($this->returnArgument(0));
     $table = $this->getMockBuilder($this->_getHelperClass())->setConstructorArgs(array($this->_escapeHtml, $this->_htmlTag, $this->_consoleUrl, $this->_dateFormat))->setMethods(array('sortableHeader', 'row'))->getMock();
     $table->expects($this->never())->method('sortableHeader');
     $table->expects($this->at(0))->method('row')->with($this->_headers, true, array(), null)->will($this->returnCallback(array($this, 'mockRow')));
     $table->expects($this->at(1))->method('row')->with(array('column1' => 'value1a', 'column2' => 'value2a'), false, array(), '')->will($this->returnCallback(array($this, 'mockRow')));
     $table->expects($this->at(2))->method('row')->with(array('column1' => 'value1b', 'column2' => 'value2b'), false, array(), 'value1b+value2b')->will($this->returnCallback(array($this, 'mockRow')));
     $this->assertEquals($this->_expected, $table($this->_data, $this->_headers, array(), array(), array(), $rowClassCallback));
 }
 /**
  * Translate and format messages with sprintf()-style placeholders
  *
  * This helper takes a list of items and returns a list of translated and
  * formatted messages. Each item can be a simple string which will just be
  * translated, or an associative array. Only the first element of an array
  * item is evaluated. The key is a message string with sprintf()-style
  * placeholders that will be translated and then be fed with the array
  * value, which must be an array of arguments. If only 1 argument is
  * required, it can be passed directly.
  *
  * Example:
  *
  *     $input = array(
  *         'message1',
  *         array('message2 %s' => 'arg'),
  *         array('message3 %s %s' => array('arg1', 'arg2'))
  *     );
  *     $output = array(
  *         'translated1',
  *         'translated2 arg',
  *         'translated3 arg1 arg2'
  *     );
  *
  * All strings and arguments get escaped. \Zend\Uri\Http arguments are
  * converted to hyperlinks.
  *
  * @param mixed[] $messages
  * @return string[]
  */
 public function __invoke(array $messages)
 {
     foreach ($messages as &$message) {
         if (is_array($message)) {
             $format = key($message);
             $args = current($message);
             if (!is_array($args)) {
                 $args = array($args);
             }
             foreach ($args as &$arg) {
                 if ($arg instanceof \Zend\Uri\Http) {
                     $arg = $this->_htmlElement->__invoke('a', $this->_escapeHtml->__invoke($arg), array('href' => $arg), true);
                 } else {
                     $arg = $this->_escapeHtml->__invoke($arg);
                 }
             }
             $message = vsprintf($this->_translate->__invoke($format), $args);
         } else {
             $message = $this->_translate->__invoke($message);
         }
     }
     return $messages;
 }
Exemple #7
0
 public function testSettingValidEncodingShouldNotThrowExceptions()
 {
     foreach ($this->supportedEncodings as $value) {
         $helper = new EscapeHelper();
         $helper->setEncoding($value);
         $helper->getEscaper();
     }
 }
 /**
  * Set EscapeHtml Helper.
  *
  * @param \Zend\View\Helper\EscapeHtml $escapeHelper
  *
  * @return self
  */
 public function setEscapeHtmlHelper(EscapeHtml $escapeHelper)
 {
     $escapeHelper->setView($this->getView());
     $this->escapeHelper = $escapeHelper;
     return $this;
 }