/**
  * @param array|object|SplFileInfo|string $item
  *
  * @return array|object|string
  */
 public function convert($item)
 {
     $filename = $this->filenameProperty ? Vale::get($item, $this->filenameProperty) : $item;
     if ($item instanceof SplFileInfo) {
         $filename = $item->getPathname();
     }
     if (!is_readable($filename)) {
         throw new \InvalidArgumentException(sprintf('The given file "%s" is not readable.', $filename));
     }
     $content = file_get_contents($filename);
     return $this->contentProperty ? Vale::set($item, $this->contentProperty, $content) : $content;
 }
Beispiel #2
0
 /**
  * @param mixed $item
  *
  * @return mixed
  */
 public function convert($item)
 {
     foreach ($this->mappings as $mapping) {
         if (!empty($mapping['from']) && !empty($mapping['to'])) {
             $item = Vale::set($item, $mapping['to'], Vale::get($item, $mapping['from']));
             if ($mapping['remove']) {
                 $item = Vale::remove($item, $mapping['from']);
             }
         } elseif (!empty($mapping['to'])) {
             $item = Vale::set([], $mapping['to'], $item);
         } elseif (!empty($mapping['from'])) {
             $item = Vale::get($item, $mapping['from']);
         }
     }
     return $item;
 }
Beispiel #3
0
 /**
  * Applies the given converter to the given field in the given item if no filter is given or if the filters returns
  * `true` for the field.
  *
  * @param mixed         $item
  * @param ConverterPipe $pipe
  *
  * @return mixed
  */
 protected function convertItemValue($item, ConverterPipe $pipe)
 {
     $value = Vale::get($item, $pipe->getField());
     $filterValue = $pipe->getFilterField() ? Vale::get($item, $pipe->getFilterField()) : $item;
     if ($pipe->getFilter() === null || $pipe->getFilter()->filter($filterValue) === true) {
         $item = Vale::set($item, $pipe->getField(), $pipe->getConverter()->convert($value));
     }
     return $item;
 }
Beispiel #4
0
 /**
  * @test
  * @covers                   Cocur\Vale\Vale::removeValue()
  * @expectedException        InvalidArgumentException
  * @expectedExceptionMessage Did not remove path ["family","seat"] in structure {"family":[]}
  */
 public function removeValueThrowsExceptionIfValueCanNotBeSetInPath()
 {
     // This should really only happen if an element in the middle of the path is a scalar value.
     $this->vale->removeValue(['family' => []], ['family', 'seat']);
 }
Beispiel #5
0
 /**
  * @param mixed $item
  *
  * @return string
  */
 protected function getFile($item)
 {
     if (empty($this->properties['template']) || !($file = Vale::get($item, $this->properties['template']))) {
         $file = $this->defaultTemplate;
     }
     return $file;
 }
 /**
  * @param mixed    $item
  * @param string[] $keys
  *
  * @return array
  */
 protected function getValues($item, array $keys)
 {
     $values = [];
     foreach ($keys as $key) {
         $values[] = Vale::get($item, $key);
     }
     return $values;
 }
Beispiel #7
0
 /**
  * Write the given item.
  *
  * @param mixed $item
  */
 public function writeItem($item)
 {
     if ($this->autoDetectHeader && $this->header === null) {
         $this->handleAutoDetectHeaders($item);
     }
     if (is_array($item)) {
         $keys = array_keys($item);
     } elseif ($this->header && is_object($item)) {
         $keys = $this->header;
     } else {
         throw new \InvalidArgumentException(sprintf('Plum\\PlumExcel\\ExcelWriter currently only supports array items or objects if headers are set using ' . 'the setHeader() method. You have passed an item of type "%s" to writeItem().', gettype($item)));
     }
     $column = 0;
     foreach ($keys as $key) {
         $value = Vale::get($item, $key);
         $this->excel->getActiveSheet()->setCellValueByColumnAndRow($column, $this->currentRow, $value);
         ++$column;
     }
     ++$this->currentRow;
 }