id = (int) $json_dict['id']; $this->type = $json_dict['type']; $this->predicate = $json_dict['predicate']; $this->params = []; foreach ($json_dict['params'] as $param) { if (($param['type'] == 'Vector')) { $param['type'] = 'Vector t'; $param['subtype'] = 'long'; } elseif (($param['type'] == 'vector<%Message>')) { $param['type'] = 'vector'; $param['subtype'] = 'message'; } elseif (($param['type'] == 'vector')) { $param['type'] = 'vector'; $param['subtype'] = 'future_salt'; } else { $param['subtype'] = null; } $this->params[] = $param; } } } class TlMethod { public function __construct($json_dict) { $this->id = (int) $json_dict['id']; $this->type = $json_dict['type']; $this->method = $json_dict['method']; $this->params = $json_dict['params']; } } class TLObject extends ArrayObject { public function __construct($tl_elem) { parent::__construct(); $this->name = $tl_elem->predicate; } } class TL { public function __construct($filename) { $TL_dict = json_decode(file_get_contents($filename), true); $this->constructors = $TL_dict['constructors']; $this->constructor_id = []; $this->constructor_type = []; foreach ($this->constructors as $elem) { $z = new TlConstructor($elem); $this->constructor_id[$z->id] = $z; $this->constructor_type[$z->predicate] = $z; } $this->methods = $TL_dict['methods']; $this->method_id = []; $this->method_name = []; foreach ($this->methods as $elem) { $z = new TlMethod($elem); $this->method_id[$z->id] = $z; $this->method_name[$z->method] = $z; } $this->struct = new \danog\PHP\StructClass(); } public function serialize_obj($type_, $kwargs) { $bytes_io = ''; if (isset($this->constructor_type[$type_])) { $tl_constructor = $this->constructor_type[$type_]; } else { throw new Exception(sprintf('Could not extract type: %s', $type_)); } $bytes_io .= $this->struct->pack('id); foreach ($tl_constructor->params as $arg) { $bytes_io .= $this->serialize_param($arg['type'], $kwargs[$arg['name']]); } return $bytes_io; } public function serialize_method($type_, $kwargs) { $bytes_io = ''; if (isset($this->method_name[$type_])) { $tl_method = $this->method_name[$type_]; } else { throw new Exception(sprintf('Could not extract type: %s', $type_)); } $bytes_io .= $this->struct->pack('id); foreach ($tl_method->params as $arg) { $bytes_io .= $this->serialize_param($arg['type'], $kwargs[$arg['name']]); } return $bytes_io; } public function serialize_param($type_, $value) { switch ($type_) { case 'int': if (!is_numeric($value)) { throw new Exception("serialize_param: given value isn't numeric"); } if (!(strlen(decbin($value)) <= 32)) { throw new Exception('Given value is too long.'); } return $this->struct->pack('struct->pack('struct->pack('struct->pack('struct->unpack('struct->unpack('struct->unpack('struct->unpack('struct->unpack(' 254) { throw new Exception('Length is too big'); } if ($l == 254) { $long_len = $this->struct->unpack('struct->unpack('deserialize($bytes_io, $subtype); } break; default: if (isset($this->constructor_type[$type_])) { $tl_elem = $this->constructor_type[$type_]; } else { $Idata = fread($bytes_io, 4); $i = $this->struct->unpack('constructor_id[$i])) { $tl_elem = $this->constructor_id[$i]; } else { throw new Exception(sprintf('Could not extract type: %s', $type_)); } } $base_boxed_types = ['Vector t', 'Int', 'Long', 'Double', 'String', 'Int128', 'Int256']; if (in_array($tl_elem->type, $base_boxed_types)) { $x = $this->deserialize($bytes_io, $tl_elem->predicate, $subtype); } else { $x = new TLObject($tl_elem); foreach ($tl_elem->params as $arg) { $x[$arg['name']] = $this->deserialize($bytes_io, $arg['type'], $arg['subtype']); } } break; } return $x; } }