PHP code example of seankndy / fluent-sonar-api

1. Go to this page and download the library: Download seankndy/fluent-sonar-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/ */

    

seankndy / fluent-sonar-api example snippets



use SeanKndy\SonarApi\Client;
use GuzzleHttp\Client as GuzzleClient;

$client = new Client(
    new GuzzleClient(),
    '<your api key>',
    'https://your-sonar-instance.sonar.software'
);

$accounts = $client
    ->accounts()
    ->with('accountStatus', 'tickets', 'company')
    ->where('id', 1234)
    ->get();
 
// $accounts is an Illuminate Collection of \SeanKndy\SonarApi\Resources\Account objects


$accounts = $client
    ->accounts()
    ->with([
        'tickets' => fn($query) => $query->sortBy('createdAt', 'ASC'),
    ])
    ->where('id', 1234)
    ->get();



namespace App;

use SeanKndy\SonarApi\Client;
use GuzzleHttp\Client as GuzzleClient;
use App\Support\SonarApi\Resources\InventoryItem;

$client = new Client(
    new GuzzleClient(),
    '<your api key>',
    'https://your-sonar-instance.sonar.software',
    [
        'inventoryItems' => InventoryItem::class,
    ],
);

$client->someObjects()
    ->where('field1', 'foo')
    ->where('field2', '!=', 'bar');
// logic:  (field1 = 'foo' AND field2 != 'bar')

$client->someObjects()
    ->where('field1', 'foo')
    ->where('field2', '!=', 'bar')
    ->orWhere('field3', 'baz');
// logic:  (field1 = 'foo' AND field2 != 'bar') OR field3 = 'baz'

$client->someObjects()
    ->where('field1', 'foo')
    ->where('field2', '!=', 'bar')
    ->orWhere('field3', 'baz')
    ->where('field4', 'qux')
// logic:  (field1 = 'foo' AND field2 != 'bar') OR (field3 = 'baz' AND field4 = 'qux')

$client->someObjects()
    ->where('field1', 'foo')
    ->where('field2', '!=', 'bar')
    ->orWhere('field3', 'baz')
    ->where('field4', ['qux', 'quux'])
// logic: INVALID, cannot call where() on array of values (which is an ORed search) after an orWhere()

$client->someObjects()
    ->where('field1', ['foo', 'bar'])
    ->where('field2', ['baz', 'qux'])
// logic:  (field1 = 'foo' OR field1 = 'bar') AND (field2 = 'baz' OR field2 = 'qux')

$client
    ->accounts()
    ->whereHas('addresses', fn($search) => $search->where('city', 'Chicago'))
    ->get()

$client
    ->accounts()
    ->whereHas('addresses', fn($search) => $search->where('city', 'Chicago'))
    ->orWhereHas('invoices')
    ->get()

$client
    ->accounts()
    ->whereNotHas('tickets')
    ->get()


$accounts = $client
    ->accounts()
    ->sortBy('createdAt', 'DESC')
    ->paginate(25, 1, 'http://site.com/some/path'); // 25 per page, 1st page, base url for links



namespace App\Support\SonarApi\Resources;

use SeanKndy\SonarApi\Resources\BaseResource;

