Ejemplo n.º 1
0
 /**
  * Build a new route URL
  *
  * The class will pass arguments values to any `$this->toUrlParam($value)` method for the
  * parameter named `param`.
  *
  * @param mixed $route_infos The information about the route to analyze, can be a string route or an array
  *                  of arguments like `param => value`
  * @param string $base_uri The URI to add the new route to
  * @param string $hash A hash tag to add to the generated URL
  * @param string $separator The argument/value separator (default is escaped ampersand : '&')
  *
  * @return string The application valid URL for the route
  *
  * @todo manage the case of $route_infos = route
  */
 public function generateUrl($route_infos, $base_uri = null, $hash = null, $separator = '&')
 {
     $url_args = $this->getArgumentsMap()->getCollection();
     $url = $base_uri;
     if (is_array($route_infos)) {
         $final_params = array();
         foreach ($route_infos as $_var => $_val) {
             if (!empty($_val)) {
                 $arg = in_array($_var, $url_args) ? array_search($_var, $url_args) : $_var;
                 $_meth = 'toUrl' . TextHelper::toCamelCase($_var);
                 if (method_exists($this, $_meth)) {
                     $_val = call_user_func_array(array($this, $_meth), array($_val));
                 }
                 if (is_string($_val)) {
                     $final_params[$this->urlEncode($arg)] = $this->urlEncode($_val);
                 } elseif (is_array($_val)) {
                     foreach ($_val as $_j => $_value) {
                         $final_params[$this->urlEncode($arg) . '[' . (is_string($_j) ? $_j : '') . ']'] = $this->urlEncode($_val);
                     }
                 }
             }
         }
         $url .= '?' . http_build_query($final_params, '', $separator);
     }
     if (!empty($hash)) {
         $url .= '#' . $hash;
     }
     return $url;
 }
Ejemplo n.º 2
0
 /**
  * Formatting file names
  *
  * @param   string  $filename   The filename to format
  * @param   boolean $lowercase  Should we return the name un lowercase (FALSE by default)
  * @param   string  $delimiter  The delimiter used for special chars substitution
  * @return  string  A filename valid on (almost) all systems
  */
 public static function formatFilename($filename = '', $lowercase = false, $delimiter = '-')
 {
     if (empty($filename)) {
         return '';
     }
     $_ext = self::getExtension($filename, true);
     if ($_ext) {
         $filename = str_replace($_ext, '', $filename);
     }
     $string = $filename;
     if ($lowercase) {
         $string = strtolower($string);
     }
     $string = str_replace(" ", $delimiter, $string);
     $string = preg_replace('#\\-+#', $delimiter, $string);
     $string = preg_replace('#([-]+)#', $delimiter, $string);
     $string = trim($string, $delimiter);
     $string = TextHelper::stripSpecialChars($string, $delimiter . '.');
     if ($_ext) {
         $string .= $_ext;
     }
     return $string;
 }
Ejemplo n.º 3
0
 public static function rssEncode($str, $cut = 800)
 {
     $str = preg_replace(',<h1(.*)</h1>,i', '', $str);
     $str = TextHelper::cut($str, $cut);
     return $str;
 }
 /**
  * @param   mixed   $name
  * @return  void
  * @throws  \Exception eventually caught
  */
 public function __unset($name)
 {
     $name = strtolower($name);
     if (array_key_exists($name, $this::$_magic_setters_mapping)) {
         $meth = $this::$_magic_setters_mapping[$name];
     } elseif (in_array($name, $this::$_magic_points_mapping)) {
         $meth = TextHelper::toCamelCase('set_point_' . $name);
     } else {
         $meth = TextHelper::toCamelCase('set_' . $name);
     }
     if (method_exists($this, $meth)) {
         try {
             call_user_func(array($this, $meth), null);
         } catch (\Exception $e) {
             throw $e;
         }
     }
 }
 /**
  * @param string $content_type
  * @return self
  */
 public function prepareContentType($content_type)
 {
     $_cls = '\\HttpFundamental\\ContentType\\' . TextHelper::toCamelCase($content_type);
     if (class_exists($_cls)) {
         return $this->setContentTypeObject(new $_cls());
     } else {
         return $this->setContentType($content_type);
     }
 }
