Skip to content

Instantly share code, notes, and snippets.

@mrclay
Created March 3, 2016 22:31
Show Gist options
  • Save mrclay/d80696e5ad6a71b70bb2 to your computer and use it in GitHub Desktop.
Save mrclay/d80696e5ad6a71b70bb2 to your computer and use it in GitHub Desktop.
<?php
namespace Elgg;
/**
* Represents a party acting as the subject or object of an action on a site, not necessarily a logged in user.
*
* Unlike \ElggUser, a party may represent an anonymous (logged out) user.
*/
class Party {
/**
* @var int
*/
private $guid;
/**
* Does this party represent an anonymous logged out user
*
* @return bool
*/
public function isAnonymous() {
return $this->guid === 0;
}
/**
* Get the party's GUID (or 0 for anonymous)
*
* @return int
*/
public function getGuid() {
return $this->guid;
}
/**
* Get a party representing the current session
*
* @return Party
*/
public static function fromSession() {
return new self(_elgg_services()->session->getLoggedInUserGuid());
}
/**
* Build from a given GUID/user entity value
*
* @param Party|int|\ElggUser $value A value given to represent a user or GUID
* @param bool $falsy_unknown If true, a falsy value will result in pulling the user GUID
* from the current session
*
* @return Party
* @throws \InvalidArgumentException
*/
public static function fromValue($value, $falsy_unknown = true) {
if ($value instanceof Party) {
return $value;
}
if ($value instanceof \ElggUser) {
return new self($value->guid);
}
if (!$value) {
if ($falsy_unknown) {
return self::fromSession();
} else {
return new self(0);
}
}
if (is_numeric($value)) {
return new self((int)$value);
}
throw new \InvalidArgumentException();
}
/**
* Constructor
*
* @param int $guid User GUID or 0 for anonymous
*/
private function __construct($guid) {
$this->guid = (int)$guid;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment