Skip to content

Commit

Permalink
Добавил сущность локации, для работы непосредственно по идентификатору
Browse files Browse the repository at this point in the history
  • Loading branch information
directorium committed May 26, 2020
1 parent fe8c79b commit c4382a4
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/Entity/DetailLawyer.php
Expand Up @@ -32,7 +32,7 @@ public function loadFromLawyer(Lawyer $lawyer): self
->setRegisterNumber($lawyer->getRegisterNumber())
->setFullName($lawyer->getFullName())
->setUrl($lawyer->getUrl())
->setTerritorialSubject($lawyer->getTerritorialSubject())
->setLocation($lawyer->getLocation())
->setCertificateNumber($lawyer->getCertificateNumber())
->setStatus($lawyer->getStatus());
}
Expand Down
18 changes: 9 additions & 9 deletions src/Entity/Lawyer.php
Expand Up @@ -30,9 +30,9 @@ class Lawyer
protected $status = '';

/**
* @var string
* @var Location
*/
protected $territorialSubject = '';
protected $location;

/**
* @var string
Expand Down Expand Up @@ -140,21 +140,21 @@ public function setStatus(string $status): self
}

/**
* @return string
* @return Location
*/
public function getTerritorialSubject(): string
public function getLocation(): Location
{
return $this->territorialSubject;
return $this->location;
}

/**
* @param string $territorialSubject
* @param Location $location
*
* @return static
* @return Lawyer
*/
public function setTerritorialSubject(string $territorialSubject): self
public function setLocation(Location $location): Lawyer
{
$this->territorialSubject = $territorialSubject;
$this->location = $location;

return $this;
}
Expand Down
56 changes: 56 additions & 0 deletions src/Entity/Location.php
@@ -0,0 +1,56 @@
<?php

namespace SomeWork\Minjust\Entity;

class Location
{
/**
* @var string
*/
protected $id = '';

/**
* @var string
*/
protected $name = '';

/**
* @return string
*/
public function getId(): string
{
return $this->id;
}

/**
* @param string $id
*
* @return Location
*/
public function setId(string $id): Location
{
$this->id = $id;

return $this;
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* @param string $name
*
* @return Location
*/
public function setName(string $name): Location
{
$this->name = $name;

return $this;
}
}
4 changes: 3 additions & 1 deletion src/Exception/MinjustException.php
Expand Up @@ -2,6 +2,8 @@

namespace SomeWork\Minjust\Exception;

interface MinjustException extends \Throwable
use Throwable;

interface MinjustException extends Throwable
{
}
2 changes: 1 addition & 1 deletion src/FindResponse.php
Expand Up @@ -119,7 +119,7 @@ public function setTotal(int $total): FindResponse
}

/**
* @param \SomeWork\Minjust\Entity\Lawyer $lawyer
* @param Lawyer $lawyer
*
* @return FindResponse
*/
Expand Down
88 changes: 80 additions & 8 deletions src/Parser/DomParser.php
Expand Up @@ -4,6 +4,7 @@

namespace SomeWork\Minjust\Parser;

use Exception;
use PHPHtmlParser\Dom;
use PHPHtmlParser\Dom\Collection;
use PHPHtmlParser\Dom\HtmlNode;
Expand All @@ -12,8 +13,10 @@
use SomeWork\Minjust\Entity\DetailLawyer;
use SomeWork\Minjust\Entity\LawFormation;
use SomeWork\Minjust\Entity\Lawyer;
use SomeWork\Minjust\Entity\Location;
use SomeWork\Minjust\Exception\BlockNotFoundException;
use SomeWork\Minjust\Exception\RuntimeException;
use SomeWork\Minjust\FindRequest;
use SomeWork\Minjust\FindResponse;

/**
Expand Down Expand Up @@ -51,6 +54,11 @@ class DomParser implements ParserInterface
*/
protected const LAWYER_DETAIL_NAME_FIELD = 'label';

/**
* @var string
*/
protected const LOCATIONS_BLOCK_SELECTOR = 'select#' . FindRequest::TERRITORIAL_SUBJECT . ' > option';

public function list(string $body): FindResponse
{
$dom = (new Dom())->loadStr($body);
Expand Down Expand Up @@ -135,7 +143,7 @@ protected function getPagination(Dom $dom): HtmlNode

try {
$pagination = $dom->find(static::PAGINATION_BLOCK_SELECTOR, 0);
} catch (\Exception $exception) {
} catch (Exception $exception) {
throw new RuntimeException($exception->getMessage(), $exception->getCode(), $exception);
}

Expand All @@ -148,7 +156,7 @@ protected function getPagination(Dom $dom): HtmlNode
}

/**
* @param \PHPHtmlParser\Dom $dom
* @param Dom $dom
*
* @return Lawyer[]
* @throws ChildNotFoundException
Expand All @@ -161,18 +169,32 @@ protected function getListLawyers(Dom $dom): array
* @var Dom\HtmlNode[]|Collection $nodes
*/
$nodes = $dom->find(static::LAWYERS_LIST_BLOCK_SELECTOR);
$locations = $this->getLocations($dom);

foreach ($nodes as $node) {
/**
* @var Dom\HtmlNode[]|Collection $tds
*/
$tds = $node->find('td');

$registerNumber = trim($tds[0]->text());
$certificateNumber = trim($tds[3]->text());
$fullName = trim($tds[1]->text(true));
$url = trim($tds[1]->firstChild()->getAttribute('href'));
$status = trim($tds[4]->text());
$location = $this->getLocationByRegisterNumber($registerNumber, $locations);

if (null === $location) {
continue;
}

$data[] = (new Lawyer())
->setRegisterNumber($tds[0]->text())
->setFullName($tds[1]->text(true))
->setUrl($tds[1]->firstChild()->getAttribute('href'))
->setTerritorialSubject($tds[2]->text())
->setCertificateNumber($tds[3]->text())
->setStatus($tds[4]->text());
->setRegisterNumber($registerNumber)
->setCertificateNumber($certificateNumber)
->setFullName($fullName)
->setUrl($url)
->setLocation($location)
->setStatus($status);
}

