Example #1
0
 public function getPricingErrors(Post $post)
 {
     $i18n = Localization::getTranslator();
     if (isset($post['sources'])) {
         $sources = explode(',', $post['sources']);
     } else {
         $sources = [];
     }
     $pricingSettings = [];
     if (empty($post['selector'])) {
         $pricingSettings[] = ['id' => '#sources-methods-error', 'error' => $i18n->_('A choice must be made between Single\\Highest\\Lowest')];
     } else {
         if ($post['selector'] === 'single') {
             if (count($sources) !== 1) {
                 $pricingSettings[] = ['id' => '#sources-methods-error', 'error' => $i18n->_('If Single is chosen, exactly 1 pricing source must be chosen.')];
             }
         } else {
             if (count($sources) < 1) {
                 $pricingSettings[] = ['id' => '#sources-methods-error', 'error' => $i18n->_('At least one pricing source must be chosen.')];
             }
         }
     }
     $providers = Config::getAvailablePricingProviders();
     foreach ($sources as $source) {
         if (!isset($providers[$source])) {
             $pricingSettings[] = ['id' => '#sources-methods-error', 'error' => $i18n->_('Unrecognized pricing source:') . ' "' . $source . '".'];
         }
     }
     if (array_search('PricingProviders\\StaticPrice', $sources) !== false) {
         if (!isset($post['staticPrice'])) {
             $pricingSettings[] = ['id' => '#sources-methods-error', 'error' => $i18n->_('When using static price a value is required.')];
         } elseif (!is_numeric($post['staticPrice'])) {
             $pricingSettings[] = ['id' => '#sources-methods-error', 'error' => $i18n->_('Static price value must be numeric.')];
         }
     }
     $modifiers = Config::getAvailablePricingModifiers();
     if (isset($post['modifier']) && is_array($post['modifier'])) {
         foreach ($post['modifier'] as $modifier => $value) {
             if (!isset($modifiers[$modifier])) {
                 $pricingSettings[] = ['id' => '#sources-methods-error', 'error' => $i18n->_('Unrecognized pricing modifier:') . ' "' . $modifier . '".'];
             }
             if (!is_numeric($value)) {
                 $pricingSettings[] = ['id' => '#sources-methods-error', 'error' => $i18n->_('Value must be numeric for:') . ' "' . $modifier . '".'];
             }
         }
     }
     return $pricingSettings;
 }