PHP code example of 4myth / api

1. Go to this page and download the library: Download 4myth/api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

4myth / api example snippets


use Myth\Api;

Api::name();
[
"name"    => "manager",
"clients" => [
      # Client name
      "client-name" => [
          "secret"   => "secret",
          "base_uri" => "http://127.0.0.1/api/v1",
          "models"   => [
              App\User::class => [
                  "uri"         => "user",
                  "transformer" => App\UserApiTransformer::class,
              ],
          ],
          "options"  => [
              "http" => [],
          ],
      ],
  ],
]

[
    "client-1" => [],
    "client-2" => [],
];

[
    "client-1" => [
        "models" => [
            App\User::class => [],
            Some\Name\SomeModel::class => [],
        ],
    ]  
];

namespace App;

use Illuminate\Database\Eloquent\Model;
use Myth\Api\Traits\HasApiManager;

class User extends Model
{
    use HasApiManager;

    // ....
}

php artisan myth:make-client-transformer TransformerName
   
public function body(): array
{
    return [];
}
 
public function body(): array
{
    return [ "name" => "MyTh", "password" => 123 ];
}

use Myth\Api;

$client = Api::client("client-name");

use Myth\Api;

$client = Api::client("client-name");

/** @var Myth\Api\ClientModelWrapper $model */
$ClientModelWrapper = $client->model(User::class);
$ClientModelWrapper = $client->model(User::find(1));

/** @var \Illuminate\Database\Eloquent\Model $model */
 $model = $ClientModelWrapper->model();

use Myth\Api;


// facade
$body = [ "name" => "MyTh", "password" => 123 ];
$response = Api::sendToClient("client-name", User::calss, $body);

// ----------------------

// client wrapper
$client = Api::client("client-name");

$user = User::find(123);
$response = $client->sendData($user);
dd($response);

$body = [ "name" => "Name", "password" => "123456789"];
$response = $client->sendData(User::class,$body);
dd($response);

// ----------------------

// by model directly
$user = User::find(2);
$response = $user->sendToClient("client-name");
dd($response);

use Myth\Api;

$data = Api::clientData("client-name", User::class, $sync = true)->get();
$data = Api::clientData("client-name", User::class, $sync = false)->get();
$data = Api::clientData("client-name", User::class, $sync = null)->get();
dd($data);

$client = Api::client('client-name');
$data = $client->model(User::class)->data($sync = true)->get();
$data = $client->model(User::class)->data($sync = false)->get();
$data = $client->model(User::class)->data($sync = null)->get();
dd($data);

$data = User::clientData("client-name", $sync = true)->get();
$data = User::clientData("client-name", $sync = false)->get();
$data = User::clientData("client-name", $sync = null)->get();
dd($data);

use Myth\Api;

$model = Api::syncWithClient("client-name", $client_id = 4, $model = User::find(1));
$model = Api::client("client-name")->syncModel($client_id = 5, $model = User::find(1));
$model = User::find(1)->syncWithClient("client-name", $client_id = 3);

use Myth\Api;

$model = Api::syncedWithClient("client-name", $client_id = 4, $model = User::find(1));
$model = Api::client("client-name")->syncedModel($client_id = 5, $model = User::find(1));
$model = User::find(1)->syncedWithClient("client-name", $client_id = 3);

use Myth\Api;

$model = Api::unsyncWithClient("client-name", $client_id = 1, $model = User::find(1));
$model = Api::client("client-name")->unsyncModel($client_id = 1, $model = User::find(1));
$model = User::find(1)->unsyncWithClient("client-name", $client_id = 1);
bash
php artisan vendor:publish --tag=myth-api