Factory.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
  4. *
  5. * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
  6. */
  7. namespace Lcobucci\JWT\Claim;
  8. use Lcobucci\JWT\Claim;
  9. /**
  10. * Class that create claims
  11. *
  12. * @deprecated This class will be removed on v4
  13. *
  14. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  15. * @since 2.0.0
  16. */
  17. class Factory
  18. {
  19. /**
  20. * The list of claim callbacks
  21. *
  22. * @var array
  23. */
  24. private $callbacks;
  25. /**
  26. * Initializes the factory, registering the default callbacks
  27. *
  28. * @param array $callbacks
  29. */
  30. public function __construct(array $callbacks = [])
  31. {
  32. $this->callbacks = array_merge(
  33. [
  34. 'iat' => [$this, 'createLesserOrEqualsTo'],
  35. 'nbf' => [$this, 'createLesserOrEqualsTo'],
  36. 'exp' => [$this, 'createGreaterOrEqualsTo'],
  37. 'iss' => [$this, 'createEqualsTo'],
  38. 'aud' => [$this, 'createEqualsTo'],
  39. 'sub' => [$this, 'createEqualsTo'],
  40. 'jti' => [$this, 'createEqualsTo']
  41. ],
  42. $callbacks
  43. );
  44. }
  45. /**
  46. * Create a new claim
  47. *
  48. * @param string $name
  49. * @param mixed $value
  50. *
  51. * @return Claim
  52. */
  53. public function create($name, $value)
  54. {
  55. if (!empty($this->callbacks[$name])) {
  56. return call_user_func($this->callbacks[$name], $name, $value);
  57. }
  58. return $this->createBasic($name, $value);
  59. }
  60. /**
  61. * Creates a claim that can be compared (greator or equals)
  62. *
  63. * @param string $name
  64. * @param mixed $value
  65. *
  66. * @return GreaterOrEqualsTo
  67. */
  68. private function createGreaterOrEqualsTo($name, $value)
  69. {
  70. return new GreaterOrEqualsTo($name, $value);
  71. }
  72. /**
  73. * Creates a claim that can be compared (greator or equals)
  74. *
  75. * @param string $name
  76. * @param mixed $value
  77. *
  78. * @return LesserOrEqualsTo
  79. */
  80. private function createLesserOrEqualsTo($name, $value)
  81. {
  82. return new LesserOrEqualsTo($name, $value);
  83. }
  84. /**
  85. * Creates a claim that can be compared (equals)
  86. *
  87. * @param string $name
  88. * @param mixed $value
  89. *
  90. * @return EqualsTo
  91. */
  92. private function createEqualsTo($name, $value)
  93. {
  94. return new EqualsTo($name, $value);
  95. }
  96. /**
  97. * Creates a basic claim
  98. *
  99. * @param string $name
  100. * @param mixed $value
  101. *
  102. * @return Basic
  103. */
  104. private function createBasic($name, $value)
  105. {
  106. return new Basic($name, $value);
  107. }
  108. }