Josegonzalez\Upload\Model\Behavior\UploadBehavior::constructFiles PHP Method

constructFiles() public method

This is done through an intermediate transformer, which should return said array. Example: [ '/tmp/path/to/file/on/disk' => 'file.pdf', '/tmp/path/to/file/on/disk-2' => 'file-preview.png', ] A user can specify a callable in the transformer setting, which can be used to construct this key/value array. This processor can be used to create the source files.
public constructFiles ( Cake\ORM\Entity $entity, array $data, string $field, array $settings, string $basepath ) : array
$entity Cake\ORM\Entity an entity
$data array the data being submitted for a save
$field string the field for which data will be saved
$settings array the settings for the current field
$basepath string a basepath where the files are written to
return array key/value pairs of temp files mapping to their names
    public function constructFiles(Entity $entity, $data, $field, $settings, $basepath)
    {
        $basepath = substr($basepath, -1) == DS ? $basepath : $basepath . DS;
        $default = 'Josegonzalez\\Upload\\File\\Transformer\\DefaultTransformer';
        $transformerClass = Hash::get($settings, 'transformer', $default);
        $results = [];
        if (is_subclass_of($transformerClass, 'Josegonzalez\\Upload\\File\\Transformer\\TransformerInterface')) {
            $transformer = new $transformerClass($this->_table, $entity, $data, $field, $settings);
            $results = $transformer->transform();
            foreach ($results as $key => $value) {
                $results[$key] = $basepath . $value;
            }
        } elseif (is_callable($transformerClass)) {
            $results = $transformerClass($this->_table, $entity, $data, $field, $settings);
            foreach ($results as $key => $value) {
                $results[$key] = $basepath . $value;
            }
        } else {
            throw new UnexpectedValueException(sprintf("'transformer' not set to instance of TransformerInterface: %s", $transformerClass));
        }
        return $results;
    }