Skip to content

Commit

Permalink
#27 Services: added parsing Firmy.cz
Browse files Browse the repository at this point in the history
  • Loading branch information
DJTommek committed Feb 22, 2021
1 parent aa44404 commit 93eff16
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/libs/BetterLocation/BetterLocationCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\BetterLocation\Service\DuckDuckGoService;
use App\BetterLocation\Service\Exceptions\InvalidLocationException;
use App\BetterLocation\Service\FacebookService;
use App\BetterLocation\Service\FirmyCzService;
use App\BetterLocation\Service\FoursquareService;
use App\BetterLocation\Service\GeocachingService;
use App\BetterLocation\Service\GlympseService;
Expand Down Expand Up @@ -288,6 +289,8 @@ public static function fromTelegramMessage(string $message, array $entities): se
}
} else if (OpenLocationCodeService::isValid($url)) {
$betterLocationsCollection[] = OpenLocationCodeService::parseCoords($url);
} else if (FirmyCzService::isUrl($url)) {
$betterLocationsCollection[] = FirmyCzService::parseUrl($url);
} else if (FacebookService::isUrl($url)) {
if ($location = FacebookService::parseUrl($url)) {
$betterLocationsCollection[] = $location;
Expand Down
74 changes: 74 additions & 0 deletions src/libs/BetterLocation/Service/FirmyCzService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php declare(strict_types=1);

namespace App\BetterLocation\Service;

use App\BetterLocation\BetterLocation;
use App\BetterLocation\BetterLocationCollection;
use App\BetterLocation\Service\Exceptions\NotImplementedException;
use App\BetterLocation\Service\Exceptions\NotSupportedException;
use App\Utils\General;
use DJTommek\MapyCzApi\MapyCzApi;

final class FirmyCzService extends AbstractService
{
const NAME = 'Firmy.cz';

const LINK = 'https://firmy.cz';

const URL_PATH_REGEX = '/^\/detail\/([0-9]+)/';

/** @throws NotSupportedException */
public static function getLink(float $lat, float $lon, bool $drive = false): string
{
if ($drive) {
throw new NotSupportedException('Drive link is not supported.');
} else {
throw new NotSupportedException('Share link is not supported.');
}
}

public static function isValid(string $input): bool
{
return self::isUrl($input);
}

public static function parseCoords(string $input): BetterLocation
{
throw new NotImplementedException('Parsing coordinates is not available.');
}

public static function isUrl(string $url): bool
{
$url = mb_strtolower($url);
$parsedUrl = General::parseUrl($url);
return (
isset($parsedUrl['host']) &&
in_array($parsedUrl['host'], ['firmy.cz', 'www.firmy.cz']) &&
isset($parsedUrl['path']) &&
preg_match(self::URL_PATH_REGEX, $parsedUrl['path'])
);
}

public static function parseUrl(string $url): BetterLocation
{
$parsedUrl = General::parseUrl($url);
preg_match(self::URL_PATH_REGEX, $parsedUrl['path'], $matches);
$firmId = (int)$matches[1];
$mapyCzApi = new MapyCzApi();
$firmDetail = $mapyCzApi->loadPoiDetails('firm', $firmId);
$location = new BetterLocation($url, $firmDetail->getLat(), $firmDetail->getLon(), self::class);
$location->setPrefixMessage(sprintf('<a href="%s">%s %s</a>', $url, self::NAME, $firmDetail->title));
$location->setAddress($firmDetail->titleVars->locationMain1);
return $location;
}

/**
* @param string $input
* @return BetterLocationCollection
* @throws NotImplementedException
*/
public static function parseCoordsMultiple(string $input): BetterLocationCollection
{
throw new NotImplementedException('Parsing multiple coordinates is not available.');
}
}
61 changes: 61 additions & 0 deletions tests/BetterLocation/Service/FirmyCzServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types=1);

use App\BetterLocation\Service\Exceptions\NotSupportedException;
use App\BetterLocation\Service\FirmyCzService;
use PHPUnit\Framework\TestCase;

final class FirmyCzServiceTest extends TestCase
{
public function testGenerateShareLink(): void
{
$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage('Share link is not supported.');
FirmyCzService::getLink(50.087451, 14.420671);
}

public function testGenerateDriveLink(): void
{
$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage('Drive link is not supported.');
FirmyCzService::getLink(50.087451, 14.420671, true);
}

public function testIfValidLinks(): void
{
$this->assertTrue(FirmyCzService::isUrl('https://www.firmy.cz/detail/13300341-restaurace-a-pivnice-u-slunce-blansko.html'));
$this->assertTrue(FirmyCzService::isUrl('https://www.firmy.cz/detail/13300341-blablabla'));
$this->assertTrue(FirmyCzService::isUrl('https://www.firmy.cz/detail/13300341'));
$this->assertTrue(FirmyCzService::isUrl('http://www.firmy.cz/detail/13300341-restaurace-a-pivnice-u-slunce-blansko.html'));
$this->assertTrue(FirmyCzService::isUrl('https://firmy.cz/detail/13300341-restaurace-a-pivnice-u-slunce-blansko.html'));
$this->assertTrue(FirmyCzService::isUrl('http://firmy.cz/detail/13300341-restaurace-a-pivnice-u-slunce-blansko.html'));
$this->assertTrue(FirmyCzService::isUrl('https://www.firmy.cz/detail/13134188-kosmeticky-salon-h2o-humpolec.html'));
$this->assertTrue(FirmyCzService::isUrl('https://www.firmy.cz/detail/13139938-zelva-beers-burgers-praha-zizkov.html'));
$this->assertTrue(FirmyCzService::isUrl('https://www.firmy.cz/detail/207772-penny-market-as.html'));

$this->assertFalse(FirmyCzService::isUrl('https://www.firma.cz/detail/13300341-restaurace-a-pivnice-u-slunce-blansko.html'));
$this->assertFalse(FirmyCzService::isUrl('http://firma.cz/detail/13300341-blablabla'));
$this->assertFalse(FirmyCzService::isUrl('https://www.firmy.cz/aaa/13300341'));
$this->assertFalse(FirmyCzService::isUrl('https://www.firmy.cz/detail/a13300341'));
}

public function testValidLinks(): void
{
$this->assertSame('49.364246,16.644386', FirmyCzService::parseUrl('https://www.firmy.cz/detail/13300341-restaurace-a-pivnice-u-slunce-blansko.html')->__toString());
$this->assertSame('49.364246,16.644386', FirmyCzService::parseUrl('https://www.firmy.cz/detail/13300341-blablabla')->__toString());
$this->assertSame('49.364246,16.644386', FirmyCzService::parseUrl('https://www.firmy.cz/detail/13300341')->__toString());
$this->assertSame('49.364246,16.644386', FirmyCzService::parseUrl('http://www.firmy.cz/detail/13300341-restaurace-a-pivnice-u-slunce-blansko.html')->__toString());
$this->assertSame('49.364246,16.644386', FirmyCzService::parseUrl('https://firmy.cz/detail/13300341-restaurace-a-pivnice-u-slunce-blansko.html')->__toString());
$this->assertSame('49.364246,16.644386', FirmyCzService::parseUrl('http://firmy.cz/detail/13300341-restaurace-a-pivnice-u-slunce-blansko.html')->__toString());
$this->assertSame('49.541035,15.361974', FirmyCzService::parseUrl('https://www.firmy.cz/detail/13134188-kosmeticky-salon-h2o-humpolec.html')->__toString());
$this->assertSame('50.087414,14.469195', FirmyCzService::parseUrl('https://www.firmy.cz/detail/13139938-zelva-beers-burgers-praha-zizkov.html')->__toString());
$this->assertSame('50.221840,12.190701', FirmyCzService::parseUrl('https://www.firmy.cz/detail/207772-penny-market-as.html')->__toString());
}

public function testInvalid(): void
{
$this->expectException(\DJTommek\MapyCzApi\MapyCzApiException::class);
$this->expectExceptionCode(404);
$this->expectExceptionMessage('Not Found');
$this->assertSame('50.077886,14.371990', FirmyCzService::parseUrl('https://www.firmy.cz/detail/9999999-restaurace-a-pivnice-u-slunce-blansko.html')->__toString());
}
}

0 comments on commit 93eff16

Please sign in to comment.