Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
Flerex committed May 14, 2021
2 parents 38de773 + 53b412f commit 2a15989
Showing 4 changed files with 68 additions and 3 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -16,11 +16,12 @@
],
"minimum-stability": "stable",
"require": {
"php": "^7.4",
"php": "^7.4||^8.0",
"myclabs/php-enum": "^1.7",
"guzzlehttp/guzzle": "^7.2",
"ext-json": "*",
"ext-simplexml": "*"
"ext-simplexml": "*",
"ext-mbstring": "*"
},
"autoload": {
"psr-4": {
1 change: 1 addition & 0 deletions src/Dtos/GasStation.php
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ final class GasStation
{
public int $id;
public string $label;
public string $labelRaw;
public ?float $price;
public DateTime $priceLastUpdatedAt;
public string $owner;
4 changes: 3 additions & 1 deletion src/QueryBuilders/StationDetailsBuilder.php
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
use Flerex\SpainGas\Enums\ServiceType;
use Flerex\SpainGas\Exceptions\LogicException;
use Flerex\SpainGas\Exceptions\NetworkException;
use Flerex\SpainGas\Utils\APIParsingUtils;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\GuzzleException;
use stdClass;
@@ -178,7 +179,8 @@ private function jsonObjectToDto(stdClass $jsonObject): GasStation
$station = new GasStation;

$station->id = $jsonObject->estacion->id;
$station->label = $jsonObject->estacion->rotulo;
$station->labelRaw = $jsonObject->estacion->rotulo;
$station->label = APIParsingUtils::beautifyLabel($jsonObject->estacion->rotulo);

$station->price = is_numeric($jsonObject->precio) ? $jsonObject->precio : null;

61 changes: 61 additions & 0 deletions src/Utils/APIParsingUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php


namespace Flerex\SpainGas\Utils;


final class APIParsingUtils
{

/**
* The following words will always appear as they are typed. This ensures that they are not turned into
* lowercase (or uppercase).
*
* @var array|string[] $fixed
*/
private const FIXED = [
'SA', 'SL', 'S.A.', 'S.L.', 'E.S.', 'ES', 'EESS', 'E.E.S.S.', 'EE.SS.', 'S.A.L.', 'S.C.A.',
'CH2M', 'DGV', 'C.T.', 'U.S.', 'V.C.', 'CDT', 'R.R.', 'SCL', 'A.I.E.', 'BP', 'A.N.', 'U.S.Coop.',
'J.A.', 'S.L',

'de', 'en', 'del', 'la', 'las', 'el', 'los'
];

/**
* By default the API provides all values as uppercase. This function allows us to transform
* the uppercase names to a more human readable format.
*
* For example, the name REPSOL will turn to Repsol.
*
* @param string $name
* @return string
*/
public static function beautifyLabel(string $name): string
{
// Remove stylized names. |> Enertrix <| turns into Enertrix
$name = preg_replace('/[<>|]/', '', $name);

$name = trim($name);

// Remove URL comma character (%2C).
$name = preg_replace('/\%2c/i', '', $name);

// Only one space allowed
$name = preg_replace('/ +/', ' ', $name);

// Replace `+` between words with spaces. Canso+carburants -> Canso carburants.
$name = preg_replace('/\b\+\b/', ' ', $name);

$name = ucwords(mb_strtolower($name));

$name = array_reduce(self::FIXED, function(string $carry, string $item) {
return preg_replace('/\b' . preg_quote($item) . '(?!\S)/i', $item, $carry);
}, $name);

// Ensure we start with capital letter if one of the prepositions was replaced at
// the beginning. (del Olmo Hermanos -> Del Olmo Hermanos)
$name = ucfirst($name);

return $name;
}
}

0 comments on commit 2a15989

Please sign in to comment.