Ejemplo n.º 6
0
#!/usr/bin/env php
<?php 
ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_STRICT);
if (empty($argv) || count($argv) < 2) {
    echo <<<EOT
usage: wrap-text.php <input-file> [output-file = input-file] [wrap-length = 75]

EOT;
    exit(1);
}
$input_file = $argv[1];
$output_file = isset($argv[2]) ? $argv[2] : $input_file;
$line_length = isset($argv[3]) ? $argv[3] : 75;
if (!file_exists($input_file) || !is_readable($input_file)) {
    echo "Input file '{$input_file}' not found or is not readable!";
    exit(1);
}
if (!touch($output_file)) {
    echo "Output file '{$output_file}' is not writable!";
    exit(1);
}
require_once __DIR__ . '/../src/SplClassLoader.php';
$classLoader = new SplClassLoader('Library', __DIR__ . '/../src');
$classLoader->register();
$content = file_get_contents($input_file);
//$wrapped_content = \Library\Converter\Html2Text::convert($content, null, $line_length);
$wrapped_content = \Library\Helper\Text::wrap($content, $line_length);
//echo $wrapped_content;
file_put_contents($output_file, $wrapped_content);
 /**
  * Cut a string at a certain characters count adding it a suffix string
  *
  * @param string $str The original string to cut
  * @param int $length The length to use before cutting the string
  * @param string $end_str A suffix string added if the original string is cutted
  * @param bool $return Return/Echo flag (default is to echo result)
  * @return mixed The result of the `_echo` function (string or bool)
  * @see _echo()
  * @see Library\Helper\Text::cut()
  */
 function _cut($str, $length = 120, $end_str = ' ...', $return = false)
 {
     return _echo(Helper\Text::cut($str, $length, $end_str), $return);
 }
Ejemplo n.º 8
0
 /**
  * Test if a classes names set is in a set of namespaces
  *
  * @param string|array $names
  * @param array $namespaces
  * @param array $logs Passed by reference
  * @return string|bool
  */
 protected function _classesInNamespaces($names, array $namespaces, array &$logs = array())
 {
     if (!is_array($names)) {
         $names = array($names);
     }
     foreach ($names as $_name) {
         foreach ($namespaces as $_namespace) {
             if (CodeHelper::namespaceExists($_namespace)) {
                 $tmp_namespace = rtrim(TextHelper::toCamelCase($_namespace), '\\') . '\\';
                 if (substr_count(TextHelper::toCamelCase($_name), $tmp_namespace) > 0) {
                     return $_name;
                 }
             } else {
                 $logs[] = $this->_getErrorMessage('Namespace "%s" not found!', $_namespace);
             }
         }
     }
     return false;
 }
Ejemplo n.º 9
0
 /**
  * @param int $str_len
  * @param bool $strip_tags
  * @return string
  */
 public function viewIntroduction($str_len = 600, $strip_tags = true, $end_str = '')
 {
     if (!isset($this->cache['docbook_introduction'])) {
         $intro = $this->getFile()->getIntroduction();
         $this->cache['docbook_introduction'] = TextHelper::cut($strip_tags ? strip_tags($intro) : $intro, $str_len, $end_str);
     }
     return $this->cache['docbook_introduction'];
 }
Ejemplo n.º 10
0
 /**
  * @covers ../../../src/Library/Helper/Text::toCamelCase()
  * @covers ../../../src/Library/Helper/Text::fromCamelCase()
  */
 public function testCamelCase()
 {
     $this->checkNoArg('toCamelCase');
     $this->assertEquals('MyCamelCasePhrase', \Library\Helper\Text::toCamelCase('my_camel_case_phrase'));
     $this->checkNoArg('fromCamelCase');
     $this->assertEquals('my_camel_case_phrase', \Library\Helper\Text::fromCamelCase('MyCamelCasePhrase'));
 }
Ejemplo n.º 11
0
 /**
  * Get a safe class property name in camel case
  * @param string $name
  * @param string $replace
  * @param bool $capitalize_first_char
  * @return string
  */
 public static function getPropertyName($name = '', $replace = '_', $capitalize_first_char = true)
 {
     return TextHelper::toCamelCase($name, $replace, $capitalize_first_char);
 }
 /**
  * @param   mixed   $name
  * @return  void
  * @throws  \Exception eventually caught
  */
 public function __unset($name)
 {
     $_name = strtolower($name);
     $meth = array_key_exists($_name, $this::$_magic_setters_mapping) ? $this::$_magic_setters_mapping[$_name] : TextHelper::toCamelCase('set_' . $_name);
     if (method_exists($this, $meth)) {
         try {
             call_user_func(array($this, $meth), null);
         } catch (\Exception $e) {
             throw $e;
         }
     } elseif (property_exists($this, $name)) {
         $this->{$name} = null;
     }
 }