Skip to content

Commit

Permalink
WIP: Passport implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
unreal4u committed Jul 26, 2018
1 parent 2feeaac commit 40760f7
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Abstracts/TelegramTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final protected function populateObject(array $data = []): TelegramTypes
{
foreach ($data as $key => $value) {
$candidateKey = null;
if (is_array($value)) {
if (\is_array($value)) {
$this->logger->debug('Array detected, mapping subobjects for key', ['key' => $key]);
$candidateKey = $this->mapSubObjects($key, $value);
}
Expand Down Expand Up @@ -73,7 +73,7 @@ protected function mapSubObjects(string $key, array $data): TelegramTypes
$key,
'https://github.com/unreal4u/telegram-api/issues'
), [
'object' => get_class($this),
'object' => \get_class($this),
'data' => $data,
]);
}
Expand Down
51 changes: 51 additions & 0 deletions src/Telegram/Methods/SetPassportDataErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Methods;

use Psr\Log\LoggerInterface;
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData;
use unreal4u\TelegramAPI\Telegram\Types\Custom\ResultBoolean;
use unreal4u\TelegramAPI\Telegram\Types\Passport\PassportElementError;

/**
* Use this method to change the title of a chat. Titles can't be changed for private chats. The bot must be an
* administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success
*
* Note: In regular groups (non-supergroups), this method will only work if the ‘All Members Are Admins’ setting is
* off in the target group
*
* Objects defined as-is july 2017
*
* @see https://core.telegram.org/bots/api#setchattitle
*/
class SetPassportDataErrors extends TelegramMethods
{
/**
* User identifier
* @var string
*/
public $user_id = 0;

/**
* A JSON-serialized array describing the errors
* @var PassportElementError[]
*/
public $errors = [];

public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
{
return new ResultBoolean($data->getResultBoolean(), $logger);
}

public function getMandatoryFields(): array
{
return [
'user_id',
'errors',
];
}
}
38 changes: 38 additions & 0 deletions src/Telegram/Types/Custom/PassportFileArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Types\Custom;

use unreal4u\TelegramAPI\Abstracts\CustomType;
use unreal4u\TelegramAPI\Telegram\Types\Passport\PassportFile;
use unreal4u\TelegramAPI\Telegram\Types\PhotoSize;
use unreal4u\TelegramAPI\Interfaces\CustomArrayType;
use Psr\Log\LoggerInterface;

/**
* Mockup class to generate a real telegram update representation
*/
class PassportFileArray extends CustomType implements CustomArrayType
{
public function __construct(array $data = null, LoggerInterface $logger = null)
{
if (count($data) !== 0) {
foreach ($data as $id => $passportFile) {
$this->data[$id] = new PassportFile($passportFile, $logger);
}
}
}

/**
* Traverses through our $data, yielding the result set
*
* @return PhotoSize[]
*/
public function traverseObject(): \Generator
{
foreach ($this->data as $passportFile) {
yield $passportFile;
}
}
}
31 changes: 31 additions & 0 deletions src/Telegram/Types/Passport/ElementError/DataField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Types\Passport\ElementError;

use unreal4u\TelegramAPI\Telegram\Types\Passport\PassportElementError;

/**
* This object represents information about an order
*
* Objects defined as-is may 2017
*
* @see https://core.telegram.org/bots/api#orderinfo
*/
class DataField extends PassportElementError
{
public $source = 'data';

/**
* Name of the data field which has the error
* @var string
*/
public $field_name = '';

/**
* Base64-encoded data hash
* @var string
*/
public $data_hash = '';
}
25 changes: 25 additions & 0 deletions src/Telegram/Types/Passport/ElementError/FrontSide.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Types\Passport\ElementError;

use unreal4u\TelegramAPI\Telegram\Types\Passport\PassportElementError;

/**
* This object represents information about an order
*
* Objects defined as-is may 2017
*
* @see https://core.telegram.org/bots/api#orderinfo
*/
class FrontSide extends PassportElementError
{
public $source = 'front_side';

/**
* Base64-encoded hash of the file with the front side of the document
* @var string
*/
public $file_hash = '';
}
25 changes: 25 additions & 0 deletions src/Telegram/Types/Passport/ElementError/ReverseSide.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Types\Passport\ElementError;

use unreal4u\TelegramAPI\Telegram\Types\Passport\PassportElementError;

/**
* This object represents information about an order
*
* Objects defined as-is may 2017
*
* @see https://core.telegram.org/bots/api#orderinfo
*/
class ReverseSide extends PassportElementError
{
public $source = 'reverse_side';

/**
* Base64-encoded hash of the file with the front side of the document
* @var string
*/
public $file_hash = '';
}
25 changes: 25 additions & 0 deletions src/Telegram/Types/Passport/ElementError/Selfie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Types\Passport\ElementError;

