/**
  * Validate element value
  *
  * If a translation adapter is registered, any error messages will be
  * translated according to the current locale, using the given error code;
  * if no matching translation is found, the original message will be
  * utilized.
  *
  * Note: The *filtered* value is validated.
  *
  * @param  array   $data
  * @param  mixed   $context
  * @return boolean
  */
 public function isValid($data, $context = null, $removeNotPresentFields = false)
 {
     if (!($data instanceof TariffPlanServicesModel || $this->_acceptArrayAsModel && is_array($data))) {
         $this->_messages = array();
         $this->_messages[self::NOT_TARIFF_PLAN_SERVICES] = $this->_messageTemplatesUser[self::NOT_TARIFF_PLAN_SERVICES];
         return false;
     }
     if (!parent::isValid($data, $context, $removeNotPresentFields)) {
         return false;
     }
     if (!$data instanceof TariffPlanServicesModel) {
         $data = new TariffPlanServicesModel($data);
     }
     $tariffs = $data->getData();
     $tariffs[] = $data->getDefaultData();
     $count = 0;
     $limit = 3;
     $validator = new TariffPlan\Services\Complex\DataTariffValidate();
     foreach ($tariffs as $tariff) {
         if (!$validator->isValid($tariff)) {
             $count++;
             if ($count > $limit) {
                 $this->_messages = array();
                 $this->_messages[self::TOO_MANY_COMPLEX_TARIFFS] = str_replace('%type%', 'data', $this->_messageTemplatesUser[self::TOO_MANY_COMPLEX_TARIFFS]);
                 return false;
             }
         }
     }
     $tariffs = $data->getOrigVoice();
     $tariffs[] = $data->getDefaultOrigVoice();
     $count = 0;
     $limit = 3;
     $validator = new TariffPlan\Services\Complex\VoiceTariffValidate();
     foreach ($tariffs as $tariff) {
         if (!$validator->isValid($tariff)) {
             $count++;
             if ($count > $limit) {
                 $this->_messages = array();
                 $this->_messages[self::TOO_MANY_COMPLEX_TARIFFS] = str_replace('%type%', 'origined voice', $this->_messageTemplatesUser[self::TOO_MANY_COMPLEX_TARIFFS]);
                 return false;
             }
         }
     }
     $tariffs = $data->getTermVoice();
     $tariffs[] = $data->getDefaultTermVoice();
     $count = 0;
     $limit = 3;
     $validator = new TariffPlan\Services\Complex\VoiceTariffValidate();
     foreach ($tariffs as $tariff) {
         if (!$validator->isValid($tariff)) {
             $count++;
             if ($count > $limit) {
                 $this->_messages = array();
                 $this->_messages[self::TOO_MANY_COMPLEX_TARIFFS] = str_replace('%type%', 'terminated voice', $this->_messageTemplatesUser[self::TOO_MANY_COMPLEX_TARIFFS]);
                 return false;
             }
         }
     }
     $tariffs = $data->getSms();
     $tariffs[] = $data->getDefaultSms();
     $count = 0;
     $limit = 3;
     $validator = new TariffPlan\Services\Complex\SmsTariffValidate();
     foreach ($tariffs as $tariff) {
         if (!$validator->isValid($tariff)) {
             $count++;
             if ($count > $limit) {
                 $this->_messages = array();
                 $this->_messages[self::TOO_MANY_COMPLEX_TARIFFS] = str_replace('%type%', 'sms', $this->_messageTemplatesUser[self::TOO_MANY_COMPLEX_TARIFFS]);
                 return false;
             }
         }
     }
     return true;
 }
 /**
  * Validate element value
  *
  * If a translation adapter is registered, any error messages will be
  * translated according to the current locale, using the given error code;
  * if no matching translation is found, the original message will be
  * utilized.
  *
  * Note: The *filtered* value is validated.
  *
  * @param  array   $data
  * @param  mixed   $context
  * @return boolean
  */
 public function isValid($data, $context = null, $removeNotPresentFields = false)
 {
     if (!($data instanceof TariffPlanServicesModel || $this->_acceptArrayAsModel && is_array($data))) {
         $this->_messages = array();
         $this->_messages[self::NOT_TARIFF_PLAN_SERVICES] = $this->_messageTemplatesUser[self::NOT_TARIFF_PLAN_SERVICES];
         return false;
     }
     if (!parent::isValid($data, $context, $removeNotPresentFields)) {
         return false;
     }
     if (!$data instanceof TariffPlanServicesModel) {
         $data = new TariffPlanServicesModel($data);
     }
     /*
      * GLOBALPORTAL-97959
      */
     $ignoreDeleted = $data->id && $data->published == ServicePackModel::STATUS_PUBLISHED;
     $tariffsData = $data->getData();
     $tariffsData[] = $data->getDefaultData();
     $validatorData = new TariffPlan\Services\Complex\DataTariffValidate(array('ignoreDeleted' => $ignoreDeleted));
     $tariffsVoice = $data->getOrigVoice();
     $tariffsVoice[] = $data->getDefaultOrigVoice();
     $validatorVoice = new TariffPlan\Services\Complex\VoiceTariffValidate(array('ignoreDeleted' => $ignoreDeleted));
     $tariffsTerm = $data->getTermVoice();
     $tariffsTerm[] = $data->getDefaultTermVoice();
     $validatorTerm = new TariffPlan\Services\Complex\VoiceTariffValidate(array('ignoreDeleted' => $ignoreDeleted));
     $tariffsSms = $data->getSms();
     $tariffsSms[] = $data->getDefaultSms();
     $validatorSms = new TariffPlan\Services\Complex\SmsTariffValidate(array('ignoreDeleted' => $ignoreDeleted));
     if (!$this->_correctNumberOfComplexTariffs($tariffsData, $validatorData, 'data') || !$this->_correctNumberOfComplexTariffs($tariffsVoice, $validatorVoice, 'originated voice') || !$this->_correctNumberOfComplexTariffs($tariffsTerm, $validatorTerm, 'terminated voice') || !$this->_correctNumberOfComplexTariffs($tariffsSms, $validatorSms, 'sms')) {
         return false;
     }
     return true;
 }