Skip to content

Commit

Permalink
Add getObjectClasses method to easily retrieve objectclasses from model
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Jun 6, 2021
1 parent 3a78759 commit 55bcde6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
20 changes: 15 additions & 5 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ public function refresh()
}

/**
* Get the models batch modifications to be processed.
* Get the model's batch modifications to be processed.
*
* @return array
*/
Expand Down Expand Up @@ -736,7 +736,7 @@ public function addModification($mod = [])
}

/**
* Get the models guid attribute key name.
* Get the model's guid attribute key name.
*
* @return string
*/
Expand All @@ -746,7 +746,7 @@ public function getGuidKey()
}

/**
* Get the models ANR attributes for querying when incompatible with ANR.
* Get the model's ANR attributes for querying when incompatible with ANR.
*
* @return array
*/
Expand Down Expand Up @@ -804,7 +804,7 @@ public function newDn($dn = null)
}

/**
* Get the models binary object GUID.
* Get the model's binary object GUID.
*
* @link https://msdn.microsoft.com/en-us/library/ms679021(v=vs.85).aspx
*
Expand All @@ -816,7 +816,17 @@ public function getObjectGuid()
}

/**
* Get the models string GUID.
* Get the model's object classes.
*
* @return array
*/
public function getObjectClasses()
{
return $this->getAttribute('objectclass') ?: [];
}

/**
* Get the model's string GUID.
*
* @return string|null
*/
Expand Down
4 changes: 1 addition & 3 deletions src/Models/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,11 @@ protected function foreignKeyIsDistinguishedName()
*/
protected function determineModelFromRelated(Model $model, array $related)
{
$objectClasses = $model->getAttribute('objectclass') ?? [];

// We must normalize all the related models object class
// names to the same case so we are able to properly
// determine the owning model from search results.
return array_search(
$this->normalizeObjectClasses($objectClasses),
$this->normalizeObjectClasses($model->getObjectClasses()),
array_map([$this, 'normalizeObjectClasses'], $related)
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Models/ModelHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function test_get_recursive_results()

$related = m::mock(ModelHasManyStub::class);
$related->shouldReceive('getDn')->andReturn('bar');
$related->shouldReceive('getAttribute')->once()->with('objectclass')->andReturnNull();
$related->shouldReceive('getObjectClasses')->once()->andReturn([]);
$related->shouldReceive('convert')->once()->andReturnSelf();
$related->shouldReceive('relation')->once()->andReturnSelf();
$related->shouldReceive('recursive')->once()->andReturnSelf();
Expand Down Expand Up @@ -146,7 +146,7 @@ public function test_detaching_all()
$parent->shouldReceive('newCollection')->once()->andReturn(new Collection());

$related = m::mock(Entry::class);
$related->shouldReceive('getAttribute')->once()->with('objectclass')->andReturnNull();
$related->shouldReceive('getObjectClasses')->once()->andReturn([]);
$related->shouldReceive('convert')->once()->andReturnSelf();
$related->shouldReceive('deleteAttribute')->once()->with(['member' => 'foo'])->andReturnTrue();

Expand Down
6 changes: 6 additions & 0 deletions tests/Models/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public function test_fill()
$this->assertEquals(2, ((new Entry())->fill(['foo' => 'bar', 'baz' => 'foo'])->countAttributes()));
}

public function test_getting_object_classes()
{
$this->assertEmpty((new Entry)->getObjectClasses());
$this->assertEquals(['foo', 'bar'], (new Entry(['objectclass' => ['foo', 'bar']]))->getObjectClasses());
}

public function test_getting_and_setting_dn()
{
$model = new Entry();
Expand Down

0 comments on commit 55bcde6

Please sign in to comment.