Skip to content

Commit

Permalink
chore(entity): move relationships related functions into trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jeabakker committed Aug 1, 2024
1 parent 4307699 commit e705e27
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 111 deletions.
119 changes: 119 additions & 0 deletions engine/classes/Elgg/Traits/Entity/Relationships.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Elgg\Traits\Entity;

/**
* Bundle all relationship related functions for an \ElggEntity
*
* @since 6.1
*/
trait Relationships {

/**
* Add a relationship between this and another entity.
*
* @tip Read the relationship like "This entity is a $relationship of $guid_two."
*
* @param int $guid_two GUID of the target entity of the relationship
* @param string $relationship The type of relationship
*
* @return bool
* @throws \Elgg\Exceptions\LengthException
*/
public function addRelationship(int $guid_two, string $relationship): bool {
$rel = new \ElggRelationship();
$rel->guid_one = $this->guid;
$rel->relationship = $relationship;
$rel->guid_two = $guid_two;

return $rel->save();
}

/**
* Check if this entity has a relationship with another entity
*
* @tip Read the relationship like "This entity is a $relationship of $guid_two."
*
* @param int $guid_two GUID of the target entity of the relationship
* @param string $relationship The type of relationship
*
* @return bool
* @since 4.3
*/
public function hasRelationship(int $guid_two, string $relationship): bool {
return (bool) _elgg_services()->relationshipsTable->check($this->guid, $relationship, $guid_two);
}

/**
* Return the relationship if this entity has a relationship with another entity
*
* @param int $guid_two GUID of the target entity of the relationship
* @param string $relationship The type of relationship
*
* @return \ElggRelationship|null
* @since 4.3
*/
public function getRelationship(int $guid_two, string $relationship): ?\ElggRelationship {
return _elgg_services()->relationshipsTable->check($this->guid, $relationship, $guid_two) ?: null;
}

/**
* Gets an array of entities with a relationship to this entity.
*
* @param array $options Options array. See elgg_get_entities()
* for a list of options. 'relationship_guid' is set to
* this entity
*
* @return \ElggEntity[]|int|mixed
* @see elgg_get_entities()
*/
public function getEntitiesFromRelationship(array $options = []) {
$options['relationship_guid'] = $this->guid;
return elgg_get_entities($options);
}

/**
* Gets the number of entities from a specific relationship type
*
* @param string $relationship Relationship type (eg "friends")
* @param bool $inverse_relationship Invert relationship
*
* @return int
*/
public function countEntitiesFromRelationship(string $relationship, bool $inverse_relationship = false): int {
return elgg_count_entities([
'relationship' => $relationship,
'relationship_guid' => $this->guid,
'inverse_relationship' => $inverse_relationship,
]);
}

/**
* Remove a relationship
*
* @param int $guid_two GUID of the target entity of the relationship
* @param string $relationship The type of relationship
*
* @return bool
*/
public function removeRelationship(int $guid_two, string $relationship): bool {
return _elgg_services()->relationshipsTable->remove($this->guid, $relationship, $guid_two);
}

/**
* Remove all relationships to or from this entity.
*
* If you pass a relationship name, only relationships matching that name will be deleted.
*
* @warning Calling this with no $relationship will clear all relationships with this entity.
*
* @param string $relationship (optional) The name of the relationship to remove
* @param bool $inverse_relationship (optional) Inverse the relationship
*
* @return bool
* @since 4.3
*/
public function removeAllRelationships(string $relationship = '', bool $inverse_relationship = false): bool {
return _elgg_services()->relationshipsTable->removeAll($this->guid, $relationship, $inverse_relationship);
}
}
110 changes: 2 additions & 108 deletions engine/classes/ElggEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Elgg\Exceptions\InvalidArgumentException as ElggInvalidArgumentException;
use Elgg\Traits\Entity\Icons;
use Elgg\Traits\Entity\Metadata;
use Elgg\Traits\Entity\Relationships;
use Elgg\Traits\Entity\Subscriptions;

/**
Expand Down Expand Up @@ -50,6 +51,7 @@ abstract class ElggEntity extends \ElggData {

use Icons;
use Metadata;
use Relationships;
use Subscriptions;

public const PRIMARY_ATTR_NAMES = [
Expand Down Expand Up @@ -342,114 +344,6 @@ public function setVolatileData(string $name, $value): void {
$this->volatile[$name] = $value;
}

/**
* Add a relationship between this and another entity.
*
* @tip Read the relationship like "This entity is a $relationship of $guid_two."
*
* @param int $guid_two GUID of the target entity of the relationship
* @param string $relationship The type of relationship
*
* @return bool
* @throws \Elgg\Exceptions\LengthException
*/
public function addRelationship(int $guid_two, string $relationship): bool {
$rel = new \ElggRelationship();
$rel->guid_one = $this->guid;
$rel->relationship = $relationship;
$rel->guid_two = $guid_two;

return $rel->save();
}

