Function.php 3.64 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
<?php

/**
* 
* Parses for an API function documentation block.
* 
* @category Text
* 
* @package Text_Wiki
* 
* @author Paul M. Jones <pmjones@php.net>
* 
* @license LGPL
* 
* @version $Id: Function.php,v 1.3 2005/02/23 17:38:29 pmjones Exp $
* 
*/

/**
* 
* Parses for an API function documentation block.
*
* @category Text
* 
* @package Text_Wiki
* 
* @author Paul M. Jones <pmjones@php.net>
* 
*/

class Text_Wiki_Parse_Function extends Text_Wiki_Parse {

    var $regex = '/^(\<function\>)\n(.+)\n(\<\/function\>)(\s|$)/Umsi';
    
    function process(&$matches)
    {
        // default options
        $opts = array(
            'name' => null,
            'access' => null,
            'return' => null,
            'params' => array(),
            'throws' => array()
        );
        
        // split apart the markup lines and loop through them
        $lines = explode("\n", $matches[2]);
        foreach ($lines as $line) {
            
            // skip blank lines
            if (trim($line) == '') {
                continue;
            }
            
            // find the first ':' on the line; the left part is the 
            // type, the right part is the value. skip lines without
            // a ':' on them.
            $pos = strpos($line, ':');
            if ($pos === false) {
                continue;
            }
            
            // $type is the line type: name, access, return, param, throws
            // 012345678901234
            // name: something
            $type = trim(substr($line, 0, $pos));
            $val = trim(substr($line, $pos+1));
            
            switch($type) {
            
            case 'a':
            case 'access':
                $opts['access'] = $val;
                break;
                
            case 'n':
            case 'name':
                $opts['name'] = $val;
                break;
                
            case 'p':
            case 'param':
                $tmp = explode(',', $val);
                $k = count($tmp);
                if ($k == 1) {
                    $opts['params'][] = array(
                        'type' => $tmp[0],
                        'descr' => null,
                        'default' => null
                    );
                } elseif ($k == 2) {
                    $opts['params'][] = array(
                        'type' => $tmp[0],
                        'descr' => $tmp[1],
                        'default' => null
                    );
                } else {
                    $opts['params'][] = array(
                        'type' => $tmp[0],
                        'descr' => $tmp[1],
                        'default' => $tmp[2]
                    );
                }
                break;
            
            case 'r':
            case 'return':
            case 'returns':
                $opts['return'] = $val;
                break;
            
            case 't':
            case 'throws':
                $tmp = explode(',', $val);
                $k = count($tmp);
                if ($k == 1) {
                    $opts['throws'][] = array(
                        'type' => $tmp[0],
                        'descr' => null
                    );
                } else {
                    $opts['throws'][] = array(
                        'type' => $tmp[0],
                        'descr' => $tmp[1]
                    );
                }
                break;
        
            default:
                $opts[$type] = $val;
                break;
            
            }
        }
        
        // add the token back in place
        return $this->wiki->addToken($this->rule, $opts) . $matches[4];
    }
}

?>