public function testAsString() { $object = new CopixErrorObject(); $object->addErrors('Test erreur'); $this->assertEquals('Test erreur', $object->asString()); $this->assertEquals('Test erreur', _toString($object)); }
/** * Smarty {textformat}{/textformat} block plugin * * Type: block function<br> * Name: textformat<br> * Purpose: format text a certain way with preset styles * or custom wrap/indent settings<br> * @link http://smarty.php.net/manual/en/language.function.textformat.php {textformat} * (Smarty online manual) * @param array * <pre> * Params: style: string (email) * indent: integer (0) * wrap: integer (80) * wrap_char string ("\n") * indent_char: string (" ") * wrap_boundary: boolean (true) * </pre> * @author Monte Ohrt <monte at ohrt dot com> * @param string contents of the block * @param Smarty clever simulation of a method * @return string string $content re-formatted */ function smarty_block_textformat($params, $content, &$smarty) { if (is_null($content)) { return; } $style = null; $indent = 0; $indent_first = 0; $indent_char = ' '; $wrap = 80; $wrap_char = "\n"; $wrap_cut = false; $assign = null; foreach ($params as $_key => $_val) { switch ($_key) { case 'style': case 'indent_char': case 'wrap_char': case 'assign': ${$_key} = _toString($_val); break; case 'indent': case 'indent_first': case 'wrap': ${$_key} = (int) $_val; break; case 'wrap_cut': ${$_key} = (bool) $_val; break; default: $smarty->trigger_error("textformat: unknown attribute '{$_key}'"); } } if ($style == 'email') { $wrap = 72; } // split into paragraphs $_paragraphs = preg_split('![\\r\\n][\\r\\n]!', $content); $_output = ''; for ($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) { if ($_paragraphs[$_x] == '') { continue; } // convert mult. spaces & special chars to single space $_paragraphs[$_x] = preg_replace(array('!\\s+!', '!(^\\s+)|(\\s+$)!'), array(' ', ''), $_paragraphs[$_x]); // indent first line if ($indent_first > 0) { $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x]; } // wordwrap sentences $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut); // indent lines if ($indent > 0) { $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]); } } $_output = implode($wrap_char . $wrap_char, $_paragraphs); return $assign ? $smarty->assign($assign, $_output) : $_output; }
function smarty_function_html_options_optoutput($key, $value, $selected) { if (!is_array($value)) { $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' . smarty_function_escape_special_chars($key) . '"'; if (in_array(_toString($key), $selected)) { $_html_result .= ' selected="selected"'; } $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n"; } else { $_html_result = smarty_function_html_options_optgroup($key, $value, $selected); } return $_html_result; }
/** * Encode une chaîne en JSON. * * @param string $pString Chaîne à encoder. * @param array $pBuffer Buffer JSON. */ private static function _encodeString($pString, &$pBuffer) { $pBuffer[] = '"'; // Remplacements faciles $escapedString = str_replace(array('\\', "\t", "\r", "\n", "", "\f", '"', '/'), array('\\\\', '\\t', '\\r', '\\n', '\\b', '\\f', '\\"', '\\/'), _toString($pString)); // Sépare la chaîne en partie UTF8 et parties non-UTF8 $parts = preg_split('/((?:[\\x20-\\x7F]|[\\xC0-\\xDF][\\x80-\\xBF]|[\\xE0-\\xEF][\\x80-\\xBF]{2}|[\\xF0-\\xF7][\\x80-\\xBF]{3})+)/', $escapedString, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($parts as $i => $part) { if ($part) { if ($i & 1 == 1) { // Partie UTF8 $pBuffer[] = $part; } else { // Partie non-UTF8 foreach (split('//', $part) as $character) { $pBuffer[] = sprintf('\\u%04X', ord($character)); } } } } $pBuffer[] = '"'; }
/** * Génère une déclaration de fonction. * * @param string $pName Nom de la fonction, ou null pour créer une fonction anonyme. * @param mixed $pArgs Liste des arguments (tableau ou chaîne). * @param string $pBody Corps de la fonction. * @return CopixJSFragment Un fragment de type instruction si la fonction est nommée * ou de type expression pour une fonction anonyme. */ public function function_($pName, $pArgs, $pBody) { $code = 'function' . (empty($pName) ? '' : ' ' . $pName) . '(' . (is_array($pArgs) ? join(',', $pArgs) : (!empty($pArgs) ? $pArgs : '')) . ')' . '{' . _toString($pBody) . '}'; if (empty($pName)) { return $this->addExpression_($code); } else { $this->addStatement_($code); } }
/** * Constructeur qui génère le message de l'exception * * @param array $pErrors Tableau d'erreur */ public function __construct($pErrors) { $this->_errors = $pErrors; parent::__construct(_toString($pErrors)); }
public function testObjnew_() { $js = new CopixJSWidget(); $obj = $js->obj->new_(5); $this->assertEquals('', _toString($js)); $this->assertEquals('new obj(5)', _toString($obj)); }
/** * Retourne un booléen à partir d'une entrée. * * Evalue les chaînes suivantes comme vrai : yes, true, enable, enabled, 1. * Evalue les chaînes suivantes comme faux: no, false, disable, disabled, 0. * Si cela ne colle pas, transforme la chaîne en entier, 0 s'évalue comme faux et tout le reste comme vrai. * * @param mixed $pBoolean L'élément à transformer. * @return boolean */ public static function getBoolean($pBoolean) { switch (strtolower(_toString($pBoolean))) { case 'yes': case 'true': case 'enable': case 'enabled': case '1': return true; case 'no': case 'false': case 'disable': case 'disabled': case '0': case '': return false; default: return self::getInt($pBoolean) != 0; } }
/** * Ajoute du javascript dans le header. * * Si $pId est fourni et a déjà été utilisé, le code existant est remplacé par $pCode. * * @param string $pCode Code à rajouter * @param string $pId Identifiant du fragment, pour éviter les doublons * @param boolean $$pDomReady Le code doit-il être encapsulé dans l'événément domready ? */ public static function addJSCode($pCode, $pId = null, $pDomReady = self::DOMREADY_AUTO) { if ($pDomReady != self::DOMREADY_ALWAYS && $pDomReady != self::DOMREADY_NEVER) { $pDomReady = self::DOMREADY_AUTO; } if ($pDomReady == self::DOMREADY_ALWAYS) { self::addJSFramework(); } if (is_array($pCode)) { $pCode = join("", $pCode); } else { $pCode = _toString($pCode); } if ($pId !== null) { self::$_JSCode[$pDomReady][$pId] = $pCode; } else { self::$_JSCode[$pDomReady][] = $pCode; } }
function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels) { $_output = ''; if ($labels) { $_output .= '<label>'; } $_output .= '<input type="checkbox" name="' . smarty_function_escape_special_chars($name) . '[]" value="' . smarty_function_escape_special_chars($value) . '"'; if (in_array(_toString($value), $selected)) { $_output .= ' checked="checked"'; } $_output .= $extra . ' />' . $output; if ($labels) { $_output .= '</label>'; } $_output .= $separator; return $_output; }
/** * Smarty {html_radios} function plugin * * File: function.html_radios.php<br> * Type: function<br> * Name: html_radios<br> * Date: 24.Feb.2003<br> * Purpose: Prints out a list of radio input types<br> * Input:<br> * - name (optional) - string default "radio" * - values (required) - array * - options (optional) - associative array * - checked (optional) - array default not set * - separator (optional) - ie <br> or * - output (optional) - the output next to each radio button * - assign (optional) - assign the output as an array to this variable * Examples: * <pre> * {html_radios values=$ids output=$names} * {html_radios values=$ids name='box' separator='<br>' output=$names} * {html_radios values=$ids checked=$checked separator='<br>' output=$names} * </pre> * @link http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios} * (Smarty online manual) * @author Christopher Kvarme <*****@*****.**> * @author credits to Monte Ohrt <monte at ohrt dot com> * @version 1.0 * @param array * @param Smarty * @return string * @uses smarty_function_escape_special_chars() */ function smarty_function_html_radios($params, &$smarty) { require_once $smarty->_get_plugin_filepath('shared', 'escape_special_chars'); $name = 'radio'; $values = null; $options = null; $selected = null; $separator = ''; $labels = true; $label_ids = false; $output = null; $extra = ''; foreach ($params as $_key => $_val) { switch ($_key) { case 'name': case 'separator': ${$_key} = _toString($_val); break; case 'checked': case 'selected': if (is_array($_val)) { $smarty->trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING); } else { $selected = (string) $_val; } break; case 'labels': case 'label_ids': ${$_key} = (bool) $_val; break; case 'options': ${$_key} = (array) $_val; break; case 'values': case 'output': ${$_key} = array_values((array) $_val); break; case 'radios': $smarty->trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING); $options = (array) $_val; break; case 'assign': break; default: if (!is_array($_val)) { $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"'; } else { $smarty->trigger_error("html_radios: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE); } break; } } if (!isset($options) && !isset($values)) { return ''; } /* raise error here? */ $_html_result = array(); if (isset($options)) { foreach ($options as $_key => $_val) { $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids); } } else { foreach ($values as $_i => $_key) { $_val = isset($output[$_i]) ? $output[$_i] : ''; $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids); } } if (!empty($params['assign'])) { $smarty->assign($params['assign'], $_html_result); } else { return implode("\n", $_html_result); } }
public function testComplexTypeValidatorWithObject() { $validator = _ctValidator()->attachTo(_validator('date'), 'datenaissance')->attachTo(_validator('date'), 'dateinscription')->required('datenaissance')->required('dateinscription'); $personne = new StdClass(); $this->assertTrue($validator->check($personne) !== true); $errors = $validator->check($personne)->asArray(); $this->assertTrue(isset($errors['datenaissance'])); $this->assertTrue(isset($errors['dateinscription'])); $personne->datenaissance = '25/12/1976'; $personne->dateinscription = '01/01/1989'; $this->assertTrue($validator->check($personne)); //test lorsqu'une propriété est indiquée comme obligatoire sans y attacher de validateur $validator = _ctValidator()->attachTo(_validator('date'), 'datenaissance')->required('datenaissance')->required('dateinscription'); $personne = new StdClass(); $personne->datenaissance = '25/12/1976'; $this->assertTrue($validator->check($personne) !== true); //Avec la factory dédiée aux objets $validator = CopixValidatorFactory::createObject()->attachTo(_validator('date'), 'datenaissance')->attachTo(_validator('date'), 'dateinscription')->required(array('datenaissance', 'dateinscription')); $personne = new StdClass(); $this->assertTrue($validator->check($personne) !== true); $errors = $validator->check($personne)->asArray(); $this->assertTrue(isset($errors['datenaissance'])); $this->assertTrue(isset($errors['dateinscription'])); $personne->datenaissance = '25/12/1976'; $personne->dateinscription = '01/01/1989'; $this->assertTrue($validator->check($personne)); //test des asserts $validator = _ctValidator()->attachTo(_validator('date'), 'datenaissance')->required('datenaissance')->required('dateinscription'); $personne = new StdClass(); $this->assertTrue($validator->check($personne) !== true); try { $validator->assert($personne); $this->assertTrue(false); } catch (CopixValidatorException $e) { $this->assertTrue(true); } $personne->datenaissance = '25/12/1976'; try { $validator->assert($personne); $this->assertTrue(false); } catch (CopixValidatorException $e) { $this->assertTrue(true); } $personne->dateinscription = '01/12/2010'; try { $validator->assert($personne); $this->assertTrue(true); } catch (CopixValidatorException $e) { $this->assertTrue(false); } //test du remplacement des messages d'erreur $validator = _ctValidator('Pas une personne valide')->attachTo(_validator('date'), 'datenaissance')->required('datenaissance')->required('dateinscription'); $this->assertTrue($validator->check(new StdClass()) !== true); $this->assertEquals(count($validator->check(new StdClass())->asArray()), 1); $this->assertEquals('Pas une personne valide', _toString($validator->check(new StdClass()))); }
/** * Smarty {html_select_date} plugin * * Type: function<br> * Name: html_select_date<br> * Purpose: Prints the dropdowns for date selection. * * ChangeLog:<br> * - 1.0 initial release * - 1.1 added support for +/- N syntax for begin * and end year values. (Monte) * - 1.2 added support for yyyy-mm-dd syntax for * time value. (Jan Rosier) * - 1.3 added support for choosing format for * month values (Gary Loescher) * - 1.3.1 added support for choosing format for * day values (Marcus Bointon) * - 1.3.2 support negative timestamps, force year * dropdown to include given date unless explicitly set (Monte) * - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that * of 0000-00-00 dates (cybot, boots) * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date} * (Smarty online manual) * @version 1.3.4 * @author Andrei Zmievski * @author Monte Ohrt <monte at ohrt dot com> * @param array * @param Smarty * @return string */ function smarty_function_html_select_date($params, &$smarty) { require_once $smarty->_get_plugin_filepath('shared', 'escape_special_chars'); require_once $smarty->_get_plugin_filepath('shared', 'make_timestamp'); require_once $smarty->_get_plugin_filepath('function', 'html_options'); /* Default values. */ $prefix = "Date_"; $start_year = strftime("%Y"); $end_year = $start_year; $display_days = true; $display_months = true; $display_years = true; $month_format = "%B"; /* Write months as numbers by default GL */ $month_value_format = "%m"; $day_format = "%02d"; /* Write day values using this format MB */ $day_value_format = "%d"; $year_as_text = false; /* Display years in reverse order? Ie. 2000,1999,.... */ $reverse_years = false; /* Should the select boxes be part of an array when returned from PHP? e.g. setting it to "birthday", would create "birthday[Day]", "birthday[Month]" & "birthday[Year]". Can be combined with prefix */ $field_array = null; /* <select size>'s of the different <select> tags. If not set, uses default dropdown. */ $day_size = null; $month_size = null; $year_size = null; /* Unparsed attributes common to *ALL* the <select>/<input> tags. An example might be in the template: all_extra ='class ="foo"'. */ $all_extra = null; /* Separate attributes for the tags. */ $day_extra = null; $month_extra = null; $year_extra = null; /* Order in which to display the fields. "D" -> day, "M" -> month, "Y" -> year. */ $field_order = 'MDY'; /* String printed between the different fields. */ $field_separator = "\n"; $time = time(); $all_empty = null; $day_empty = null; $month_empty = null; $year_empty = null; $extra_attrs = ''; foreach ($params as $_key => $_value) { switch ($_key) { case 'prefix': case 'time': case 'start_year': case 'end_year': case 'month_format': case 'day_format': case 'day_value_format': case 'field_array': case 'day_size': case 'month_size': case 'year_size': case 'all_extra': case 'day_extra': case 'month_extra': case 'year_extra': case 'field_order': case 'field_separator': case 'month_value_format': case 'month_empty': case 'day_empty': case 'year_empty': ${$_key} = _toString($_value); break; case 'all_empty': ${$_key} = _toString($_value); $day_empty = $month_empty = $year_empty = $all_empty; break; case 'display_days': case 'display_months': case 'display_years': case 'year_as_text': case 'reverse_years': ${$_key} = (bool) $_value; break; default: if (!is_array($_value)) { $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"'; } else { $smarty->trigger_error("html_select_date: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE); } break; } } if (preg_match('!^-\\d+$!', $time)) { // negative timestamp, use date() $time = date('Y-m-d', $time); } // If $time is not in format yyyy-mm-dd if (preg_match('/^(\\d{0,4}-\\d{0,2}-\\d{0,2})/', $time, $found)) { $time = $found[1]; } else { // use smarty_make_timestamp to get an unix timestamp and // strftime to make yyyy-mm-dd $time = strftime('%Y-%m-%d', smarty_make_timestamp($time)); } // Now split this in pieces, which later can be used to set the select $time = explode("-", $time); // make syntax "+N" or "-N" work with start_year and end_year if (preg_match('!^(\\+|\\-)\\s*(\\d+)$!', $end_year, $match)) { if ($match[1] == '+') { $end_year = strftime('%Y') + $match[2]; } else { $end_year = strftime('%Y') - $match[2]; } } if (preg_match('!^(\\+|\\-)\\s*(\\d+)$!', $start_year, $match)) { if ($match[1] == '+') { $start_year = strftime('%Y') + $match[2]; } else { $start_year = strftime('%Y') - $match[2]; } } if (strlen($time[0]) > 0) { if ($start_year > $time[0] && !isset($params['start_year'])) { // force start year to include given date if not explicitly set $start_year = $time[0]; } if ($end_year < $time[0] && !isset($params['end_year'])) { // force end year to include given date if not explicitly set $end_year = $time[0]; } } $field_order = strtoupper($field_order); $html_result = $month_result = $day_result = $year_result = ""; $field_separator_count = -1; if ($display_months) { $field_separator_count++; $month_names = array(); $month_values = array(); if (isset($month_empty)) { $month_names[''] = $month_empty; $month_values[''] = ''; } for ($i = 1; $i <= 12; $i++) { $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000)); $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000)); } $month_result .= '<select name='; if (null !== $field_array) { $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"'; } else { $month_result .= '"' . $prefix . 'Month"'; } if (null !== $month_size) { $month_result .= ' size="' . $month_size . '"'; } if (null !== $month_extra) { $month_result .= ' ' . $month_extra; } if (null !== $all_extra) { $month_result .= ' ' . $all_extra; } $month_result .= $extra_attrs . '>' . "\n"; $month_result .= smarty_function_html_options(array('output' => $month_names, 'values' => $month_values, 'selected' => (int) $time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int) $time[1], 1, 2000)) : '', 'print_result' => false), $smarty); $month_result .= '</select>'; } if ($display_days) { $field_separator_count++; $days = array(); if (isset($day_empty)) { $days[''] = $day_empty; $day_values[''] = ''; } for ($i = 1; $i <= 31; $i++) { $days[] = sprintf($day_format, $i); $day_values[] = sprintf($day_value_format, $i); } $day_result .= '<select name='; if (null !== $field_array) { $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"'; } else { $day_result .= '"' . $prefix . 'Day"'; } if (null !== $day_size) { $day_result .= ' size="' . $day_size . '"'; } if (null !== $all_extra) { $day_result .= ' ' . $all_extra; } if (null !== $day_extra) { $day_result .= ' ' . $day_extra; } $day_result .= $extra_attrs . '>' . "\n"; $day_result .= smarty_function_html_options(array('output' => $days, 'values' => $day_values, 'selected' => $time[2], 'print_result' => false), $smarty); $day_result .= '</select>'; } if ($display_years) { $field_separator_count++; if (null !== $field_array) { $year_name = $field_array . '[' . $prefix . 'Year]'; } else { $year_name = $prefix . 'Year'; } if ($year_as_text) { $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"'; if (null !== $all_extra) { $year_result .= ' ' . $all_extra; } if (null !== $year_extra) { $year_result .= ' ' . $year_extra; } $year_result .= ' />'; } else { $years = range((int) $start_year, (int) $end_year); if ($reverse_years) { rsort($years, SORT_NUMERIC); } else { sort($years, SORT_NUMERIC); } $yearvals = $years; if (isset($year_empty)) { array_unshift($years, $year_empty); array_unshift($yearvals, ''); } $year_result .= '<select name="' . $year_name . '"'; if (null !== $year_size) { $year_result .= ' size="' . $year_size . '"'; } if (null !== $all_extra) { $year_result .= ' ' . $all_extra; } if (null !== $year_extra) { $year_result .= ' ' . $year_extra; } $year_result .= $extra_attrs . '>' . "\n"; $year_result .= smarty_function_html_options(array('output' => $years, 'values' => $yearvals, 'selected' => $time[0], 'print_result' => false), $smarty); $year_result .= '</select>'; } } // Loop thru the field_order field for ($i = 0; $i <= 2; $i++) { $c = substr($field_order, $i, 1); switch ($c) { case 'D': $html_result .= $day_result; break; case 'M': $html_result .= $month_result; break; case 'Y': $html_result .= $year_result; break; } // Add the field seperator if ($i < $field_separator_count) { $html_result .= $field_separator; } } return $html_result; }