Skip to content

Commit

Permalink
added full lawyer building
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Pinchuk committed Sep 18, 2019
1 parent 7655f98 commit 8fa1628
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 20 deletions.
40 changes: 40 additions & 0 deletions src/Client.php
@@ -0,0 +1,40 @@
<?php

namespace SomeWork\Minjust;

use GuzzleHttp\RequestOptions;

class Client
{
private const SERVICE_URL = 'http://lawyers.minjust.ru';

/**
* @var \GuzzleHttp\Client
*/
private $client;

public function __construct(\GuzzleHttp\Client $client)
{
$this->client = $client;
}

public function list(array $formData = []): string
{
return $this
->client
->request('GET', static::SERVICE_URL . '/Lawyers', [
RequestOptions::QUERY => $formData,
])
->getBody()
->getContents();
}

public function detail(string $url)
{
return $this
->client
->request('GET', static::SERVICE_URL . $url)
->getBody()
->getContents();
}
}
6 changes: 6 additions & 0 deletions src/Entity/FullLawyer.php
Expand Up @@ -17,6 +17,12 @@ class FullLawyer extends Lawyer
public static function init(Lawyer $lawyer): FullLawyer
{
return (new self())
->loadFromLawyer($lawyer);
}

public function loadFromLawyer(Lawyer $lawyer): self
{
return $this
->setRegisterNumber($lawyer->getRegisterNumber())
->setFullName($lawyer->getFullName())
->setUrl($lawyer->getUrl())
Expand Down
29 changes: 29 additions & 0 deletions src/FindRequest.php
Expand Up @@ -5,12 +5,19 @@
class FindRequest
{
private const FULL_NAME = 'lawyername';

private const REGISTER_NUMBER = 'regnumber';

private const CERTIFICATE_NUMBER = 'lawicard';

private const STATUS = 'lawstatus';

private const FORM_OF_LEGAL_PRACTICE = 'formation';

private const TERRITORIAL_SUBJECT = 'lawregion';

private const MAX = 'max';

private const OFFSET = 'offset';

private const MAX_VALUE_MAX = 100;
Expand Down Expand Up @@ -45,6 +52,8 @@ class FindRequest
*/
protected $territorialSubject;

protected $fullData = false;

/**
* @var int
*/
Expand Down Expand Up @@ -233,4 +242,24 @@ public function setOffset(int $offset): FindRequest

return $this;
}

/**
* @param bool $fullData
*
* @return FindRequest
*/
public function setFullData(bool $fullData = true): FindRequest
{
$this->fullData = $fullData;

return $this;
}

/**
* @return bool
*/
public function isFullData(): bool
{
return $this->fullData;
}
}
27 changes: 26 additions & 1 deletion src/FindResponse.php
Expand Up @@ -5,10 +5,15 @@
class FindResponse
{
/**
* @var array
* @var \SomeWork\Minjust\Entity\Lawyer[]
*/
protected $elements = [];

/**
* @var \Generator|\SomeWork\Minjust\Entity\FullLawyer[]
*/
protected $fullElements;

/**
* @var int
*/
Expand Down Expand Up @@ -115,4 +120,24 @@ public function addElement(array $element): self

return $this;
}

/**
* @return \Generator|\SomeWork\Minjust\Entity\FullLawyer[]
*/
public function getFullElements()
{
return $this->fullElements;
}

/**
* @param \Generator|\SomeWork\Minjust\Entity\FullLawyer[] $fullElements
*
* @return static
*/
public function setFullElements(\Generator $fullElements): self
{
$this->fullElements = $fullElements;

return $this;
}
}
41 changes: 37 additions & 4 deletions src/Parser.php
Expand Up @@ -2,9 +2,11 @@

namespace SomeWork\Minjust;

use PHPHtmlParser\Dom;
use SomeWork\Minjust\Entity\FullLawyer;
use SomeWork\Minjust\Entity\LawFormation;
use SomeWork\Minjust\Entity\Lawyer;
use SomeWork\Minjust\Strategy\ParseStrategyInterface;
use PHPHtmlParser\Dom;

