Exemple #1
0
 /**
  * Factory method for creating a Named CRS object from properties.
  *
  * @see CoordinateReferenceSystem::jsonUnserialize()
  * @param array|object $properties
  * @return Named
  * @throws UnserializationException
  */
 protected static function jsonUnserializeFromProperties($properties)
 {
     if (!is_array($properties) && !is_object($properties)) {
         throw UnserializationException::invalidProperty('Named CRS', 'properties', $properties, 'array or object');
     }
     $properties = new \ArrayObject($properties);
     if (!$properties->offsetExists('name')) {
         throw UnserializationException::missingProperty('Named CRS', 'properties.name', 'string');
     }
     $name = (string) $properties['name'];
     return new self($name);
 }
 /**
  * Factory method for creating a Linked CRS object from properties.
  *
  * @see CoordinateReferenceSystem::jsonUnserialize()
  * @param array|object $properties
  * @return Linked
  * @throws UnserializationException
  */
 protected static function jsonUnserializeFromProperties($properties)
 {
     if (!is_array($properties) && !is_object($properties)) {
         throw UnserializationException::invalidProperty('Linked CRS', 'properties', $properties, 'array or object');
     }
     $properties = new \ArrayObject($properties);
     if (!$properties->offsetExists('href')) {
         throw UnserializationException::missingProperty('Linked CRS', 'properties.href', 'string');
     }
     $href = (string) $properties['href'];
     $type = isset($properties['type']) ? (string) $properties['type'] : null;
     return new self($href, $type);
 }
 /**
  * @see JsonUnserializable::jsonUnserialize()
  */
 public static final function jsonUnserialize($json)
 {
     if (!is_array($json) && !is_object($json)) {
         throw UnserializationException::invalidValue('CRS', $json, 'array or object');
     }
     $json = new \ArrayObject($json);
     if (!$json->offsetExists('type')) {
         throw UnserializationException::missingProperty('CRS', 'type', 'string');
     }
     if (!$json->offsetExists('properties')) {
         throw UnserializationException::missingProperty('CRS', 'properties', 'array or object');
     }
     $type = (string) $json['type'];
     $properties = $json['properties'];
     switch ($type) {
         case 'link':
             return Linked::jsonUnserializeFromProperties($properties);
         case 'name':
             return Named::jsonUnserializeFromProperties($properties);
     }
     throw UnserializationException::unsupportedType('CRS', $type);
 }
 /**
  * @see JsonUnserializable::jsonUnserialize()
  */
 public static final function jsonUnserialize($json)
 {
     if (!is_array($json) && !is_object($json)) {
         throw UnserializationException::invalidValue('GeoJson', $json, 'array or object');
     }
     $json = new \ArrayObject($json);
     if (!$json->offsetExists('type')) {
         throw UnserializationException::missingProperty('GeoJson', 'type', 'string');
     }
     $type = (string) $json['type'];
     $args = array();
     switch ($type) {
         case 'LineString':
         case 'MultiLineString':
         case 'MultiPoint':
         case 'MultiPolygon':
         case 'Point':
         case 'Polygon':
             if (!$json->offsetExists('coordinates')) {
                 throw UnserializationException::missingProperty($type, 'coordinates', 'array');
             }
             if (!is_array($json['coordinates'])) {
                 throw UnserializationException::invalidProperty($type, 'coordinates', $json['coordinates'], 'array');
             }
             $args[] = $json['coordinates'];
             break;
         case 'Feature':
             $geometry = isset($json['geometry']) ? $json['geometry'] : null;
             $properties = isset($json['properties']) ? $json['properties'] : null;
             if (isset($geometry) && !is_array($geometry) && !is_object($geometry)) {
                 throw UnserializationException::invalidProperty($type, 'geometry', $geometry, 'array or object');
             }
             if (isset($properties) && !is_array($properties) && !is_object($properties)) {
                 throw UnserializationException::invalidProperty($type, 'properties', $properties, 'array or object');
             }
             $args[] = isset($geometry) ? self::jsonUnserialize($geometry) : null;
             $args[] = isset($properties) ? (array) $properties : null;
             $args[] = isset($json['id']) ? $json['id'] : null;
             break;
         case 'FeatureCollection':
             if (!$json->offsetExists('features')) {
                 throw UnserializationException::missingProperty($type, 'features', 'array');
             }
             if (!is_array($json['features'])) {
                 throw UnserializationException::invalidProperty($type, 'features', $json['features'], 'array');
             }
             $args[] = array_map(array('self', 'jsonUnserialize'), $json['features']);
             break;
         case 'GeometryCollection':
             if (!$json->offsetExists('geometries')) {
                 throw UnserializationException::missingProperty($type, 'geometries', 'array');
             }
             if (!is_array($json['geometries'])) {
                 throw UnserializationException::invalidProperty($type, 'geometries', $json['geometries'], 'array');
             }
             $args[] = array_map(array('self', 'jsonUnserialize'), $json['geometries']);
             break;
         default:
             throw UnserializationException::unsupportedType('GeoJson', $type);
     }
     if (isset($json['bbox'])) {
         $args[] = BoundingBox::jsonUnserialize($json['bbox']);
     }
     if (isset($json['crs'])) {
         $args[] = CoordinateReferenceSystem::jsonUnserialize($json['crs']);
     }
     $class = sprintf('GeoJson\\%s\\%s', strncmp('Feature', $type, 7) === 0 ? 'Feature' : 'Geometry', $type);
     $class = new \ReflectionClass($class);
     return $class->newInstanceArgs($args);
 }