Exemple #1
0
 /**
  * @param string $data
  */
 public function __construct($data = null)
 {
     // Cache the descriptor instance
     $this->_descriptor = Protobuf::getRegistry()->getDescriptor($this);
     // Assign default values to extensions
     foreach ($this->_descriptor->getFields() as $f) {
         if ($f->isExtension() && $f->hasDefault()) {
             $this->_extensions[$f->getName()] = $f->getDefault();
         }
     }
     if (NULL !== $data) {
         $this->parse($data);
     }
 }
Exemple #2
0
 protected function _describe(Protobuf\Descriptor $descriptor)
 {
     $name = $descriptor->getName();
     if (isset($this->_resources[$name])) {
         return $this->_resources[$name];
     }
     // Create the message descriptor resource
     $res = \protobuf_desc_message($name);
     // Add the resource immediately in the dictionary to support cyclic references
     $this->_resources[$name] = $res;
     $lazy = $this->getOption('lazy');
     // Iterate over all the fields to setup the message
     foreach ($descriptor->getFields() as $field) {
         $type = $field->getType();
         // Nested messages need to be populated first
         if ($type === Protobuf\Protobuf::TYPE_MESSAGE) {
             // When in lazy decoding mode we handle nested messages as binary fields
             if ($lazy) {
                 $type = Protobuf\Protobuf::TYPE_BYTES;
             } else {
                 // Try to obtain the message descriptor resource for this field
                 $descr = Protobuf\Protobuf::getRegistry()->getDescriptor($field->getReference());
                 if (!$descr) {
                     throw new \RuntimeException('Unable to find a descriptor for message "' . $field->getReference() . '"');
                 }
                 $nested = $this->_describe($descr);
             }
         }
         //printf("N: %d R: %d T: %d Name: %s P: %d\n", $field->getNumber(), $field->getRule(), $type, $field->getName(), $field->isPacked() ? PROTOBUF_FLAG_PACKED : 0);
         // Append the field definition to the message
         \protobuf_desc_field($res, $field->getNumber(), $field->getRule(), $type, $field->getName(), $field->isPacked() ? PROTOBUF_FLAG_PACKED : 0, $type === Protobuf\Protobuf::TYPE_MESSAGE ? $nested : NULL);
     }
     return $res;
 }
Exemple #3
0
 /**
  * @param mixed $data
  */
 public function __construct($data = null)
 {
     // Cache the descriptor instance for this class
     if (!static::$__descriptor) {
         static::$__descriptor = Protobuf::getRegistry()->getDescriptor($this);
     }
     // Alias the descriptor to this object instance since it's faster to access
     $this->_descriptor = static::$__descriptor;
     // BACKWARDS COMPATIBILITY: Unset public properties
     $publicfields = get_object_vars($this);
     foreach ($this->_descriptor->getFields() as $field) {
         $name = $field->name;
         if (array_key_exists($name, $publicfields)) {
             //trigger_error('DESTROYING PUBLIC FIELD: '  . $name);
             unset($this->{$name});
         }
     }
     if (NULL !== $data) {
         $this->parse($data);
     }
 }