Git Product home page Git Product logo

Comments (4)

ffdixon avatar ffdixon commented on August 23, 2024

Good feedback! Your right that it's still in it's early days. We want BigBlueButton to have the best possible API for PHP.

from bigbluebutton-api-php.

GhaziTriki avatar GhaziTriki commented on August 23, 2024

Thank you for your feedback @goldsky.

As @ffdixon said this API is still a baby and we want it to grow with a lot of good care.

Here you are talking about two things:

  1. Converting object to array for API responses.
  2. Store properties in array property instead of using object properties.

I agree the way you described is the fastest way to implement features. However, this API is developed with the following approaches in mind:

  • The API would have a logic associated to it.
  • Data should be passed to functions (class methods here) safely.
  • The code need to be used easily by a junior developer, he can understand it. It also need to be used fast by a senior developer, he doesn't need to dig in the BBB API documentation or this API source code to check what array keys he should add.
  • Code must easy to maintain, fully tested and fully covered.

Using arrays is the way most PHP developers are used to, but developers implementing BBB for their customers are coming from different backgrounds. We need to keep this as much understandable and easy to use as possible without forcing a developer to read the API documentation.

from bigbluebutton-api-php.

goldsky avatar goldsky commented on August 23, 2024

he doesn't need to dig in the BBB API documentation or this API source code to check what array keys he should add.

I think we have different opinion on this.
If the API has different wordings to the parameters, then it makes people reading the documentation twice, eg on create:

  1. the BBB's parameter: name
  2. this API: setMeetingName

while I'm proposing

$param->set('use_the_same_bbb_parameter_here', $value);

which makes less confusion, not even to mention about the difference parameters of name and meetingName wordings.

I understand your opinion, though, but on this topic, I just want to target the Parameters classes, which give a XML object response, which I don't think have any benefit for bringing entire XML's methods into the response while what user needs are only the key=>value pairs output.

Or, for making this optional, how about adding some kind of fromArray($params) and toArray() methods?

Example on CreateMeetingParameters

namespace BigBlueButton\Parameters;

/**
 * Class CreateMeetingParameters.
 */
class CreateMeetingParameters extends BaseParameters
{
    /**
     * @var array
     */
    private $meetingId;

    /**
     * @var string
     */
    private $meetingName;

    /////////////////////////////
    // ... other properties here
    /////////////////////////////

    /**
     * @var string
     */
    private $query = array();

    /**
     * @var array
     */
    private $meta = array();

    /**
     * CreateMeetingParameters constructor.
     *
     * @param $meetingId
     * @param $meetingName
     */
    public function __construct($meetingId, $meetingName)
    {
        $this->meetingId   = $meetingId;
        $this->meetingName = $meetingName;
    }

    /////////////////////////////
    // ... other methods here
    /////////////////////////////

    public function fromArray($params)
    {
        foreach ($params as $k => $v) {
            $this->query[$k] = $v;
        }
    }

    public function toArray()
    {
        return $this->query;
    }

    /**
     * @param string $key
     *
     * @return mixed
     */
    public function get($key)
    {
        return $this->query[$key];
    }

    /**
     * @param string $key
     * @param mixed  $val
     *
     * @return CreateMeetingParameters
     */
    public function set($key, $val = '')
    {
        $this->query[$key] = $val;

        return $this;
    }

    /**
     * @return string
     */
    public function getMeta($key)
    {
        return $this->meta[$key];
    }

    /**
     * @param string $key
     * @param string $value
     *
     * @return CreateMeetingParameters
     */
    public function setMeta($key, $value)
    {
        $key = preg_replace('/^meta_/', '', $key);
        $this->meta[$key] = $value;

        return $this;
    }

}

Example on Meeting

namespace BigBlueButton\Core;

/**
 * Class Meeting
 * @package BigBlueButton\Core
 */
class Meeting
{
    /**
     * @var string
     */
    private $meetingId;

    /**
     * @var string
     */
    private $meetingName;

    /////////////////////////////
    // ... other properties here
    /////////////////////////////

    /**
     * @var bool
     */
    private $hasUserJoined;

    /**
     * @var $xml \SimpleXMLElement
     */
    private $rawXML;

    /**
     * Meeting constructor.
     * @param $xml \SimpleXMLElement
     */
    public function __construct($xml)
    {
        // ... adding this
        $this->rawXML = $xml;

        $this->meetingId             = $xml->meetingID->__toString();
        $this->meetingName           = $xml->meetingName->__toString();
        $this->creationTime          = doubleval($xml->createTime);
        $this->creationDate          = $xml->createDate->__toString();
        $this->voiceBridge           = intval($xml->voiceBridge);
        $this->dialNumber            = $xml->dialNumber->__toString();
        $this->attendeePassword      = $xml->attendeePW->__toString();
        $this->moderatorPassword     = $xml->moderatorPW->__toString();
        $this->hasBeenForciblyEnded  = $xml->hasBeenForciblyEnded->__toString() == 'true';
        $this->isRunning             = $xml->running->__toString() == 'true';
        $this->participantCount      = intval($xml->participantCount);
        $this->listenerCount         = intval($xml->listenerCount);
        $this->voiceParticipantCount = intval($xml->voiceParticipantCount);
        $this->videoCount            = intval($xml->videoCount);
        $this->duration              = intval($xml->duration);
        $this->hasUserJoined         = $xml->hasUserJoined->__toString() == 'true';
    }

