Example #1
0
 public function validatePromotion($attribute)
 {
     if ($attribute !== 'promotion') {
         return true;
     }
     $promotion = $this->{$attribute};
     if (empty($promotion['type'])) {
         throw new BadRequestHttpException('missing promotion type');
     }
     if (empty($promotion['data']) || !is_array($promotion['data'])) {
         throw new InvalidParameterException(['campaignProducts' => \Yii::t('product', 'product_required')]);
     }
     $promotion['data'] = MongodbUtil::toMongoIdList($promotion['data']);
     $productCount = Product::count(['_id' => ['$in' => $promotion['data']], 'isBindCode' => true]);
     if ($productCount != count($promotion['data'])) {
         throw new InvalidParameterException(['promotionProduct' => \Yii::t('product', 'error_promotion_product')]);
     } else {
         $this->{$attribute} = $promotion;
     }
     if (!in_array($promotion['products'], [self::TYPE_GIFT_PRODUCT_FIRST, self::TYPE_GIFT_PRODUCT_UNLIMITED]) && !is_array($promotion['products'])) {
         throw new InvalidParameterException(['promotionCampaigns' => \Yii::t('product', 'invalid_gift_campaigns')]);
     }
     if (is_array($promotion['products'])) {
         $promotion['products'] = MongodbUtil::toMongoIdList($promotion['products']);
         if (count($promotion['products']) == Product::count(['_id' => ['$in' => $promotion['products']]])) {
             $this->{$attribute} = $promotion;
         } else {
             throw new BadRequestHttpException('error products');
         }
     }
     if (!is_array($promotion['tags'])) {
         throw new BadRequestHttpException('tags must be array');
     }
     if (!is_array($promotion['channels'])) {
         throw new BadRequestHttpException('channels must be array');
     }
     if (empty($promotion['gift']) || !is_array($promotion['gift'])) {
         return new BadRequestHttpException('gift must be array');
     }
     $gift = GiftValidator::validateGift($promotion['gift']);
     $promotion['gift'] = $gift;
     $this->{$attribute} = $promotion;
 }