Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.

Adding a Function

Peter Bishop edited this page Mar 8, 2020 · 2 revisions

Adding a new API function for use within this framework is easy. To start, add a new .php file to the Functions directory with your function name as the file name. For example, AddCredit.php.

Use the following template for your new file:

<?php

namespace WHMCSAPI\Functions;

class AddCredit
{
    public static $action = 'AddCredit';

    public const ATTRIBUTES = [
        'clientid', 'description', 'amount',
        'date', 'adminid', 'type',
    ];

    public const REQUIRED_ATTRIBUTES = [
        'clientid', 'description', 'amount',
    ];

    public const ADDITIONAL_REQUIREMENTS = [
        'clientid' => 'numeric',
        'amount'   => 'float',
        'date'     => 'date',
        'adminid'  => 'numeric',
        'type' => [
            'add', 'remove',
        ],
    ];

    public const DEFAULTS = [
        'type' => 'add',
    ];
}

All four of the above attributes should be present:

Parameter Type Description
$action string The name of the class or API function being called.
ATTRIBUTES array All attributes that the function can support.
REQUIRED_ATTRIBUTES array Any attributes which must be specified before execute.
ADDITIONAL_REQUIREMENTS array Not necessarily required, but must adhere to specific requirements if set. e.g. array, datetime, date, ipaddress, email or an array of accepted strings
DEFAULTS array A key/value pair or 'attribute' => 'default' which sets a default value on initialization.

After creating your function file, create a file in the tests directory named after your function, with Test.php appended. (e.g. AddCreditTest.php)

Finally, ensure the test covers at minimum:

  • Calling the command
  • Failing when required attributes are not set
  • Ensuring attributes (minimum the required attributes)
  • That the API call can be made

Clone this wiki locally