Skip to content

export to csv example throws an error #108

Open

Description

@ioweb-gr

Hi,
I just tried the csv example

<?php

use Port\Csv\CsvWriter;
use Port\Reader\ArrayReader;
use Port\Steps\StepAggregator;
use Port\Steps\Step\ValueConverterStep;

// Your input data
$reader = new ArrayReader(array(
    array(
        'first',        // This is for the CSV header
        'last',
        array(
            'first' => 'james',
            'last'  => 'Bond'
        ),
        array(
            'first' => 'hugo',
            'last'  => 'Drax'
        )
    ))
);

// Create the workflow from the reader
$workflow = new StepAggregator($reader);

// Add the writer to the workflow
$workflow->addWriter(new CsvWriter(STDOUT));

// As you can see in the input data, the first names are not capitalized
// correctly. Let's fix that with a value converter:
$valueConverterStep = new ValueConverterStep();
$valueConverterStep->add('first', function ($value) {
    return ucfirst($value);
});
$workflow->addStep($valueConverterStep);

// Process the workflow
$workflow->process();

but I receive this error

    0.0182     571640   2. Port\Steps\StepAggregator->process() /mnt/g/Projects/scratches/arraytocsv/test.php:41
    0.0210     604616   3. Port\Steps\StepAggregator->Port\Steps\{closure}() /mnt/g/Projects/scratches/arraytocsv/vendor/portphp/steps/src/StepAggregator.php:121
    0.0210     604616   4. Port\Steps\Step\ValueConverterStep->process() /mnt/g/Projects/scratches/arraytocsv/vendor/portphp/steps/src/StepAggregator.php:178
    0.0234     764216   5. Symfony\Component\PropertyAccess\PropertyAccessor->getValue() /mnt/g/Projects/scratches/arraytocsv/vendor/portphp/steps/src/Step/ValueConverterStep.php:40
    0.0251     792512   6. Symfony\Component\PropertyAccess\PropertyAccessor->readPropertiesUntil() /mnt/g/Projects/scratches/arraytocsv/vendor/symfony/property-access/PropertyAccessor.php:165
    0.0251     792888   7. Symfony\Component\PropertyAccess\PropertyAccessor->readProperty() /mnt/g/Projects/scratches/arraytocsv/vendor/symfony/property-access/PropertyAccessor.php:392


Activity

  1. ribeiropaulor commented on Sep 16, 2020

    @ribeiropaulor

    Hello, your code is broken.

    Following is a working refactored code.

    use Port\Csv\CsvWriter;
    use Port\Reader\ArrayReader;
    use Port\Steps\StepAggregator;
    use Port\Steps\Step\ValueConverterStep;
    
    // Your input data
    $reader = new ArrayReader(array(
        array('first', 'last'),
        array( 'first' => 'james', 'last'  => 'Bond' ),
        array( 'first' => 'hugo', 'last'  => 'Drax' ))
    );
    
    // Create the workflow from the reader
    $workflow = new StepAggregator($reader);
    
    // Add the writer to the workflow
    $workflow->addWriter($writer = new CsvWriter());
    $writer->setStream(STDOUT);
    
    // As you can see in the input data, the first names are not capitalized
    // correctly. Let's fix that with a value converter:
    $valueConverterStep = new ValueConverterStep();
    // to access array keys, `Symfony\Component\PropertyAccess\PropertyAccessor`
    // needs a special syntax. See https://symfony.com/doc/current/components/property_access.html
    $valueConverterStep->add('[first]', function ($value) {
        return ucfirst($value);
    });
    $workflow->addStep($valueConverterStep);
    
    // Process the workflow
    $workflow->process();
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions