Exemplo n.º 1
0
 /**
  * Generates class constructor and params list
  *
  * @param FieldDescriptor[] $fields Field descriptors list
  * @param CodeStringBuffer  $buffer Code buffer to write to
  *
  * @return null
  */
 private function _createClassConstructor($name, $fields, CodeStringBuffer $buffer)
 {
     $buffer->append('/* Field index constants */');
     foreach ($fields as $field) {
         $buffer->append('const ' . $field->getConstName() . ' = ' . $field->getNumber() . ';');
     }
     $buffer->newline();
     $buffer->append('/* @var string class type */')->append('protected static $class_type = "' . $name . '";');
     $buffer->newline();
     $buffer->append('/* @var array Field descriptors */')->append('protected static $fields = array(')->increaseIdentation();
     foreach ($fields as $field) {
         $type = $this->_getType($field);
         $buffer->append('self::' . $field->getConstName() . ' => array(')->increaseIdentation();
         if (!is_null($field->getDefault())) {
             if ($type == ProtobufMessage::PB_TYPE_STRING) {
                 $buffer->append('\'default\' => \'' . addslashes($field->getDefault()) . '\', ');
             } else {
                 if ($field->isProtobufScalarType()) {
                     $buffer->append('\'default\' => ' . $field->getDefault() . ', ');
                 } else {
                     $className = $this->_createClassName($field->getTypeDescriptor());
                     $buffer->append('\'default\' => ' . $className . '::' . $field->getDefault() . ', ');
                 }
             }
         }
         $buffer->append('\'name\' => \'' . addslashes($field->getName()) . '\'' . ',');
         if (!$field->isRepeated()) {
             $buffer->append('\'required\' => ' . ($field->isOptional() ? 'false' : 'true') . ',');
         } else {
             $buffer->append('\'repeated\' => true,');
         }
         if (is_int($type)) {
             $buffer->append('\'type\' => ' . $type . ',');
         } else {
             $buffer->append('\'type\' => \'' . $type . '\'');
         }
         $buffer->decreaseIdentation();
         $buffer->append('),');
     }
     $buffer->decreaseIdentation()->append(');')->newline();
     $comment = new CommentStringBuffer(self::TAB, self::EOL);
     $comment->append('Constructs new message container and clears its internal state')->newline()->appendParam('return', 'null');
     $buffer->append($comment)->append('public function __construct()')->append('{')->increaseIdentation()->append('$this->reset();')->decreaseIdentation()->append('}')->newline();
     $buffer->append($comment)->append('public function copy($obj)')->append('{')->increaseIdentation()->append('$this->values = $obj->values;')->decreaseIdentation()->append('}')->newline();
     $comment = new CommentStringBuffer(self::TAB, self::EOL);
     $comment->append('Clears message values and sets default ones')->newline()->appendParam('return', 'null');
     $buffer->append($comment)->append('public function reset()')->append('{')->increaseIdentation();
     foreach ($fields as $field) {
         $type = $this->_getType($field);
         if ($field->isRepeated()) {
             $buffer->append('$this->values[self::' . $field->getConstName() . '] = array();');
         } else {
             if ($field->isOptional() && !is_null($field->getDefault())) {
                 if ($field->isProtobufScalarType()) {
                     $buffer->append('$this->values[self::' . $field->getConstName() . '] = ' . $field->getDefault() . ';');
                 } else {
                     $className = $this->_createClassName($field->getTypeDescriptor());
                     $buffer->append('$this->values[self::' . $field->getConstName() . '] = ' . $className . '::' . $field->getDefault() . ';');
                 }
             } else {
                 $buffer->append('$this->values[self::' . $field->getConstName() . '] = null;');
             }
         }
     }
     $buffer->decreaseIdentation()->append('}')->newline();
 }