Add in unit test of getCoordinates Address action and update TestProvider to be like...
[civicrm-core.git] / Civi / Api4 / Action / Address / GetCoordinates.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13 namespace Civi\Api4\Action\Address;
14
15 use Civi\Api4\Generic\Result;
16
17 /**
18 * Converts an address string to lat/long coordinates.
19 *
20 * @method $this setAddress(string $address)
21 * @method string getAddress()
22 */
23 class GetCoordinates extends \Civi\Api4\Generic\AbstractAction {
24
25 /**
26 * Address string to convert to lat/long
27 *
28 * @var string
29 * @required
30 */
31 protected $address;
32
33 public function _run(Result $result) {
34 $geocodingClassName = \CRM_Utils_GeocodeProvider::getUsableClassName();
35 $geocodingProvider = \CRM_Utils_GeocodeProvider::getConfiguredProvider();
36 if (!is_callable([$geocodingProvider, 'getCoordinates'])) {
37 throw new \API_Exception('Geocoding provider does not support getCoordinates');
38 }
39 $coord = $geocodingClassName::getCoordinates($this->address);
40 if (isset($coord['geo_code_1'], $coord['geo_code_2'])) {
41 $result[] = $coord;
42 }
43 elseif (!empty($coord['geo_code_error'])) {
44 throw new \API_Exception('Geocoding failed. ' . $coord['geo_code_error']);
45 }
46 }
47
48 }