| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2025 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Door Access device capability. Such objects are |
| 16 |
* serialized and stored onto the database as blobs. |
| 17 |
* |
| 18 |
* @since 1.18.4 (J) - 1.8.4 (WP) |
| 19 |
*/ |
| 20 |
final class VBODooraccessDeviceCapability |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @var ?string |
| 24 |
*/ |
| 25 |
protected ?string $id = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var ?string |
| 29 |
*/ |
| 30 |
protected ?string $title = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var ?string |
| 34 |
*/ |
| 35 |
protected ?string $description = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var ?string |
| 39 |
*/ |
| 40 |
protected ?string $icon = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var ?array |
| 44 |
*/ |
| 45 |
protected ?array $params = null; |
| 46 |
|
| 47 |
/** |
| 48 |
* @var ?string |
| 49 |
*/ |
| 50 |
protected ?string $callback = null; |
| 51 |
|
| 52 |
/** |
| 53 |
* Class constructor. |
| 54 |
*/ |
| 55 |
public function __construct() |
| 56 |
{} |
| 57 |
|
| 58 |
/** |
| 59 |
* Magic method to get or set protected properties. |
| 60 |
* |
| 61 |
* @param string $name The method name called. |
| 62 |
* @param array $arguments The arguments of the method called. |
| 63 |
* |
| 64 |
* @return mixed |
| 65 |
* |
| 66 |
* @throws Exception |
| 67 |
*/ |
| 68 |
public function __call(string $name, array $arguments) |
| 69 |
{ |
| 70 |
if (preg_match('/^get([a-z]+)/i', $name, $matches)) { |
| 71 |
// getter method invoked |
| 72 |
$property = strtolower($matches[1]); |
| 73 |
|
| 74 |
// access the requested property, if set |
| 75 |
return $this->{$property} ?? null; |
| 76 |
} |
| 77 |
|
| 78 |
if (preg_match('/^set([a-z]+)/i', $name, $matches) && $arguments) { |
| 79 |
// setter method invoked |
| 80 |
$property = strtolower($matches[1]); |
| 81 |
|
| 82 |
if (property_exists($this, $property)) { |
| 83 |
// set the value for the requested property |
| 84 |
$this->{$property} = $arguments[0]; |
| 85 |
} |
| 86 |
|
| 87 |
// return self for chain-ability |
| 88 |
return $this; |
| 89 |
} |
| 90 |
|
| 91 |
throw new Exception(sprintf('Could not call device capability method %s.', $name), 500); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Tells if the device capability provides parameters for its execution. |
| 96 |
* |
| 97 |
* @return bool |
| 98 |
*/ |
| 99 |
public function providesParams() |
| 100 |
{ |
| 101 |
return !empty($this->params); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Tells if the device capability meets the minimum requirements. |
| 106 |
* |
| 107 |
* @return bool |
| 108 |
*/ |
| 109 |
public function isValid() |
| 110 |
{ |
| 111 |
return !empty($this->id) && !empty($this->title) && !empty($this->callback); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Executes the capability through the callback instructions, if set. |
| 116 |
* |
| 117 |
* @param VBODooraccessIntegrationAware $integration The provider integration object. |
| 118 |
* @param VBODooraccessIntegrationDevice $device The device executing the capability. |
| 119 |
* @param ?array $options Optional settings obtained from the params. |
| 120 |
* |
| 121 |
* @return ?string Optional execution result string to display. |
| 122 |
* |
| 123 |
* @throws Exception |
| 124 |
*/ |
| 125 |
public function execute(VBODooraccessIntegrationAware $integration, VBODooraccessIntegrationDevice $device, ?array $options = null) |
| 126 |
{ |
| 127 |
if (!is_string($this->callback) || !is_callable([$integration, $this->callback])) { |
| 128 |
throw new Exception('Could not execute device capability.', 500); |
| 129 |
} |
| 130 |
|
| 131 |
// execute the capability on the provider integration object |
| 132 |
return call_user_func_array([$integration, $this->callback], [$device, $options]); |
| 133 |
} |
| 134 |
} |
| 135 |
|