return $data;
Expand Down Expand Up @@ -209,4 +231,54 @@ public function detail(string $body): DetailLawyer

return $lawyer;
}

/**
* @param string $registerNumber
* @param Location[] $locations
*
* @return Location|null
* @return Location|null
*/
protected function getLocationByRegisterNumber(string $registerNumber, array $locations): ?Location
{
[$locationId,] = explode('/', $registerNumber);

foreach ($locations as $location) {
if ($location->getId() === $locationId) {
return $location;
}
}

return null;
}

protected function getLocations(Dom $dom)
{
/**
* @var Dom\HtmlNode[] $nodes
*/
$nodes = $dom->find(static::LOCATIONS_BLOCK_SELECTOR)->toArray();

$nodes = array_filter(
$nodes,
static function (HtmlNode $htmlNode) {
return $htmlNode->getAttribute('value');
}
);

return array_map(
static function (HtmlNode $htmlNode) {
$id = trim($htmlNode->getAttribute('value'));

$name = $htmlNode->text();
$name = str_replace('[' . $id . ']', '', $name);
$name = trim($name);

return (new Location())
->setId($id)
->setName($name);
},
$nodes
);
}
}
5 changes: 2 additions & 3 deletions tests/Unit/ClientTest.php
Expand Up @@ -156,7 +156,7 @@ public function testHttpClientExceptionHandleRequest(): void
* @return mixed Method return.
* @throws ReflectionException
*/
public function invokeMethod(&$object, $methodName, array $parameters = [])
public function invokeMethod($object, $methodName, array $parameters = [])
{
$reflection = new ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
Expand All @@ -166,7 +166,7 @@ public function invokeMethod(&$object, $methodName, array $parameters = [])
}

/**
* @throws \ReflectionException
* @throws ReflectionException
*/
public function testWrongStatusCodeException(): void
{
Expand All @@ -188,7 +188,6 @@ public function testWrongStatusCodeException(): void
$this->expectException(WrongStatusCodeException::class);

$client = new Client($httpClient, $requestFactory);
/* @noinspection PhpUnhandledExceptionInspection */
$this->invokeMethod($client, 'handleRequest', [$request]);
}
}
23 changes: 16 additions & 7 deletions tests/Unit/Entity/DetailLawyerTest.php
Expand Up @@ -10,9 +10,10 @@
use SomeWork\Minjust\Entity\DetailLawyer;
use SomeWork\Minjust\Entity\LawFormation;
use SomeWork\Minjust\Entity\Lawyer;
use SomeWork\Minjust\Entity\Location;

/**
* @coversDefaultClass \SomeWork\Minjust\Entity\DetailLawyer
* @coversDefaultClass DetailLawyer
* @covers \SomeWork\Minjust\Entity\DetailLawyer
*/
class DetailLawyerTest extends TestCase
Expand All @@ -38,9 +39,9 @@ protected function getPropertyValue(object $object, string $property)
/**
* @depends testEmpty
*
* @param \SomeWork\Minjust\Entity\DetailLawyer $lawyer
* @param DetailLawyer $lawyer
*
* @return \SomeWork\Minjust\Entity\DetailLawyer
* @return DetailLawyer
*/
public function testSet(DetailLawyer $lawyer): DetailLawyer
{
Expand All @@ -59,7 +60,7 @@ public function testSet(DetailLawyer $lawyer): DetailLawyer
/**
* @depends testSet
*
* @param \SomeWork\Minjust\Entity\DetailLawyer $lawyer
* @param DetailLawyer $lawyer
*/
public function testGet(DetailLawyer $lawyer): void
{
Expand All @@ -71,13 +72,17 @@ public function testGet(DetailLawyer $lawyer): void

public function testLoadFromLawyer(): void
{
$location = (new Location())
->setId('01')
->setName('testLocation');

$lawyer = (new Lawyer())
->setUrl('testUrl')
->setFullName('testFullName')
->setStatus('testStatus')
->setRegisterNumber('testRegisterNumber')
->setCertificateNumber('testCertificateNumber')
->setTerritorialSubject('testTerritorialSubject');
->setLocation($location);

$detailLawyer = (new DetailLawyer())->loadFromLawyer($lawyer);

Expand All @@ -86,21 +91,25 @@ public function testLoadFromLawyer(): void
$this->assertEquals($lawyer->getStatus(), $detailLawyer->getStatus());
$this->assertEquals($lawyer->getRegisterNumber(), $detailLawyer->getRegisterNumber());
$this->assertEquals($lawyer->getCertificateNumber(), $detailLawyer->getCertificateNumber());
$this->assertEquals($lawyer->getTerritorialSubject(), $detailLawyer->getTerritorialSubject());
$this->assertEquals($lawyer->getLocation(), $detailLawyer->getLocation());
}

/**
* @covers ::__construct
*/
public function testConstruct(): void
{
$location = (new Location())
->setId('01')
->setName('test Location');

$lawyer = (new Lawyer())
->setUrl('testUrl')
->setFullName('testFullName')
->setStatus('testStatus')
->setRegisterNumber('testRegisterNumber')
->setCertificateNumber('testCertificateNumber')
->setTerritorialSubject('testTerritorialSubject');
->setLocation($location);

$detailLawyer = $this->createPartialMock(DetailLawyer::class, ['loadFromLawyer']);

Expand Down

0 comments on commit c4382a4

Please sign in to comment.