You are here

protected function Geolocation::prepareValue in Geolocation Field 8.3

Same name and namespace in other branches
  1. 8.2 src/Feeds/Target/Geolocation.php \Drupal\geolocation\Feeds\Target\Geolocation::prepareValue()

Prepares a single value.

Parameters

int $delta: The field delta.

array $values: The values.

Overrides FieldTargetBase::prepareValue

1 call to Geolocation::prepareValue()
Geolocation::prepareValues in src/Feeds/Target/Geolocation.php
Prepares the the values that will be mapped to an entity.

File

src/Feeds/Target/Geolocation.php, line 33

Class

Geolocation
Defines a geolocation field mapper.

Namespace

Drupal\geolocation\Feeds\Target

Code

protected function prepareValue($delta, array &$values) {

  // Both latitude and longitude must be set in order to prepare the values.
  // Check if both contain values.
  foreach ([
    'lat',
    'lng',
  ] as $key) {
    if (!isset($values[$key])) {

      // Value is not set. Abort.
      throw new EmptyFeedException();
    }
    if (empty($values[$key]) && $values[$key] !== '0' && $values[$key] !== 0) {

      // Value is empty and is not zero. Abort.
      throw new EmptyFeedException();
    }
  }
  $values['lat'] = floatval($values['lat']);
  $values['lng'] = floatval($values['lng']);
  $values['lat_sin'] = sin(deg2rad($values['lat']));
  $values['lat_cos'] = cos(deg2rad($values['lat']));
  $values['lng_rad'] = deg2rad($values['lng']);
  return $values;
}