use unreal4u\TelegramAPI\Telegram\Types\Passport\PassportElementError;

/**
* This object represents information about an order
*
* Objects defined as-is july 2018
*
* @see https://core.telegram.org/bots/api#passportelementerrorselfie
*/
class Selfie extends PassportElementError
{
public $source = 'selfie';

/**
* Base64-encoded hash of the file with the front side of the document
* @var string
*/
public $file_hash = '';
}
36 changes: 36 additions & 0 deletions src/Telegram/Types/Passport/EncryptedCredentials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Types\Passport;

use unreal4u\TelegramAPI\Abstracts\TelegramTypes;

/**
* This object represents information about an order
*
* Objects defined as-is may 2017
*
* @see https://core.telegram.org/bots/api#orderinfo
*/
class EncryptedCredentials extends TelegramTypes
{
/**
* Base64-encoded encrypted JSON-serialized data with unique user's payload, data hashes and secrets required for
* EncryptedPassportElement decryption and authentication
* @var string
*/
public $data = '';

/**
* Base64-encoded data hash for data authentication
* @var string
*/
public $hash = '';

/**
* Base64-encoded secret, encrypted with the bot's public RSA key, required for data decryption
* @var string
*/
public $secret = '';
}
85 changes: 85 additions & 0 deletions src/Telegram/Types/Passport/EncryptedPassportElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Types\Passport;

use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
use unreal4u\TelegramAPI\Telegram\Types\Custom\PassportFileArray;

/**
* This object represents information about an order
*
* Objects defined as-is may 2017
*
* @see https://core.telegram.org/bots/api#orderinfo
*/
class EncryptedPassportElement extends TelegramTypes
{
/**
* Element type. One of "personal_details", "passport", "driver_license", "identity_card", "internal_passport",
* "address", "utility_bill", "bank_statement", "rental_agreement", "passport_registration",
* "temporary_registration", "phone_number", "email".
* @var string
*/
public $type = '';

/**
* Optional. Base64-encoded encrypted Telegram Passport element data provided by the user, available for
* "personal_details", "passport", "driver_license", "identity_card", "identity_passport" and "address" types. Can
* be decrypted and verified using the accompanying EncryptedCredentials.
* @var string
*/
public $data = '';

/**
* Optional. User's verified phone number, available only for "phone_number" type
* @var string
*/
public $phone_number = '';

/**
* Optional. User's verified email address, available only for "email" type
* @var string
*/
public $email = '';

/**
* Optional. Array of encrypted files with documents provided by the user, available for "utility_bill",
* "bank_statement", "rental_agreement", "passport_registration" and "temporary_registration" types. Files can be
* decrypted and verified using the accompanying EncryptedCredentials.
* @var PassportFileArray
*/
public $files;

/**
* Optional. Encrypted file with the front side of the document, provided by the user. Available for "passport",
* "driver_license", "identity_card" and "internal_passport". The file can be decrypted and verified using the
* accompanying EncryptedCredentials.
* @var PassportFile
*/
public $front_side;

/**
* Optional. Encrypted file with the reverse side of the document, provided by the user. Available for
* "driver_license" and "identity_card". The file can be decrypted and verified using the accompanying
* EncryptedCredentials.
* @var PassportFile
*/
public $reverse_side;

/**
* Optional. Encrypted file with the selfie of the user holding a document, provided by the user; available for
* "passport", "driver_license", "identity_card" and "internal_passport". The file can be decrypted and verified
* using the accompanying EncryptedCredentials.
* @var PassportFile
*/
public $selfie;

public function getMandatoryFields(): array
{
return [
'type',
];
}
}
41 changes: 41 additions & 0 deletions src/Telegram/Types/Passport/PassportData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Types\Passport;

use unreal4u\TelegramAPI\Abstracts\TelegramTypes;

/**
* This object represents information about an order
*
* Objects defined as-is may 2017
*
* @see https://core.telegram.org/bots/api#orderinfo
*/
class PassportData extends TelegramTypes
{
/**
* Array with information about documents and other Telegram Passport elements that was shared with the bot
* @var string
*/
public $data;

/**
* Encrypted credentials required to decrypt the data
* @var EncryptedCredentials
*/
public $credentials;

protected function mapSubObjects(string $key, array $data): TelegramTypes
{
switch ($key) {
case 'data':
// TODO
case 'credentials':
return new EncryptedCredentials($data, $this->logger);
}

return parent::mapSubObjects($key, $data);
}
}
Loading

0 comments on commit 40760f7

Please sign in to comment.