You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.8 KiB

3 years ago
  1. Dot Access Data
  2. ===============
  3. [![Latest Version](https://img.shields.io/packagist/v/dflydev/dot-access-data.svg?style=flat-square)](https://packagist.org/packages/dflydev/dot-access-data)
  4. [![Total Downloads](https://img.shields.io/packagist/dt/dflydev/dot-access-data.svg?style=flat-square)](https://packagist.org/packages/dflydev/dot-access-data)
  5. [![Software License](https://img.shields.io/badge/License-MIT-brightgreen.svg?style=flat-square)](LICENSE)
  6. [![Build Status](https://img.shields.io/github/workflow/status/dflydev/dflydev-dot-access-data/Tests/main.svg?style=flat-square)](https://github.com/dflydev/dflydev-dot-access-data/actions?query=workflow%3ATests+branch%3Amain)
  7. [![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/dflydev/dflydev-dot-access-data.svg?style=flat-square)](https://scrutinizer-ci.com/g/dflydev/dflydev-dot-access-data/code-structure/)
  8. [![Quality Score](https://img.shields.io/scrutinizer/g/dflydev/dflydev-dot-access-data.svg?style=flat-square)](https://scrutinizer-ci.com/g/dflydev/dflydev-dot-access-data)
  9. Given a deep data structure, access data by dot notation.
  10. Requirements
  11. ------------
  12. * PHP (7.1+)
  13. > For PHP (5.3+) please refer to version `1.0`.
  14. Usage
  15. -----
  16. Abstract example:
  17. ```php
  18. use Dflydev\DotAccessData\Data;
  19. $data = new Data;
  20. $data->set('a.b.c', 'C');
  21. $data->set('a.b.d', 'D1');
  22. $data->append('a.b.d', 'D2');
  23. $data->set('a.b.e', ['E0', 'E1', 'E2']);
  24. // C
  25. $data->get('a.b.c');
  26. // ['D1', 'D2']
  27. $data->get('a.b.d');
  28. // ['E0', 'E1', 'E2']
  29. $data->get('a.b.e');
  30. // true
  31. $data->has('a.b.c');
  32. // false
  33. $data->has('a.b.d.j');
  34. // 'some-default-value'
  35. $data->get('some.path.that.does.not.exist', 'some-default-value');
  36. // throws a MissingPathException because no default was given
  37. $data->get('some.path.that.does.not.exist');
  38. ```
  39. A more concrete example:
  40. ```php
  41. use Dflydev\DotAccessData\Data;
  42. $data = new Data([
  43. 'hosts' => [
  44. 'hewey' => [
  45. 'username' => 'hman',
  46. 'password' => 'HPASS',
  47. 'roles' => ['web'],
  48. ],
  49. 'dewey' => [
  50. 'username' => 'dman',
  51. 'password' => 'D---S',
  52. 'roles' => ['web', 'db'],
  53. 'nick' => 'dewey dman',
  54. ],
  55. 'lewey' => [
  56. 'username' => 'lman',
  57. 'password' => 'LP@$$',
  58. 'roles' => ['db'],
  59. ],
  60. ],
  61. ]);
  62. // hman
  63. $username = $data->get('hosts.hewey.username');
  64. // HPASS
  65. $password = $data->get('hosts.hewey.password');
  66. // ['web']
  67. $roles = $data->get('hosts.hewey.roles');
  68. // dewey dman
  69. $nick = $data->get('hosts.dewey.nick');
  70. // Unknown
  71. $nick = $data->get('hosts.lewey.nick', 'Unknown');
  72. // DataInterface instance
  73. $dewey = $data->getData('hosts.dewey');
  74. // dman
  75. $username = $dewey->get('username');
  76. // D---S
  77. $password = $dewey->get('password');
  78. // ['web', 'db']
  79. $roles = $dewey->get('roles');
  80. // No more lewey
  81. $data->remove('hosts.lewey');
  82. // Add DB to hewey's roles
  83. $data->append('hosts.hewey.roles', 'db');
  84. $data->set('hosts.april', [
  85. 'username' => 'aman',
  86. 'password' => '@---S',
  87. 'roles' => ['web'],
  88. ]);
  89. // Check if a key exists (true to this case)
  90. $hasKey = $data->has('hosts.dewey.username');
  91. ```
  92. `Data` may be used as an array, since it implements `ArrayAccess` interface:
  93. ```php
  94. // Get
  95. $data->get('name') === $data['name']; // true
  96. $data['name'] = 'Dewey';
  97. // is equivalent to
  98. $data->set($name, 'Dewey');
  99. isset($data['name']) === $data->has('name');
  100. // Remove key
  101. unset($data['name']);
  102. ```
  103. `/` can also be used as a path delimiter:
  104. ```php
  105. $data->set('a/b/c', 'd');
  106. echo $data->get('a/b/c'); // "d"
  107. $data->get('a/b/c') === $data->get('a.b.c'); // true
  108. ```
  109. License
  110. -------
  111. This library is licensed under the MIT License - see the LICENSE file
  112. for details.
  113. Community
  114. ---------
  115. If you have questions or want to help out, join us in the
  116. [#dflydev](irc://irc.freenode.net/#dflydev) channel on irc.freenode.net.