Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ RUN composer install $COMPOSER_FLAGS --no-scripts --no-autoloader
COPY . .
RUN composer install $COMPOSER_FLAGS

CMD composer ci
CMD php ./example/run.php
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Component extends \Keboola\Component\BaseComponent
// 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 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');
Expand Down Expand Up @@ -109,17 +109,17 @@ To implement a sync action

### Custom getters in config

You might want to add getter methods for custom parameters in the config. That way you don't need to remember exact keys (`parameters.errorCount.maximumAllowed`), but instead use a method to retrieve the value (`$config->getMaximumAllowedErrorCount()`).
You might want to add getter methods for custom parameters in the config. That way you don't need to remember exact keys (`parameters.customKey.customSubKey`), but instead use a method to retrieve the value (`$config->getCustomSubKey()`).

Simply create your own `Config` class, that extends `BaseConfig` and override `\Keboola\Component\BaseComponent::getConfigClass()` method to return your new class name.

```php
class MyConfig extends \Keboola\Component\Config\BaseConfig
{
public function getMaximumAllowedErrorCount()
public function getCustomSubKey()
{
$defaultValue = 0;
return $this->getValue(['parameters', 'errorCount', 'maximumAllowed'], $defaultValue);
return $this->getValue(['parameters', 'customKey', 'customSubKey'], $defaultValue);
}
}
```
Expand Down Expand Up @@ -149,10 +149,10 @@ class MyConfigDefinition extends \Keboola\Component\Config\BaseConfigDefinition
$parametersNode
->isRequired()
->children()
->arrayNode('errorCount')
->arrayNode('customKey')
->isRequired()
->children()
->integerNode('maximumAllowed')
->integerNode('customSubKey')
->isRequired();
return $parametersNode;
}
Expand Down
16 changes: 13 additions & 3 deletions example/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ 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 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');
$customParameterOrNull = $this->getConfig()->getValue(['parameters', 'anotherCustomKey'], 'someDefaultValue');

// get manifest for input file
$fileManifest = $this->getManifestManager()->getFileManifest('input-file.csv');
Expand Down Expand Up @@ -53,4 +53,14 @@ protected function getSyncActions(): array
{
return ['custom' => 'customSyncAction'];
}

protected function getConfigDefinitionClass(): string
{
return MyConfigDefinition::class;
}

protected function getConfigClass(): string
{
return MyConfig::class;
}
}
4 changes: 2 additions & 2 deletions example/MyConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

class MyConfig extends BaseConfig
{
public function getMaximumAllowedErrorCount(): int
public function getCustomSubKey(): int
{
$defaultValue = 0;
return $this->getIntValue(['parameters', 'errorCount', 'maximumAllowed'], $defaultValue);
return $this->getIntValue(['parameters', 'customKey', 'customSubKey'], $defaultValue);
}
}
4 changes: 2 additions & 2 deletions example/MyConfigDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ protected function getParametersDefinition(): ArrayNodeDefinition
$parametersNode
->isRequired()
->children()
->arrayNode('errorCount')
->arrayNode('customKey')
->isRequired()
->children()
->integerNode('maximumAllowed')
->integerNode('customSubKey')
->isRequired();
return $parametersNode;
}
Expand Down
1 change: 0 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint"/>
<exclude name="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration"/>
</rule>
<exclude-pattern>*/src/Kernel.php</exclude-pattern>
</ruleset>
2 changes: 1 addition & 1 deletion src/Config/BaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BaseConfig implements ConfigInterface
*/
public function __construct(
array $config,
?ConfigurationInterface $configDefinition = null
?ConfigurationInterface $configDefinition = null,
) {
$this->setConfigDefinition($configDefinition);
$this->setConfig($config);
Expand Down
4 changes: 2 additions & 2 deletions src/Manifest/ManifestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ManifestManager
private string $dataDir;

public function __construct(
string $dataDir
string $dataDir,
) {
$this->dataDir = $dataDir;
}
Expand All @@ -36,7 +36,7 @@ final public function getManifestFilename(string $fileName): string

public function writeFileManifest(
string $fileName,
OutFileManifestOptions $options
OutFileManifestOptions $options,
): void {
$tableManifestName = $this->getManifestFilename($fileName);
$this->internalWriteFileManifest($tableManifestName, $options->toArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
bool $nullable = true,
bool $primaryKey = false,
?string $description = null,
?array $metadata = null
?array $metadata = null,
) {
$this->setName($name);
if ($dataTypes !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private function setTableMetadata(ManifestOptions $manifestOptions, array $data)
private function setSchema(
ManifestOptions $manifestOptions,
array $data,
?string $metadataBackend
?string $metadataBackend,
): void {
$schema = [];

Expand Down Expand Up @@ -226,7 +226,7 @@ private function setMetadata(
array &$metadata,
?string &$description,
bool &$primaryKey,
bool &$isNullable
bool &$isNullable,
): void {
if ($meta['key'] === 'KBC.description') {
$description = $meta['value'];
Expand Down