Comment on page
How to use
In the current version of our PHP SDK you can find only a few methods that you can implement in your code but they're so powerful that enable you to do pretty anything with MBurger CMS.
The SDK will return an array representing the response. By default methods that returns a list of objects are limited to 25 elements. See below for how to use pagination.
This SDK offers a fluent syntax composed of these parts:
- 1.Class instance
- 2.Parameters or Modifiers
- 3.Desired function
Below are reported all parameters/modifiers and functions with some examples.
For a complete list of all available parameters we remand to the official API documentation https://docs.mburger.cloud/api-docs-1.
To instantiate the SDK simply use:
$sdk = new MBurger();
or use the compact version (useful for chaining):
$response = (new MBurger())->getProject();
To obtain a project
$response = (new MBurger())->getProject();
To obtain a list of blocks
$response = (new MBurger())->getBlocks();
To obtain a specific block by id
$response = (new MBurger())->getBlock($block_id);
To obtain a list of sections
$response = (new MBurger())->getSections($block_id);
To obtain a specific section by id or slug
$response = (new MBurger())->getSection($block_id_or_slug);
Below a list of modifiers (or middle methods) useful to personalize the request.
3.3.1 Locale
To request a specific locale use the
locale(string $locale)
modifier. Example:$response = (new MBurger())->locale($locale)->getProject();
If the requested locale is not present an automatic fallback will be done. If you want to force the fallback if the requested locale is not present or is empty use the
forceLocaleFallback()
modifier:$response = (new MBurger())->forceLocaleFallback()->getBlock();
3.3.2 Pagination
To use pagination in functions that returns a list of items use the modifiers
skip(int $skip)
and take(int $take)
. Example:$response = (new MBurger())->skip(5)->take(50)->getSections(100);
Defaults are 0 and 25. The response will include a meta field containing the info for pagination, like total items and actual index.
3.3.3 Including
To include relations on the response are available a set of convenience methods. This can be useful, for example, for obtaining in one request all the desired sections with the related elements.
NOTE: Not all methods are compatible with all functions. The SDK will throw anMBurgeInvalidRequestException
exception. Check our API references for more info.
NOTE: loading a lot of relations in one request can have a negative impact on performances.
include(array $include)
: generic method, you can pass an array of desired relations.includeBlocks()
: it will include blocks. Available only ongetProject()
.includeSections()
: it will include sections. Available only ongetBlocks()
andgetBlock()
.includeElements()
: it will include elements. Available only ongetSections()
andgetSection()
.includeStructure()
: it will include block structure. Available only ongetProject()
,getBlocks()
andgetBlock()
.includeBeacons()
: it will include beacons. Available only ongetProject()
,getSections()
andgetSection()
.includeContracts()
: it will include blocks. Available only ongetProject()
.
Example: get first 15 sections of block 100 with the related elements
$response = (new MBurger())->take(15)->includeElements()->getSections(100);
3.3.4 Sorting
To apply specific orders is available this method
sortBy(string $value, string $direction = 'asc')
. the first argument specify on which value do the sorting, and the second the direction. Example:$response = (new MBurger())->sortBy('created_at', 'desc')->take(15)->getSections(100);
The default sorting in by id and ascending.
NOTE: this method is only available on functions that returns a list of items.
3.3.4 Filtering
To filter items are available a set of convenience methods.
NOTE: Not all methods are compatible with all functions. The SDK will throw anMBurgeInvalidRequestException
exception. Check our API references for more info.
NOTE: these methods are only available on functions that returns a list of items.
filterByIds(array $ids)
: it filters based an array on id with an exact match. Available only ongetBlocks()
andgetSections()
.filterByRelation(int $block_id, int $section_id)
: it filters based on related sections. Available only ongetSections()
.filterByValue(array $values, string $element_name = null)
: it filters based on array of values. Specifying the second parameterelement_name
the filtering in done only on elements the match the name. Available only ongetSections()
.filterByTitle(string $title)
: it filters based on title. Available only ongetBlocks()
.filterByGeofence(float $latNE, float $latSW, float $lngNE, float $lngSW)
: it filters based on geofence rectangle. Available only ongetSections()
.
3.3.5 Distance
If you have sections with elements of type address (which automatically contains coordinates) you can obtain your distance from the "section" (like a POI) with the method
istance(float $latitude, float $longitude)
by providing your coordinates. Example:$response = (new MBurger())->distance(22.231232, 16.325322)->getSections(100);
NOTE: this method is only available on functionsgetSections()
.
3.3.6 Slug
Normally a section is retrieved by id or slug. MBurger tries to infer which method to use based on the type of the parameter: if it's numeric it will use the id, if it's a string it will use the slug. Is possible to force using the slug with the following method
forceSlug()
. Example:$response = (new MBurger())->locale('en')->forceSlug()->getSection('321287');
NOTE: this method is only available on functionsgetSection()
.
MBurger SDK use an exception based error system, in case of error it will throw an exception with a descritive message.
Every function contains a method
cache(int $cache_ttl = 0)
to automatically cache the response. The TTL is in seconds. Example:$response = (new MBurger())->cache(300)->getSections(100);
There are utility methods useful to transform the response in a more usable format.
To transform blocks use
transformBlocks($blocks)
. Example:$blocks = (new MBurger())->getBlocks();
$data = MBurger::transformBlocks($blocks);
Last modified 2yr ago