Completed
Push — master ( 808509...f69da3 )
by Christopher
06:05
created

LdapUtils   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 1
dl 0
loc 88
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A explodeDn() 0 10 4
A implodeDn() 0 12 3
A getRdnFromDn() 0 7 1
A recursive_implode() 0 19 4
1
<?php
2
/**
3
 * This file is part of the LdapTools package.
4
 *
5
 * (c) Chad Sikorra <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace chrmorandi\ldap;
12
13
14
/**
15
 * Some common helper LDAP functions.
16
 *
17
 * @author Chad Sikorra <[email protected]>
18
 */
19
class LdapUtils
20
{
21
    
22
    /**
23
     * Converts a string distinguished name into its separate pieces.
24
     *
25
     * @param string $dn
26
     * @param int $withAttributes Set to 0 to get the attribute names along with the value.
27
     * @return array
28
     */
29
    public static function explodeDn($dn, $withAttributes = 1)
30
    {
31
        $pieces = ldap_explode_dn($dn, $withAttributes);
32
33
        if ($pieces === false || !isset($pieces['count']) || $pieces['count'] == 0) {
34
            throw new \yii\base\InvalidParamException(sprintf('Unable to parse DN "%s".', $dn));
35
        }
36
        unset($pieces['count']);
37
        return $pieces;
38
    }
39
40
    /**
41
     * Given a DN as an array in ['cn=Name', 'ou=Employees', 'dc=example', 'dc=com'] form, return it as its string
42
     * representation that is safe to pass back to a query or to save back to LDAP for a DN.
43
     *
44
     * @param array $dn
45
     * @return string
46
     */
47
    public static function implodeDn(array $dn)
48
    {
49
        foreach ($dn as $index => $piece) {
50
            $values = explode('=', $piece, 2);
51
            if (count($values) === 1) {
52
                throw new InvalidArgumentException(sprintf('Unable to parse DN piece "%s".', $values[0]));
53
            }
54
            $dn[$index] = $values[0].'='.$values[1];
55
        }
56
57
        return implode(',', $dn);
58
    }
59
60
    /**
61
     * Given a full escaped DN return the RDN in escaped form.
62
     *
63
     * @param string $dn
64
     * @return string
65
     */
66
    public static function getRdnFromDn($dn)
67
    {
68
        $rdn = self::explodeDn($dn, 0)[0];
69
        $rdn = explode('=', $rdn, 2);
70
71
        return $rdn[0].'='.$rdn[1];
72
    }
73
    
74
    /**
75
     * Recursively implodes an array with optional key inclusion
76
     * 
77
     * Example of $include_keys output: key, value, key, value, key, value
78
     * 
79
     * @access  public
80
     * @param   array   $array         multi-dimensional array to recursively implode
81
     * @param   string  $glue          value that glues elements together	
82
     * @param   bool    $include_keys  include keys before their values
83
     * @param   bool    $trim_all      trim ALL whitespace from string
84
     * @return  string  imploded array
85
     */ 
86
    public static function recursive_implode(array $array, $glue = ',', $include_keys = false, $trim_all = true)
87
    {
88
            $glued_string = '';
89
90
            // Recursively iterates array and adds key/value to glued string
91
            array_walk_recursive($array, function($value, $key) use ($glue, $include_keys, &$glued_string)
92
            {
93
                    $include_keys and $glued_string .= $key.$glue;
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
94
                    $glued_string .= $value.$glue;
95
            });
96
97
            // Removes last $glue from string
98
            strlen($glue) > 0 and $glued_string = substr($glued_string, 0, -strlen($glue));
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
99
100
            // Trim ALL whitespace
101
            $trim_all and $glued_string = preg_replace("/(\s)/ixsm", '', $glued_string);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
102
103
            return (string) $glued_string;
104
    }
105
106
}
107