command_option.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /*
  3. * FusionPBX
  4. * Version: MPL 1.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is FusionPBX
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Mark J Crane <[email protected]>
  20. * Portions created by the Initial Developer are Copyright (C) 2008-2024
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. * Mark J Crane <[email protected]>
  25. * Tim Fry <[email protected]>
  26. */
  27. /**
  28. * Container object for creating command line options when creating a service
  29. * @author Tim Fry <[email protected]>
  30. */
  31. class command_option {
  32. private $short_option;
  33. private $long_option;
  34. private $description;
  35. private $short_description;
  36. private $long_description;
  37. private $functions;
  38. /**
  39. * Constructs an empty command_option
  40. */
  41. public function __construct() {
  42. $this->short_option = '';
  43. $this->long_option = '';
  44. $this->description = '';
  45. $this->short_description = '';
  46. $this->long_description = '';
  47. $this->functions = [];
  48. }
  49. /**
  50. * A factory method to create a new command_option
  51. * @param type $options
  52. * @return command_option
  53. */
  54. public static function new(...$options): command_option {
  55. $obj = new command_option();
  56. //automatically assign properties to the object that were passed in key/value pairs
  57. self::parse_options($obj, $options);
  58. //return the command_option with all properties filled in that were passed
  59. return $obj;
  60. }
  61. // used to parse object values when created
  62. private static function parse_options($obj, $options) {
  63. foreach ($options as $key => $value) {
  64. if (is_array($value)) {
  65. self::parse_options($obj, $value);
  66. }
  67. //call the method with the name of $key and pass it $value
  68. if (method_exists($obj, $key)) {
  69. $obj->{$key}($value);
  70. } elseif (property_exists($obj, $key)) {
  71. $obj->{$key} = $value;
  72. }
  73. }
  74. }
  75. /**
  76. * Sets or returns the short option value
  77. * @param string|null $short_option
  78. * @return $this
  79. */
  80. public function short_option(?string $short_option = null) {
  81. if (!empty($short_option)) {
  82. $this->short_option = $short_option;
  83. return $this;
  84. }
  85. return $this->short_option;
  86. }
  87. /**
  88. * Sets or returns the long option value
  89. * @param string|null $long_option
  90. * @return $this
  91. */
  92. public function long_option(?string $long_option = null) {
  93. if (!empty($long_option)) {
  94. $this->long_option = $long_option;
  95. return $this;
  96. }
  97. return $this->long_option;
  98. }
  99. /**
  100. * Set the general description
  101. * @param string|null $description
  102. * @return $this
  103. */
  104. public function description(?string $description = null) {
  105. if (!empty($description)) {
  106. $this->description = $description;
  107. return $this;
  108. }
  109. return $this->description;
  110. }
  111. /**
  112. * Sets or returns the short_description. If short_description is empty then the short_option is used as a default.
  113. * @param string|null $short_description When parameter is null, it returns the currently set value. When not null the short description is set to the passed value.
  114. * @return $this
  115. */
  116. public function short_description(?string $short_description = null) {
  117. if (!empty($short_description)) {
  118. $this->short_description = $short_description;
  119. return $this;
  120. }
  121. if (empty($this->short_description)) {
  122. if (str_ends_with($this->short_option, ':')) {
  123. $short = rtrim($this->short_option, ':');
  124. $short_description = "-$short <value>";
  125. } else {
  126. $short_description = '-' . $this->short_option;
  127. }
  128. } else {
  129. $short_description = $this->short_description;
  130. }
  131. return $short_description;
  132. }
  133. /**
  134. * Sets or returns the long_description. If long_description is empty then the long_option is used as a default.
  135. * @param string|null $long_description When parameter is null, it returns the currently set value. When not null the long description is set to the passed value.
  136. * @return $this
  137. */
  138. public function long_description(?string $long_description = null) {
  139. if ($long_description !== null) {
  140. $this->long_description = $long_description;
  141. return $this;
  142. }
  143. if (empty($this->long_description)) {
  144. if (str_ends_with($this->long_option, ':')) {
  145. $long = rtrim($this->long_option, ':');
  146. $long_description = "--$long <value>";
  147. } else {
  148. $long_description = '--' . $this->long_option;
  149. }
  150. } else {
  151. $long_description = $this->long_description;
  152. }
  153. return $long_description;
  154. }
  155. /**
  156. * Adds an array of callback functions replacing the existing callback functions
  157. * @param array|null $functions
  158. * @return $this
  159. */
  160. public function functions(?array $functions = null) {
  161. if ($functions !== null) {
  162. $this->functions = $functions;
  163. return $this;
  164. }
  165. return $this->functions;
  166. }
  167. /**
  168. * Appends the callback function to the array of existing callback functions
  169. * @param string|null $function When function param is set, the callback function will be appended to the list of functions. When called without a param, the array will be returned of current callbacks.
  170. * @return $this|array Returns the array of callbacks if no parameters passed or this object when appending a callback
  171. */
  172. public function callback(?string $function = null) {
  173. if ($function !== null) {
  174. $this->functions += [$function];
  175. return $this;
  176. }
  177. return $this->functions;
  178. }
  179. /**
  180. * Appends the callback function to the array of existing callback functions
  181. * @param string|null $function
  182. * @return $this
  183. */
  184. public function function_append(?string $function = null) {
  185. if ($function !== null) {
  186. $this->functions += [$function];
  187. return $this;
  188. }
  189. return $this->functions;
  190. }
  191. /**
  192. * Returns the array structure required for service
  193. * @return array
  194. */
  195. public function to_array(): array {
  196. $array['short_option'] = $this->short_option();
  197. $array['long_option'] = $this->long_option();
  198. $array['description'] = $this->description();
  199. $array['short_description'] = $this->short_description();
  200. $array['long_description'] = $this->long_description();
  201. $array['functions'] = $this->functions();
  202. return $array;
  203. }
  204. }
  205. /* Examples
  206. $command_option = command_option::new([
  207. 'short_option'=>'m',
  208. 'long_option' =>'my-option',
  209. 'description' =>'Create an option that uses -m or --my-option command-line parameter'
  210. ]);
  211. $command_option = command_option::new()
  212. ->short_option('m')
  213. ->long_option('my-option')
  214. ->description('Create an option that uses -m or --my-option command-line parameter');
  215. echo $command_option->description();
  216. $command_parsing_array = $command_option->to_array();
  217. print_r($command_parsing_array);
  218. //*/