سؤال

هنا نسخة مبسطة من طريقة واحدة لدي.
لا تزال تبدو معقدة للغاية
كيف يمكن للمرء أن يعيد هذا الجنون؟

protected function isTextValid()
{
    if( $this->data['allow_num'] ){
        if( $this->data['allow_space'] ){
            if( preg_match( '#^[a-zA-Z0-9\s]$#', $this->val ) ){
                return true;
            }
            else{
                $this->messages = foo ? foo : bar;
                return false;
            }
        }
        else{
            if( preg_match( '#^[a-zA-Z0-9]$#', $this->val ) ){
                return true;
            }
            else{
                $this->messages = foo? foor : bar;
                return false;
            }
        }
    }
    else{
        if( $this->data['allow_space'] ){
            if( preg_match( '#^[a-zA-Z\s]$#', $this->val ) ){
                return true;
            }
            else{
                $this->messages = foo ? foor : bar;
                return false;
            }
        }
        else{
            if( preg_match( '#^[a-zA-Z]$#', $this->val  ) ){
                return true;
            }
            else{
                $this->messages =  foo ? foo: bar;
                return false;
            }
        }
    }
}

أحاول إعادة صياغته باستخدام نمط الحالة ولكن دون جدوى لأنني لست على دراية بالنمط.
هذا ما فعلته ولكن Quickyly abondon.

interface ValidatorState{
  public function isValid();
}

class AllowNumberAndSpace implement ValidatorState{
   protected $context;
   public function __construct(Context $context){$this->context = $context}

    public function isValid(){
       if( preg_match( .. ) ){
            return true;
        }
        else{
            $this->messages = foo ? foo : bar;
            return false;
        }
      $this->context->setState(OtherState);
    }
}

Class Context{
    protected $state;
    protected $allow_num_space_state;

    public function __construct(){
        $this->allow_num_space_state = new AllowNumberAndSpace($this);
       $this->state = $this->allow_num_space_state;
    }

   public function isValid(){
       return $this->state->isValid();
   }

  public function setState($state){$this->state = $state}
}

من الواضح هذا الاختبار فقط أولاً if فرع ، كيف يمكنني التحقق تلقائيًا من الفرع الآخر أيضًا؟
أنا متأكد تمامًا من وجود خطأ في مقاربي.
هل هناك طريقة لإصلاح نمط الحالة هذا لاختبار الجميع if فرع؟

تحرير
ما تفعله هذه الطريقة هو التحقق من ما إذا كان $this->value تحتوي على القيمة المتوقعة بناءً على سمة التكوين المخزنة في $this->data

مثال $this->data = array('allow_num'=>true), ، إذا $this->value='a1' إنه يعتبر مثالًا صالحًا $this->data = array('allow_num'=>false), ، إذا $this->value='a1' إنه يعتبر غير صالح

هل هناك طريقة لتبسيط هذه الطريقة؟

هل كانت مفيدة؟

المحلول

بادئ ذي بدء ، حاول ألا تتكيف مع هذا. في رأيي ، فإن الكود ليس معقدًا بما يكفي لتبرير استخدام أنماط التصميم الموجهة للكائنات.

كما أراها ، فإن الكود الخاص بك يتلخص بشكل أساسي في التحقق من صحة الإدخال مع تعبيرات منتظمة مختلفة (والتي تعتمد على بعض العلامات المحددة للمستخدمين مثل allow_num و allow_space.

لذا فإن اقتراحي هو ما يلي (إعادة البناء الأساسية هي إنشاء جزء من التعبير العادي بناءً على التكوين من قبل استخدام هذا التعبير في أي منطق التحقق من الصحة):

protected function isTextValid() {
    $allowedCharacters = 'A-Za-z';

    if ($this->data['allow_spaces']) {
        $allowedCharacters .= '\s';
    }
    if ($this->data['allow_num']) {
        $allowedCharacters .= '\d';
    }

    if (!preg_match("#^[{$allowedCharacters}]+\$#", $this->value)) {
        $this->messages = foo ? foor : bar;
        return false;
    }
    return true;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top