/** * @param $params * @param $allowedParams * @param string $validationLevel allows override global validation level settings * @return array */ public static function sanitize($params, $allowedParams, $validationLevel = null) { $notAllowedParams = self::getNotAllowedParams($params, $allowedParams); if (count($notAllowedParams) > 0) { if ($validationLevel === null) { $validationLevel = BlockCypherConfigManager::getInstance()->get('validation.level'); } foreach ($notAllowedParams as $key => $value) { $validationMessage = "Param {$key} not allowed: It can be a typo in the param name or you should update the PHP SDK library."; switch ($validationLevel) { case 'log': // logs the error message to logger only (default) $logger = BlockCypherLoggingManager::getInstance(__CLASS__); $logger->warning($validationMessage); break; case 'strict': // throws a php notice message trigger_error($validationMessage, E_USER_NOTICE); break; case 'disable': // disable the validation break; } } if ($validationLevel == 'strict') { // Do not add not allowed params to the url $params = array_intersect_key($params, $allowedParams); return $params; } return $params; } return $params; }
/** * Helper method for validating if the class contains accessor methods (getter and setter) for a given attribute * * @param BlockCypherModel $class An object of BlockCypherModel * @param string $attributeName Attribute name * @return bool */ public static function validate(BlockCypherModel $class, $attributeName) { $mode = BlockCypherConfigManager::getInstance()->get('validation.level'); // Default value if validation.level was not specified. if (is_array($mode)) { $mode = 'log'; } // Check valid validation level if (!in_array($mode, array('log', 'strict', 'disabled'))) { trigger_error('Invalid validation.level in configuration', E_USER_NOTICE); } if (!empty($mode) && $mode != 'disabled') { //Check if $attributeName is string if (gettype($attributeName) !== 'string') { return false; } //If the mode is disabled, bypass the validation foreach (array('set' . $attributeName, 'get' . $attributeName) as $methodName) { if (get_class($class) == get_class(new BlockCypherModel())) { // Silently return false on cases where you are using BlockCypherModel instance directly return false; } elseif (!method_exists($class, $methodName)) { //Delegate the error based on the choice $className = is_object($class) ? get_class($class) : (string) $class; $errorMessage = "Missing Accessor: {$className}:{$methodName}. You might be using older version of SDK. If not, create an issue at https://github.com/blockcypher/php-client/issues"; BlockCypherLoggingManager::getInstance(__CLASS__)->debug($errorMessage); if ($mode == 'strict') { trigger_error($errorMessage, E_USER_NOTICE); } return false; } } return true; } return false; }
public function tearDown() { BlockCypherConfigManager::getInstance()->addConfigs(array('validation.level' => 'strict')); }
public function testInvalidMagicMethodWithValidationLevel() { BlockCypherConfigManager::getInstance()->addConfigs(array('validation.level' => 'log')); $obj = new SimpleClass(); $obj->invalid2 = "value2"; $this->assertEquals($obj->invalid2, "value2"); BlockCypherConfigManager::getInstance()->addConfigs(array('validation.level' => 'strict')); }
/** * Returns the Value of the key if found in given config, or from BlockCypher Config Manager * Returns null if not found * * @param $key * @param $config * @return null|string */ private static function getConfigValue($key, $config) { $config = $config && is_array($config) ? $config : BlockCypherConfigManager::getInstance()->getConfigHashmap(); return array_key_exists($key, $config) ? trim($config[$key]) : null; }
/** * Gets a specific configuration from key * * @param $searchKey * @return mixed */ public function get($searchKey) { return BlockCypherConfigManager::getInstance()->get($searchKey); }
/** * Create singleton instance for this class. * * @param array|null $config * @return BlockCypherCredentialManager */ public static function getInstance($config = null) { if (!self::$instance) { self::$instance = new self($config == null ? BlockCypherConfigManager::getInstance()->getConfigHashmap() : $config); } return self::$instance; }