Completed
Push — development ( 6a24df...5afdf5 )
by Nils
07:52
created

adLDAPCollection::__get()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 14
nc 7
nop 1
dl 0
loc 21
rs 7.1428
c 0
b 0
f 0
1
<?php
2
namespace adLDAP\collections;
3
use adLDAP\adLDAP;
4
/**
5
 * PHP LDAP CLASS FOR MANIPULATING ACTIVE DIRECTORY 
6
 * Version 5.0.0
7
 * 
8
 * PHP Version 5 with SSL and LDAP support
9
 * 
10
 * Written by Scott Barnett, Richard Hyland
11
 *   email: [email protected], [email protected]
12
 *   http://github.com/adldap/adLDAP
13
 * 
14
 * Copyright (c) 2006-2014 Scott Barnett, Richard Hyland
15
 * 
16
 * We'd appreciate any improvements or additions to be submitted back
17
 * to benefit the entire community :)
18
 * 
19
 * This library is free software; you can redistribute it and/or
20
 * modify it under the terms of the GNU Lesser General Public
21
 * License as published by the Free Software Foundation; either
22
 * version 2.1 of the License.
23
 * 
24
 * This library is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27
 * Lesser General Public License for more details.
28
 * 
29
 * @category ToolsAndUtilities
30
 * @package adLDAP
31
 * @subpackage Collection
32
 * @author Scott Barnett, Richard Hyland
33
 * @copyright (c) 2006-2014 Scott Barnett, Richard Hyland
34
 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html LGPLv2.1
35
 * @version 5.0.0
36
 * @link http://github.com/adldap/adLDAP
37
 */
38
abstract class adLDAPCollection
39
{
40
    /**
41
     * The current adLDAP connection via dependency injection
42
     * 
43
     * @var adLDAP
44
     */
45
    protected $adldap;
46
    
47
    /**
48
     * The current object being modifed / called
49
     * 
50
     * @var mixed
51
     */
52
    protected $currentObject;
53
    
54
    /**
55
     * The raw info array from Active Directory
56
     * 
57
     * @var array
58
     */
59
    protected $info;
60
    
61
    public function __construct($info, adLDAP $adldap) {
62
        $this->setInfo($info);   
63
        $this->adldap = $adldap;
64
    }
65
    
66
    /**
67
     * Set the raw info array from Active Directory
68
     * 
69
     * @param array $info
70
     */
71
    public function setInfo(array $info) {
72
        if ($this->info && sizeof($info) >= 1) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->info of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
73
            unset($this->info);
74
        }
75
        $this->info = $info;   
76
    }
77
    
78
    /**
79
     * Magic get method to retrieve data from the raw array in a formatted way
80
     * 
81
     * @param string $attribute
82
     * @return mixed
83
     */
84
    public function __get($attribute) {
85
        if (isset($this->info[0]) && is_array($this->info[0])) {
86
            foreach ($this->info[0] as $keyAttr => $valueAttr) {
87
                if (strtolower($keyAttr) == strtolower($attribute)) {
88
                    if ($this->info[0][strtolower($attribute)]['count'] == 1) {
89
                        return $this->info[0][strtolower($attribute)][0];   
90
                    } else {
91
                        $array = array();
92
                        foreach ($this->info[0][strtolower($attribute)] as $key => $value) {
93
                            if ((string) $key != 'count') {
94
                                $array[$key] = $value;
95
                            } 
96
                        }  
97
                        return $array;   
98
                    }
99
                }   
100
            }
101
        } else {
102
            return NULL;   
103
        }
104
    }    
105
    
106
    /**
107
     * Magic set method to update an attribute
108
     * 
109
     * @param string $attribute
110
     * @param string $value
111
     * @return bool
112
     */
113
    abstract public function __set($attribute, $value);
114
    
115
    /** 
116
     * Magic isset method to check for the existence of an attribute 
117
     * 
118
     * @param string $attribute 
119
     * @return bool 
120
     */ 
121
    public function __isset($attribute) {
122
        if (isset($this->info[0]) && is_array($this->info[0])) { 
123
            foreach ($this->info[0] as $keyAttr => $valueAttr) { 
124
                if (strtolower($keyAttr) == strtolower($attribute)) { 
125
                    return true; 
126
                } 
127
            } 
128
        } 
129
        return false; 
130
        } 
131
}
132
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
133