<commit_before><?php

namespace Kingsquare\Parser\Banking\Mt940\Engine;

use Kingsquare\Banking\Transaction\Type;
use Kingsquare\Parser\Banking\Mt940\Engine;

/**
 * Kbs parser for Kingsquare mt940 package.
 *
 * @package Kingsquare\Parser\Banking\Mt940\Engine
 * @author <NAME> (<EMAIL>)
 * @license http://opensource.org/licenses/MIT MIT
 */
class Kbs extends Engine
{
    const IBAN = '[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}(?:[a-zA-Z0-9]?){0,16}';

    /**
     * @inheritdoc
     */
    protected function parseStatementBank()
    {
        return 'KBS';
    }

    /**
     * @inheritdoc
     */
    protected function parseTransactionAccount()
    {
        $results = [];

        // SEPA MT940 Structured
        if (preg_match('/^:86:.*?\/IBAN\/(' . self::IBAN . ')/ims', $this->getCurrentTransactionData(), $results)
            && !empty($results[1])
        ) {
            return $this->sanitizeAccount($results[1]);
        }

        $pattern = '/^:86:.*?REK:\s*(?<account>' . self::IBAN . '|\d+)/ims';
        if (preg_match($pattern, $this->getCurrentTransactionData(), $results)
            && !empty($results['account'])
        ) {
            return $results['account'];
        }

        return '';
    }

    /**
     * @inheritdoc
     */
    protected function parseTransactionAccountName()
    {
        $results = [];

        // SEPA MT940 Structured
        if (preg_match('#/NAME/(.*?)/(EREF|REMI|ADDR)/#ms', $this->getCurrentTransactionData(), $results)
            && !empty($results[1])
        ) {
            $accountName = trim($results[1]);
            if (!empty($accountName)) {
                return $this->sanitizeAccountName($accountName);
            }
        }

        if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results)
            && !empty($results[1])
        ) {
            return trim($results[1]);
        }
        if (preg_match('#/NAME/(.*?)\n?/(REMI|CSID)/#ms', $this->getCurrentTransactionData(), $results)
            && !empty($results[1])
        ) {
            return trim($results[1]);
        }

        return '';
    }

    /**
     * @inheritdoc
     */
    protected function parseTransactionDescription()
    {
        $description = parent::parseTransactionDescription();

        // SEPA MT940 Structured
        if (strpos($description, '/REMI/') !== false
            && preg_match('#/REMI/(.*)[/:]?#', $description, $results) && !empty($results[1])
        ) {
            return $results[1];
        }

        $accountIsInDescription = strpos($description, 'REK:');
        if ($accountIsInDescription !== false) {
            return trim(substr($description, 0, $accountIsInDescription));
        }

        $name = $this->parseTransactionAccountName();
        if ($name === '') {
            return $description;
        }
        $accountNameIsInDescription = strpos($description, $name);
        if ($accountNameIsInDescription !== false) {
            return trim(substr($description, 0, $accountNameIsInDescription - 6));
        }
        return $description;
    }

    /**
     * @inheritdoc
     */
    public static function isApplicable($string)
    {
        $firstline = strtok($string, "\r\n\t");

        return strpos($firstline, 'F01KOBSMK2XAXXX1201000000') !== false;
    }

    /**
     * @TODO WIP get this into the transaction somehow.. (possibly as a decorator over the transactions?)
     * @return int
     */<commit_msg>minor bug fixed for KBS engine<commit_after><?php

namespace Kingsquare\Parser\Banking\Mt940\Engine;

use Kingsquare\Banking\Transaction\Type;
use Kingsquare\Parser\Banking\Mt940\Engine;

/**
 * Kbs parser for Kingsquare mt940 package.
 *
 * @package Kingsquare\Parser\Banking\Mt940\Engine
 * @author <NAME>erse (<EMAIL>)
 * @license http://opensource.org/licenses/MIT MIT
 */
class Kbs extends Engine
{
    const IBAN = '[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}(?:[a-zA-Z0-9]?){0,16}';

    /**
     * @inheritdoc
     */
    protected function parseStatementBank()
    {
        return 'KBS';
    }

    /**
     * @inheritdoc
     */
    protected function parseTransactionAccount()
    {
        $results = [];

        // SEPA MT940 Structured
        if (preg_match('/^:86:.*?\/IBAN\/(' . self::IBAN . ')/ims', $this->getCurrentTransactionData(), $results)
            && !empty($results[1])
        ) {
            return $this->sanitizeAccount($results[1]);
        }

        $pattern = '/^:86:.*?REK:\s*(?<account>' . self::IBAN . '|\d+)/ims';
        if (preg_match($pattern, $this->getCurrentTransactionData(), $results)
            && !empty($results['account'])
        ) {
            return $results['account'];
        }

        return '';
    }

    /**
     * @inheritdoc
     */
    protected function parseTransactionAccountName()
    {
        $results = [];

        // SEPA MT940 Structured
        if (preg_match('#/NAME/(.*?)/(EREF|REMI|ADDR)/#ms', $this->getCurrentTransactionData(), $results)
            && !empty($results[1])
        ) {
            $accountName = trim($results[1]);
            if (!empty($accountName)) {
                return $this->sanitizeAccountName($accountName);
            }
        }

        if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results)
            && !empty($results[1])
        ) {
            return trim($results[1]);
        }
        if (preg_match('#/NAME/(.*?)\n?/(REMI|CSID)/#ms', $this->getCurrentTransactionData(), $results)
            && !empty($results[1])
        ) {
            return trim($results[1]);
        }

        return '';
    }

    /**
     * @inheritdoc
     */
    protected function parseTransactionDescription()
    {
        $description = parent::parseTransactionDescription();

        // SEPA MT940 Structured
        if (strpos($description, '/REMI/') !== false
            && preg_match('#/REMI/(.*)[/:]?#', $description, $results) && !empty($results[1])
        ) {
            return $results[1];
        }

        $accountIsInDescription = strpos($description, 'REK:');
        if ($accountIsInDescription !== false) {
            return trim(substr($description, 0, $accountIsInDescription));
        }

        $name = $this->parseTransactionAccountName();
        if ($name === '') {
            return $description;
        }
        $accountNameIsInDescription = strpos($description, $name);
        if ($accountNameIsInDescription !== false) {
            return trim(substr($description, 0, $accountNameIsInDescription - 6));
        }
        return $description;
    }

    /**
     * @inheritdoc
     */
    public static function isApplicable($string)
    {
        $firstline = strtok($string, "\r\n\t");

        return strpos($firstline, 'F01KOBSMK2XAXXX') !== false;
    }

    /**
     * @TODO WIP get this into the transaction somehow.. (possibly as a decorator over the transactions?)
     * @return int
     */
{'fraction_non_alphanumeric': 0.17275747508305647, 'fraction_numerical': 0.017622418026866964, 'mean_word_length': 2.164533820840951, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '809183', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2063, 'n_words': 353}