Changeset b5ebb8d for src/webservice


Ignore:
Timestamp:
Aug 31, 2015, 11:02:06 PM (11 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
master
Children:
69c8290
Parents:
a0f5395
Message:

Changes to Location:

  • Renamed zipCode to postalCode
  • added validation function for latitude and longitude
Location:
src/webservice
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/webservice/htdocs/SiWeather/Webservice/Location.php

    ra0f5395 rb5ebb8d  
    2929class Location
    3030{
    31         public function __construct($city, $zipCode = "",
     31        public function __construct($city, $postalCode = "",
    3232                                                                $regionName = "", $regionCode = "",
    3333                                                                $countryName = "", $countryCode = "",
    … …  
    3636
    3737                $this->_city = "";
    38                 $this->_zipCode = "";
     38                $this->_postalCode = "";
    3939                $this->_regionName = "";
    4040                $this->_regionCode = "";
    … …  
    4747
    4848                $this->setCity($city);
    49                 $this->setZipCode($zipCode);
     49                $this->setPostalCode($postalCode);
    5050                $this->setRegion($regionName, $regionCode);
    5151                $this->setCountry($countryName, $countryCode);
    … …  
    6262        }
    6363       
    64         public function setZipCode($code){
    65                 $this->_zipCode = $code;
     64        public function setPostalCode($code){
     65                $this->_postalCode = $code;
    6666        }
    6767       
    68         public function zipCode(){
    69                 return $this->_zipCode;
     68        public function postalCode(){
     69                return $this->_postalCode;
    7070        }
    7171       
    … …  
    110110          */
    111111        public function setLocation($latitude, $longitude){
    112                 if(is_numeric($latitude) && ($latitude > -90.0 && $latitude < 90.0))
     112                if(self::validLatitude($latitude))
    113113                        $this->_latitude = $latitude;
    114                 if(is_numeric($longitude) && ($longitude > -180.0 && $longitude < 180.0))
     114                if(self::validLongitude($longitude))
    115115                        $this->_longitude = $longitude;
    116116        }
    … …  
    127127                return $this->longitude() ." ". $this->latitude();
    128128        }
    129        
     129
     130        public static function validLatitude($latitude){
     131                if(is_numeric($latitude) && (-90.0 <= $latitude && $latitude <= 90.0))
     132                        return true;
     133                return false;
     134        }
     135
     136        public static function validLongitude($longitude){
     137                if(is_numeric($longitude) && (-180.0 <= $longitude && $longitude <= 180.0))
     138                        return true;
     139                return false;
     140        }
     141
    130142        public function setTimeZone($zone){
    131143                if(preg_match("/^[\D]+\/[\D]+$/", $zone) > 0)
    … …  
    142154        private $_regionName;
    143155        private $_city;
    144         private $_zipCode;
     156        private $_postalCode;
    145157        private $_timeZone;
    146158        private $_latitude;
  • src/webservice/tests/LocationTest.php

    ra0f5395 rb5ebb8d  
    3333        }
    3434       
    35         public function testZipCode(){
     35        public function testPostalCode(){
    3636                $location = new \SiWeather\Webservice\Location("", "");
    3737               
    3838                $value = "12345";
    39                 $location->setZipCode($value);
    40                 $this->assertEquals($location->zipCode(), $value);
     39                $location->setPostalCode($value);
     40                $this->assertEquals($location->postalCode(), $value);
    4141        }
    4242       
    … …  
    5959                $this->assertEquals($location->countryName(), $value1);
    6060                $this->assertEquals($location->countryCode(), $value2);
     61        }
     62
     63        public function testValidLatitude_data(){
     64                $result = array();
     65                $result[] = array(null, false);
     66                $result[] = array("asdasd", false);
     67                $result[] = array(0, true);
     68                $result[] = array(-90, true);
     69                $result[] = array(90, true);
     70                $result[] = array(85.1234, true);
     71                $result[] = array(-90.0001, false);
     72                $result[] = array(90.0001, false);
     73                return $result;
     74        }
     75
     76        /**
     77                * @dataProvider testValidLatitude_data
     78                */
     79        public function testValidLatitude($latitude, $result){
     80                $this->assertEquals(\SiWeather\Webservice\Location::validLatitude($latitude), $result);
     81        }
     82
     83        public function testValidLongitude_data(){
     84                $result = array();
     85                $result[] = array(null, false);
     86                $result[] = array("asdasd", false);
     87                $result[] = array(0, true);
     88                $result[] = array(-90, true);
     89                $result[] = array(90, true);
     90                $result[] = array(85.1234, true);
     91                $result[] = array(-180.0001, false);
     92                $result[] = array(180.0001, false);
     93                return $result;
     94        }
     95
     96        /**
     97                * @dataProvider testValidLongitude_data
     98                */
     99        public function testValidLongitude($longitude, $result){
     100                $this->assertEquals(\SiWeather\Webservice\Location::validLongitude($longitude), $result);
    61101        }
    62102       
    … …  
    82122        public function constructor(){
    83123                $city = "Nice Place";
    84                 $zipCode = "0001";
     124                $postalCode = "0001";
    85125                $regionName = "Nice Region";
    86126                $regionCode = "NR";
    … …  
    89129                $longitude = 50;
    90130                $latitude = 20;
    91                 $location = new \SiWeather\Webservice\Location($city, $zipCode, $regionName,
     131                $location = new \SiWeather\Webservice\Location($city, $postalCode, $regionName,
    92132                                                                                                           $regionCode, $countryName, $countryCode,
    93133                                                                                                           $latitude, $longitude);
    94134               
    95135                $this->assertEquals($location->city(), $city);
    96                 $this->assertEquals($location->zipCode(), $zipCode);
     136                $this->assertEquals($location->postalCode(), $postalCode);
    97137                $this->assertEquals($location->regionName(), $regionName);
    98138                $this->assertEquals($location->regionCode(), $regionCode);
Note: See TracChangeset for help on using the changeset viewer.