Skip to content

Extend result from Yandex - #624

Merged
Nyholm merged 3 commits into
geocoder-php:masterfrom
Nyholm:issue-532
May 22, 2017
Merged

Nyholm merged 3 commits into
geocoder-php:masterfrom
Nyholm:issue-532

Conversation

@Nyholm

@Nyholm Nyholm commented May 20, 2017

Copy link
Copy Markdown
Member

This will fix #532 and #513

@Nyholm Nyholm added this to the 4.0.0 milestone May 20, 2017
@willdurand
willdurand requested a review from vicchi May 20, 2017 10:20
'country' => isset($details['CountryName']) ? $details['CountryName'] : null,
'countryCode' => isset($details['CountryNameCode']) ? $details['CountryNameCode'] : null,
]);
$builder->setStreetNumber(isset($flatArray['PremiseNumber']) ? $flatArray['PremiseNumber'] : null);

@willdurand willdurand May 22, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we go for PHP >= 7, we might want to use ??

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree. But should PHP >= 7 be a requirement for 4.0?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #634

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. It is time :p

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wohoo!

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class YandexAddress extends Address

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I'll make sure to send a PR. Thanks

*
* @return YandexAddress
*/
public function setPrecision($precision)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VO with setter?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I agree with you. But I'm not sure how to make it work without a setter. Look at the LocationBuilder. Do you have any suggestions?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see much sense in LocationBuilder, I'd write something like

$adminLevels = ...;

$locations[] = new YandexLocation(
    isset($flatArray['name']) ? $flatArray['name'] : null,
    isset($flatArray['precision']) ? $flatArray['precision'] : null,
    $adminLevels,
    ...
);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LocationBuilder is meant to remove that large array all providers were building.

Your suggestion means that the (already big and ugly) constructor of Address has to be overridden. Is that somehow avoidable? Could we make the constructor prettier?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your suggestion means that the (already big and ugly) constructor of Address has to be overridden. Is that somehow avoidable? Could we make the constructor prettier?

OK, it is "big and ugly". What's the problem with it? If Address requires a lot of params, it is natural that constructor is "big and ugly". Setter doesn't make code "prettier", it just makes object less explicit, it hides the problem (if it does exist).

If you ask me, many arguments for data object is OK, it is not a service.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okey, Thank you. But how to call the constructor to subclasses of Address with the LocationBuilder? (which should be renamed to AddressBuilder)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wikipedia states that the builder pattern solves the the telescoping constructor anti-pattern problem. C#, for example, in the past had no default values for properties and default values for arguments, so there were 2 ways to make default values: constructor overloading and the builder pattern. Regarding to your case with Address I don't see why you need the builder.

Technically, it just replaces

$a = ...;
$b = ...;
$c = ... ? ... : null;

$address = new Address($a, $b, $c);

with

$builder->setA(...);
$builder->setB(...);
$builder->setC(... ? ... : null);

$address = $builder->build();

What problem does the LocationBuilder solve?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that all providers is not setting all data. Some data might me permanently ignored by some providers. (MaxMindBinary is a perfect example). The LocationBuilder brings an easy way (with setter/typehints) to add some/all data. The logic (order of constructor parameters) is hidden away. Before the LocationBuilder was introduces we used an assoc array.

Example:

$builder->setE($httpResponse['e']);
$address = $builder->build();

Compared with:

$e = $httpResponse['e'];
$address = new Address(null, null, null, null, $e);

But how to call the constructor to subclasses of Address with the LocationBuilder?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compared with:

Why not? Provider makes translation from external API to your generic Address class, it is OK to have some dirty code here, isn't it? Offtopic: https://wiki.php.net/rfc/named_params

If you can't avoid some setter-like method, I suggest to use with-er syntax:

$address = $address->withFoo('bar'); // with* clones the object and return changed one

At least, it makes object immutable.

svrdlic added a commit to svrdlic/Geocoder that referenced this pull request May 29, 2017
Nyholm pushed a commit that referenced this pull request May 30, 2017
* adding support for formatted_address for GoogleMaps

* cs

* fix for immutable address, PR #624 #652
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Impossible to get subway station name in Yandex provider

3 participants