Check attribute validity and return null on empty but valid attributes
This commit is contained in:
		
							parent
							
								
									45228dfad4
								
							
						
					
					
						commit
						275bda0fc4
					
				@ -4,5 +4,16 @@ namespace GeoIP2\Record;
 | 
			
		||||
 | 
			
		||||
abstract class AbstractPlaceRecord extends AbstractRecord
 | 
			
		||||
{
 | 
			
		||||
  private $languages;
 | 
			
		||||
 | 
			
		||||
  public function __construct($record, $languages){
 | 
			
		||||
    $this->languages = $languages;
 | 
			
		||||
    parent::__construct($record);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public function name() {
 | 
			
		||||
    foreach($this->languages as $language) {
 | 
			
		||||
      if (isset($this->names[$language])) return $this->names[$language];
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@ -11,8 +11,13 @@ abstract class AbstractRecord
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public function __get($attr) {
 | 
			
		||||
    if (isset($this->record[$attr])) return $this->record[$attr];
 | 
			
		||||
 | 
			
		||||
    throw new RuntimeException("Unknown attribute: $attr");
 | 
			
		||||
    $valid = in_array($attr, $this->validAttributes);
 | 
			
		||||
    if ($valid && isset($this->record[$attr])){
 | 
			
		||||
      return $this->record[$attr];
 | 
			
		||||
    } elseif ($valid) {
 | 
			
		||||
      return null;
 | 
			
		||||
    } else {
 | 
			
		||||
      throw new \RuntimeException("Unknown attribute: $attr");
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@ -4,5 +4,5 @@ namespace GeoIP2\Record;
 | 
			
		||||
 | 
			
		||||
class City extends AbstractPlaceRecord
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
  protected $validAttribute = Array('confidence', 'geoname_id', 'names');
 | 
			
		||||
}
 | 
			
		||||
@ -4,5 +4,7 @@ namespace GeoIP2\Record;
 | 
			
		||||
 | 
			
		||||
class Continent extends AbstractPlaceRecord
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
  protected $validAttributes = Array('continent_code',
 | 
			
		||||
                                     'geoname_id',
 | 
			
		||||
                                     'names');
 | 
			
		||||
}
 | 
			
		||||
@ -5,17 +5,8 @@ namespace GeoIP2\Record;
 | 
			
		||||
class Country extends AbstractPlaceRecord
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
  private $languages;
 | 
			
		||||
 | 
			
		||||
  public function __construct($record, $languages){
 | 
			
		||||
    $this->languages = $languages;
 | 
			
		||||
    parent::__construct($record);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public function name() {
 | 
			
		||||
    foreach($this->languages as $language) {
 | 
			
		||||
      if (isset($this->names[$language])) return $this->names[$language];
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  protected $validAttributes = Array('confidence',
 | 
			
		||||
                                   'geoname_id',
 | 
			
		||||
                                   'iso_code',
 | 
			
		||||
                                   'names');
 | 
			
		||||
}
 | 
			
		||||
@ -4,5 +4,11 @@ namespace GeoIP2\Record;
 | 
			
		||||
 | 
			
		||||
class Location extends AbstractRecord
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
  protected $validAttributes = Array('accuracy_radius',
 | 
			
		||||
                                     'latitude',
 | 
			
		||||
                                     'longitude',
 | 
			
		||||
                                     'metro_code',
 | 
			
		||||
                                     'postal_code',
 | 
			
		||||
                                     'postal_confidence',
 | 
			
		||||
                                     'time_zone');
 | 
			
		||||
}
 | 
			
		||||
@ -4,5 +4,5 @@ namespace GeoIP2\Record;
 | 
			
		||||
 | 
			
		||||
class Postal extends AbstractRecord
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
  protected $validAttributes = Array('code', 'confidence');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -4,5 +4,9 @@ namespace GeoIP2\Record;
 | 
			
		||||
 | 
			
		||||
class RepresentedCountry extends Country
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
  protected $validAttributes = Array('confidence',
 | 
			
		||||
                                   'geoname_id',
 | 
			
		||||
                                   'iso_code',
 | 
			
		||||
                                   'names',
 | 
			
		||||
                                   'type');
 | 
			
		||||
}
 | 
			
		||||
@ -4,5 +4,8 @@ namespace GeoIP2\Record;
 | 
			
		||||
 | 
			
		||||
class Subdivision extends AbstractPlaceRecord
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
  protected $validAttributes = Array('confidence',
 | 
			
		||||
                                     'geoname_id',
 | 
			
		||||
                                     'iso_code',
 | 
			
		||||
                                     'names');
 | 
			
		||||
}
 | 
			
		||||
@ -4,5 +4,14 @@ namespace GeoIP2\Record;
 | 
			
		||||
 | 
			
		||||
class Traits extends AbstractRecord
 | 
			
		||||
{
 | 
			
		||||
  protected $validAttributes = Array('autonomous_system_number',
 | 
			
		||||
                                     'autonomous_system_organization',
 | 
			
		||||
                                     'domain',
 | 
			
		||||
                                     'is_anonymous_proxy',
 | 
			
		||||
                                     'is_satellite_provider',
 | 
			
		||||
                                     'isp',
 | 
			
		||||
                                     'ip_address',
 | 
			
		||||
                                     'organization',
 | 
			
		||||
                                     'user_type');
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user