gupalo / item-syncer
Item Syncer
Installs: 2 576
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
- doctrine/orm: ^2.14
- gupalo/dateutils: ^1.13
- phpspec/prophecy-phpunit: ^2.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
Sync remote to local items.
Install
composer require gupalo/item-syncer
How to use
Create 2 arrays with items implementing \Gupalo\ItemSyncer\SyncableEntityInterface
:
remoteItems
: usually from external API - source of truthlocalItems
: items from your DB
Update logic:
- remote item that is missing locally - create
- remote item that exists locally - update (you implement logic which properties should be updated)
- local item that is missing remotely - you decide by selecting sync method:
syncKeeping
: don't do anythingsyncArchiving
: if local item has methodarchive
then archive local itemsyncRemoving
: remove local items
If you use Doctrine and save diff to database then use DbItemSyncer
. If you have your own logic of processing diff
then use ItemSyncer and its diffKeeping
, diffArchiving
, diffRemoving
methods.
Example
$remoteItems = array_map( static fn(array $a) => Country::createFromApi($a), $this->countryApiClient->getCountries() ); $localItems = $this->countryRepository->findAll(); $diff = $this->dbItemSyncer->syncArchiving($remoteItems, $localItems); print_r($diff->stat()); // something like ['created' => 2, 'updated' => 180]