Example #1
0
 /**
  *
  */
 protected function encodeArrayToXML($data, $parent, $pretty_print = false)
 {
     $content = '';
     $parent_sing = Inflector::singularize($parent);
     $numeric_keys = Arrays::isNumericallyKeyed($data);
     if ($pretty_print !== false) {
         $prefix = str_repeat('  ', $pretty_print);
     } else {
         $prefix = '';
     }
     foreach ($data as $key => $value) {
         $key = $numeric_keys ? $parent_sing : $key;
         $content .= $prefix . '<' . htmlspecialchars($key) . '>';
         switch (true) {
             case is_array($value):
             case is_object($value):
                 if ($pretty_print !== false) {
                     $content .= "\n";
                 }
                 $content .= $this->encodeArrayToXML($value, $key, $pretty_print !== false ? $pretty_print + 1 : false) . $prefix;
                 break;
             case is_bool($value):
                 $content .= $value ? 'true' : 'false';
                 break;
             case is_string($value):
             case is_numeric($value):
             default:
                 $content .= htmlspecialchars($value);
                 break;
         }
         $content .= '</' . htmlspecialchars($key) . '>';
         if ($pretty_print !== false) {
             $content .= "\n";
         }
     }
     return $content;
 }
Example #2
0
 /**
  * @dataProvider  fixLetterCaseProvider()
  * @depends       testPluralize()
  * @depends       testSingularize()
  */
 public function testFixLetterCase($a, $b, $pluralise)
 {
     # We disable simplification for this test to ensure that we do not split
     # as camel case when testing with silly capitalisation.
     if ($pluralise) {
         $this->assertSame($b, Inflector::pluralize($a, false));
     } else {
         $this->assertSame($b, Inflector::singularize($a, false));
     }
 }
Example #3
0
 /**
  * Takes a string or phrase, and pluralizes the last word.  If there is a 
  * number in the phrase, conditionally pluralize the last word based on the 
  * number.
  *
  * @see http://thecodingway.com/blog/robs-tips-and-tricks/php-pluralizing-with-style/
  *
  * @param   string  $string  The string to pluralize.
  *
  * @return  string
  */
 public static function pluralize($string)
 {
     # Regular expression for matching textual number.
     static $regex0 = '/(?:^|)((?<!point )(?:minus )?one(?! point)).*?(\\pL+)\\PL*$/';
     # Regular expression for matching normal number.
     static $regex1 = '/(?:^|)(-?(?<!(?:\\.|\\d))1(?:\\.0+)?)?\\PN*?(\\pL+)\\PL*$/';
     # Reguular expression for word replacement.
     static $regex2 = '/(\\PL|one |^)(\\pL+)(\\PL*)$/';
     if (!preg_match($regex0, $string, $match)) {
         if (!preg_match($regex1, $string, $match)) {
             # We had an error matching the phrase, assume we cannot pluralize it.
             return $string;
         }
     }
     array_shift($match);
     list($amount, $word) = $match;
     if ($word && !$amount) {
         $word = Inflector::pluralize($word);
         $string = preg_replace($regex2, "\$1{$word}\$3", $string);
     }
     return $string;
 }