PHP code example of keboola / php-component

1. Go to this page and download the library: Download keboola/php-component library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

keboola / php-component example snippets



class Component extends \Keboola\Component\BaseComponent
{
    protected function run(): void
    {
        // get parameters
        $parameters = $this->getConfig()->getParameters();

        // get value of customKey.customSubKey parameter and fail if missing
        $customParameter = $this->getConfig()->getValue(['parameters', 'customKey', 'customSubKey']);

        // get value with default value if not present
        $customParameterOrNull = $this->getConfig()->getValue(['parameters', 'customKey'], 'someDefaultValue');

        // get manifest for input file
        $fileManifest = $this->getManifestManager()->getFileManifest('input-file.csv');

        // get manifest for input table
        $tableManifest = $this->getManifestManager()->getTableManifest('in.tableName');

        // write manifest for output file
        $this->getManifestManager()->writeFileManifest(
            'out-file.csv',
            (new OutFileManifestOptions())
                ->setTags(['tag1', 'tag2'])
        );

        // write manifest for output table
        $this->getManifestManager()->writeTableManifest(
            'data.csv',
            (new OutTableManifestOptions())
                ->setPrimaryKeyColumns(['id'])
                ->setDestination('out.report'),
            true // legacy manifest format flag
        );
    }

    protected function customSyncAction(): array
    {
        return ['result' => 'success', 'data' => ['joe', 'marry']];
    }

    protected function getSyncActions(): array
    {
        return ['custom' => 'customSyncAction'];
    }
}



declare(strict_types=1);

use Keboola\Component\Logger;

nent($logger);
    $app->execute();
    exit(0);
} catch (\Keboola\Component\UserException $e) {
    $logger->error($e->getMessage());
    exit(1);
} catch (\Throwable $e) {
    $logger->critical(
        get_class($e) . ':' . $e->getMessage(),
        [
            'errFile' => $e->getFile(),
            'errLine' => $e->getLine(),
            'errCode' => $e->getCode(),
            'errTrace' => $e->getTraceAsString(),
            'errPrevious' => $e->getPrevious() ? get_class($e->getPrevious()) : '',
        ]
    );
    exit(2);
}

class MyConfig extends \Keboola\Component\Config\BaseConfig 
{
    public function getCustomSubKey()
    {
        $defaultValue = 0;
        return $this->getValue(['parameters', 'customKey', 'customSubKey'], $defaultValue);
    }
}

class MyComponent extends \Keboola\Component\BaseComponent
{
    protected function getConfigClass(): string
    {
        return MyConfig::class;
    }
}

class MyConfigDefinition extends \Keboola\Component\Config\BaseConfigDefinition
{
    protected function getParametersDefinition()
    {
        $parametersNode = parent::getParametersDefinition();
        $parametersNode
            ->isRequired()
            ->children()
                ->arrayNode('customKey')
                    ->isRequired()
                    ->children()
                        ->integerNode('customSubKey')
                            ->isRequired();
        return $parametersNode;
    }
}

class MyComponent extends \Keboola\Component\BaseComponent
{
    protected function getConfigDefinitionClass(): string
    {
        return MyConfigDefinition::class;
    }
}

composer