Example #1
0
 /**
  * Generates the method signatures for all methods (including dynamic ones)
  * 
  * @param  boolean $include_doc_comments  If the doc block comments for each method should be included
  * @return array  An associative array of method name => method signature
  */
 public function reflect($include_doc_comments = FALSE)
 {
     $signatures = array();
     $class = get_class($this);
     $table = fORM::tablize($class);
     $schema = fORMSchema::retrieve($class);
     $columns_info = $schema->getColumnInfo($table);
     foreach ($columns_info as $column => $column_info) {
         $camelized_column = fGrammar::camelize($column, TRUE);
         // Get and set methods
         $signature = '';
         if ($include_doc_comments) {
             $fixed_type = $column_info['type'];
             if ($fixed_type == 'blob') {
                 $fixed_type = 'string';
             }
             if ($fixed_type == 'date') {
                 $fixed_type = 'fDate';
             }
             if ($fixed_type == 'timestamp') {
                 $fixed_type = 'fTimestamp';
             }
             if ($fixed_type == 'time') {
                 $fixed_type = 'fTime';
             }
             $signature .= "/**\n";
             $signature .= " * Gets the current value of " . $column . "\n";
             $signature .= " * \n";
             $signature .= " * @return " . $fixed_type . "  The current value\n";
             $signature .= " */\n";
         }
         $get_method = 'get' . $camelized_column;
         $signature .= 'public function ' . $get_method . '()';
         $signatures[$get_method] = $signature;
         $signature = '';
         if ($include_doc_comments) {
             $fixed_type = $column_info['type'];
             if ($fixed_type == 'blob') {
                 $fixed_type = 'string';
             }
             if ($fixed_type == 'date') {
                 $fixed_type = 'fDate|string';
             }
             if ($fixed_type == 'timestamp') {
                 $fixed_type = 'fTimestamp|string';
             }
             if ($fixed_type == 'time') {
                 $fixed_type = 'fTime|string';
             }
             $signature .= "/**\n";
             $signature .= " * Sets the value for " . $column . "\n";
             $signature .= " * \n";
             $signature .= " * @param  " . $fixed_type . " \$" . $column . "  The new value\n";
             $signature .= " * @return fActiveRecord  The record object, to allow for method chaining\n";
             $signature .= " */\n";
         }
         $set_method = 'set' . $camelized_column;
         $signature .= 'public function ' . $set_method . '($' . $column . ')';
         $signatures[$set_method] = $signature;
         // The encode method
         $signature = '';
         if ($include_doc_comments) {
             $signature .= "/**\n";
             $signature .= " * Encodes the value of " . $column . " for output into an HTML form\n";
             $signature .= " * \n";
             if (in_array($column_info['type'], array('time', 'timestamp', 'date'))) {
                 $signature .= " * @param  string \$date_formatting_string  A date() compatible formatting string\n";
             }
             if (in_array($column_info['type'], array('float'))) {
                 $signature .= " * @param  integer \$decimal_places  The number of decimal places to include - if not specified will default to the precision of the column or the current value\n";
             }
             $signature .= " * @return string  The HTML form-ready value\n";
             $signature .= " */\n";
         }
         $encode_method = 'encode' . $camelized_column;
         $signature .= 'public function ' . $encode_method . '(';
         if (in_array($column_info['type'], array('time', 'timestamp', 'date'))) {
             $signature .= '$date_formatting_string';
         }
         if (in_array($column_info['type'], array('float'))) {
             $signature .= '$decimal_places=NULL';
         }
         $signature .= ')';
         $signatures[$encode_method] = $signature;
         // The prepare method
         $signature = '';
         if ($include_doc_comments) {
             $signature .= "/**\n";
             $signature .= " * Prepares the value of " . $column . " for output into HTML\n";
             $signature .= " * \n";
             if (in_array($column_info['type'], array('time', 'timestamp', 'date'))) {
                 $signature .= " * @param  string \$date_formatting_string  A date() compatible formatting string\n";
             }
             if (in_array($column_info['type'], array('float'))) {
                 $signature .= " * @param  integer \$decimal_places  The number of decimal places to include - if not specified will default to the precision of the column or the current value\n";
             }
             if (in_array($column_info['type'], array('varchar', 'char', 'text'))) {
                 $signature .= " * @param  boolean \$create_links_and_line_breaks  Will cause links to be automatically converted into [a] tags and line breaks into [br] tags \n";
             }
             $signature .= " * @return string  The HTML-ready value\n";
             $signature .= " */\n";
         }
         $prepare_method = 'prepare' . $camelized_column;
         $signature .= 'public function ' . $prepare_method . '(';
         if (in_array($column_info['type'], array('time', 'timestamp', 'date'))) {
             $signature .= '$date_formatting_string';
         }
         if (in_array($column_info['type'], array('float'))) {
             $signature .= '$decimal_places=NULL';
         }
         if (in_array($column_info['type'], array('varchar', 'char', 'text'))) {
             $signature .= '$create_links_and_line_breaks=FALSE';
         }
         $signature .= ')';
         $signatures[$prepare_method] = $signature;
         // The inspect method
         $signature = '';
         if ($include_doc_comments) {
             $signature .= "/**\n";
             $signature .= " * Returns metadata about " . $column . "\n";
             $signature .= " * \n";
             $elements = array('type', 'not_null', 'default', 'comment');
             if (in_array($column_info['type'], array('varchar', 'char', 'text'))) {
                 $elements[] = 'valid_values';
                 $elements[] = 'max_length';
             }
             if ($column_info['type'] == 'float') {
                 $elements[] = 'decimal_places';
             }
             if ($column_info['type'] == 'integer') {
                 $elements[] = 'auto_increment';
                 $elements[] = 'min_value';
                 $elements[] = 'max_value';
             }
             $signature .= " * @param  string \$element  The element to return. Must be one of: '" . join("', '", $elements) . "'.\n";
             $signature .= " * @return mixed  The metadata array or a single element\n";
             $signature .= " */\n";
         }
         $inspect_method = 'inspect' . $camelized_column;
         $signature .= 'public function ' . $inspect_method . '($element=NULL)';
         $signatures[$inspect_method] = $signature;
     }
     fORMRelated::reflect($class, $signatures, $include_doc_comments);
     fORM::callReflectCallbacks($class, $signatures, $include_doc_comments);
     $reflection = new ReflectionClass($class);
     $methods = $reflection->getMethods();
     foreach ($methods as $method) {
         $signature = '';
         if (!$method->isPublic() || $method->getName() == '__call') {
             continue;
         }
         if ($method->isFinal()) {
             $signature .= 'final ';
         }
         if ($method->isAbstract()) {
             $signature .= 'abstract ';
         }
         if ($method->isStatic()) {
             $signature .= 'static ';
         }
         $signature .= 'public function ';
         if ($method->returnsReference()) {
             $signature .= '&';
         }
         $signature .= $method->getName();
         $signature .= '(';
         $parameters = $method->getParameters();
         foreach ($parameters as $parameter) {
             if (substr($signature, -1) == '(') {
                 $signature .= '';
             } else {
                 $signature .= ', ';
             }
             if ($parameter->isArray()) {
                 $signature .= 'array ';
             }
             if ($parameter->getClass()) {
                 $signature .= $parameter->getClass()->getName() . ' ';
             }
             if ($parameter->isPassedByReference()) {
                 $signature .= '&';
             }
             $signature .= '$' . $parameter->getName();
             if ($parameter->isDefaultValueAvailable()) {
                 $val = var_export($parameter->getDefaultValue(), TRUE);
                 if ($val == 'true') {
                     $val = 'TRUE';
                 }
                 if ($val == 'false') {
                     $val = 'FALSE';
                 }
                 if (is_array($parameter->getDefaultValue())) {
                     $val = preg_replace('#array\\s+\\(\\s+#', 'array(', $val);
                     $val = preg_replace('#,(\\r)?\\n  #', ', ', $val);
                     $val = preg_replace('#,(\\r)?\\n\\)#', ')', $val);
                 }
                 $signature .= '=' . $val;
             }
         }
         $signature .= ')';
         if ($include_doc_comments) {
             $comment = $method->getDocComment();
             $comment = preg_replace('#^\\t+#m', '', $comment);
             $signature = $comment . "\n" . $signature;
         }
         $signatures[$method->getName()] = $signature;
     }
     ksort($signatures);
     return $signatures;
 }