remotelyliving /
doorkeeper
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace RemotelyLiving\Doorkeeper\Identification; |
||
| 6 | |||
| 7 | abstract class AbstractIdentification implements IdentificationInterface |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var string|int |
||
| 11 | */ |
||
| 12 | private $identifier; |
||
| 13 | |||
| 14 | private string $identityHash; |
||
| 15 | |||
| 16 | private string $type; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param string|int $identifier |
||
| 20 | */ |
||
| 21 | final public function __construct($identifier) |
||
| 22 | { |
||
| 23 | if (!is_scalar($identifier) || is_bool($identifier)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 24 | throw new \InvalidArgumentException('Identifier must be a scalar, alpha numeric value'); |
||
| 25 | } |
||
| 26 | |||
| 27 | $this->validate($identifier); |
||
| 28 | |||
| 29 | $this->identifier = $identifier; |
||
| 30 | $this->type = get_class($this); |
||
| 31 | $this->identityHash = self::createIdentityHash($this); |
||
| 32 | } |
||
| 33 | |||
| 34 | final public function getIdentifier() |
||
| 35 | { |
||
| 36 | return $this->identifier; |
||
| 37 | } |
||
| 38 | |||
| 39 | final public function getType(): string |
||
| 40 | { |
||
| 41 | return $this->type; |
||
| 42 | } |
||
| 43 | |||
| 44 | final public function equals(IdentificationInterface $identity): bool |
||
| 45 | { |
||
| 46 | return $this->identityHash === self::createIdentityHash($identity); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string|int|float $value |
||
| 51 | */ |
||
| 52 | abstract protected function validate($value): void; |
||
| 53 | |||
| 54 | private static function createIdentityHash(IdentificationInterface $identification): string |
||
| 55 | { |
||
| 56 | return md5($identification->getType() . (string)$identification->getIdentifier()); |
||
| 57 | } |
||
| 58 | } |
||
| 59 |