Skip to content

Commit cabd793

Browse files
author
Moderyo
committed
feat: Moderyo PHP SDK v2.0.7
- Full 27-category content moderation - Policy decision with triggered rules, highlights, confidence scores - Simplified scores (toxicity, hate, harassment, scam, violence, fraud) - Long text analysis with sentence-level scoring - Batch moderation support - PHP 8.4+ property hooks (isBlocked, isFlagged, isAllowed) - Typed exception hierarchy - Laravel integration (ServiceProvider, Facade, Middleware, Validation Rule) - Automatic retry with exponential backoff - 32 passing unit tests
0 parents  commit cabd793

25 files changed

+2273
-0
lines changed

.github/workflows/ci.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
php-version: ['8.4']
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ matrix.php-version }}
23+
extensions: json, mbstring
24+
coverage: none
25+
26+
- name: Install dependencies
27+
run: composer install --no-interaction --prefer-dist
28+
29+
- name: Run tests
30+
run: vendor/bin/phpunit --colors=always

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/vendor/
2+
/coverage/
3+
/.phpunit.cache/
4+
composer.lock
5+
.env
6+
*.phar
7+
.idea/
8+
.vscode/
9+
.DS_Store

CHANGELOG.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Changelog
2+
3+
All notable changes to the Moderyo PHP SDK will be documented in this file.
4+
5+
## [2.0.7] - 2025-02-17
6+
7+
### Added
8+
- Full 27-category content moderation support
9+
- Policy decision with triggered rules, highlights, and confidence scores
10+
- Long text analysis with sentence-level scoring
11+
- Batch moderation support
12+
- Simplified scores (toxicity, hate, harassment, scam, violence, fraud)
13+
- Detected phrases extraction
14+
- Abuse signals support
15+
- Shadow mode decision support
16+
- Laravel integration (Service Provider, Facade, Middleware, Validation Rule)
17+
- Automatic retry with exponential backoff
18+
- PHP 8.4+ property hooks for computed properties (`isBlocked`, `isFlagged`, `isAllowed`)
19+
- Typed exception hierarchy (Authentication, RateLimit, Validation, QuotaExceeded, Network)
20+
- Both camelCase and snake_case config key support
21+
- Constructor accepts API key string or ModeryoConfig object
22+
23+
### Changed
24+
- Version bump from 0.2.0 to 2.0.7
25+
- PHP requirement updated to ^8.4 (uses property hooks)
26+
- Separated exception classes into individual files (PSR-4 compliance)
27+
28+
## [0.2.0] - 2025-01-15
29+
30+
### Added
31+
- Initial SDK implementation
32+
- Basic moderation endpoint
33+
- Guzzle HTTP client integration

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024-2025 Moderyo
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Moderyo PHP SDK
2+
3+
Official PHP SDK for the Moderyo Content Moderation API.
4+
5+
[![PHP Version](https://img.shields.io/badge/php-%3E%3D8.4-blue)](https://php.net)
6+
[![Packagist Version](https://img.shields.io/packagist/v/moderyo/sdk)](https://packagist.org/packages/moderyo/sdk)
7+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
8+
9+
## Requirements
10+
11+
- PHP 8.4 or higher
12+
- Guzzle HTTP 7.x
13+
14+
## Installation
15+
16+
```bash
17+
composer require moderyo/sdk
18+
```
19+
20+
## Quick Start
21+
22+
```php
23+
use Moderyo\ModeryoClient;
24+
25+
$client = new ModeryoClient('your-api-key');
26+
27+
$result = $client->moderate('Hello, this is a friendly message!');
28+
29+
if ($result->isBlocked) {
30+
echo "BLOCKED: " . ($result->policyDecision?->reason ?? 'Policy violation') . "\n";
31+
} elseif ($result->isFlagged) {
32+
echo "FLAGGED for review\n";
33+
} else {
34+
echo "ALLOWED\n";
35+
}
36+
```
37+
38+
## Configuration
39+
40+
### API Key String
41+
42+
```php
43+
$client = new ModeryoClient('your-api-key');
44+
```
45+
46+
### API Key with Options
47+
48+
```php
49+
$client = new ModeryoClient('your-api-key', [
50+
'baseUrl' => 'https://api.moderyo.com',
51+
'timeout' => 60,
52+
'maxRetries' => 5,
53+
]);
54+
```
55+
56+
### ModeryoConfig Object
57+
58+
```php
59+
use Moderyo\ModeryoConfig;
60+
use Moderyo\ModeryoClient;
61+
62+
$config = new ModeryoConfig([
63+
'apiKey' => 'your-api-key',
64+
'baseUrl' => 'https://api.moderyo.com',
65+
'timeout' => 30,
66+
'maxRetries' => 3,
67+
'retryDelay' => 1.0,
68+
'defaultModel' => 'omni-moderation-latest',
69+
]);
70+
71+
$client = new ModeryoClient($config);
72+
```
73+
74+
### Environment Variables
75+
76+
```php
77+
// Set MODERYO_API_KEY and optionally MODERYO_BASE_URL
78+
$client = ModeryoClient::fromEnv();
79+
```
80+
81+
## Moderation
82+
83+
### Basic
84+
85+
```php
86+
$result = $client->moderate('Text to check');
87+
88+
echo "Blocked: " . ($result->isBlocked ? 'Yes' : 'No') . "\n";
89+
echo "Flagged: " . ($result->isFlagged ? 'Yes' : 'No') . "\n";
90+
echo "Allowed: " . ($result->isAllowed ? 'Yes' : 'No') . "\n";
91+
```
92+
93+
### With Options
94+
95+
```php
96+
$result = $client->moderate('Text to check', [
97+
'model' => 'omni-moderation-latest',
98+
'longTextMode' => true,
99+
'mode' => 'enforce',
100+
'risk' => 'balanced',
101+
]);
102+
```
103+
104+
### Batch
105+
106+
```php
107+
$batch = $client->moderateBatch(['Hello', 'Bad text', 'Spam']);
108+
echo "Blocked: " . count($batch->getBlocked()) . "\n";
109+
echo "Has blocked: " . ($batch->hasBlocked() ? 'Yes' : 'No') . "\n";
110+
```
111+
112+
## Error Handling
113+
114+
```php
115+
use Moderyo\Exceptions\AuthenticationException;
116+
use Moderyo\Exceptions\RateLimitException;
117+
use Moderyo\Exceptions\ValidationException;
118+
use Moderyo\Exceptions\QuotaExceededException;
119+
use Moderyo\Exceptions\NetworkException;
120+
use Moderyo\Exceptions\ModeryoException;
121+
122+
try {
123+
$result = $client->moderate($text);
124+
} catch (AuthenticationException $e) {
125+
// Invalid API key (401)
126+
} catch (RateLimitException $e) {
127+
sleep((int) $e->retryAfter);
128+
} catch (ValidationException $e) {
129+
// Invalid input (400/422)
130+
} catch (QuotaExceededException $e) {
131+
// Plan quota exceeded (402)
132+
} catch (NetworkException $e) {
133+
// Connection/timeout after retries
134+
} catch (ModeryoException $e) {
135+
echo "Error: " . $e->getMessage() . "\n";
136+
}
137+
```
138+
139+
## Laravel Integration
140+
141+
Add MODERYO_API_KEY=your-key to .env. Service provider auto-discovers.
142+
143+
```php
144+
use Moderyo\Laravel\Facades\Moderyo;
145+
$result = Moderyo::moderate('Check this text');
146+
```
147+
148+
## Running Tests
149+
150+
```bash
151+
composer install
152+
vendor/bin/phpunit
153+
```
154+
155+
## License
156+
157+
MIT - see [LICENSE](LICENSE).

composer.json

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"name": "moderyo/sdk",
3+
"description": "Official PHP SDK for Moderyo Content Moderation API",
4+
"version": "2.0.7",
5+
"type": "library",
6+
"license": "MIT",
7+
"keywords": [
8+
"moderyo",
9+
"moderation",
10+
"content-moderation",
11+
"safety",
12+
"ai",
13+
"trust-safety"
14+
],
15+
"authors": [
16+
{
17+
"name": "Moderyo",
18+
"email": "dev@moderyo.com",
19+
"homepage": "https://moderyo.com"
20+
}
21+
],
22+
"homepage": "https://github.com/moderyo/moderyo-php",
23+
"support": {
24+
"issues": "https://github.com/moderyo/moderyo-php/issues",
25+
"docs": "https://docs.moderyo.com/php"
26+
},
27+
"require": {
28+
"php": "^8.4",
29+
"ext-json": "*",
30+
"guzzlehttp/guzzle": "^7.0",
31+
"psr/http-client": "^1.0",
32+
"psr/log": "^3.0"
33+
},
34+
"require-dev": {
35+
"phpunit/phpunit": "^10.0",
36+
"phpstan/phpstan": "^1.10",
37+
"squizlabs/php_codesniffer": "^3.7",
38+
"mockery/mockery": "^1.6",
39+
"orchestra/testbench": "^8.0"
40+
},
41+
"autoload": {
42+
"psr-4": {
43+
"Moderyo\\": "src/"
44+
}
45+
},
46+
"autoload-dev": {
47+
"psr-4": {
48+
"Moderyo\\Tests\\": "tests/"
49+
}
50+
},
51+
"scripts": {
52+
"test": "phpunit",
53+
"test:coverage": "phpunit --coverage-html coverage",
54+
"analyse": "phpstan analyse src --level=8",
55+
"cs": "phpcs src --standard=PSR12",
56+
"cs:fix": "phpcbf src --standard=PSR12"
57+
},
58+
"extra": {
59+
"laravel": {
60+
"providers": [
61+
"Moderyo\\Laravel\\ModeryoServiceProvider"
62+
],
63+
"aliases": {
64+
"Moderyo": "Moderyo\\Laravel\\Facades\\Moderyo"
65+
}
66+
}
67+
},
68+
"config": {
69+
"sort-packages": true,
70+
"optimize-autoloader": true
71+
},
72+
"minimum-stability": "stable",
73+
"prefer-stable": true
74+
}

config/moderyo.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Moderyo API Key
7+
|--------------------------------------------------------------------------
8+
|
9+
| Your Moderyo API key. You can find this in your Moderyo dashboard.
10+
|
11+
*/
12+
'api_key' => env('MODERYO_API_KEY'),
13+
14+
/*
15+
|--------------------------------------------------------------------------
16+
| Base URL
17+
|--------------------------------------------------------------------------
18+
|
19+
| The base URL for the Moderyo API. You typically don't need to change this.
20+
|
21+
*/
22+
'base_url' => env('MODERYO_BASE_URL', 'https://api.moderyo.com'),
23+
24+
/*
25+
|--------------------------------------------------------------------------
26+
| Timeout
27+
|--------------------------------------------------------------------------
28+
|
29+
| The timeout for API requests in seconds.
30+
|
31+
*/
32+
'timeout' => env('MODERYO_TIMEOUT', 30),
33+
34+
/*
35+
|--------------------------------------------------------------------------
36+
| Max Retries
37+
|--------------------------------------------------------------------------
38+
|
39+
| The maximum number of retries for failed requests.
40+
|
41+
*/
42+
'max_retries' => env('MODERYO_MAX_RETRIES', 3),
43+
];

0 commit comments

Comments
 (0)