class SmartIpLocation in Smart IP 8.2
Same name and namespace in other branches
- 8.4 src/SmartIpLocation.php \Drupal\smart_ip\SmartIpLocation
- 8.3 src/SmartIpLocation.php \Drupal\smart_ip\SmartIpLocation
Implements wrapper and utility methods for Smart IP's data location.
@package Drupal\smart_ip
Hierarchy
- class \Drupal\smart_ip\SmartIpLocation implements SmartIpLocationInterface
Expanded class hierarchy of SmartIpLocation
1 string reference to 'SmartIpLocation'
1 service uses SmartIpLocation
File
- src/
SmartIpLocation.php, line 15 - Contains \Drupal\smart_ip\SmartIpLocation.
Namespace
Drupal\smart_ipView source
class SmartIpLocation implements SmartIpLocationInterface {
/**
* All Smart IP location data.
*
* @var array
*/
protected $allData = array();
/**
* Original or raw location data from Smart IP data source.
*
* @var mixed
*/
protected $originalData;
/**
* The source ID.
*
* @var integer
*/
protected $source;
/**
* The IP address.
*
* @var string
*/
protected $ipAddress;
/**
* The country.
*
* @var string
*/
protected $country;
/**
* The ISO 3166 2-character country code.
*
* @var string
*/
protected $countryCode;
/**
* The city.
*
* @var string
*/
protected $city;
/**
* The region (FIPS).
*
* @var string
*/
protected $region;
/**
* The region code (FIPS).
*
* @var string
*/
protected $regionCode;
/**
* The postal / ZIP code.
*
* @var string
*/
protected $zip;
/**
* The longitute.
*
* @var integer
*/
protected $longitute;
/**
* The latitute.
*
* @var integer
*/
protected $latitute;
/**
* The timestamp of the request made.
*
* @var integer
*/
protected $timestamp;
/**
* The time zone.
*
* @var string
*/
protected $timeZone;
/**
* Constructs Smart IP location.
*
* @param array $values
* Array of values for the Smart IP location.
*/
public function __construct(array $values = array()) {
if (!empty($values)) {
$this
->setData($values);
}
}
/**
* {@inheritdoc}
*/
public function get($key = NULL) {
if (!empty($key)) {
$value = $this->{$key};
if (!empty($value)) {
return $value;
}
$this
->getData();
if (isset($this->allData[$key])) {
return $this->allData[$key];
}
else {
return NULL;
}
}
return $this
->getData();
}
/**
* {@inheritdoc}
*/
public function getData() {
if (empty($this->allData)) {
$user = \Drupal::currentUser();
SmartIp::updateUserLocation();
// Get current user's stored location from session
$data = SmartIp::getSession('smart_ip');
if (empty($data['location']) && $user
->id() != 0) {
/** @var \Drupal\user\UserData $userData */
$userData = \Drupal::service('user.data');
// Get current user's stored location from user_data
$data = $userData
->get('smart_ip', $user
->id(), 'geoip_location');
}
if (!empty($data['location'])) {
// Populate the Smart IP location from current user's data or session
$this
->setData($data['location']);
}
}
return $this->allData;
}
/**
* {@inheritdoc}
*/
public function set($key, $value) {
$this->{$key} = $value;
$this->allData[$key] = $value;
}
/**
* {@inheritdoc}
*/
public function setData(array $values = array()) {
foreach ($values as $key => $value) {
$this
->set($key, $value);
}
}
/**
* {@inheritdoc}
*/
public function save() {
$user = \Drupal::currentUser();
$data['location'] = $this->allData;
// Allow other modules to modify country list via hook_smart_ip_user_save_alter()
\Drupal::moduleHandler()
->alter('smart_ip_user_save', $user, $data);
// Save the Smart IP location in current user's session
SmartIp::setSession('smart_ip', $data);
if ($user
->id() != 0) {
/** @var \Drupal\user\UserData $userData */
$userData = \Drupal::service('user.data');
// Save the Smart IP location to current user's user_data
$userData
->set('smart_ip', $user
->id(), 'geoip_location', $data);
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SmartIpLocation:: |
protected | property | All Smart IP location data. | |
SmartIpLocation:: |
protected | property | The city. | |
SmartIpLocation:: |
protected | property | The country. | |
SmartIpLocation:: |
protected | property | The ISO 3166 2-character country code. | |
SmartIpLocation:: |
protected | property | The IP address. | |
SmartIpLocation:: |
protected | property | The latitute. | |
SmartIpLocation:: |
protected | property | The longitute. | |
SmartIpLocation:: |
protected | property | Original or raw location data from Smart IP data source. | |
SmartIpLocation:: |
protected | property | The region (FIPS). | |
SmartIpLocation:: |
protected | property | The region code (FIPS). | |
SmartIpLocation:: |
protected | property | The source ID. | |
SmartIpLocation:: |
protected | property | The timestamp of the request made. | |
SmartIpLocation:: |
protected | property | The time zone. | |
SmartIpLocation:: |
protected | property | The postal / ZIP code. | |
SmartIpLocation:: |
public | function |
Gets an item in Smart IP location data or all the Smart IP location data
if supplied no parameter. Overrides SmartIpLocationInterface:: |
|
SmartIpLocation:: |
public | function |
Gets all the Smart IP location data. Overrides SmartIpLocationInterface:: |
|
SmartIpLocation:: |
public | function |
Saves the Smart IP location data to user data and session (for anonymous,
saves to session only). Overrides SmartIpLocationInterface:: |
|
SmartIpLocation:: |
public | function |
Sets an item in Smart IP location data. Overrides SmartIpLocationInterface:: |
|
SmartIpLocation:: |
public | function |
Sets the Smart IP location data.. Overrides SmartIpLocationInterface:: |
|
SmartIpLocation:: |
public | function | Constructs Smart IP location. | |
SmartIpLocationInterface:: |
constant | Source ID for Google Map Geocoded Smart IP as geolocation source. | ||
SmartIpLocationInterface:: |
constant | Source ID for pure Smart IP as geolocation source. | ||
SmartIpLocationInterface:: |
constant | Source ID for W3C as geolocation source. |