Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parable to parable-php #12

Merged
merged 1 commit into from
Mar 5, 2019
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
3 changes: 2 additions & 1 deletion .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use PhpCsFixer\Finder;
*/
$finder = Finder::create()
->in(__DIR__)
->exclude('tmp');
->exclude('tmp')
->append(['convert', 'csv2qif', 'help', 'validate']);

return Config::create()
->setFinder($finder)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ dmvdbrugge@macbook:~/csv2qif$ ./help
Help /_/

Available commands:
convert Converts given ING CSV to QIF
csv2qif Shows a UI for convert/validate
convert Converts given ING CSV to QIF.
csv2qif Shows a UI for convert/validate.
help Shows all commands available.
validate Validates given ruleset for use with convert
validate Validates given ruleset for use with convert.
```

## Examples & Documentation
Expand Down
30 changes: 17 additions & 13 deletions app/Actors/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Csv2Qif\Actors;

use Csv2Qif\Console\Output;
use Csv2Qif\Event\Hook;
use Csv2Qif\File\CsvReader;
use Csv2Qif\File\QifWriter;
use Csv2Qif\RuleSet\RuleSetMatcher;
use Csv2Qif\Transactions\Transformer;
use Parable\Console\Output;
use Parable\DI\Container;
use Parable\Di\Container;

use function ceil;
use function memory_get_peak_usage;
Expand All @@ -18,27 +18,31 @@

class Converter
{
/** @var Container */
private $container;

/** @var Hook */
private $hook;

/** @var Output */
private $output;

public function __construct(Hook $hook, Output $output)
public function __construct(Container $container, Hook $hook, Output $output)
{
$this->hook = $hook;
$this->output = $output;
$this->container = $container;
$this->hook = $hook;
$this->output = $output;
}

public function convert(string $csv, ?string $qif = null, string $ruleSet = '', int $debugLevel = 0)
public function convert(string $csv, ?string $qif = null, string $ruleSet = '', int $debugLevel = 0): void
{
$start = microtime(true);
$qif = $qif ?? (rtrim($csv, '.csv') . '.qif');

// Prepare reader/writer/transformer
$csvReader = Container::create(CsvReader::class);
$qifWriter = Container::create(QifWriter::class);
$transformer = Container::create(Transformer::class);
$csvReader = $this->container->build(CsvReader::class);
$qifWriter = $this->container->build(QifWriter::class);
$transformer = $this->container->build(Transformer::class);

$csvReader->setFile($csv);
$qifWriter->setFile($qif);
Expand All @@ -53,7 +57,7 @@ public function convert(string $csv, ?string $qif = null, string $ruleSet = '',
};

// Hook the counter into the event
$this->hook->into(CsvReader::TRANSACTION_READ, $updateCounter);
$this->hook->listen(CsvReader::TRANSACTION_READ, $updateCounter);

if ($debugLevel >= 2) {
$this->addDebugHooks($debugLevel);
Expand All @@ -66,7 +70,7 @@ public function convert(string $csv, ?string $qif = null, string $ruleSet = '',
$usingRuleSet = $ruleSet ? " using ruleset {$ruleSet}" : '';

$this->output->cursorReset();
$this->output->writeln([
$this->output->writelns([
"{$counter} Transactions converted{$usingRuleSet}",
'',
"Source: {$csv}",
Expand All @@ -88,10 +92,10 @@ private function addDebugHooks(int $debugLevel): void
: $this->output->writeln(" <green>{$payload}</green>");
};

$this->hook->into(RuleSetMatcher::MATCH_FOUND, $printMatch);
$this->hook->listen(RuleSetMatcher::MATCH_FOUND, $printMatch);

if ($debugLevel >= 3) {
$this->hook->into(RuleSetMatcher::MATCH_FALLBACK, $printMatch);
$this->hook->listen(RuleSetMatcher::MATCH_FALLBACK, $printMatch);
}
}
}
26 changes: 13 additions & 13 deletions app/Actors/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace Csv2Qif\Actors;

use Csv2Qif\Console\Output;
use Csv2Qif\Event\Hook;
use Csv2Qif\RuleSet\Rules\Rules\Rule;
use Csv2Qif\RuleSet\Rules\Rules\RuleHasReason;
use Csv2Qif\RuleSet\RuleSetValidator;
use Parable\Console\Output;
use Parable\DI\Container;

class Validator
{
Expand All @@ -17,23 +16,24 @@ class Validator
/** @var Output */
private $output;

public function __construct(Hook $hook, Output $output)
/** @var RuleSetValidator */
private $validator;

public function __construct(Hook $hook, Output $output, RuleSetValidator $validator)
{
$this->hook = $hook;
$this->output = $output;
$this->hook = $hook;
$this->output = $output;
$this->validator = $validator;
}

public function validate(string $ruleSet, int $verbose = 0): int
{
// Prepare things
$validator = Container::get(RuleSetValidator::class);

if ($verbose > 0) {
$this->addVerboseHooks($verbose);
}

// Magic happens here
$errorCount = $validator->validateAll($ruleSet);
$errorCount = $this->validator->validateAll($ruleSet);

// Print out final info
if ($errorCount) {
Expand All @@ -59,7 +59,7 @@ public function validate(string $ruleSet, int $verbose = 0): int

private function addVerboseHooks(int $verbose): void
{
$this->hook->into(RuleSetValidator::VALIDATE_ERROR, function (string $event, string $message) {
$this->hook->listen(RuleSetValidator::VALIDATE_ERROR, function (string $event, string $message) {
$this->output->writeln("<red>{$message}</red>");
});

Expand All @@ -70,7 +70,7 @@ private function addVerboseHooks(int $verbose): void

private function addVerbose2Hooks(int $verbose): void
{
$this->hook->into(RuleSetValidator::VALIDATE_MATCHER_VALID, function (string $event, string $name) {
$this->hook->listen(RuleSetValidator::VALIDATE_MATCHER_VALID, function (string $event, string $name) {
$this->output->writeln("<green>Matcher {$name} is valid.</green>");
});

Expand All @@ -81,12 +81,12 @@ private function addVerbose2Hooks(int $verbose): void

private function addVerbose3Hooks(int $verbose): void
{
$this->hook->into(RuleSetValidator::VALIDATE_MATCHER_START, function (string $event, string $name) {
$this->hook->listen(RuleSetValidator::VALIDATE_MATCHER_START, function (string $event, string $name) {
$this->output->newline();
$this->output->writeln("<yellow>Validating matcher {$name}.</yellow>");
});

$this->hook->into(RuleSetValidator::VALIDATE_RULE_ERROR, function (string $event, Rule $rule) {
$this->hook->listen(RuleSetValidator::VALIDATE_RULE_ERROR, function (string $event, Rule $rule) {
$error = 'Invalid rule: ' . $rule->getOrigin();
$reason = '';

Expand Down
12 changes: 5 additions & 7 deletions app/Command/Convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Csv2Qif\Command;

use Csv2Qif\Actors\Converter;
use Csv2Qif\Event\Hook;
use Parable\Console\Command;
use Parable\Console\Parameter;

Expand All @@ -19,17 +18,17 @@ class Convert extends Command

protected $description = 'Converts given ING CSV to QIF.';

/** @var Hook */
private $hook;
/** @var Converter */
private $converter;

public function __construct(Hook $hook)
public function __construct(Converter $converter)
{
$this->addArgument(self::ARG_CSV, Parameter::PARAMETER_REQUIRED);
$this->addArgument(self::ARG_QIF);
$this->addOption(self::OPT_RULESET, Parameter::OPTION_VALUE_REQUIRED);
$this->addOption(self::OPT_DEBUG);

$this->hook = $hook;
$this->converter = $converter;
}

public function run(): void
Expand All @@ -41,7 +40,6 @@ public function run(): void
$ruleSet = $this->parameter->getOption(self::OPT_RULESET) ?? '';
$debugLevel = (int) $this->parameter->getOption(self::OPT_DEBUG);

$converter = new Converter($this->hook, $this->output);
$converter->convert($csv, $qif, $ruleSet, $debugLevel);
$this->converter->convert($csv, $qif, $ruleSet, $debugLevel);
}
}
11 changes: 10 additions & 1 deletion app/Command/Ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Csv2Qif\UiComponents\MainWindow;
use Parable\Console\Command;
use Parable\Di\Container;

use function extension_loaded;
use function UI\run;
Expand All @@ -14,6 +15,14 @@ class Ui extends Command

protected $description = 'Shows a UI for convert/validate.';

/** @var Container */
private $container;

public function __construct(Container $container)
{
$this->container = $container;
}

public function run(): void
{
if (!extension_loaded('ui')) {
Expand All @@ -26,7 +35,7 @@ public function run(): void
exit(1);
}

$window = new MainWindow();
$window = $this->container->get(MainWindow::class);
$window->show();

run();
Expand Down
12 changes: 5 additions & 7 deletions app/Command/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Csv2Qif\Command;

use Csv2Qif\Actors\Validator;
use Csv2Qif\Event\Hook;
use Parable\Console\Command;
use Parable\Console\Parameter;

Expand All @@ -16,15 +15,15 @@ class Validate extends Command

protected $description = 'Validates given ruleset for use with convert.';

/** @var Hook */
private $hook;
/** @var Validator */
private $validator;

public function __construct(Hook $hook)
public function __construct(Validator $validator)
{
$this->addArgument(self::ARG_RULESET, Parameter::PARAMETER_REQUIRED);
$this->addOption(self::OPT_VERBOSE);

$this->hook = $hook;
$this->validator = $validator;
}

public function run(): void
Expand All @@ -33,8 +32,7 @@ public function run(): void
$ruleSet = $this->parameter->getArgument(self::ARG_RULESET);
$verbose = (int) ($this->parameter->getOption(self::OPT_VERBOSE) ?? 0);

$validator = new Validator($this->hook, $this->output);
$errorCount = $validator->validate($ruleSet, $verbose);
$errorCount = $this->validator->validate($ruleSet, $verbose);

// Signal result to the outside world
exit($errorCount);
Expand Down
29 changes: 29 additions & 0 deletions app/Console/Output.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Csv2Qif\Console;

use Throwable;

/**
* TODO: Remove this intermediate class when parable-php/console#2 is merged.
*/
class Output extends \Parable\Console\Output
{
public function parseTags(string $line): string
{
$tags = $this->getTagsFromString($line);

foreach ($tags as $tag) {
try {
$code = $this->getCodeFor($tag);
} catch (Throwable $throwable) {
continue;
}

$line = str_replace("<{$tag}>", $code, $line);
$line = str_replace("</{$tag}>", $this->predefinedTags['default'], $line);
}

return $line . $this->predefinedTags['default'];
}
}
8 changes: 5 additions & 3 deletions app/Event/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Csv2Qif\Event;

class Hook extends \Parable\Event\Hook
use Parable\Event\EventManager;

class Hook extends EventManager
{
public function reset(?string $event = null): void
{
if ($event !== null) {
$this->hooks[$event] = [];
$this->listeners[$event] = [];
} else {
$this->hooks = [];
$this->listeners = [];
}
}
}
2 changes: 1 addition & 1 deletion app/File/QifWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public function writeTransactions(iterable $transactions): void

private function writeTransaction(QIF\Transaction $transaction): void
{
fwrite($this->handle, (string) $transaction . PHP_EOL);
fwrite($this->handle, $transaction . PHP_EOL);
}
}
3 changes: 1 addition & 2 deletions app/RuleSet/RuleSetMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Csv2Qif\RuleSet\Description\DescriptionMatcher;
use Csv2Qif\RuleSet\Rules\Rules\RuleAllOf;
use Csv2Qif\Transactions\IngTransaction;
use Parable\DI\Container;

use function is_array;
use function str_replace;
Expand All @@ -27,7 +26,7 @@ class RuleSetMatcher

public function __construct(DescriptionMatcher $description, Hook $hook)
{
$this->config = Container::create(RuleSetConfig::class);
$this->config = new RuleSetConfig();
$this->description = $description;
$this->hook = $hook;
}
Expand Down
3 changes: 1 addition & 2 deletions app/RuleSet/RuleSetValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Csv2Qif\RuleSet\Rules\Rules\RuleAllOf;
use Csv2Qif\Transactions\IngTransaction;
use Generator;
use Parable\DI\Container;

use function is_array;
use function is_string;
Expand All @@ -31,7 +30,7 @@ class RuleSetValidator

public function __construct(DescriptionValidator $description, Hook $hook)
{
$this->config = Container::create(RuleSetConfig::class);
$this->config = new RuleSetConfig();
$this->description = $description;
$this->hook = $hook;
}
Expand Down
Loading