quickstart.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. .. _quickstart:
  2. ===============
  3. Getting Started
  4. ===============
  5. Requirements
  6. ############
  7. ramsey/uuid |version| requires the following:
  8. * PHP 8.0+
  9. * `ext-json <https://www.php.net/manual/en/book.json.php>`_
  10. The JSON extension is normally enabled by default, but it is possible to disable
  11. it. Other required extensions include
  12. `PCRE <https://www.php.net/manual/en/book.pcre.php>`_
  13. and `SPL <https://www.php.net/manual/en/book.spl.php>`_. These standard
  14. extensions cannot be disabled without patching PHP's build system and/or C
  15. sources.
  16. ramsey/uuid recommends installing/enabling the following extensions. While not
  17. required, these extensions improve the performance of ramsey/uuid.
  18. * `ext-gmp <https://www.php.net/manual/en/book.gmp.php>`_
  19. * `ext-bcmath <https://www.php.net/manual/en/book.bc.php>`_
  20. Install With Composer
  21. #####################
  22. The only supported installation method for ramsey/uuid is
  23. `Composer <https://getcomposer.org>`_. Use the following command to add
  24. ramsey/uuid to your project dependencies:
  25. .. code-block:: bash
  26. composer require ramsey/uuid
  27. Using ramsey/uuid
  28. #################
  29. After installing ramsey/uuid, the quickest way to get up-and-running is to use
  30. the static generation methods.
  31. .. code-block:: php
  32. use Ramsey\Uuid\Uuid;
  33. $uuid = Uuid::uuid4();
  34. printf(
  35. "UUID: %s\nVersion: %d\n",
  36. $uuid->toString(),
  37. $uuid->getFields()->getVersion()
  38. );
  39. This will return an instance of :php:class:`Ramsey\\Uuid\\Rfc4122\\UuidV4`.
  40. .. tip::
  41. .. rubric:: Use the Interfaces
  42. Feel free to use ``instanceof`` to check the specific instance types of
  43. UUIDs. However, when using type hints, it's best to use the interfaces.
  44. The most lenient interface is :php:interface:`Ramsey\\Uuid\\UuidInterface`,
  45. while :php:interface:`Ramsey\\Uuid\\Rfc4122\\UuidInterface` ensures the
  46. UUIDs you're using conform to the `RFC 4122`_ standard. If you're not sure
  47. which one to use, start with the stricter
  48. :php:interface:`Rfc4122\\UuidInterface <Ramsey\\Uuid\\Rfc4122\\UuidInterface>`.
  49. ramsey/uuid provides a number of helpful static methods that help you work with
  50. and generate most types of UUIDs, without any special customization of the
  51. library.
  52. .. list-table::
  53. :widths: 25 75
  54. :align: center
  55. :header-rows: 1
  56. * - Method
  57. - Description
  58. * - :php:meth:`Uuid::uuid1() <Ramsey\\Uuid\\Uuid::uuid1>`
  59. - This generates a :ref:`rfc4122.version1` UUID.
  60. * - :php:meth:`Uuid::uuid2() <Ramsey\\Uuid\\Uuid::uuid2>`
  61. - This generates a :ref:`rfc4122.version2` UUID.
  62. * - :php:meth:`Uuid::uuid3() <Ramsey\\Uuid\\Uuid::uuid3>`
  63. - This generates a :ref:`rfc4122.version3` UUID.
  64. * - :php:meth:`Uuid::uuid4() <Ramsey\\Uuid\\Uuid::uuid4>`
  65. - This generates a :ref:`rfc4122.version4` UUID.
  66. * - :php:meth:`Uuid::uuid5() <Ramsey\\Uuid\\Uuid::uuid5>`
  67. - This generates a :ref:`rfc4122.version5` UUID.
  68. * - :php:meth:`Uuid::uuid6() <Ramsey\\Uuid\\Uuid::uuid6>`
  69. - This generates a :ref:`rfc4122.version6` UUID.
  70. * - :php:meth:`Uuid::uuid7() <Ramsey\\Uuid\\Uuid::uuid7>`
  71. - This generates a :ref:`rfc4122.version7` UUID.
  72. * - :php:meth:`Uuid::isValid() <Ramsey\\Uuid\\Uuid::isValid>`
  73. - Checks whether a string is a valid UUID.
  74. * - :php:meth:`Uuid::fromString() <Ramsey\\Uuid\\Uuid::fromString>`
  75. - Creates a UUID instance from a string UUID.
  76. * - :php:meth:`Uuid::fromBytes() <Ramsey\\Uuid\\Uuid::fromBytes>`
  77. - Creates a UUID instance from a 16-byte string.
  78. * - :php:meth:`Uuid::fromInteger() <Ramsey\\Uuid\\Uuid::fromInteger>`
  79. - Creates a UUID instance from a string integer.
  80. * - :php:meth:`Uuid::fromDateTime() <Ramsey\\Uuid\\Uuid::fromDateTime>`
  81. - Creates a version 1 UUID instance from a PHP `DateTimeInterface`_.
  82. .. _RFC 4122: https://tools.ietf.org/html/rfc4122
  83. .. _DateTimeInterface: https://www.php.net/datetimeinterface