class Parser
{
Expand All @@ -28,7 +30,7 @@ public function __construct(array $strategies)
* @throws \PHPHtmlParser\Exceptions\NotLoadedException
* @throws \PHPHtmlParser\Exceptions\StrictException
*/
public function buildResponse(string $body): FindResponse
public function buildListResponse(string $body): FindResponse
{
$dom = new Dom();
$dom->load($body);
Expand All @@ -38,7 +40,7 @@ public function buildResponse(string $body): FindResponse
$findResponse
->setPage($strategy->getPage($dom))
->setTotalPage($strategy->getTotalPage($dom))
->setElements($this->getElements($dom));
->setElements($this->getListElements($dom));

return $findResponse;
}
Expand All @@ -61,7 +63,7 @@ protected function guessStrategy(Dom $dom)
* @throws \PHPHtmlParser\Exceptions\ChildNotFoundException
* @throws \PHPHtmlParser\Exceptions\NotLoadedException
*/
public function getElements(Dom $dom): array
protected function getListElements(Dom $dom): array
{
$data = [];
/**
Expand All @@ -87,4 +89,35 @@ public function getElements(Dom $dom): array

return $data;
}

public function buildFullLawyer(Lawyer $lawyer, string $body): FullLawyer
{
$dom = new Dom();
$dom->load($body);

return $this
->getDetailLawyer($dom)
->loadFromLawyer($lawyer);
}

protected function getDetailLawyer(Dom $dom): FullLawyer
{
$nodes = $dom->find('.floating > p.row')->toArray();

return (new FullLawyer())
->setLawFormation($this->getLawFormation($nodes))
->setChamberOfLaw(trim($nodes[5]->text()));
}

protected function getLawFormation(array $nodes): ?LawFormation
{
$formation = (new LawFormation())
->setOrganizationalForm($nodes[7]->text())
->setName($nodes[9]->text())
->setAddress($nodes[11]->text())
->setPhone($nodes[13]->text())
->setEmail($nodes[15]->text());

return $formation->getOrganizationalForm() ? $formation : null;
}
}
49 changes: 34 additions & 15 deletions src/Service.php
Expand Up @@ -2,13 +2,8 @@

namespace SomeWork\Minjust;

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;

class Service
{
private const SERVICE_URL = 'http://lawyers.minjust.ru/Lawyers';

/**
* @var Client
*/
Expand Down Expand Up @@ -38,7 +33,7 @@ public function findFromTo(FindRequest $findRequest, int $startPage = 1, int $en
$findRequest->setOffset(($startPage - 1) * $findRequest->getMax());
$findResponse = $this->find($findRequest);

yield from $findResponse->getElements();
yield from $findRequest->isFullData() ? $findResponse->getFullElements() : $findResponse->getElements();
if ($findResponse->getTotalPage() === 1) {
return;
}
Expand All @@ -48,25 +43,49 @@ public function findFromTo(FindRequest $findRequest, int $startPage = 1, int $en
for ($i = $findResponse->getPage(); $i < $endPage; $i++) {
$endPage = $endPage < $findResponse->getTotalPage() ? $endPage : $findResponse->getTotalPage();
$findRequest->setOffset($i * $findRequest->getMax());
yield from $this->find($findRequest)->getElements();
yield from $findRequest->isFullData() ?
$this->find($findRequest)->getFullElements() :
$this->find($findRequest)->getElements();
}
}

/**
* @param FindRequest $findRequest
*
* @return FindResponse
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \PHPHtmlParser\Exceptions\ChildNotFoundException
* @throws \PHPHtmlParser\Exceptions\CircularException
* @throws \PHPHtmlParser\Exceptions\CurlException
* @throws \PHPHtmlParser\Exceptions\NotLoadedException
* @throws \PHPHtmlParser\Exceptions\StrictException
*/
public function find(FindRequest $findRequest): FindResponse
{
return $this->parser->buildResponse(
$this
->client
->request('GET', static::SERVICE_URL, [
RequestOptions::QUERY => $findRequest->getFormData(),
])
->getBody()
$response = $this->parser->buildListResponse(
$this->client->list($findRequest->getFormData())
);

$response->setFullElements(
$this->loadDetails(
$response->getElements()
)
);

return $response;
}

/**
* @param \SomeWork\Minjust\Entity\Lawyer[] $lawyers
*
* @return \Generator
*/
protected function loadDetails(array $lawyers): \Generator
{
foreach ($lawyers as $lawyer) {
yield $this->parser->buildFullLawyer(
$lawyer,
$this->client->detail($lawyer->getUrl())
);
}
}
}

0 comments on commit 8fa1628

Please sign in to comment.