Skip to content

Commit

Permalink
Allow association and virtual field handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MisatoTremor committed May 22, 2019
1 parent 2337185 commit 3ed7b63
Show file tree
Hide file tree
Showing 28 changed files with 473 additions and 177 deletions.
2 changes: 1 addition & 1 deletion Annotation/ImportExclude.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

/**
/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand Down
5 changes: 5 additions & 0 deletions AvroCsvBundle.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Avro\CsvBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand Down
16 changes: 16 additions & 0 deletions AvroCsvEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,20 @@ final class AvroCsvEvents
* @Event("Avro\CsvBundle\Event\RowErrorEvent")
*/
const ROW_ERROR = 'avro_csv.row_error';
/**
* The ASSOCIATION_FIELD event occurs when the importer hits an association field.
*
* This event allows you to implement handling of the association.
*
* @Event("Avro\CsvBundle\Event\AssociationFieldEvent")
*/
const ASSOCIATION_FIELD = 'avro_csv.association_field';
/**
* The CUSTOM_FIELD event occurs when the importer hits an import field that is not recognized by Doctrine.
*
* This event allows you to implement handling of these fields.
*
* @Event("Avro\CsvBundle\Event\CustomFieldEvent")
*/
const CUSTOM_FIELD = 'avro_csv.custom_field';
}
2 changes: 1 addition & 1 deletion Controller/ExportController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

/**
/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand Down
12 changes: 6 additions & 6 deletions Controller/ImportController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

/**
/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand Down Expand Up @@ -87,7 +87,7 @@ public function uploadAction($alias)
* Move the csv file to a temp dir and get the user to map the fields.
*
* @param Request $request The request
* @param string $alias The objects alias
* @param string $alias The objects alias
*
* @return Response
*/
Expand Down Expand Up @@ -118,7 +118,7 @@ public function mappingAction(Request $request, $alias)

// Recreate form and create proper fields child for each header
$form = $this->formFactory->create(ImportFormType::class, null, ['field_choices' => $fieldChoices]);
$form->get('fields')->setData(array_fill_keys((array)$headers, null));
$form->get('fields')->setData(array_fill_keys((array) $headers, null));
$form->handleRequest($request);

