Viewing File: /home/ubuntu/route-and-root-backend-base/app/Rules/DiscountRule.php

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class DiscountRule implements Rule
{

    protected $discount, $discount_type, $original_price;

    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct($discount, $discount_type, $original_price)
    {
        $this->discount = $discount;

        $this->discount_type = $discount_type;

        $this->original_price = $original_price;
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {

        if($this->discount_type == DISCOUNT_TYPE_PERCENTAGE && $this->discount > 100) {

            return false;

        } elseif($this->discount_type == DISCOUNT_TYPE_FLAT_PRICE && $this->discount > $this->original_price) {

            return false;
        }

        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        if($this->discount_type == DISCOUNT_TYPE_FLAT_PRICE) {

            $message = tr('discount_higher_than_original_price_error');
            
        } else {

            $message = tr('discount_percentage_higher_than_original_price_error', $this->discount_type);
        }

        return $message;
    }
}
Back to Directory File Manager