Apache Zeta Components Manual :: File Source for string.php

Source for file string.php

Documentation is available at string.php

  1. <?php
  2. /**
  3.  * File containing the ezcConsoleStringTool class.
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @package ConsoleTools
  23.  * @version //autogentag//
  24.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25.  * @filesource
  26.  */
  27. /**
  28.  * String tool class.
  29.  *
  30.  * Tool class for the ConsoleTools package. Contains multi-byte encoding save
  31.  * string methods.
  32.  * 
  33.  * @package ConsoleTools
  34.  * @version //autogen//
  35.  * @access private
  36.  */
  37. class ezcConsoleStringTool
  38. {
  39.     /**
  40.      * Binary safe wordwrap() replacement.
  41.      *
  42.      * This method is a multi-byte encoding safe replacement for the PHP
  43.      * function wordwrap(). It mimics exactly the behavior of wordwrap(), but
  44.      * uses iconv_* functions with UTF-8 encoding. The parameters received by
  45.      * this method equal the parameters of {@link http://php.net/wordwrap}
  46.      * wordwrap()}. Note: Make sure to only hand UTF-8 encoded content to this
  47.      * method.
  48.      * 
  49.      * @param string $str 
  50.      * @param int $width 
  51.      * @param string $break 
  52.      * @param bool $cut 
  53.      * @return string|false
  54.      */
  55.     public function wordWrap$str$width 75$break "\n"$cut false )
  56.     {
  57.         $strlen   iconv_strlen$str'UTF-8' );
  58.         $breaklen iconv_strlen$break'UTF-8' );
  59.         $newtext  '';
  60.  
  61.         if $strlen === )
  62.         {
  63.             return '';
  64.         }
  65.     
  66.         if $breaklen === )
  67.         {
  68.             return false;
  69.         }
  70.  
  71.         if $width === && $cut )
  72.         {
  73.             return false;
  74.         }
  75.  
  76.         $laststart  $lastspace 0;
  77.         $breakstart iconv_substr$break01'UTF-8' );
  78.  
  79.         for $current 0$current $strlen$current++ )
  80.         {
  81.             $char iconv_substr$str$current1'UTF-8' );
  82.  
  83.             // Existing line break, copy line and  start a new one
  84.             if $char === $breakstart
  85.                  && $current $breaklen $strlen
  86.                  && iconv_substr$str$current$breaklen'UTF-8' === $break
  87.                )
  88.             {
  89.                 $newtext .= iconv_substr$str$laststart$current $laststart $breaklen'UTF-8' );
  90.                 $current += $breaklen 1;
  91.                 $laststart $lastspace $current 1;
  92.             }
  93.  
  94.             // Keep track of spaces, if line break is necessary, do it
  95.             else if $char === ' ' )
  96.             {
  97.                 if $current $laststart >= $width )
  98.                 {
  99.                     $newtext .= iconv_substr$str$laststart$current $laststart'UTF-8' )
  100.                         . $break;
  101.                     $laststart $current 1;
  102.                 }
  103.                 $lastspace $current;
  104.             }
  105.  
  106.             // Special cut case, if no space has been seen
  107.             else if $current $laststart >= $width
  108.                       && $cut && $laststart >= $lastspace
  109.                     )
  110.             {
  111.                 $newtext .= iconv_substr$str$laststart$current $laststart'UTF-8' )
  112.                     . $break;
  113.                 $laststart $lastspace $current;
  114.             }
  115.  
  116.  
  117.             // Usual case that line got longer than expected
  118.             else if $current $laststart >= $width
  119.                       && $laststart $lastspace
  120.                     )
  121.             {
  122.                 $newtext .= iconv_substr$str$laststart$lastspace $laststart'UTF-8' )
  123.                     . $break;
  124.                 // $laststart = $lastspace = $lastspace + 1;
  125.                 $laststart = ++$lastspace;
  126.             }
  127.         }
  128.  
  129.         // Rest of the string
  130.         if $laststart !== $current )
  131.         {
  132.             $newtext .= iconv_substr$str$laststart$current $laststart'UTF-8' );
  133.         }
  134.  
  135.         return $newtext;
  136.     }
  137.  
  138.     /**
  139.      * Binary safe str_pad() replacement.
  140.      *
  141.      * This method is a multi-byte encoding safe replacement for the PHP
  142.      * function str_pad().  It mimics exactly the behavior of str_pad(), but
  143.      * uses iconv_* functions with UTF-8 encoding. The parameters received by
  144.      * this method equal the parameters of {@link http://php.net/str_pad}
  145.      * str_pad()}. Note: Make sure to hand only UTF-8 encoded content to this
  146.      * method.
  147.      * 
  148.      * @param string $input 
  149.      * @param int $padLength 
  150.      * @param string $padString 
  151.      * @param int $padType 
  152.      * @return string 
  153.      */
  154.     public function strPad$input$padLength$padString ' '$padType STR_PAD_RIGHT )
  155.     {
  156.         $input     = (string) $input;
  157.  
  158.         $strLen    iconv_strlen$input'UTF-8' );
  159.         $padStrLen iconv_strlen$padString'UTF-8' );
  160.  
  161.         if $strLen >= $padLength )
  162.         {
  163.             return $input;
  164.         }
  165.  
  166.         if $padType === STR_PAD_BOTH )
  167.         {
  168.             return $this->strPad(
  169.                 $this->strPad(
  170.                     $input,
  171.                     $strLen ceil( ( $padLength $strLen ),
  172.                     $padString
  173.                 ),
  174.                 $padLength,
  175.                 $padString,
  176.                 STR_PAD_LEFT
  177.             );
  178.         }
  179.  
  180.         $fullStrRepeats = (int) ( ( $padLength $strLen $padStrLen );
  181.         $partlyPad iconv_substr(
  182.             $padString,
  183.             0,
  184.             ( ( $padLength $strLen $padStrLen )
  185.         );
  186.  
  187.         $padding str_repeat$padString$fullStrRepeats $partlyPad;
  188.  
  189.         switch $padType )
  190.         {
  191.             case STR_PAD_LEFT:
  192.                 return $padding $input;
  193.             case STR_PAD_RIGHT:
  194.             default:
  195.                 return $input $padding;
  196.         }
  197.     }
  198. }
  199.  
  200. ?>
Documentation generated by phpDocumentor 1.4.3