Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 86
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
BaseMapElement
0.00% covered (danger)
0.00%
0 / 86
0.00% covered (danger)
0.00%
0 / 13
1406
0.00% covered (danger)
0.00%
0 / 1
 getElementName
n/a
0 / 0
n/a
0 / 0
0
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 __get
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getProperty
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 __set
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setProperty
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
 unsetProperty
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 parse
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 parseCoordinates
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 parseProperties
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
132
 isValid
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 reset
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getErrorMessages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getData
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2namespace MultiMaps;
3
4use MediaWiki\MediaWikiServices;
5use MediaWiki\Title\Title;
6
7/**
8 * Base class for collection of map elements
9 *
10 * @file BaseService.php
11 * @ingroup MultiMaps
12 * @author Pavel Astakhov <pastakhov@yandex.ru>
13 * @license GPL-2.0-or-later
14 * @property-read array $pos Array of geographic coordinates
15 * @property string $title Title of element
16 * @property string $text Popup text of element
17 */
18abstract class BaseMapElement {
19
20    /**
21     * Geographic coordinates
22     * @var Point[]
23     */
24    protected $coordinates;
25
26    /**
27     * @todo Description
28     * @var bool
29     */
30    protected $isValid;
31
32    /**
33     * An array that is used to accumulate the error messages
34     * @var array
35     */
36    protected $errormessages;
37
38    /**
39     * Array of properties available for this element
40     * @var array
41     */
42    protected $availableProperties;
43
44    /**
45     * Array of element properties
46     * @var array
47     */
48    protected $properties;
49
50    /**
51     * Returns element name
52     * return string Element name
53     */
54    abstract public function getElementName();
55
56    function __construct() {
57        $this->availableProperties = [
58            'title',
59            'text',
60        ];
61
62        $this->reset();
63    }
64
65    public function __get( $name ) {
66        return $this->getProperty( $name );
67    }
68
69    /**
70     * Get element property by name
71     * @param string $name
72     * @return mixed
73     */
74    public function getProperty( $name ) {
75        $name = strtolower( $name );
76
77        switch ( $name ) {
78            case 'pos':
79                return $this->coordinates;
80            default:
81                if ( isset( $this->properties[$name] ) ) {
82                    return $this->properties[$name];
83                }
84                break;
85        }
86        return null;
87    }
88
89    public function __set( $name, $value ) {
90        $this->setProperty( $name, $value );
91    }
92
93    /**
94     * Set element property by name
95     * @param string $name
96     * @param mixed $value
97     * @return bool
98     */
99    public function setProperty( $name, $value ) {
100        $name = strtolower( $name );
101        if ( array_search( $name, $this->availableProperties ) === false ) {
102            return false;
103        }
104
105        if ( $name == 'title' || $name == 'text' ) {
106            $parser = MediaWikiServices::getInstance()->getParser();
107            $title = Title::makeTitle( NS_SPECIAL, 'BadTitle/BaseMapElement' );
108            $value = trim( $value );
109            if ( defined( 'LINGO_VERSION' ) === true ) { // Do not allow Lingo extension to process value
110                $value .= "\n__NOGLOSSARY__";
111            }
112            $options = \ParserOptions::newFromAnon();
113            $this->properties[$name] = $parser->parse( $value, $title, $options )->getText( [ 'unwrap' => true ] );
114        } elseif ( is_string( $value ) ) {
115            $value = trim( $value );
116            $this->properties[$name] = htmlspecialchars( $value, ENT_NOQUOTES );
117        } else {
118            $this->properties[$name] = $value;
119        }
120        return true;
121    }
122
123    /**
124     * Unset element property by name
125     * @param string $name
126     */
127    public function unsetProperty( $name ) {
128        $name = strtolower( $name );
129
130        if ( isset( $this->properties[$name] ) ) {
131            unset( $this->properties[$name] );
132        }
133    }
134
135    /**
136     * Filling properties of the object according to the obtained data
137     * @global string $egMultiMaps_DelimiterParam
138     * @param string $param
139     * @param string|null $service Name of map service
140     * @return bool returns false if there were errors during parsing, it does not mean that the item was not added. Check with isValid()
141     */
142    public function parse( $param, $service = null ) {
143        global $egMultiMaps_DelimiterParam;
144        $this->reset();
145
146        $arrayparam = explode( $egMultiMaps_DelimiterParam, $param );
147
148        // The first parameter should always be coordinates
149        $coordinates = array_shift( $arrayparam );
150        if ( $this->parseCoordinates( $coordinates, $service ) === false ) {
151            $this->errormessages[] = \wfMessage( 'multimaps-unable-create-element', $this->getElementName() )->escaped();
152            return false;
153        }
154
155        // These parameters are optional
156        $this->isValid = true;
157        return $this->parseProperties( $arrayparam );
158    }
159
160    /**
161     * Filling property 'coordinates'
162     * @global string $egMultiMaps_CoordinatesSeparator
163     * @param string $coordinates
164     * @param string|null $service Name of map service
165     * @return bool
166     */
167    protected function parseCoordinates( $coordinates, $service = null ) {
168        global $egMultiMaps_CoordinatesSeparator;
169
170        $array = explode( $egMultiMaps_CoordinatesSeparator, $coordinates );
171        foreach ( $array as $value ) {
172            $point = new Point();
173            if ( $point->parse( $value, $service ) ) {
174                $this->coordinates[] = $point;
175            } else {
176                $this->errormessages[] = \wfMessage( 'multimaps-unable-parse-coordinates', $value )->escaped();
177                return false;
178            }
179        }
180        return true;
181    }
182
183    /**
184     * @param array $param
185     * @return bool false if there were errors during parsing
186     */
187    protected function parseProperties( array $param ) {
188        $return = true;
189        // filling properties with the names
190        $matches = [];
191        $properties = implode( '|', $this->availableProperties );
192        foreach ( $param as $key => $paramvalue ) {
193            if ( preg_match( "/^\\s*($properties)\\s*=(.+)$/si", $paramvalue, $matches ) ) {
194                if ( !$this->setProperty( $matches[1], $matches[2] ) ) {
195                    $return = false;
196                }
197                unset( $param[$key] );
198            }
199        }
200
201        // filling properties without the names
202        reset( $param );
203        $value = current( $param );
204        if ( $value === false ) {
205            return $return;
206        }
207        foreach ( $this->availableProperties as $name ) {
208            if ( $this->getProperty( $name ) === null ) {
209                if ( preg_match( '/^\s*$/s', $value ) == false ) { // Ignore empty values
210                    if ( !$this->setProperty( $name, $value ) ) {
211                        $return = false;
212                    }
213                }
214                $value = next( $param );
215                if ( $value === false ) {
216                    return $return;
217                }
218            }
219        }
220
221        $this->errormessages[] = \wfMessage( 'multimaps-element-more-parameters', $this->getElementName() )->escaped();
222        $notprocessed = [ $value ];
223        while ( $value = next( $param ) ) {
224            $notprocessed[] = $value;
225        }
226        $this->errormessages[] = \wfMessage( 'multimaps-element-parameters-not-processed', '"' . implode( '", "', $notprocessed ) . '"' )->escaped();
227        return false;
228    }
229
230    /**
231     * Checks if the object is valid
232     * @return bool
233     */
234    public function isValid() {
235        return $this->isValid;
236    }
237
238    /**
239     * Initializes the object again, and makes it invalid
240     */
241    public function reset() {
242        $this->isValid = false;
243        $this->coordinates = [];
244        $this->errormessages = [];
245        $this->properties = [];
246    }
247
248    /**
249     * Returns an error messages
250     * @return array
251     */
252    public function getErrorMessages() {
253        return $this->errormessages;
254    }
255
256    /**
257     * Returns an array of data
258     * @return array
259     */
260    public function getData() {
261        if ( $this->isValid() ) {
262            $ret = [];
263            foreach ( $this->coordinates as $pos ) {
264                $ret['pos'][] = $pos->getData();
265            }
266            return array_merge( $ret, $this->properties );
267        }
268    }
269}