partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

Configuration.php (5232B)


      1 <?php
      2 
      3 namespace PartKeepr\ImportBundle\Configuration;
      4 
      5 use Symfony\Component\PropertyAccess\PropertyAccess;
      6 
      7 class Configuration extends BaseConfiguration
      8 {
      9     /**
     10      * @var FieldConfiguration[]
     11      */
     12     private $fields = [];
     13 
     14     /**
     15      * @var ManyToOneConfiguration[]
     16      */
     17     private $manyToOneAssociations = [];
     18 
     19     /**
     20      * @var OneToManyConfiguration[]
     21      */
     22     private $oneToManyAssociations = [];
     23 
     24     public function parseConfiguration($importConfiguration)
     25     {
     26         if (property_exists($importConfiguration, "fields")) {
     27             foreach ($importConfiguration->fields as $field => $configuration) {
     28                 if ($this->classMetadata->hasField($field)) {
     29                     $fieldConfiguration = new FieldConfiguration(
     30                         $this->classMetadata,
     31                         $this->baseEntity,
     32                         $this->reflectionService,
     33                         $this->em,
     34                         $this->advancedSearchFilter,
     35                         $this->iriConverter
     36                     );
     37                     $fieldConfiguration->setFieldName($field);
     38 
     39                     $fieldConfiguration->setPath($this->getPath($field));
     40                     if ($fieldConfiguration->parseConfiguration($configuration) !== false) {
     41                         $this->fields[] = $fieldConfiguration;
     42                     }
     43                 } else {
     44                     //throw new \Exception("Field $field not found in ".$this->baseEntity);
     45                 }
     46             }
     47         }
     48 
     49         if (property_exists($importConfiguration, "manytoone")) {
     50             foreach ($importConfiguration->manytoone as $manyToOne => $configuration) {
     51                 if ($this->classMetadata->hasAssociation($manyToOne)) {
     52                     $targetClass = $this->classMetadata->getAssociationTargetClass($manyToOne);
     53                     $cm = $this->em->getClassMetadata($targetClass);
     54 
     55                     $manyToOneconfiguration = new ManyToOneConfiguration(
     56                         $cm,
     57                         $targetClass,
     58                         $this->reflectionService,
     59                         $this->em,
     60                         $this->advancedSearchFilter,
     61                         $this->iriConverter
     62                     );
     63                     $manyToOneconfiguration->setAssociationName($manyToOne);
     64                     $manyToOneconfiguration->setPath($this->getPath($manyToOne));
     65 
     66                     if ($manyToOneconfiguration->parseConfiguration($configuration) !== false) {
     67                         $this->manyToOneAssociations[] = $manyToOneconfiguration;
     68                     }
     69                 } else {
     70                     //throw new \Exception("Association $manyToOne not found in ".$this->baseEntity);
     71                 }
     72             }
     73         }
     74 
     75         if (property_exists($importConfiguration, "onetomany")) {
     76             foreach ($importConfiguration->onetomany as $oneToMany => $configuration) {
     77                 if ($this->classMetadata->hasAssociation($oneToMany)) {
     78                     $targetClass = $this->classMetadata->getAssociationTargetClass($oneToMany);
     79                     $cm = $this->em->getClassMetadata($targetClass);
     80                     $oneToManyConfiguration = new OneToManyConfiguration(
     81                         $cm,
     82                         $targetClass,
     83                         $this->reflectionService,
     84                         $this->em,
     85                         $this->advancedSearchFilter,
     86                         $this->iriConverter
     87                     );
     88                     $oneToManyConfiguration->setAssociationName($oneToMany);
     89                     $oneToManyConfiguration->setPath($this->getPath($oneToMany));
     90                     if ($oneToManyConfiguration->parseConfiguration($configuration) !== false) {
     91                         $this->oneToManyAssociations[] = $oneToManyConfiguration;
     92                     }
     93                 } else {
     94                     //throw new \Exception("Association $oneToMany not found in ".$this->baseEntity);
     95                 }
     96             }
     97         }
     98 
     99         return true;
    100     }
    101 
    102     public function import($row, $obj = null)
    103     {
    104         if ($obj === null) {
    105             $obj = new $this->baseEntity();
    106             $this->persist($obj);
    107         }
    108 
    109         $accessor = PropertyAccess::createPropertyAccessor();
    110 
    111         foreach ($this->fields as $field) {
    112             $name = $field->getFieldName();
    113             $data = $field->import($row);
    114 
    115             if ($data !== null) {
    116                 $accessor->setValue($obj, $name, $data);
    117             }
    118         }
    119 
    120         foreach ($this->manyToOneAssociations as $manyToOneAssociation) {
    121             $name = $manyToOneAssociation->getAssociationName();
    122             $data = $manyToOneAssociation->import($row);
    123             if ($data !== null) {
    124                 $accessor->setValue($obj, $name, $data);
    125             }
    126         }
    127 
    128         foreach ($this->oneToManyAssociations as $oneToManyAssociation) {
    129             $name = $oneToManyAssociation->getAssociationName();
    130             $data = $oneToManyAssociation->import($row);
    131             if ($data !== null) {
    132                 $accessor->setValue($obj, $name, [$data]);
    133             }
    134         }
    135 
    136         return $obj;
    137     }
    138 }