/** * Test array_unset_keys */ function test_array_unset_keys() { $data = array('first' => 'one', 'second' => 'two', 'third' => 'three'); $keys_to_remove = array('first', 'second', 'notpresent'); array_unset_keys($data, $keys_to_remove); $this->assertFalse(array_has_key($data, "first")); $this->assertFalse(array_has_key($data, "second")); $this->assertTrue(array_has_key($data, "third")); $this->assertFalse(array_has_key($data, "notpresent")); }
/** * Alias for the array_unset_keys() function. * * \param $arr * The array containing the data (original) * * \param $keys * A numeric array containing all key names to be unset. * * \see array_unset_keys */ function array_remove_keys(&$arr, $keys) { return array_unset_keys($arr, $keys); }
assert('true === array_has_value($data, "three")'); assert('false === array_has_value($data, "foo")'); assert('false === array_has_value($data, "first")'); /* test array_unset_key */ $data = array('first' => 'one', 'second' => 'two', 'third' => 'three'); array_unset_key($data, 'first'); assert('false === array_has_key($data, "first")'); assert('true === array_has_key($data, "third")'); array_unset_key($data, 'third'); assert('false === array_has_key($data, "third")'); array_unset_key($data, 'notpresent'); assert('false === array_has_key($data, "notpresent")'); /* test array_unset_keys */ $data = array('first' => 'one', 'second' => 'two', 'third' => 'three'); $keys_to_remove = array('first', 'second', 'notpresent'); array_unset_keys($data, $keys_to_remove); assert('false === array_has_key($data, "first")'); assert('false === array_has_key($data, "second")'); assert('true === array_has_key($data, "third")'); assert('false === array_has_key($data, "notpresent")'); /* test array_clear */ $data = array('first' => 'one', 'second' => 'two', 'third' => 'three'); array_clear($data); assert('count($data) == 0'); /* test array_flip_string_keys */ $data = array('first' => 'one', 'second' => 'two', 'third' => 'three', 4 => 'four', 5 => 'five', 'six' => 6); $data = array_flip_string_keys($data); assert('true === array_has_key($data, "one")'); assert('true === array_has_key($data, "two")'); assert('false === array_has_key($data, "first")'); assert('true === array_has_key($data, 4)');
/** * Connects to the PostgreSQL database. * * \param settings An associative array with connection settings: the * 'hostname', 'username' and 'password' indices will be used for connection * setttings. The key 'keep_settings' can be used to indicate whether the * settings are stored. The 'escape_column_names' and 'escape_table_names' * keys can be set to indicate whether column and table names should be * escaped when using CRUD functions. */ function connect(array $settings) { if (is_null($settings)) { $settings = array(); } assert('is_assoc_array($settings)'); /* We support "hostname", "username" and "pass" too, although the * real connection string uses other names */ $aliases = array('hostname' => 'host', 'pass' => 'password', 'username' => 'user', 'database' => 'dbname'); foreach ($aliases as $old => $new) { if (array_has_key($settings, $old)) { array_set_default($settings, $new, $settings[$old]); } } /* List of keywords that are allowed in the connection string: */ $keywords = array('host', 'hostaddr', 'port', 'dbname', 'user', 'password', 'connect_timeout', 'options', 'sslmode', 'service'); /* Create a connection string from the supplied values, leaving out * illegal (name, value) pairs */ $options = array(); foreach ($keywords as $keyword) { if (array_key_exists($keyword, $settings)) { $value = $settings[$keyword]; assert('is_string($value)'); /* Escape single and double quotes */ $value = str_replace("'", "\\'", $value); $value = str_replace('"', '\\"', $value); $options[] = sprintf('%s=%s', $keyword, $value); } } $connection_string = implode(' ', $options); $this->id = pg_connect($connection_string) or trigger_error(sprintf('Could not connect to database %s', $settings['dbname']), E_USER_ERROR); /* Keep connection settings only if requested. This makes select_db() * work, but stores the plaintext password in the object's memory. */ if (!array_get_default($settings, 'keep_settings', false)) { /* Unset both the aliases and the connection keywords */ array_unset_keys($settings, array_keys($aliases)); array_unset_keys($settings, $keywords); } $this->settings = $settings; }