generated from DJTommek/php-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#27 Services: added parsing Firmy.cz
- Loading branch information
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |