source: Dev/trunk/src/client/util/docscripts/lib/parser/DojoParameter.php

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 4.8 KB
Line 
1<?php
2
3require_once('DojoString.php');
4require_once('DojoNull.php');
5require_once('DojoBoolean.php');
6require_once('DojoVariable.php');
7require_once('DojoObject.php');
8require_once('DojoFunctionDeclare.php');
9require_once('DojoBlock.php');
10
11class DojoParameter extends DojoBlock
12{
13  private $object = 'DojoParameter';
14
15  private $terminator = ')';
16
17  private $parameter_value;
18  private $parameter_type;
19
20  public function __construct($package, $line_number = false, $position = false, $terminator = ')'){
21    parent::__construct($package, $line_number, $position);
22    $this->terminator = $terminator;
23  }
24
25  public function __destruct() {
26    parent::__destruct();
27    unset($this->parameter_value);
28    unset($this->parameter_type);
29  }
30
31  public function exists(){
32    return !empty($this->parameter_value);
33  }
34
35  public function isA($class){
36    if (!$this->parameter_value) {
37      $this->getValue();
38    }
39    if ($this->parameter_value instanceof $class) {
40      return true;
41    }
42    return false;
43  }
44
45  public function build(){
46    if (!$this->start) {
47      debug_print_backtrace();
48      die("DojoParameter->build() used before setting a start position");
49    }
50
51    $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1], false, false, true);
52    list($line_number, $position) = Text::findTermination($lines, ',' . $this->terminator, '(){}[]');
53    $this->setEnd($line_number, $position);
54    return $this->end;
55  }
56
57  public function getString(){
58    if ($this->isA(DojoString)) {
59      return $this->parameter_value->getValue();
60    }
61    return '';
62  }
63
64  public function getObject(){
65    if ($this->isA(DojoObject)) {
66      return $this->parameter_value;
67    }
68    return new DojoObject($this->package);
69  }
70
71  public function getFunction(){
72    if ($this->isA(DojoFunctionDeclare)) {
73      $this->parameter_value->build();
74      return $this->parameter_value;
75    }
76    return new DojoFunctionDeclare($this->package);
77  }
78
79  public function getArray(){
80    if ($this->isA(DojoArray)) {
81      $this->parameter_value->build();
82      return $this->parameter_value;
83    }
84    require_once('DojoArray.php'); // Chase condition
85    return new DojoArray($this->package);
86  }
87
88  public function getVariable(){
89    if ($this->isA(DojoVariable)) {
90      return $this->parameter_value->getValue();
91    }
92    return new DojoVariable('');
93  }
94
95  public function setVariable($value){
96    if ($this->isA(DojoVariable)) {
97      $this->parameter_value->setValue($value);
98    }
99  }
100
101  public function getValue(){
102    if ($this->parameter_value) {
103      return $this->parameter_value;
104    }
105
106    $lines = Text::chop($this->package->getSource(), $this->start[0], $this->start[1], $this->end[0], $this->end[1], true);
107    $parameter_value = Text::trim(implode("\n", $lines));
108
109    if ($parameter_value{0} == '"' || $parameter_value{0} == "'") {
110      $this->parameter_value = new DojoString($parameter_value);
111    }
112    elseif ($parameter_value{0} == '{') {
113      foreach ($lines as $line_number => $line) {
114        if (($position = strpos($line, '{')) !== false) {
115          $this->parameter_value = new DojoObject($this->package, $line_number, $position);
116          break;
117        }
118      }
119    }
120    elseif (strpos($parameter_value, 'function') === 0) {
121      foreach ($lines as $line_number => $line) {
122        if (($position = strpos($line, 'function')) !== false) {
123          $this->parameter_value = new DojoFunctionDeclare($this->package, $line_number, $position);
124          break;
125        }
126      }
127    }
128    elseif ($parameter_value{0} == '[') {
129      foreach ($lines as $line_number => $line) {
130        if (($position = strpos($line, '[')) !== false) {
131          require_once('DojoArray.php'); // Chase condition
132          $this->parameter_value = new DojoArray($this->package, $line_number, $position);
133          break;
134        }
135      }
136    }
137    elseif ($parameter_value == 'null' || $parameter_value == 'undefined') {
138        $this->parameter_value = new DojoNull($parameter_value);
139    }
140    elseif ($parameter_value == 'true' || $parameter_value == 'false') {
141      $this->parameter_value = new DojoBoolean($parameter_value);
142    }
143    elseif (!empty($parameter_value)) {
144      $this->parameter_value = new DojoVariable($parameter_value);
145    }
146
147    return $this->parameter_value;
148  }
149 
150  public function getType(){
151    if ($this->parameter_type) {
152      return $this->parameter_type;
153    }
154
155    $type = array();
156    $lines = Text::chop($this->package->getSource(), $this->start[0], $this->start[1], $this->end[0], $this->end[1], true);
157    foreach ($lines as $line) {
158      list($first, $middle, $last, $data, $multiline) = Text::findComments($line, $multiline);
159      $type = array_merge($type, array($first, $middle, $last));
160    }
161
162    return $this->parameter_type = implode(' ', array_diff($type, array('')));
163  }
164}
165 
166?>
Note: See TracBrowser for help on using the repository browser.