partkeepr

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

FieldConfiguration.php (3105B)


      1 <?php
      2 
      3 namespace PartKeepr\ImportBundle\Configuration;
      4 
      5 class FieldConfiguration extends BaseConfiguration
      6 {
      7     const FIELDCONFIGURATION_IGNORE = "ignore";
      8     const FIELDCONFIGURATION_COPYFROM = "copyFrom";
      9     const FIELDCONFIGURATION_FIXEDVALUE = "fixedValue";
     10 
     11     const fieldConfigurationModes = [
     12         self::FIELDCONFIGURATION_COPYFROM,
     13         self::FIELDCONFIGURATION_FIXEDVALUE,
     14         self::FIELDCONFIGURATION_IGNORE,
     15     ];
     16 
     17     private $fieldConfiguration;
     18 
     19     private $fieldName;
     20 
     21     /**
     22      * @return mixed
     23      */
     24     public function getFieldName()
     25     {
     26         return $this->fieldName;
     27     }
     28 
     29     private $fixedValue;
     30 
     31     private $copyFromField;
     32 
     33     public function setFieldName($fieldName)
     34     {
     35         $this->fieldName = $fieldName;
     36     }
     37 
     38     public function parseConfiguration($configuration)
     39     {
     40         if (!property_exists($configuration, "fieldConfiguration")) {
     41             return false;
     42 
     43             throw new \Exception("The key fieldConfiguration does not exist!");
     44         }
     45 
     46         if (!in_array($configuration->fieldConfiguration, self::fieldConfigurationModes)) {
     47             throw new \Exception("The key fieldConfiguration contains an invalid value!");
     48         }
     49 
     50         $this->fieldConfiguration = $configuration->fieldConfiguration;
     51 
     52         switch ($this->fieldConfiguration) {
     53             case self::FIELDCONFIGURATION_FIXEDVALUE:
     54                 if (!property_exists($configuration, "setToValue")) {
     55                     throw new \Exception("The key setToValue does not exist for mode fixedValue!");
     56                 }
     57 
     58                 $this->fixedValue = $configuration->setToValue;
     59                 break;
     60             case self::FIELDCONFIGURATION_COPYFROM:
     61                 if (!property_exists($configuration, "copyFromField")) {
     62                     throw new \Exception("The key copyFromField does not exist for mode copyFrom!");
     63                 }
     64 
     65                 $this->copyFromField = $configuration->copyFromField;
     66                 break;
     67             default:
     68                 break;
     69         }
     70 
     71         return true;
     72     }
     73 
     74     /**
     75      * Imports a given row.
     76      *
     77      * @param $row array The row to import
     78      *
     79      * @return string|null
     80      */
     81     public function import($row)
     82     {
     83         switch ($this->fieldConfiguration) {
     84             case self::FIELDCONFIGURATION_FIXEDVALUE:
     85                 $this->log(sprintf("Set field %s to fixed value %s", $this->fieldName, $this->fixedValue));
     86 
     87                 return $this->fixedValue;
     88             break;
     89             case self::FIELDCONFIGURATION_COPYFROM:
     90                 if (!array_key_exists($this->copyFromField, $row)) {
     91                     $this->log(sprintf("Error: Column %s for %s does not exist", $this->copyFromField, $this->fieldName));
     92 
     93                     return null;
     94                 }
     95                 $this->log(sprintf("Set field %s to value %s (import column %s)", $this->fieldName, $row[$this->copyFromField], $this->copyFromField));
     96 
     97                 return $row[$this->copyFromField];
     98             break;
     99             default:
    100                 return null;
    101         }
    102     }
    103 }