Completed
Push — master ( 684184...9a35aa )
by Jeroen De
08:07
created

includes/parserhooks/Maps_Finddestination.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
use DataValues\Geo\Formatters\GeoCoordinateFormatter;
3
4
/**
5
 * Class for the 'finddestination' parser hooks, which can find a
6
 * destination given a starting point, an initial bearing and a distance.
7
 * 
8
 * @since 0.7
9
 * 
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
14
class MapsFinddestination extends ParserHook {
0 ignored issues
show
Deprecated Code introduced by
The class ParserHook has been deprecated with message: since 1.0 in favour of the ParserHooks library

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
15
16
	/**
17
	 * Gets the name of the parser hook.
18
	 * @see ParserHook::getName
19
	 * 
20
	 * @since 0.7
21
	 * 
22
	 * @return string
23
	 */
24
	protected function getName() {
25
		return 'finddestination';
26
	}
27
	
28
	/**
29
	 * Returns an array containing the parameter info.
30
	 * @see ParserHook::getParameterInfo
31
	 * 
32
	 * @since 0.7
33
	 * 
34
	 * @return array
35
	 */
36
	protected function getParameterInfo( $type ) {
37
		global $egMapsAvailableGeoServices, $egMapsDefaultGeoService, $egMapsAvailableCoordNotations;
38
		global $egMapsCoordinateNotation, $egMapsAllowCoordsGeocoding, $egMapsCoordinateDirectional;	 
39
		
40
		$params = [];
41
42
		$params['location'] = [
43
			'dependencies' => [ 'mappingservice', 'geoservice' ],
44
			'type' => 'mapslocation',
45
		];
46
47
		$params['format'] = [
48
			'default' => $egMapsCoordinateNotation,
49
			'values' => $egMapsAvailableCoordNotations,
50
			'aliases' => 'notation',
51
			'tolower' => true,
52
		];
53
54
		$params['directional'] = [
55
			'type' => 'boolean',
56
			'default' => $egMapsCoordinateDirectional,
57
		];
58
59
		$params['bearing'] = [
60
			'type' => 'float',
61
		];
62
63
		$params['distance'] = [
64
			'type' => 'distance',
65
		];
66
67
		$params['mappingservice'] = [
68
			'default' => '',
69
			'values' => MapsMappingServices::getAllServiceValues(),
70
			'tolower' => true,
71
		];
72
73
		$params['geoservice'] = [
74
			'default' => $egMapsDefaultGeoService,
75
			'aliases' => 'service',
76
			'values' => $egMapsAvailableGeoServices,
77
			'tolower' => true,
78
		];
79
80
		$params['allowcoordinates'] = [
81
			'type' => 'boolean',
82
			'default' => $egMapsAllowCoordsGeocoding,
83
		];
84
85
		// Give grep a chance to find the usages:
86
		// maps-finddestination-par-location, maps-finddestination-par-format,
87
		// maps-finddestination-par-directional, maps-finddestination-par-bearing,
88
		// maps-finddestination-par-distance, maps-finddestination-par-mappingservice,
89
		// maps-finddestination-par-geoservice, maps-finddestination-par-allowcoordinates
90
		foreach ( $params as $name => &$param ) {
91
			$param['message'] = 'maps-finddestination-par-' . $name;
92
		}
93
94
		return $params;
95
	}
96
	
97
	/**
98
	 * Returns the list of default parameters.
99
	 * @see ParserHook::getDefaultParameters
100
	 * 
101
	 * @since 0.7
102
	 * 
103
	 * @return array
104
	 */
105
	protected function getDefaultParameters( $type ) {
106
		return [ 'location', 'bearing', 'distance' ];
107
	}
108
	
109
	/**
110
	 * Renders and returns the output.
111
	 * @see ParserHook::render
112
	 * 
113
	 * @since 0.7
114
	 * 
115
	 * @param array $parameters
116
	 * 
117
	 * @return string
118
	 */
119
	public function render( array $parameters ) {
120
		$destination = MapsGeoFunctions::findDestination(
121
			$parameters['location']->getCoordinates(),
122
			$parameters['bearing'],
123
			$parameters['distance']
124
		);
125
126
		$options = new \ValueFormatters\FormatterOptions( [
127
			GeoCoordinateFormatter::OPT_FORMAT => $parameters['format'],
128
			GeoCoordinateFormatter::OPT_DIRECTIONAL => $parameters['directional'],
129
			GeoCoordinateFormatter::OPT_PRECISION => 1 / 360000
130
		] );
131
132
		$formatter = new GeoCoordinateFormatter( $options );
133
134
		$geoCoords = new \DataValues\LatLongValue( $destination['lat'], $destination['lon'] );
0 ignored issues
show
Deprecated Code introduced by
The class DataValues\LatLongValue has been deprecated with message: since 1.0, use the base class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
135
		$output = $formatter->format( $geoCoords );
136
137
		return $output;
138
	}
139
140
	/**
141
	 * @see ParserHook::getMessage()
142
	 * 
143
	 * @since 1.0
144
	 */
145
	public function getMessage() {
146
		return 'maps-finddestination-description';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'maps-finddestination-description'; (string) is incompatible with the return type of the parent method ParserHook::getMessage of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
147
	}	
148
	
149
}