PHPUnit_Util_XML::assertValidKeys PHP Méthode

assertValidKeys() public static méthode

Validate list of keys in the associative array.
Author: Mike Naberezny ([email protected])
Author: Derek DeVries ([email protected])
public static assertValidKeys ( array $hash, array $validKeys ) : array
$hash array
$validKeys array
Résultat array
    public static function assertValidKeys(array $hash, array $validKeys)
    {
        $valids = array();
        // Normalize validation keys so that we can use both indexed and
        // associative arrays.
        foreach ($validKeys as $key => $val) {
            is_int($key) ? $valids[$val] = NULL : ($valids[$key] = $val);
        }
        $validKeys = array_keys($valids);
        // Check for invalid keys.
        foreach ($hash as $key => $value) {
            if (!in_array($key, $validKeys)) {
                $unknown[] = $key;
            }
        }
        if (!empty($unknown)) {
            throw new InvalidArgumentException('Unknown key(s): ' . implode(', ', $unknown));
        }
        // Add default values for any valid keys that are empty.
        foreach ($valids as $key => $value) {
            if (!isset($hash[$key])) {
                $hash[$key] = $value;
            }
        }
        return $hash;
    }

Usage Example

Exemple #1
0
 public function testAssertValidKeysInvalidKeys()
 {
     $options = array('testA' => 1, 'testD' => 2, 'testE' => 3);
     $valid = array('testA', 'testB', 'testC');
     try {
         $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
         $this->fail();
     } catch (PHPUnit_Framework_Exception $e) {
         $this->assertEquals('Unknown key(s): testD, testE', $e->getMessage());
     }
 }
All Usage Examples Of PHPUnit_Util_XML::assertValidKeys