Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace Adldap\Models; |
| 4 | |
| 5 | use Adldap\Query\Builder; |
| 6 | use Adldap\Schemas\ActiveDirectory; |
| 7 | use Adldap\Schemas\SchemaInterface; |
| 8 | |
| 9 | /** |
| 10 | * Class Factory. |
| 11 | * |
| 12 | * Creates new LDAP models. |
| 13 | */ |
| 14 | class Factory |
| 15 | { |
| 16 | /** |
| 17 | * The LDAP query builder. |
| 18 | * |
| 19 | * @var Builder |
| 20 | */ |
| 21 | protected $query; |
| 22 | |
| 23 | /** |
| 24 | * The LDAP schema. |
| 25 | * |
| 26 | * @var SchemaInterface |
| 27 | */ |
| 28 | protected $schema; |
| 29 | |
| 30 | /** |
| 31 | * Constructor. |
| 32 | * |
| 33 | * @param Builder $builder |
| 34 | */ |
| 35 | public function __construct(Builder $builder) |
| 36 | { |
| 37 | $this->setQuery($builder) |
| 38 | ->setSchema($builder->getSchema()); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Sets the current query builder. |
| 43 | * |
| 44 | * @param Builder $builder |
| 45 | * |
| 46 | * @return $this |
| 47 | */ |
| 48 | public function setQuery(Builder $builder) |
| 49 | { |
| 50 | $this->query = $builder; |
| 51 | |
| 52 | return $this; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Sets the current schema. |
| 57 | * |
| 58 | * If null is given, a default ActiveDirectory schema is set. |
| 59 | * |
| 60 | * @param SchemaInterface|null $schema |
| 61 | * |
| 62 | * @return $this |
| 63 | */ |
| 64 | public function setSchema(SchemaInterface $schema = null) |
| 65 | { |
| 66 | $this->schema = $schema ?: new ActiveDirectory(); |
| 67 | |
| 68 | return $this; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Creates a new generic LDAP entry instance. |
| 73 | * |
| 74 | * @param array $attributes |
| 75 | * |
| 76 | * @return Entry |
| 77 | */ |
| 78 | public function entry(array $attributes = []) |
| 79 | { |
| 80 | $model = $this->schema->entryModel(); |
| 81 | |
| 82 | return new $model($attributes, $this->query); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Creates a new user instance. |
| 87 | * |
| 88 | * @param array $attributes |
| 89 | * |
| 90 | * @return User |
| 91 | */ |
| 92 | public function user(array $attributes = []) |
| 93 | { |
| 94 | $model = $this->schema->userModel(); |
| 95 | |
| 96 | return (new $model($attributes, $this->query)) |
| 97 | ->setAttribute($this->schema->objectClass(), $this->schema->userObjectClasses()); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Creates a new organizational unit instance. |
| 102 | * |
| 103 | * @param array $attributes |
| 104 | * |
| 105 | * @return OrganizationalUnit |
| 106 | */ |
| 107 | public function ou(array $attributes = []) |
| 108 | { |
| 109 | $model = $this->schema->organizationalUnitModel(); |
| 110 | |
| 111 | return (new $model($attributes, $this->query)) |
| 112 | ->setAttribute($this->schema->objectClass(), [ |
| 113 | $this->schema->top(), |
| 114 | $this->schema->organizationalUnit(), |
| 115 | ]); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Creates a new organizational unit instance. |
| 120 | * |
| 121 | * @param array $attributes |
| 122 | * |
| 123 | * @return Organization |
| 124 | */ |
| 125 | public function organization(array $attributes = []) |
| 126 | { |
| 127 | $model = $this->schema->organizationModel(); |
| 128 | |
| 129 | return (new $model($attributes, $this->query)) |
| 130 | ->setAttribute($this->schema->objectClass(), [ |
| 131 | $this->schema->top(), |
| 132 | $this->schema->organization(), |
| 133 | ]); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Creates a new group instance. |
| 138 | * |
| 139 | * @param array $attributes |
| 140 | * |
| 141 | * @return Group |
| 142 | */ |
| 143 | public function group(array $attributes = []) |
| 144 | { |
| 145 | $model = $this->schema->groupModel(); |
| 146 | |
| 147 | return (new $model($attributes, $this->query)) |
| 148 | ->setAttribute($this->schema->objectClass(), [ |
| 149 | $this->schema->top(), |
| 150 | $this->schema->objectCategoryGroup(), |
| 151 | ]); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Creates a new organizational unit instance. |
| 156 | * |
| 157 | * @param array $attributes |
| 158 | * |
| 159 | * @return Container |
| 160 | */ |
| 161 | public function container(array $attributes = []) |
| 162 | { |
| 163 | $model = $this->schema->containerModel(); |
| 164 | |
| 165 | return (new $model($attributes, $this->query)) |
| 166 | ->setAttribute($this->schema->objectClass(), $this->schema->objectClassContainer()); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Creates a new user instance as a contact. |
| 171 | * |
| 172 | * @param array $attributes |
| 173 | * |
| 174 | * @return User |
| 175 | */ |
| 176 | public function contact(array $attributes = []) |
| 177 | { |
| 178 | $model = $this->schema->contactModel(); |
| 179 | |
| 180 | return (new $model($attributes, $this->query)) |
| 181 | ->setAttribute($this->schema->objectClass(), [ |
| 182 | $this->schema->top(), |
| 183 | $this->schema->person(), |
| 184 | $this->schema->organizationalPerson(), |
| 185 | $this->schema->contact(), |
| 186 | ]); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Creates a new computer instance. |
| 191 | * |
| 192 | * @param array $attributes |
| 193 | * |
| 194 | * @return Computer |
| 195 | */ |
| 196 | public function computer(array $attributes = []) |
| 197 | { |
| 198 | $model = $this->schema->computerModel(); |
| 199 | |
| 200 | return (new $model($attributes, $this->query)) |
| 201 | ->setAttribute($this->schema->objectClass(), [ |
| 202 | $this->schema->top(), |
| 203 | $this->schema->person(), |
| 204 | $this->schema->organizationalPerson(), |
| 205 | $this->schema->user(), |
| 206 | $this->schema->computer(), |
| 207 | ]); |
| 208 | } |
| 209 | } |