/Webdav/src/namespace_registry.php

https://github.com/F5/zetacomponents · PHP · 190 lines · 57 code · 9 blank · 124 comment · 3 complexity · 30d80d23ee4848dcb8323054bac3acc0 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcWebdavNamespaceRegistry class.
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package Webdav
  23. * @version //autogentag//
  24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25. */
  26. /**
  27. * Class to map XML namespaces to their shortcuts.
  28. *
  29. * An instance of this class is used in {@link ezcWebdavXmlTool} to keep track
  30. * of used namespace shortcuts and the creation of new ones, if necessary.
  31. *
  32. * @package Webdav
  33. * @version //autogen//
  34. */
  35. class ezcWebdavNamespaceRegistry implements ArrayAccess
  36. {
  37. /**
  38. * Counter to create new shortcuts.
  39. *
  40. * @var int
  41. */
  42. protected $shortcutCounter = 0;
  43. /**
  44. * Base string to be used for new shortcuts.
  45. *
  46. * @var string
  47. */
  48. protected $shortcutBase = 'ezc';
  49. /**
  50. * Maps namespace URIs to shortcuts.
  51. *
  52. * <code>
  53. * array(
  54. * 'uri' => '<shortcut>',
  55. * // ...
  56. * )
  57. * </code>
  58. *
  59. * @var array(string=>string)
  60. */
  61. protected $namespaceMap = array();
  62. /**
  63. * Stores shortcuts that are already in use.
  64. *
  65. * <code>
  66. * array(
  67. * '<shortcut>' => true,
  68. * // ...
  69. * )
  70. * </code>
  71. *
  72. * @var array(string=>bool)
  73. */
  74. protected $usedShortcuts = array();
  75. /**
  76. * Create a new namespace registry.
  77. *
  78. * Registers the standard namespace 'DAV:' with the shortcut 'D', which is
  79. * common in the RFC document.
  80. *
  81. * @return void
  82. */
  83. public function __construct()
  84. {
  85. $this['DAV:'] = 'D';
  86. $this[ezcWebdavLockPlugin::XML_NAMESPACE] = 'ezclock';
  87. }
  88. /**
  89. * ArrayAccess set access.
  90. *
  91. * Required by the ArrayAccess interface.
  92. *
  93. * @param string $offset
  94. * @param string $value
  95. * @return void
  96. * @ignore
  97. *
  98. * @throws ezcBaseValueException
  99. * if the given namespace is already registered.
  100. */
  101. public function offsetSet( $offset, $value )
  102. {
  103. if ( isset( $this->namespaceMap[$offset] ) )
  104. {
  105. throw new ezcBaseValueException(
  106. 'offset',
  107. $offset,
  108. 'non-existent'
  109. );
  110. }
  111. $this->namespaceMap[$offset] = $value;
  112. $this->usedShortcuts[$value] = true;
  113. }
  114. /**
  115. * ArrayAccess get access.
  116. *
  117. * Required by the ArrayAccess interface.
  118. *
  119. * @param string $offset
  120. * @return string
  121. * @ignore
  122. */
  123. public function offsetGet( $offset )
  124. {
  125. if ( !isset( $this->namespaceMap[$offset] ) )
  126. {
  127. $this[$offset] = $this->newShortcut();
  128. }
  129. return $this->namespaceMap[$offset];
  130. }
  131. /**
  132. * Array unset() access.
  133. *
  134. * Required by the ArrayAccess interface.
  135. *
  136. * @param string $offset
  137. * @return void
  138. * @ignore
  139. */
  140. public function offsetUnset( $offset )
  141. {
  142. if ( isset( $this->namespaceMap[$offset] ) )
  143. {
  144. unset( $this->usedShortcuts[$this->namespaceMap[$offset]] );
  145. unset( $this->namespaceMap[$offset] );
  146. }
  147. }
  148. /**
  149. * Array isset() access.
  150. *
  151. * Required by the ArrayAccess interface.
  152. *
  153. * @param string $offset
  154. * @return bool
  155. * @ignore
  156. */
  157. public function offsetExists( $offset )
  158. {
  159. return isset( $this->namespaceMap[$offset] );
  160. }
  161. /**
  162. * Creates a new namespace shortcut.
  163. *
  164. * Produces a new shortcut for a namespace by using {@link
  165. * $this->shortcutBase} and the first 5 characters of the MD5 hash of the
  166. * current microtime. Only returns unused shortcuts.
  167. *
  168. * @return string
  169. */
  170. protected function newShortcut()
  171. {
  172. do
  173. {
  174. $shortcut = sprintf( "%s%'05s", $this->shortcutBase, $this->shortcutCounter++ );
  175. }
  176. while ( isset( $this->usedShortcuts[$shortcut] ) );
  177. return $shortcut;
  178. }
  179. }
  180. ?>