aratech/kontentai

A lightweight package for wrapping kontentai PHP SDK

v0.9.56 2023-10-10 12:55 UTC

This package is auto-updated.

Last update: 2024-04-10 14:06:23 UTC


README

Summary

A simple Wrapper Class to the already existing Delivery Client that is provided by the lovely people at the Kontent.ai team.

Developers

Developed By Ammar Jlies, Jimmy Jradeh.

Installation Process

You must have a Laravel Application before you can install this package. After installing your laravel application you can run:

composer require aratech/kontentai

or adjust your composer.json file:

{
    "require": {
        "aratech/kontentai": "^0.7.0"
    }
}

Create a Kontent-ai Client

Creating a Kontent.ai Client is simple and easy, start by adding your Kontent.ai project key to your .env file, and name it KONTENT_AI_KEY:

KONTENT_AI_KEY = "Put Your Project Key Here"

Now, you can create a Kontent.ai Client and assign it to a variable ($app in the example below):

use Aratech\Kontentai;

$app = Kontentai::createClient();

Using our solution for querying

To query an item from your Kontent.ai project just use the -> operator:

$result = $app->about_us;

If you want to query multiple items, you can (almost) treat the querying process as if you are doing a query using the Laravel Query Builder Class:

$app->where("name", "article");

Returned type is a Kontentai Object (the same variable $app).

So, you can chain as many methods as you like on the $app client, and when you're done just use the fetch function to fetch the results:

$results = $app->language('es-ES')->where("name", "article")->fetch();

Returned type here is a Laravel collection conatining all the items that are returned from the query.

One Special use case we can do, is using the fetch method without any chained methods behind it:

$results = $app->fetch();

This will return all the items that are available in your Kontent.ai project.

You can also use the Methods that are already built in the DeliveryClient provided by Kontent-ai and chain them with our provided methods:

$results = $app->language('es-ES')->where("name", "article")->orderAsc('elements.product_name')->limit(10)->fetch();

By default, the chained methods will be cleared after you call the fetch() method, but, you can also clear them manually by calling the clearQuery() method on the app object:

$results = $app->clearQuery();

IF you want to grab an instance of the default Kontent.ai client, you can use the getClient() method:

$client = $app->getClient();

Available Methods

where($key, $value) find($id)

What's the idea behind this class?

The idea is to provide a way to chain methods on top of each other as if you are using the Laravel built-in Query Builder class.