/**
* Check if this entity has a relationship with another entity
*
* @tip Read the relationship like "This entity is a $relationship of $guid_two."
*
* @param int $guid_two GUID of the target entity of the relationship
* @param string $relationship The type of relationship
*
* @return bool
* @since 4.3
*/
public function hasRelationship(int $guid_two, string $relationship): bool {
return (bool) _elgg_services()->relationshipsTable->check($this->guid, $relationship, $guid_two);
}

/**
* Return the relationship if this entity has a relationship with another entity
*
* @param int $guid_two GUID of the target entity of the relationship
* @param string $relationship The type of relationship
*
* @return \ElggRelationship|null
* @since 4.3
*/
public function getRelationship(int $guid_two, string $relationship): ?\ElggRelationship {
return _elgg_services()->relationshipsTable->check($this->guid, $relationship, $guid_two) ?: null;
}

/**
* Gets an array of entities with a relationship to this entity.
*
* @param array $options Options array. See elgg_get_entities()
* for a list of options. 'relationship_guid' is set to
* this entity
*
* @return \ElggEntity[]|int|mixed
* @see elgg_get_entities()
*/
public function getEntitiesFromRelationship(array $options = []) {
$options['relationship_guid'] = $this->guid;
return elgg_get_entities($options);
}

/**
* Gets the number of entities from a specific relationship type
*
* @param string $relationship Relationship type (eg "friends")
* @param bool $inverse_relationship Invert relationship
*
* @return int
*/
public function countEntitiesFromRelationship(string $relationship, bool $inverse_relationship = false): int {
return elgg_count_entities([
'relationship' => $relationship,
'relationship_guid' => $this->guid,
'inverse_relationship' => $inverse_relationship,
]);
}

/**
* Remove a relationship
*
* @param int $guid_two GUID of the target entity of the relationship
* @param string $relationship The type of relationship
*
* @return bool
*/
public function removeRelationship(int $guid_two, string $relationship): bool {
return _elgg_services()->relationshipsTable->remove($this->guid, (string) $relationship, (int) $guid_two);
}

/**
* Remove all relationships to or from this entity.
*
* If you pass a relationship name, only relationships matching that name will be deleted.
*
* @warning Calling this with no $relationship will clear all relationships with this entity.
*
* @param string|null $relationship (optional) The name of the relationship to remove
* @param bool $inverse_relationship (optional) Inverse the relationship
*
* @return bool
* @since 4.3
*/
public function removeAllRelationships(string $relationship = '', bool $inverse_relationship = false): bool {
return _elgg_services()->relationshipsTable->removeAll($this->guid, $relationship, $inverse_relationship);
}

/**
* Removes all river items related to this entity
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Elgg\Integration;
namespace Elgg\Traits\Entity;

use Elgg\IntegrationTestCase;

class ElggRelationshipTest extends IntegrationTestCase {
abstract class RelationshipsIntegrationTestCase extends IntegrationTestCase {

/**
* @var \ElggEntity
Expand Down Expand Up @@ -32,14 +32,21 @@ public function up() {
$this->user = $this->createUser();
elgg()->session_manager->setLoggedInUser($this->user);

$this->entity1 = $this->createObject(['subtype' => 'elgg_relationship_test']);
$this->entity1 = $this->getEntity();
$this->entity2 = $this->createObject(['subtype' => 'elgg_relationship_test']);
$this->entity3 = $this->createObject(['subtype' => 'elgg_relationship_test']);
}

public function down() {
_elgg_services()->events->restore();
}

/**
* Get the testing entity
*
* @return \ElggEntity
*/
abstract protected function getEntity(): \ElggEntity;

public function testAddRelationship() {
// test adding a relationship
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Elgg\Traits\Entity;

use Elgg\Traits\Entity\RelationshipsIntegrationTestCase;

class ElggGroupRelationshipsIntegrationTest extends RelationshipsIntegrationTestCase {

protected function getEntity(): \ElggEntity {
return $this->createGroup();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Elgg\Traits\Entity;

use Elgg\Traits\Entity\RelationshipsIntegrationTestCase;

class ElggObjectRelationshipsIntegrationTest extends RelationshipsIntegrationTestCase {

protected function getEntity(): \ElggEntity {
return $this->createObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Elgg\Traits\Entity;

use Elgg\Traits\Entity\RelationshipsIntegrationTestCase;

class ElggSiteRelationshipsIntegrationTest extends RelationshipsIntegrationTestCase {

protected function getEntity(): \ElggEntity {
return $this->createSite();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Elgg\Traits\Entity;

use Elgg\Traits\Entity\RelationshipsIntegrationTestCase;

class ElggUserRelationshipsIntegrationTest extends RelationshipsIntegrationTestCase {

protected function getEntity(): \ElggEntity {
return $this->createUser();
}
}

0 comments on commit e705e27

Please sign in to comment.