UserIdentityValue.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Value object representing a user's identity.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. namespace MediaWiki\User;
  23. use Wikimedia\Assert\Assert;
  24. /**
  25. * Value object representing a user's identity.
  26. *
  27. * @since 1.31
  28. */
  29. class UserIdentityValue implements UserIdentity {
  30. /**
  31. * @var int
  32. */
  33. private $id;
  34. /**
  35. * @var string
  36. */
  37. private $name;
  38. /**
  39. * @var int
  40. */
  41. private $actor;
  42. /**
  43. * @param int $id
  44. * @param string $name
  45. * @param int $actor
  46. */
  47. public function __construct( $id, $name, $actor ) {
  48. Assert::parameterType( 'integer', $id, '$id' );
  49. Assert::parameterType( 'string', $name, '$name' );
  50. Assert::parameterType( 'integer', $actor, '$actor' );
  51. $this->id = $id;
  52. $this->name = $name;
  53. $this->actor = $actor;
  54. }
  55. /**
  56. * @return int The user ID. May be 0 for anonymous users or for users with no local account.
  57. */
  58. public function getId() {
  59. return $this->id;
  60. }
  61. /**
  62. * @return string The user's logical name. May be an IPv4 or IPv6 address for anonymous users.
  63. */
  64. public function getName() {
  65. return $this->name;
  66. }
  67. /**
  68. * @return int The user's actor ID. May be 0 if no actor ID has been assigned.
  69. */
  70. public function getActorId() {
  71. return $this->actor;
  72. }
  73. /**
  74. * @since 1.32
  75. *
  76. * @param UserIdentity $user
  77. * @return bool
  78. */
  79. public function equals( UserIdentity $user ) {
  80. // XXX it's not clear whether central ID providers are supposed to obey this
  81. return $this->getName() === $user->getName();
  82. }
  83. /**
  84. * @since 1.34
  85. *
  86. * @return bool True if user is registered on this wiki, i.e., has a user ID. False if user is
  87. * anonymous or has no local account (which can happen when importing). This is equivalent to
  88. * getId() != 0 and is provided for code readability.
  89. */
  90. public function isRegistered() {
  91. return $this->getId() != 0;
  92. }
  93. }