    /////////////////////////////
    // ... other methods here
    /////////////////////////////

    public function toArray()
    {
        return json_decode(json_encode($this->rawXML), true);
    }

}

from bigbluebutton-api-php.

goldsky avatar goldsky commented on August 23, 2024

FYI, I'm building a MODX CMS integration and recreating my own API heavily inspired by this API, but based on my purpose.

So for example to create a meeting,

The createMeeting method is this short:

    public function createMeeting($params, array $meta = array(), $postFields = '')
    {
        if (!isset($params['meetingID']) || empty($params['meetingID'])) {
            $params['meetingID'] = uniqid();
        }
        if (!isset($params['name']) || empty($params['name'])) {
            $params['name'] = 'BBBx-' . $params['meetingID'];
        }
        $meta = array_merge($meta, array(
            'origin' => 'MODX',
            'origin-url' => MODX_SITE_URL,
            'origin-name' => $this->modx->getOption('site_name'),
            'origin-extra' => 'bbbx',
            'origin-extra-version' => $this->config['version'],
            'origin-extra-author' => 'goldsky <[email protected]>',
        ));
        $response = $this->server->createMeeting($params, $meta, $postFields);
        if (empty($response) || (isset($response['returncode']) && $response['returncode'] == 'FAILED')) {
            $err = 'Unable to connect to server: ' . $response['message'];
            $this->setError($err);
            $this->modx->log(modX::LOG_LEVEL_ERROR, $err, '', __METHOD__, __FILE__, __LINE__);
            return;
        }

        return $response;
    }

which previously was this long

    public function createMeeting($options, $xml = '')
    {
        if (!isset($options['meetingID']) || empty($options['meetingID'])) {
            $meetingID = uniqid();
        } else {
            $meetingID = $options['meetingID'];
        }
        unset($options['meetingID']);
        if (!isset($options['name']) || empty($options['name'])) {
            $meetingName = 'BBBx-' . $meetingID;
        } else {
            $meetingName = $options['meetingName'];
        }
        unset($options['meetingName']);

        $params = new \BigBlueButton\Parameters\CreateMeetingParameters($meetingID, $meetingName);

        foreach ($options as $k => $v) {
            if (is_string($v)) {
                $v = trim($v);
            }
            if (empty($v)) {
                continue;
            }
            switch ($k) {
                case 'attendeePW':
                    $params->setAttendeePassword($v);
                    break;
                case 'moderatorPW':
                    $params->setModeratorPassword($v);
                    break;
                case 'welcome':
                    $params->setWelcomeMessage($v);
                    break;
                case 'dialNumber':
                    $params->setDialNumber($v);
                    break;
                case 'voiceBridge':
                    $params->setVoiceBridge($v);
                    break;
                case 'webVoice':
                    $params->setWebVoice($v);
                    break;
                case 'maxParticipants':
                    $params->setMaxParticipants((int) $v);
                    break;
                case 'logoutURL':
                    $params->setLogoutUrl($v);
                    break;
                case 'record':
                    $params->setRecord((boolean) $v);
                    break;
                case 'duration':
                    $params->setDuration((int) $v);
                    break;
                case 'meta':
                    $meta = array_map('trim', explode(',', $v));
                    foreach ($meta as $val) {
                        list($x, $y) = explode('=', $val);
                        $params->setMeta($x, $y);
                    }
                    break;
                case 'moderatorOnlyMessage':
                    $params->setModeratorOnlyMessage($v);
                    break;
                case 'autoStartRecording':
                    $params->setAutoStartRecording((boolean) $v);
                    break;
                case 'allowStartStopRecording':
                    $params->setAllowStartStopRecording((boolean) $v);
                    break;
                default:
                    break;
            }
        }
        $params->setMeta('origin', 'MODX');
        $params->setMeta('origin-url', MODX_SITE_URL);
        $params->setMeta('origin-name', $this->modx->getOption('site_name'));
        $params->setMeta('origin-extra', 'bbbx');
        $params->setMeta('origin-extra-version', $this->config['version']);
        $params->setMeta('origin-extra-author', 'goldsky <[email protected]>');

        $response = $this->bbb->createMeeting($params, $xml);
        $result = $response->getRawXml();
        if (empty($result) || !isset($result->returncode) || $result->returncode == 'FAILED') {
            $err = 'Creating meeting was failed.';
            $this->setError($err);
            $this->modx->log(modX::LOG_LEVEL_ERROR, $err);
            return;
        }
        $meeting = json_decode(json_encode($result), true);
        unset($meeting['returncode']);

        return $meeting;
    }

The switch-case is really a tedious job.

from bigbluebutton-api-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.