/** * @param mixed $item * * @return mixed */ public function convert($item) { $file = $this->getFile($item); $template = $this->twig->loadTemplate($file . $this->fileExtension); $rendered = $template->render($this->getContext($item)); if (empty($this->properties['target'])) { return $rendered; } return Vale::set($item, $this->properties['target'], $rendered); }
/** * @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; }
/** * @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; }
/** * 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; }
/** * @test * @covers Cocur\Vale\Vale::set() */ public function setSetsValue() { $result = Vale::set([], ['name'], 'Tyrion'); $this->assertSame('Tyrion', $result['name']); }