|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Supasta\FnsService\Entity; |
| 4 | + |
| 5 | +use DateTime; |
| 6 | +use Supasta\FnsService\Contract\FnsEntityInterface; |
| 7 | +use Supasta\FnsService\Contract\FullName as ContractFullName; |
| 8 | + |
| 9 | +/** |
| 10 | + * Class FnsEntity |
| 11 | + */ |
| 12 | +class FnsEntity implements FnsEntityInterface |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var FullName The full name of the entity. |
| 16 | + */ |
| 17 | + private ContractFullName $fullName; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var DateTime The birth date of the entity. |
| 21 | + */ |
| 22 | + private DateTime $birthDay; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var int The document type of the entity. |
| 26 | + */ |
| 27 | + private $docType = 10; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var string|int The identification document of the entity. |
| 31 | + */ |
| 32 | + private $identifyDocument; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var array The query string parameters for the FNS service. |
| 36 | + */ |
| 37 | + private $queryString; |
| 38 | + |
| 39 | + /** |
| 40 | + * FnsEntity constructor. |
| 41 | + * |
| 42 | + * @param FullName $fullName The full name of the entity. |
| 43 | + * @param DateTime $birthDay The birth date of the entity. |
| 44 | + * @param FnsDocumentType $docType The document type of the entity. |
| 45 | + * @param mixed $identifyDocument The identification document of the entity. |
| 46 | + */ |
| 47 | + public function __construct(ContractFullName $fullName, DateTime $birthDay, $identifyDocument) |
| 48 | + { |
| 49 | + $this->fullName = $fullName; |
| 50 | + $this->birthDay = $birthDay; |
| 51 | + $this->identifyDocument = $identifyDocument; |
| 52 | + $this->prepare(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Prepare the query string parameters for the FNS service. |
| 57 | + * |
| 58 | + * @return self |
| 59 | + */ |
| 60 | + protected function prepare(): self |
| 61 | + { |
| 62 | + $this->queryString = [ |
| 63 | + 'fam' => $this->fullName->lastName, |
| 64 | + 'nam' => $this->fullName->firstName, |
| 65 | + 'otch' => $this->fullName->patronymic, |
| 66 | + 'docno' => $this->identifyDocument, |
| 67 | + 'c' => 'find', |
| 68 | + 'captcha' => '', |
| 69 | + 'captchaToken' => '', |
| 70 | + 'bdate' => $this->birthDay->format('d.m.Y'), |
| 71 | + 'doctype' => $this->docType |
| 72 | + ]; |
| 73 | + |
| 74 | + if (!$this->fullName->patronymic) { |
| 75 | + $this->queryString['opt_otch'] = '1'; |
| 76 | + } |
| 77 | + |
| 78 | + return $this; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * URL-encode the query string parameters. |
| 83 | + * |
| 84 | + * @return string The URL-encoded query string. |
| 85 | + */ |
| 86 | + public function urlEncode(): string |
| 87 | + { |
| 88 | + return http_build_query($this->queryString); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get the query string parameters as an array. |
| 93 | + * |
| 94 | + * @return array The query string parameters as an array. |
| 95 | + */ |
| 96 | + public function toArray(): array |
| 97 | + { |
| 98 | + return $this->queryString; |
| 99 | + } |
| 100 | +} |
0 commit comments