Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,50 @@ try {
}
```

## Usage with Laravel

The SDK includes a Laravel service provider for easy integration.

### Configuration

Add the following to your `config/services.php`:

```php
'wati' => [
'endpoint' => env('WATI_ENDPOINT'),
'token' => env('WATI_TOKEN'),
],
```

Add to your `.env` file:

```env
WATI_ENDPOINT=https://your-instance.wati.io/123456
WATI_TOKEN=your-bearer-token
```

### Usage

The service provider is auto-discovered. Simply inject or resolve `WatiClient`:

```php
use Wati\Http\WatiClient;
use Wati\Api\GetContacts;

class ContactController
{
public function __construct(
private readonly WatiClient $wati
) {}

public function index()
{
$response = $this->wati->send(new GetContacts());
return json_decode($response->getBody()->getContents(), true);
}
}
```

## API Reference

For full API documentation, visit [Wati API Docs](https://docs.wati.io/reference/introduction).
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ All notable changes to `phpjuice/wati-sdk` will be documented in this file.
- Pre-built API classes for Wati API:
- `Wati\Api\GetContacts` - Get paginated list of contacts
- PHP 8.3+ support
- Add Laravel service provider
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"require": {
"php": "^8.3 || ^8.4",
"ext-json": "*",
"phpjuice/wati-http-client": "^1.0"
"phpjuice/wati-http-client": "^1.0",
"illuminate/support": "^10.0|^11.0|^12.0"
},
"require-dev": {
"laravel/pint": "^1.27",
Expand Down Expand Up @@ -74,5 +75,12 @@
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"extra": {
"laravel": {
"providers": [
"Wati\\Laravel\\WatiServiceProvider"
]
}
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ parameters:
- tests
excludePaths:
- tests/*/data/*
- src/Laravel
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
<include>
<directory>src</directory>
</include>
<exclude>
<directory>src/Laravel</directory>
</exclude>
</source>
</phpunit>
18 changes: 9 additions & 9 deletions src/Api/Dto/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public function __construct(
public static function fromArray(array $data): self
{
return new self(
id: data_get($data, 'id', ''),
phone: data_get($data, 'phone', ''),
fullName: data_get($data, 'fullName', ''),
wAid: is_string($v = data_get($data, 'wAid')) ? $v : null,
firstName: is_string($v = data_get($data, 'firstName')) ? $v : null,
email: is_string($v = data_get($data, 'email')) ? $v : null,
contactStatus: is_string($v = data_get($data, 'contactStatus')) ? $v : null,
created: is_string($v = data_get($data, 'created')) ? $v : null,
lastUpdated: is_string($v = data_get($data, 'lastUpdated')) ? $v : null,
id: data_get_str($data, 'id', ''),
phone: data_get_str($data, 'phone', ''),
fullName: data_get_str($data, 'fullName', ''),
wAid: is_string($v = data_get_str($data, 'wAid')) ? $v : null,
firstName: is_string($v = data_get_str($data, 'firstName')) ? $v : null,
email: is_string($v = data_get_str($data, 'email')) ? $v : null,
contactStatus: is_string($v = data_get_str($data, 'contactStatus')) ? $v : null,
created: is_string($v = data_get_str($data, 'created')) ? $v : null,
lastUpdated: is_string($v = data_get_str($data, 'lastUpdated')) ? $v : null,
);
}
}
24 changes: 24 additions & 0 deletions src/Laravel/WatiServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Wati\Laravel;

use Illuminate\Support\ServiceProvider;
use Override;
use Wati\Http\WatiClient;
use Wati\Http\WatiEnvironment;

final class WatiServiceProvider extends ServiceProvider
{
#[Override]
public function register(): void
{
$this->app->singleton(WatiClient::class, fn (): WatiClient => new WatiClient(
new WatiEnvironment(
endpoint: config('services.wati.endpoint'),
bearerToken: config('services.wati.token'),
)
));
}
}
2 changes: 1 addition & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @param TDefault $default
* @return string|TDefault
*/
function data_get(array $data, string $key, mixed $default = null): mixed
function data_get_str(array $data, string $key, mixed $default = null): mixed
{
if (! array_key_exists($key, $data)) {
return $default;
Expand Down
32 changes: 16 additions & 16 deletions tests/helpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,42 @@
it('returns value when key exists', function (): void {
$data = ['name' => 'John', 'email' => 'john@example.com'];

expect(data_get($data, 'name'))->toBe('John')
->and(data_get($data, 'email'))->toBe('john@example.com');
expect(data_get_str($data, 'name'))->toBe('John')
->and(data_get_str($data, 'email'))->toBe('john@example.com');
});

it('returns default when key does not exist', function (): void {
$data = ['name' => 'John'];

expect(data_get($data, 'email'))->toBeNull()
->and(data_get($data, 'email', 'default'))->toBe('default');
expect(data_get_str($data, 'email'))->toBeNull()
->and(data_get_str($data, 'email', 'default'))->toBe('default');
});

it('returns default when value is null', function (): void {
$data = ['name' => null];

expect(data_get($data, 'name'))->toBeNull()
->and(data_get($data, 'name', 'default'))->toBe('default');
expect(data_get_str($data, 'name'))->toBeNull()
->and(data_get_str($data, 'name', 'default'))->toBe('default');
});

it('returns default when value is empty string', function (): void {
$data = ['name' => ''];

expect(data_get($data, 'name'))->toBeNull()
->and(data_get($data, 'name', 'default'))->toBe('default');
expect(data_get_str($data, 'name'))->toBeNull()
->and(data_get_str($data, 'name', 'default'))->toBe('default');
});

it('trims string values', function (): void {
$data = ['name' => ' John '];

expect(data_get($data, 'name'))->toBe('John');
expect(data_get_str($data, 'name'))->toBe('John');
});

it('returns default when trimmed value is empty', function (): void {
$data = ['name' => ' '];

expect(data_get($data, 'name'))->toBeNull()
->and(data_get($data, 'name', 'default'))->toBe('default');
expect(data_get_str($data, 'name'))->toBeNull()
->and(data_get_str($data, 'name', 'default'))->toBe('default');
});

it('returns non-string values as-is', function (): void {
Expand All @@ -51,14 +51,14 @@
'price' => 19.99,
];

expect(data_get($data, 'count'))->toBe(42)
->and(data_get($data, 'active'))->toBeTrue()
->and(data_get($data, 'items'))->toBe(['a', 'b'])
->and(data_get($data, 'price'))->toBe(19.99);
expect(data_get_str($data, 'count'))->toBe(42)
->and(data_get_str($data, 'active'))->toBeTrue()
->and(data_get_str($data, 'items'))->toBe(['a', 'b'])
->and(data_get_str($data, 'price'))->toBe(19.99);
});

it('returns default for missing nested keys', function (): void {
$data = ['user' => ['name' => 'John']];

expect(data_get($data, 'email', 'missing'))->toBe('missing');
expect(data_get_str($data, 'email', 'missing'))->toBe('missing');
});