|
| 1 | +## Usage: extending `AbstractRemoteFunctionCall` |
| 2 | + |
| 3 | +Let's assume you have a SAP remote function called |
| 4 | + `MY_COOL_SAP_REMOTE_FUNCTION`, that takes a date as input value and returns |
| 5 | + the according week. |
| 6 | + |
| 7 | +The class `MyCoolSapRemoteFunction` contains only code, that configures the API |
| 8 | + of your SAP remote function: |
| 9 | + |
| 10 | +* `getName()` returns the SAP remote function name. |
| 11 | +* `setDate()` sets the SAP remote function parameter. |
| 12 | +* `getReturnTypecast()` casts the result of the SAP remote function call to a |
| 13 | + DateTime object. |
| 14 | + |
| 15 | +```php |
| 16 | +<?php |
| 17 | + |
| 18 | +use phpsap\saprfc\AbstractRemoteFunctionCall; |
| 19 | +use kbATeam\TypeCast\TypeCastValue; |
| 20 | +use phpsap\DateTime\SapDateTime; |
| 21 | + |
| 22 | +/** |
| 23 | + * Class MyCoolSapRemoteFunction |
| 24 | + * |
| 25 | + * Get the calendar week to a given date. |
| 26 | + */ |
| 27 | +class MyCoolSapRemoteFunction extends AbstractRemoteFunctionCall |
| 28 | +{ |
| 29 | + /** |
| 30 | + * Define the name of the remote function. |
| 31 | + * @return string |
| 32 | + */ |
| 33 | + public function getName() |
| 34 | + { |
| 35 | + return 'MY_COOL_SAP_REMOTE_FUNCTION'; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Set the Date parameter for the remote function call. |
| 40 | + * @param \DateTime $dateTime The date to set. |
| 41 | + * @return $this |
| 42 | + */ |
| 43 | + public function setDate(\DateTime $dateTime) |
| 44 | + { |
| 45 | + $this->setParam('IV_DATE', $dateTime->format(SapDateTime::SAP_DATE)); |
| 46 | + return $this; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Define typecasting for SAP remote function return value. |
| 51 | + * @return \kbATeam\TypeCast\TypeCastValue |
| 52 | + */ |
| 53 | + protected function getReturnTypecast() |
| 54 | + { |
| 55 | + return new TypeCastValue(function ($result) { |
| 56 | + return SapDateTime::createFromFormat(SapDateTime::SAP_WEEK, $result['E_WEEK']); |
| 57 | + }); |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +With this you will be able to call your SAP remote function in your PHP code |
| 63 | + without having to worry about modules or connections. Plus you get an implicit |
| 64 | + documentation about your SAP remote function in case your editor supports auto |
| 65 | + completion based on composers autoloader. You can even test the API of the SAP |
| 66 | + remote function using unit tests. |
| 67 | + |
| 68 | +In case you're wondering about the SAP connection configuration here, |
| 69 | + [read how to configure a connection](saprfc-config). |
| 70 | + |
| 71 | +```php |
| 72 | +<?php |
| 73 | + |
| 74 | +use phpsap\saprfc\SapRfcConfigA; |
| 75 | + |
| 76 | +//stored configuration, JSON encoded |
| 77 | +$config = '{ |
| 78 | + "ashost": "sap.example.com", |
| 79 | + "sysnr": "001", |
| 80 | + "client": "01", |
| 81 | + "user": "username", |
| 82 | + "passwd": "password" |
| 83 | +}'; |
| 84 | +//create a new SAP remote function instance using the stored configuration |
| 85 | +$dateToWeek = new MyCoolSapRemoteFunction(new SapRfcConfigA($config)); |
| 86 | +/** |
| 87 | + * Set the parameter for the SAP remote function. |
| 88 | + * Your editor shows you the available parameters of the SAP remote function |
| 89 | + * and their value types. |
| 90 | + */ |
| 91 | +$dateToWeek->setDate(new DateTime('now')); |
| 92 | +//call the SAP remote function |
| 93 | +$week = $dateToWeek->invoke(); |
| 94 | +//echo "<year>W<week>" |
| 95 | +echo $week->format('o\Ww') . PHP_EOL; |
| 96 | +``` |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +[Go back to usage overview](usage) |
0 commit comments