vrok / import-export
Helper to import from / export to JSON object graphs.
Requires
- php: ^8.3
- doctrine/common: ^3.5.0
- doctrine/persistence: ^3.4.0|^4.0.0
- symfony/property-access: ^7.0.0
Requires (Dev)
- doctrine/orm: ^3.0.2|^4.0.x-dev
- friendsofphp/php-cs-fixer: ^v3.68.2
- phpunit/phpunit: ^11.5.3
- rector/rector: ^2.0.7
- roave/security-advisories: dev-latest
- symfony/cache: ^7.0.0
README
Doctrine Helper to import from / export to JSON object graphs
Uses Symfony's PropertyAccess
to convert objects to arrays or generate objects
from arrays. Which classes & properties can be exported/imported (and how) can
be controlled with the ExportableProperty
and ImportableProperty
PHP
attributes: Only classes that have at least one ExportableProperty
can be
converted to array, only classes that have at least one ImportableProperty
can
be instantiated from array.
Uses Doctrines ClassUtils
to safely handle proxies.
Usage
Short Example, for more details see ExportEntity / ImportEntity and ExportHelperTest / ImportHelperTest for all features. Allows to export referenced entities (or only their identifiers) and collections.
use Vrok\ImportExport\ExportableProperty; use Vrok\ImportExport\ExportHelper; use Vrok\ImportExport\ImportableProperty; use Vrok\ImportExport\ImportHelper; class Entity { #[ExportableProperty] #[ImportableProperty] public int $id = 0; #[ExportableProperty] #[ImportableProperty] public ?\DateTimeImmutable $timestamp = null; } $entity = new Entity(); $entity->id = 1; $entity->timestamp = new \DateTimeImmutable(); $helper = new ExportHelper(); $export = $helper->objectToArray($entity); $exportList = $helper->collectionToArray([$entity]); /* $export === [ 'id' => 1, 'timestamp' => '2022-03-23....', ] */ $helper = new ImportHelper(); $newInstance = $helper->objectFromArray($export, Entity::class); $newInstances = $helper->collectionFromArray([$export], Entity::class);
Features
Export
- can output object graphs, if a property pointing to another entity or DTO is
marked with
ExportableProperty
- can export an identifier, to be later mapped to the actual record, by using
the
referenceByIdentifier
argument of theExportableProperty
attribute, e.g. instead of exporting the wholeUser
, only it's ID could be exported for thecreatedBy
property of another record. - can export collections of objects (either by using
collectionToArray
or when a property is a DoctrineCollection
or an array and marked withasList
), even when they are of different types (e.g. through inheritance). An_entityClass
field will contain the actual class. - can limit exported data to only allowed properties by using the
propertyFilter
argument offromArray
- can prevent (otherwise exportable property) from being exported by using the
propertyFilter
argument offromArray
and settingisExcludeFilter
totrue
Import
- can reference existing records: if a property (collection or single object)
points to an "importable entity" and the dataset contains an
int
/string
the givenObjectManager
is used to fetch the specified record from the database - handle object graphs:
{ "name": "base entity", "nested": { "description": "for a property with type hint", "child": { "value": "for a property that is e.g. typed as an Interface", "_entityClass": "\\My\\DtoClass" } } }
- import only permitted fields (even when the dataset contains more, potentially
importable properties) by using the
propertyFilter
argument offromArray
- exclude fields from importing (even when they are in the dataset and potentially
importable) by using the
propertyFilter
argument offromArray
and settingisExcludeFilter
totrue
- automatically persist generated
Dependencies
doctrine/common
for theClassUtils
(proxy handling) andCollection
propertiesdoctrine/persistence
for theObjectManager
(importing references)symfony/property-access
for setting/reading (private, protected) properties
Future Improvements
- what could be done with Symfony's serializer?
- can already transform object (graphs) to array
- properties can be marked with groups to be exportable
- how to mark properties to only be exported as identifier? new attribute? how implement that, how is APIP doing this?
- how to mark array properties to be treated as DTO-List?
- how to ignore some properties without the need to hardcode groups? (exclude-filter)
- how to limit to some properties without the need to hardcode groups? (include-filter)
- evaluate Doctrine's ORM collection attributes to check for allowed element class
- Improve union type handling (e.g. multiple base types: int|string)
- not only export to arrays but also directly to files
- support exporting to multiple files (split to reduce size for large datasets)
- not only import from arrays but also directly from JSON files
- support importing from multiple files (have been split to reduce size)