CustomConverter.php 4.27 KB
<?php
namespace common\components\parsers;

use common\components\CustomVarDamp;
use yii\multiparser\Converter;
use backend\models\Details;
use backend\models\DetailsCrosses;
use backend\models\ImportersPrefix;

class CustomConverter extends Converter
{

    public static $sign;
    public static $multiplier;
    public static $importer_id;
    public static $brand;


    public static function convertToDetails(array $row)
    {
        // присвоим полный артикул
        $row['FULL_ARTICLE'] = $row['ARTICLE'];

        $details_model = new Details();
        // проверим все ли обязательные колонки были указаны пользователем
        $details_model->load(['Details' => $row]);

        if (!$details_model->validate()) {
            $errors = '';
            foreach ($details_model->errors as $key => $arr_errors) {
                $errors .= "Аттрибут $key - " . implode(' , ', $arr_errors);
            }
            throw new \ErrorException($errors);
        }
        return $row;
    }

    public static function convertToCrosses( array $row )
    {

        $details_model = new DetailsCrosses();
        // проверим все ли обязательные колонки были указаны пользователем
        $details_model->load(['DetailsCrosses' => $row]);

        if (!$details_model->validate()) {
            $errors = '';
            foreach ($details_model->errors as $key => $arr_errors) {
                $errors .= "Аттрибут $key - " . implode(' , ', $arr_errors);
            }
            throw new \ErrorException($errors);
        }
        return $row;
    }
    public function ConvertToMultiply(array $row)
    {
        $PRICE = $row['PRICE'];
        $sign = self::$sign;
        $multiplier = self::$multiplier;
        //CustomVarDamp::dumpAndDie(self);
        if (isset($sign)) {
            if ($sign == '+') {
                if ($multiplier > 0) {
                    $PRICE += $multiplier;
                }
            } else if ($sign == '-') {
                if ($multiplier > 0) {
                    $PRICE -= $multiplier;
                }
            } else if ($sign == '*') {
                if ($multiplier > 0) {
                    $PRICE *= $multiplier;
                }
            } else if ($sign == '/') {
                if ($multiplier > 0) {
                    $PRICE /= $multiplier;
                }
            }
        }

        $row['PRICE'] = $PRICE;

        return $row;

    }

    public static function convertToArticle( $value )
    {

        if (is_array($value)) {

            $row = $value;
            // 1. Уберем префикс который разделен пробелом (если он есть)
            $words = explode(" ", $row['ARTICLE']);
            if (count($words) > 1) {
                array_shift($words);
                $row['ARTICLE'] = implode(" ", $words);
            }

            if( isset( $row['BRAND'] ) && isset( self::$importer_id ) ){
                // 2. Уберем брендовый префикс (если он есть)
                self::$brand = $row['BRAND'];
                $prefix = '';
                // запрос закешируем
                $prefix = ImportersPrefix::getDb()->cache( function ($db) {
                    return ImportersPrefix::find()->where([ 'importer_id' => self::$importer_id,
                        'brand' => self::$brand ])->one();
                });

                if ($prefix) {
                    $row['BRAND'] = str_replace($prefix, "", $row['BRAND']);
                }
            }

            return $row;

        } else {
            $words = explode( " ", $value );
            if ( count( $words ) > 1) {
                array_shift( $words );
                $value = implode( " ", $words );
            }

            return $value;
        }
    }

    public static function convertToBrand($value)
    {
        $res = self::convertToEncode($value);;
        $res = trim(strtoupper($res));
        $res = str_replace("Ä", "A", str_replace("Ö", "O", str_replace("Ü", "U", str_replace("Ë", "E", str_replace("Ò", "O", $res)))));
        $res = str_replace(array('@', '#', '~', '"', "'", "?", "!"), '', $res);

        return $res;
    }


}