Repository navigation
Extend result from Yandex - #624
Conversation
| 'country' => isset($details['CountryName']) ? $details['CountryName'] : null, | ||
| 'countryCode' => isset($details['CountryNameCode']) ? $details['CountryNameCode'] : null, | ||
| ]); | ||
| $builder->setStreetNumber(isset($flatArray['PremiseNumber']) ? $flatArray['PremiseNumber'] : null); |
There was a problem hiding this comment.
if we go for PHP >= 7, we might want to use ??
There was a problem hiding this comment.
Yeah, I agree. But should PHP >= 7 be a requirement for 4.0?
| /** | ||
| * @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
| */ | ||
| class YandexAddress extends Address |
There was a problem hiding this comment.
Yes. I'll make sure to send a PR. Thanks
| * | ||
| * @return YandexAddress | ||
| */ | ||
| public function setPrecision($precision) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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,
...
);There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Okey, Thank you. But how to call the constructor to subclasses of Address with the LocationBuilder? (which should be renamed to AddressBuilder)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 oneAt least, it makes object immutable.
This will fix #532 and #513