class Company extends BaseResource
{
    public int $id;  
    public string $name;  
    public string $checksPayableTo;  
    public string $country;  
    public string $phoneNumber;  
    public string $primaryColor;  
    public string $secondaryColor;  
    public ?string $customerPortalUrl;  
    public bool $default;  
    public bool $enabled;  
    public bool $showRemittanceSlip;  
    public ?string $websiteAddress;
    public ?string $taxIdentification;
    public \DateTime $createdAt;
    public \DateTime $updatedAt; 
    /**
     * @var \SeanKndy\SonarApi\Resources\Account[]
     * /
    public array $accounts;
}


use SeanKndy\SonarApi\Client;
use SeanKndy\SonarApi\Resources\Ticket;
use SeanKndy\SonarApi\Mutations\Inputs\InputBuilder;
use SeanKndy\SonarApi\Types\Int64Bit;
use GuzzleHttp\Client as GuzzleClient;

$client = new Client(
    new GuzzleClient(),
    '<your api key>',
    'https://your-sonar-instance.sonar.software'
);

$ticket = $client
    ->mutations()
    ->updateTicket([
        'id!' => Int64Bit(12345),
        'input' => fn(InputBuilder $input) => $input->type('UpdateTicketMutationInput')->data([
            'subject' => 'An updated ticket subject',
        ])
    ])
    ->return(Ticket::class)
    ->run()
 
// $ticket will be an instance of newly created \SeanKndy\SonarApi\Resources\Ticket object


use SeanKndy\SonarApi\Client;
use SeanKndy\SonarApi\Resources\Ticket;
use SeanKndy\SonarApi\Types\Int64Bit;
use SeanKndy\SonarApi\Mutations\Inputs\UpdateTicketMutationInput;
use GuzzleHttp\Client as GuzzleClient;

$client = new Client(
    new GuzzleClient(),
    '<your api key>',
    'https://your-sonar-instance.sonar.software'
);

$ticket = $client
    ->mutations()
    ->updateTicket([
        'id!' => Int64Bit(12345),
        'input' => new UpdateTicketMutationInput([
            'subject' => 'An updated ticket subject',
        ])
    ])
    ->return(Ticket::class)
    ->run()
 
// $ticket will be an instance of newly created \SeanKndy\SonarApi\Resources\Ticket object


  
namespace App\Support\SonarApi\Mutations\Inputs;

use SeanKndy\SonarApi\Mutations\Inputs\BaseInput;
  
class UpdateTicketMutationInput extends BaseInput  
{  
    protected string $subject;
    protected string $description;  
    protected string $status; 
    protected string $priority;
    protected int $ticketParentId;
    protected bool $unsetTicketGroupId;  
}

$updateTicketMutationInput = new UpdateTicketMutationInput(['subject' => 'new subject']);


use SeanKndy\SonarApi\Client;
use SeanKndy\SonarApi\Resources\SuccessResponse;
use SeanKndy\SonarApi\Types\Int64Bit;
use SeanKndy\SonarApi\Mutations\Inputs\InputBuilder;
use GuzzleHttp\Client as GuzzleClient;

$client = new Client(
    new GuzzleClient(),
    '<your api key>',
    'https://your-sonar-instance.sonar.software'
);

// 
// inline approach
//
$successResponse = $client
    ->mutations()
    ->createDataUsages([
        // notice the type name wrapped in brackets [] below
        'input' => fn(InputBuilder $input) => $input->type('[CreateDataUsageMutationInput]')->data([
            [
                'accountId' => net Int64Bit(12345),
                'dataSourceIdentifier' => 'usage-collector1',
                // etc
            ],
            // ...
        ])
    ])
    ->return(SuccessResponse::class)
    ->run()

// 
// class-based input approach
//
$successResponse = $client
    ->mutations()
    ->createDataUsages([
        'input' => [
            new CreateDataUsageMutationInput([
                'accountId' => net Int64Bit(12345),
                'dataSourceIdentifier' => 'usage-collector1',
                // etc
            ]),
            new CreateDataUsageMutationInput([
                //...
            ]),
            // ...
        ],
    ])
    ->return(SuccessResponse::class)
    ->run()


use SeanKndy\SonarApi\Client;
use GuzzleHttp\Client as GuzzleClient;

$client = new Client(
    new GuzzleClient(),
    '<your api key>',
    'https://your-sonar-instance.sonar.software'
);

// get the first \SeanKndy\SonarApi\Resources\File resource associated to Account 1234
$file = $client
    ->files()
    ->where('fileableId', 1234)
    ->where('fileableType', 'Account')
    ->first();

// $file is just an API resource object.  now let's get a stream to the actual file data.
$fileStream = $client->fileStream($file);
    
// $fileStream is now a stream resource


use SeanKndy\SonarApi\Client;
use GuzzleHttp\Client as GuzzleClient;

$client = new Client(
    new GuzzleClient(),
    '<your api key>',
    'https://your-sonar-instance.sonar.software'
);

// get Account 1234 -- we'll upload the file and associate it to this resource.
$account = $client
    ->accounts()
    ->where('id', 1234)
    ->first();

// upload file, associate it to $account
$client
    ->uploadFile(
        '/path/to/file', // path or resource to file to upload
        'testing.txt' // name to give file in Sonar
    )->associateResource($account);