$rows = $this->reader->getRows($this->container->getParameter('avro_csv.sample_count'));
Expand All @@ -129,7 +129,7 @@ public function mappingAction(Request $request, $alias)
[
'form' => $form->createView(),
'alias' => $alias,
'headers' => array_combine((array)$headers, (array)$fileHeaders),
'headers' => array_combine((array) $headers, (array) $fileHeaders),
'headersJson' => json_encode(
$this->caseConverter->toTitleCase($fileHeaders),
JSON_FORCE_OBJECT
Expand All @@ -152,7 +152,7 @@ public function mappingAction(Request $request, $alias)
* Previews the uploaded csv and allows the user to map the fields.
*
* @param Request $request The request
* @param string $alias The objects alias
* @param string $alias The objects alias
*
* @return Response
*/
Expand Down Expand Up @@ -184,7 +184,7 @@ public function processAction(Request $request, $alias)

$request->getSession()->getFlashBag()->set(
'success',
$this->importer->getImportCount() . ' items imported. ' . $this->importer->getImportErrors() . ' errors.'
$this->importer->getImportCount().' items imported. '.$this->importer->getImportErrors().' errors.'
);
} else {
$request->getSession()->getFlashBag()->set('error', 'Import failed. Please try again.');
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/AvroCsvExtension.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

/**
/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

/**
/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand Down
2 changes: 1 addition & 1 deletion Doctrine/Importer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

/**
/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand Down
101 changes: 101 additions & 0 deletions Event/AssociationFieldEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Avro\CsvBundle\Event;

use Symfony\Component\EventDispatcher\Event;

/**
* Row association event.
*
* @author Steffen Roßkamp <[email protected]>
*/
class AssociationFieldEvent extends Event
{
protected $object;
protected $associationMapping;
protected $row;
protected $fields;
protected $headers;
protected $index;

/**
* @param object $object
* @param array $associationMapping
* @param array $row
* @param array $fields
* @param array $headers
* @param int $index
*/
public function __construct(object $object, array $associationMapping, array $row, array $fields, array $headers, int $index)
{
$this->object = $object;
$this->associationMapping = $associationMapping;
$this->row = $row;
$this->fields = $fields;
$this->headers = $headers;
$this->index = $index;
}

/**
* @return object
*/
public function getObject(): object
{
return $this->object;
}

/**
* Get field association mapping.
*
* @return array
*/
public function getAssociationMapping(): array
{
return $this->associationMapping;
}

/**
* Get field row.
*
* @return array
*/
public function getRow(): array
{
return $this->row;
}

/**
* Get mapped fields.
*
* @return array
*/
public function getFields(): array
{
return $this->fields;
}

/**
* Get CSV headers.
*
* @return array
*/
public function getHeaders(): array
{
return $this->headers;
}

/**
* Get the current field index.
*
* @return int
*/
public function getIndex(): int
{
return $this->index;
}
}
102 changes: 102 additions & 0 deletions Event/CustomFieldEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Avro\CsvBundle\Event;

use ReflectionClass;
use Symfony\Component\EventDispatcher\Event;

/**
* Row association event.
*
* @author Steffen Roßkamp <[email protected]>
*/
class CustomFieldEvent extends Event
{
protected $object;
protected $reflectionClass;
protected $row;
protected $fields;
protected $headers;
protected $index;

/**
* @param object $object
* @param ReflectionClass $reflectionClass
* @param array $row
* @param array $fields
* @param array $headers
* @param int $index
*/
public function __construct(object $object, ReflectionClass $reflectionClass, array $row, array $fields, array $headers, int $index)
{
$this->object = $object;
$this->reflectionClass = $reflectionClass;
$this->row = $row;
$this->fields = $fields;
$this->headers = $headers;
$this->index = $index;
}

/**
* @return object
*/
public function getObject(): object
{
return $this->object;
}

/**
* Get object reflection class.
*
* @return ReflectionClass
*/
public function getReflectionClass(): ReflectionClass
{
return $this->reflectionClass;
}

/**
* Get field row.
*
* @return array
*/
public function getRow(): array
{
return $this->row;
}

/**
* Get mapped fields.
*
* @return array
*/
public function getFields(): array
{
return $this->fields;
}

/**
* Get CSV headers.
*
* @return array
*/
public function getHeaders(): array
{
return $this->headers;
}

/**
* Get the current field index.
*
* @return int
*/
public function getIndex(): int
{
return $this->index;
}
}
6 changes: 3 additions & 3 deletions Event/ExportEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

/**
/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand Down Expand Up @@ -33,7 +33,7 @@ public function __construct(ExporterInterface $exporter)
*
* @return ExporterInterface
*/
public function getExporter()
public function getExporter(): ExporterInterface
{
return $this->exporter;
}
Expand All @@ -43,7 +43,7 @@ public function getExporter()
*
* @return QueryBuilder
*/
public function getQueryBuilder()
public function getQueryBuilder(): QueryBuilder
{
return $this->exporter->getQueryBuilder();
}
Expand Down
6 changes: 3 additions & 3 deletions Event/ExportedEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

/**
/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand All @@ -21,7 +21,7 @@ class ExportedEvent extends Event
/**
* @param string $content Csv data
*/
public function __construct($content)
public function __construct(string $content)
{
$this->content = $content;
}
Expand All @@ -31,7 +31,7 @@ public function __construct($content)
*
* @return string
*/
public function getContent()
public function getContent(): string
{
return $this->content;
}
Expand Down
Loading

0 comments on commit 3ed7b63

Please sign in to comment.