Format.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class Format extends StaticFactory
00016     {
00017         // dumb and straigh beautifier
00018         public static function indentize($data)
00019         {
00020             $out    = null;
00021             
00022             $indent = 0;
00023             $chain  = 1;
00024             $first  = true; // for schema.php-like files
00025             
00026             foreach (explode("\n", $data) as $string) {
00027                 $string = preg_replace('~^[\t]+~', null, rtrim($string))."\n";
00028                 
00029                 if ($string == "}\n") {
00030                     $indent -= $chain;
00031                     $chain = 1;
00032                 } elseif ($string == ")->\n")
00033                     --$indent;
00034                 elseif ($string == ")\n")
00035                     --$indent;
00036                 elseif ($string == ");\n")
00037                     --$indent;
00038                 elseif ($string == "),\n")
00039                     --$indent;
00040                 elseif ($string == "?>\n")
00041                     $indent = 0;
00042                 elseif ($string[0] == '?')
00043                     ++$indent;
00044                 
00045                 if ($string <> "\n") {
00046                     if ($indent > 0)
00047                         $out .= str_pad(null, $indent, "\t", STR_PAD_LEFT).$string;
00048                     else
00049                         $out .= $string;
00050                 }
00051 
00052                 if (substr($string, -2 ,2) == "{\n")
00053                     ++$indent;
00054                 elseif (
00055                     substr_count($string, "'") == 2
00056                     && substr($string, -3, 3) == "=>\n"
00057                 ) {
00058                     ++$indent;
00059                     ++$chain;
00060                 } elseif (
00061                     $string[0] == '$'
00062                     && (
00063                         substr($string, -2, 2) == "=\n"
00064                         || substr($string, -3, 3) == "->\n"
00065                     )
00066                 ) {
00067                     ++$indent;
00068                     ++$chain;
00069                 } elseif (substr($string, -2, 2) == "(\n")
00070                     ++$indent;
00071                 elseif ($string == "\n" && $indent == 0) {
00072                     ++$indent;
00073                 } elseif ($string == "return\n") {
00074                     ++$indent;
00075                     ++$chain;
00076                 } elseif ($string == "\n" && $chain > 1) {
00077                     $indent -= $chain - 1;
00078                     $chain = 1;
00079                 } elseif ($string[0] == ':') {
00080                     --$indent;
00081                 } elseif ($string == "),\n")
00082                     --$indent;
00083                 
00084                 if ($string == "\n") {
00085                     if (!$first && ($indent > 0)) {
00086                         $out .= str_pad(null, $indent, "\t", STR_PAD_LEFT).$string;
00087                     } else {
00088                         $out .= $string;
00089                         $first = false;
00090                     }
00091                 }
00092             }
00093             
00094             return $out;
00095         }
00096     }
00097 ?>