Example #1
0
 /**
  * Parses/maps the text json to the current class
  *
  * @param string $jsonText JSON data that represents the current class
  *
  * @return object A new object of the current class
  * @throws Telebot_InvalidJsonException When a required property is missing or unexpected data
  *     provided
  */
 public static function parse($jsonText)
 {
     // Create a mapper instance
     $mapper = JsonMapper::getInstance();
     // To throw an exception if there's a missing required property
     $mapper->bExceptionOnMissingData = true;
     try {
         // Map the parsed json to an object of the current class
         $obj = $mapper->map(json_decode($jsonText), self::instantiate(__CLASS__));
     } catch (\JsonMapper_Exception $e) {
         // This is usually caused when a missing required property is detected
         throw new Telebot_InvalidJsonException("Invalid telegram update json data: " . $e->getMessage(), 0, $e);
     }
     return $obj;
 }
Example #2
0
 /**
  * A setter for `$result`
  *
  * @param mixed $result
  */
 public function setResult($result)
 {
     $this->result = JsonMapper::getInstance()->map($result, Jsonable::instantiate($this->resultType));
 }
Example #3
0
 /**
  * @param $method
  * @param $resultClass
  * @param array $args
  *
  * @return Response
  * @throws \JsonMapper_Exception
  */
 protected function performMethod($method, $resultClass, $args = [])
 {
     $end_point = $this->api_url . '/' . $method;
     if (empty($args)) {
         $content_type = 'Content-type: application/x-www-form-urlencoded';
         $content = null;
     } else {
         $multipart_boundary = uniqid('---Telebot_FromBoundary');
         $content_type = 'Content-type: multipart/form-data' . '; boundary=' . $multipart_boundary;
         $content = self::renderFormDataContent($args, $multipart_boundary);
     }
     // Setting a custom stream context,
     $context = stream_context_create(['http' => ['ignore_errors' => true, 'method' => 'POST', 'header' => $content_type, 'content' => $content]]);
     // Call telegram api, and return the response
     $responseText = file_get_contents($end_point, false, $context);
     $response = JsonMapper::getInstance()->map(json_decode($responseText), Jsonable::instantiate(Response::class, [$resultClass]));
     return $response;
 }