media-cloud-sync
/
includes
/
sdk
/
google
/
ramsey
/
uuid
/
src
/
Provider
/
Node
/
StaticNodeProvider.php
StaticNodeProvider.php in Media Cloud Sync 1.2.9, at includes/sdk/google/ramsey/uuid/src/Provider/Node/StaticNodeProvider.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the ramsey/uuid library |
| 5 | * |
| 6 | * For the full copyright and license information, please view the LICENSE |
| 7 | * file that was distributed with this source code. |
| 8 | * |
| 9 | * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | */ |
| 12 | declare (strict_types=1); |
| 13 | namespace Dudlewebs\WPMCS\Ramsey\Uuid\Provider\Node; |
| 14 | |
| 15 | use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\InvalidArgumentException; |
| 16 | use Dudlewebs\WPMCS\Ramsey\Uuid\Provider\NodeProviderInterface; |
| 17 | use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Hexadecimal; |
| 18 | use function dechex; |
| 19 | use function hexdec; |
| 20 | use function str_pad; |
| 21 | use function substr; |
| 22 | use const STR_PAD_LEFT; |
| 23 | /** |
| 24 | * StaticNodeProvider provides a static node value with the multicast bit set |
| 25 | * |
| 26 | * @link http://tools.ietf.org/html/rfc4122#section-4.5 RFC 4122, ยง 4.5: Node IDs that Do Not Identify the Host |
| 27 | */ |
| 28 | class StaticNodeProvider implements NodeProviderInterface |
| 29 | { |
| 30 | /** |
| 31 | * @var Hexadecimal |
| 32 | */ |
| 33 | private $node; |
| 34 | /** |
| 35 | * @param Hexadecimal $node The static node value to use |
| 36 | */ |
| 37 | public function __construct(Hexadecimal $node) |
| 38 | { |
| 39 | if (strlen($node->toString()) > 12) { |
| 40 | throw new InvalidArgumentException('Static node value cannot be greater than 12 hexadecimal characters'); |
| 41 | } |
| 42 | $this->node = $this->setMulticastBit($node); |
| 43 | } |
| 44 | public function getNode(): Hexadecimal |
| 45 | { |
| 46 | return $this->node; |
| 47 | } |
| 48 | /** |
| 49 | * Set the multicast bit for the static node value |
| 50 | */ |
| 51 | private function setMulticastBit(Hexadecimal $node): Hexadecimal |
| 52 | { |
| 53 | $nodeHex = str_pad($node->toString(), 12, '0', STR_PAD_LEFT); |
| 54 | $firstOctet = substr($nodeHex, 0, 2); |
| 55 | $firstOctet = str_pad(dechex(hexdec($firstOctet) | 0x1), 2, '0', STR_PAD_LEFT); |
| 56 | return new Hexadecimal($firstOctet . substr($nodeHex, 2)); |
| 57 | } |
| 58 | } |
| 59 |