ludicat / api-gar
用于GAR法国行政API的PSR-18 API包装器
v1.0.0
2022-10-19 11:10 UTC
Requires
- php: >=7.3
- ext-simplexml: *
Requires (Dev)
- phpunit/phpunit: ^9.5
Suggests
- php-http/guzzle7-adapter: ^1.0
README
GAR是法国政府的主要SSO,主要用于学院和高中。此SDK为PHP集成提供了抽象层。
支持的当前GAR版本:6.1 - 2022年6月
安装
运行
composer req ludicat/api-gar
您需要一个PSR-18 http api包,这里包含一个内置的GuzzleAdapter。如果您想创建自己的,只需扩展Ludicat\ApiGar\Adapter\AbstractAbonnementWs
composer req php-http/guzzle7-adapter
快速入门
我们假设您已经申请了GAR服务并获得了一个认证的pem密钥。(请记得向他们提供您的应用程序“通知”)
首先,创建一个客户端
$serviceProvider = new \Ludicat\ApiGar\Model\ServiceProvider(
'123456789', // Your company siret
\Ludicat\ApiGar\Model\ServiceProvider::ISNI_NONE, // ISNI number or none constant
'ark:/12345/myRessource' // Your resource ID
);
// Could be another adapter
$adapter = new \Ludicat\ApiGar\Adapter\GuzzleAdapter(
__DIR__.'/config/cert/app.pem', // Pem key (certified by government)
__DIR__.'/config/cert/app.key', // Private key
'Accordeon' // Private key pass,
Ludicat\ApiGar\Adapter\AbonnementWsInterface::ENDPOINT_DEV // Default one
// Ludicat\ApiGar\Adapter\AbonnementWsInterface::ENDPOINT_PROD // Once you're ready for production, please use this constant instead.
);
$client = new \Ludicat\ApiGar\Client($adapter, $serviceProvider);
然后您可以调用任何方法
// Create / retrieve the Abonnement object, you can pass the $serviceProvider for default values preset (recommanded)
$abonnement = new \Ludicat\ApiGar\Model\Abonnement($serviceProvider);
$abonnement
->setCommentaireAbonnement('Ceci est un test')
->setDebutValidite((new \DateTime())->format('Y-m-d\T00:00:00'))
->setAnneeFinValidite(date('Y') . '-' . (date('Y')+1))
->addUaiEtab('0560010G')
->setLibelleRessource('Centre de ressources pedagogiques en francais')
->setTypeAffectation(\Ludicat\ApiGar\Model\Abonnement::TYPE_INDIV)
->setCategorieAffectation(\Ludicat\ApiGar\Model\Abonnement::CATEGORY_TRANSFERABLE)
->setNbLicenceGlobale(\Ludicat\ApiGar\Model\Abonnement::UNLIMITED_LICENCE)
->setCodeProjetRessource('CODEPRJ001')
;
// Optional, you can validate the object here for easier data control
$validator = new \Ludicat\ApiGar\Validator\AbonnementValidator();
$validatorResponse = $validator->validate($abonnement);
if (!$validatorResponse->isValid()) {
foreach ($validatorResponse->getViolations() as $violation) {
printf('%s: %s', $violation->getProperty(), $violation->getMessage());
echo PHP_EOL;
}
// Send request
$response = $client->create($abonnement);
// $response = $client->update($abonnement);
// $response = $client->delete($abonnement);
// Please note they use 201 (as it should be) for OK create response, not 200
if (201 === $response->getStatusCode()) {
// Logic
}
// Don't forget to save your idAbonnement somewhere if you used the default generated one
获取所有Abonnement
// Create client
// Then just call this method
$result = $client->getAbonnements();
您可以根据特定标准进行筛选(如GAR文档中所述)
$abonnementFilter = new \Ludicat\ApiGar\Model\AbonnementFilter();
$abonnementFilter
->setTri(\Ludicat\ApiGar\Model\AbonnementFilter::TRI_DSC) // Default sort column to id abonnement
->addFiltre((new \Ludicat\ApiGar\Model\Filtre(
\Ludicat\ApiGar\Model\Filtre::FILTRE_PUBLIC_CIBLE,
\Ludicat\ApiGar\Model\Abonnement::PUBLIC_CIBLE_ELEVE
)))
->addFiltre((new \Ludicat\ApiGar\Model\Filtre(
\Ludicat\ApiGar\Model\Filtre::FILTRE_CATEGORY_AFFECTATION,
\Ludicat\ApiGar\Model\Abonnement::CATEGORY_TRANSFERABLE
)))
;
// A list of Abonnement objects
$result = $client->getAbonnements($abonnementFilter);
获取所有Etablissement
// A list of Etablissement objects
$result = $client->getEtablissements();
运行测试
make tests