Skip to content

michalmarcinkowski/sylius-api-php

This branch is 26 commits behind Lakion/sylius-api-php:master.

Repository files navigation

Sylius API PHP client library

Build status... Code Quality

PHP client for RESTful Sylius API!

Usage

Initialize client with OAuth2 authentication and mapping

$auth_client = new GuzzleHttp\Client([
    // URL for access_token request
    'base_url' => 'http://demo.sylius.org/oauth/v2/token',
]);
// oauth configuration
$auth_config = [
    "client_id" => "1_demo_client",
    "client_secret" => "secret_demo_client",
    "username" => 'api@example.com',
    "password" => 'api'
];
// create password grant-type
$grant_type = new \Nmrkt\GuzzleOAuth2\GrantType\PasswordCredentials($auth_client, $auth_config);
// create oauth subscriber with password grant-type
$oauthSubscriber = new \Nmrkt\GuzzleOAuth2\OAuth2Subscriber($grant_type);
// initialize uri mapping for custom uris, all other uris are autogenerated (plural name of the resource, in accordance with RESTful API resource naming best practices)
$uriMap = new \Sylius\Api\Map\ArrayUriMap(
    [
        'product-variants' => 'products/{productId}/variants',
        'taxons'           => 'taxonomies/{taxonomyId}/taxons',
        'items'            => 'orders/{orderId}/items',
        'coupons'          => 'promotions/{promotionId}/coupons',
        'provinces'        => 'countries/{countryId}/provinces'
    ]
);
// initializer api resolver
$apiResolver = new \Sylius\Api\ApiResolver($uriMap);
// create client
$client = \Sylius\Api\Client::createFromUrl('http://demo.sylius.org/api/', $apiResolver, $oauthSubscriber);

Get API for resource

$taxonomiesApi = $client->getApi('taxonomies');
$taxonsApi = $client->getApi('taxons');

Create resource

// right now because of translations you have to do it this way (will be changed in the nearest future to simply allow for ['name' => 'My taxonomy'])
$taxonomy = $taxonomiesApi->create(['translations' => ['en' => ['name' => 'My taxonomy']]]);
// for custom uris you have to specify uri parameters ('taxonomyId')
$taxon = $taxonsApi->create(['translations' => ['en' => ['name' => 'My taxon']]], ['taxonomyId' => $taxonomy['id']);

Update resource

$taxonomy = $taxonomiesApi->update($taxonomyId, ['translations' => ['en' => ['name' => 'My taxonomy updated']]]);
// for custom uris you have to specify uri parameters ('taxonomyId')
$taxon = $taxonsApi->update($taxonId, $taxonomy['translations' => ['en' => ['name' => 'My taxon' updated]]], ['taxonomyId' => $taxonomyId]);

Get resource

$taxonomy = $taxonomiesApi->get($taxonomyId);
// for custom uris you have to specify uri parameters ('taxonomyId')
$taxon = $taxonsApi->get($taxonId, ['taxonomyId' => $taxonomyId]);

Get all resources

  • Get paginated resources
// you can getPaginated resources (it returns array)
$taxonomies = $taxonomiesApi->getPaginated($page, $limitPerPage);
$taxons = $taxonsApi->getPaginated($page, $limitPerPage, ['taxonomyId' => $taxonomyId]);
  • Create paginator for resource
// you can create taxonomies paginator
$taxonomiesPaginator = $taxonomiesApi->createPaginator($limitPerPage);
// for custom uris you have to specify uri parameters ('taxonomyId'), rest is the same
$taxonsPaginator = $taxonsApi->createPaginator($limitPerPage, ['taxonomyId' => $taxonomyId]);
// returns array of elements for current page
$taxonomies = $taxonomiesPaginator->getCurrentPageResults();
// ... (do something with current page taxonomies)
// and go through all results
while ($taxonomiesPaginator->hasNextPage()) {
    $taxonomiesPaginator->nextPage();
    $taxonomies = $taxonomiesPaginator->getCurrentPageResults();
    // ... (do something with current page taxonomies)
}
  • Get all resources
// getAll method gets all resources, so you have to be careful about the memory usage
$taxonomies = $taxonomiesApi->getAll();
// for custom uris you have to specify uri parameters ('taxonomyId')
$taxons = $taxonsApi->getAll(['taxonomyId' => $taxonomyId]);

Delete resource

// returns whether the deletion was successful or not
// for custom uris you have to specify uri parameters ('taxonomyId')
$trueOrFalse = $taxonsApi->delete($taxonId, ['taxonomyId' => $taxonomyId]);
$trueOrFalse = $taxonomiesApi->delete($taxonomyId);

Bug tracking

This component uses GitHub issues. If you have found bug, please create an issue.

MIT License

License can be found here.

Authors

The library was originally created by:

See the list of contributors.

About

PHP client for RESTful Sylius API!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%