An open API service indexing awesome lists of open source software.

https://github.com/aaron-apelt/akeneo-php-lib

An extension of the Akeneo PHP API client for easier Akeneo service development.
https://github.com/aaron-apelt/akeneo-php-lib

akeneo api client library pim

Last synced: about 1 month ago
JSON representation

An extension of the Akeneo PHP API client for easier Akeneo service development.

Awesome Lists containing this project

README

          

# Akeneo PHP Lib

A modern, type-safe PHP library for interacting with the [Akeneo PIM API](https://api.akeneo.com/).
Designed for clean domain models, batch processing, and flexible (de)serialization.

---

## Features

- **Akeneo Object Abstractions**: Simple entity models for Akeneo objects.
- **Adapters**: Easy-to-use adapters for batch import/export with Akeneo.
- **Fluent Collections**: Powerful, lazy-evaluated collection operations.
- **Flexible Serialization**: Customizable serialization/denormalization.
- **Querying**: Query builder for advanced product searches.
- **Batch Upserts**: Efficient upsert and callback handling for large-scale imports.
- **Strict Types & Modern PHP**: Uses PHP 8+ features and strict typing throughout.

---

## Installation

```bash
composer require aaron-apelt/akeneo-php-lib
```

---

## Basic Usage

### Instantiate the Adapter

```php
use Akeneo\Pim\ApiClient\AkeneoPimClientBuilder;
use AkeneoLib\Adapter\ProductAdapter;
use AkeneoLib\Serializer\Serializer;

$clientBuilder = new AkeneoPimClientBuilder('https://your-akeneo-url.example.com');
$client = $clientBuilder
->buildAuthenticatedByPassword('client_id', 'secret', 'user', 'password');

$productApi = $client->getProductApi();
$serializer = new Serializer();

$adapter = new ProductAdapter($productApi, $serializer);
```

### Fetch Products

```php
foreach ($adapter->all() as $product) {
// $product is an instance of AkeneoLib\Entity\Product
echo $product->getIdentifier();
}
```

### Working with Fluent Collections

The `all()` method returns a `FluentAdapterResult` that supports lazy-evaluated collection operations:

```php
use AkeneoLib\Search\QueryParameter;

// Chain operations - only processes items as needed
$adapter->all()
->filter(fn($product) => $product->isEnabled())
->map(fn($product) => $product->getIdentifier())
->take(100)
->toArray();

// Pagination
$page2 = $adapter->all()
->skip(50)
->take(50)
->toArray();

// Execute side effects while maintaining chain ability
$adapter->all()
->each(fn($product) => logger()->info("Processing {$product->getIdentifier()}"))
->filter(fn($product) => $product->getFamily() === 'electronics')
->toArray();

// Terminal operations
$firstEnabled = $adapter->all()->first(fn($p) => $p->getEnabled());
$lastProduct = $adapter->all()->last();

// Practical reduce example - collect all identifiers
$identifiers = $adapter->all()->reduce(
fn($acc, $product) => [...$acc, $product->getIdentifier()],
[]
);
```

#### Available Methods

**Lazy Operations** (maintain lazy evaluation):
- `filter(callable $callback)` - Filter items
- `map(callable $callback)` - Transform items
- `each(callable $callback)` - Execute side effects
- `take(int $limit)` - Limit to first N items
- `skip(int $count)` - Skip first N items
- `chunk(int $size)` - Split into chunks

**Terminal Operations** (materialize the collection):
- `toArray()` - Convert to array
- `first(?callable $callback = null)` - Get first item
- `last(?callable $callback = null)` - Get last item
- `reduce(callable $callback, $initial)` - Reduce to single value
- `sort(callable $callback)` - Sort items (⚠️ loads all into memory)
- `unique(?callable $callback = null)` - Filter duplicates (⚠️ loads all into memory)

**Note**: Operations marked with ⚠️ materialize the entire collection into memory. Use with caution on large datasets from API cursors.

### Upsert Products in Batches

```php
$product = new AkeneoLib\Entity\Product('my-sku');
// ...set properties, values, etc.

$adapter->stage($product); // Will push automatically when batch size is reached
$adapter->push(); // Manually push any remaining staged products
```

### Custom Response Callback

```php
$adapter->onResponse(function ($response, $products, $dateTime) {
// Handle API response or log batch import results here
});
```

---

## Advanced

- **Querying**: Use `AkeneoLib\Search\QueryParameter` to filter, sort, and paginate.
- **Custom Serialization**: Swap out or configure the serializer.

---

## Testing

This library is tested with [Pest](https://pestphp.com/).
To run tests:

```bash
composer install
composer test
```