protected function execute($value, EntityInterface $entity = null) { $null_value = $this->getOption(AttributeInterface::OPTION_NULL_VALUE, null); if ($value === $null_value || $value === '') { // accept NULL or empty string as valid NULL value when NULL value is NULL :D $this->setSanitizedValue($null_value); return true; } try { if (is_array($value)) { $geopoint = GeoPoint::createFromArray($value); } elseif ($value instanceof GeoPoint) { $geopoint = GeoPoint::createFromArray($value->toNative()); } else { $this->throwError('invalid_type', ['value' => $value, 'type' => gettype($value)], IncidentInterface::CRITICAL); return false; } $treat_null_island_as_null = $this->getOption(self::OPTION_NULL_ISLAND_AS_NULL, true); if ($treat_null_island_as_null === true && $geopoint->isNullIsland()) { // use (0,0) as NULL as that's a geocoding failure or not wanted or wanted as NULL trigger $this->setSanitizedValue($null_value); return true; } // set the sanitized new geopoint data $this->setSanitizedValue($geopoint); } catch (BadValueException $e) { $this->throwError('invalid_data', ['error' => $e->getMessage()], IncidentInterface::CRITICAL); return false; } return true; }
public function testComparisonOfTwoSimilarGeoPointsSucceeds() { $other_gp = new GeoPoint([GeoPoint::PROPERTY_LONGITUDE => 12.34, GeoPoint::PROPERTY_LATITUDE => 53.21]); $gp = GeoPoint::createFromArray($other_gp->toNative()); $this->assertEquals(12.34, $gp->getLongitude()); $this->assertEquals(53.21, $gp->getLatitude()); $this->assertEquals(12.34, $gp->getLon()); $this->assertEquals(53.21, $gp->getLat()); $this->assertTrue($gp->similarTo($other_gp)); }
public function testToNativeRoundtrip() { $gp_data = [GeoPoint::PROPERTY_LONGITUDE => 123.456, GeoPoint::PROPERTY_LATITUDE => 12.3456]; $native = [GeoPoint::PROPERTY_LONGITUDE => 123.456, GeoPoint::PROPERTY_LATITUDE => 12.3456]; $attribute = new GeoPointAttribute('gp', $this->getTypeMock()); $valueholder = $attribute->createValueHolder(); $valueholder->setValue(GeoPoint::createFromArray($gp_data)); $this->assertInstanceOf(GeoPoint::CLASS, $valueholder->getValue()); $this->assertEquals($native, $valueholder->toNative()); $result = $valueholder->setValue($valueholder->toNative()); $this->assertEquals(IncidentInterface::SUCCESS, $result->getSeverity()); $this->assertInstanceOf(GeoPoint::CLASS, $valueholder->getValue()); $this->assertEquals($native, $valueholder->toNative()); }