frozensheep / rightmoveADF

PHP library for the Rightmove Real Time Property Datafeed (ADF).
MIT License
17 stars 20 forks source link

Remove requirement to use constants #5

Closed richplane closed 7 years ago

richplane commented 8 years ago

I have a database full of properties (as I imagine most people will). Each property has a property type. I already have a lookup table of property types to Rightmove property type integers. This system requires me to use the constants defined in Frozensheep\RightmoveADF\Values\PropertyTypes. This means that I need another column to map these onto the constant names and then I have to retrieve the constants by doing

$type = constant('Frozensheep\RightmoveADF\Values\PropertyTypes::' . $rtdf_lookup[$advert->property_type]);
$objRequest->property->property_type = $type;

It seems so unnecessary to do this when I could just do

$objRequest->property->property_type = $advert->RM_type;

...and the same goes for all the fields that are restricted to the constants in \Values. I have no problem with being validated against a list of acceptable values, that's great, but I don't see why I have to go via the predefined constants.

Especially when they have misspellings like "ReteirmentProperty" and "MewsHous".

jacobwyke commented 7 years ago

You don't have to use the constants....they are just there to validate the values or else you get errors back from the API, so they are checked locally before sending the request.

$objRequest->property->property_type = Frozensheep\RightmoveADF\Values\PropertyTypes::GroundFloorFlat;
$objRequest->property->property_type = 7;
$objRequest->property->property_type = $from_a_db['property_type'];
$objRequest->property->property_type = $from_an_object->property_type;

Will all work. You just have to ensure the values are allowed. I seem to remember I also ran into some that were different in the real time feed to the older .blm version.

richplane commented 7 years ago

This wasn't my experience. I should have quoted the error I got when I tried this, apologies.

I tried to set $objRequest->property->property_type = $rightmove_type

(where $rightmove_type = 3)

and got: Fatal error: Uncaught exception 'UnexpectedValueException' with message 'Value '3' is not part of the enum Frozensheep\RightmoveADF\Values\PropertyTypes'

The value '3' is clearly one of the available property types - it seems that you are using the php-enum class that forces us to use the constants. Am I wrong?

jacobwyke commented 7 years ago

Ah ok.

The issue here is the data type. It is expecting an int and is getting a string:

//setting an int - this works
$objRequest->property->property_type = 3;

//setting a string - this throws an error
$objRequest->property->property_type = '3';

//casting the value to an int
$objRequest->property->property_type = (int)'3';
$objRequest->property->property_type = (int)$rightmove_type;

The rightmove API expects an integer and will throw an error if your JSON uses a string instead of an int.

richplane commented 7 years ago

:blush: You're quite correct. The error message was a bit oblique, but I should have twigged.