/** * Get instance * * @throws \Exception */ public static function get() { $instance = static::getInstance(); if (!$instance->validated) { throw new \Exception(i18n::sp('%s is not initialized.', get_called_class()), 500); } return $instance; }
/** * Override this to validate setting * * @throws \Exception * @return bool */ public function validate() { $errors = []; foreach (['shop_id', 'shop_pass', 'site_id', 'site_pass'] as $key) { $value = $this->{$key}; if (empty($value)) { $errors[] = i18n::sp('%s is empty.', $key); } } if (!isset($this->creds['sandbox'])) { $errors[] = i18n::sp('Sandbox should be explicitly specified.'); } if ($errors) { throw new \Exception(implode("\n", $errors), 500); } return true; }
/** * Convert specified key * * @param array|string $params * @param array $keys * @return array|false * @throws \Exception */ public static function getSpecifiedParams(array $params = [], $keys = []) { $keys = (array) $keys; if ($lacks = Validator::lack($params, $keys)) { $message = i18n::sp('Required keys[%s] don\'t exist.', implode(', ', $lacks)); throw new \Exception($message); } $param = []; foreach ($keys as $key => $required) { switch ($key) { case 'SiteID': case 'SitePass': case 'ShopID': case 'ShopPass': $prop = strtolower(preg_replace_callback('#(?<!^)(?<![A-Z])([A-Z])#', function ($match) { return '_' . $match[1]; }, $key)); $param[$key] = Credential::get()->{$prop}; break; default: if (isset($params[$key])) { $param[$key] = $params[$key]; } break; } } return $param; }