Source for file Set.php

Documentation is available at Set.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Collections
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../Collections/Collection.php';
  24. 24:  
  25. 25: require_once dirname(__FILE__'/../Collections/ISet.php';
  26. 26:  
  27. 27:  
  28. 28:  
  29. 29: /**
  30. 30:  * Provides the base class for a collection that contains no duplicate elements.
  31. 31:  *
  32. 32:  * @author     David Grudl
  33. 33:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  34. 34:  * @package    Nette\Collections
  35. 35:  */
  36. 36: class Set extends Collection implements ISet
  37. 37: {
  38. 38:  
  39. 39:  
  40. 40:     /**
  41. 41:      * Appends the specified element to the end of this collection.
  42. 42:      * @param  mixed 
  43. 43:      * @return bool  true if this collection changed as a result of the call
  44. 44:      * @throws InvalidArgumentException, \NotSupportedException
  45. 45:      */
  46. 46:     public function append($item)
  47. 47:     {
  48. 48:         $this->beforeAdd($item);
  49. 49:  
  50. 50:         if (is_object($item)) {
  51. 51:             $key spl_object_hash($item);
  52. 52:             if (parent::offsetExists($key)) {
  53. 53:                 return FALSE;
  54. 54:             }
  55. 55:             parent::offsetSet($key$item);
  56. 56:             return TRUE;
  57. 57:  
  58. 58:         else {
  59. 59:             $key $this->search($item);
  60. 60:             if ($key === FALSE{
  61. 61:                 parent::offsetSet(NULL$item);
  62. 62:                 return TRUE;
  63. 63:             }
  64. 64:             return FALSE;
  65. 65:         }
  66. 66:     }
  67. 67:  
  68. 68:  
  69. 69:  
  70. 70:     /**
  71. 71:      * Returns the index of the first occurrence of the specified element,
  72. 72:      * or FALSE if this collection does not contain this element.
  73. 73:      * @param  mixed 
  74. 74:      * @return int|FALSE
  75. 75:      * @throws InvalidArgumentException
  76. 76:      */
  77. 77:     protected function search($item)
  78. 78:     {
  79. 79:         if (is_object($item)) {
  80. 80:             $key spl_object_hash($item);
  81. 81:             return parent::offsetExists($key$key FALSE;
  82. 82:  
  83. 83:         else {
  84. 84:             return array_search($item$this->getArrayCopy()TRUE);
  85. 85:         }
  86. 86:     }
  87. 87:  
  88. 88:  
  89. 89:  
  90. 90:     /********************* ArrayObject cooperation ****************d*g**/
  91. 91:  
  92. 92:  
  93. 93:  
  94. 94:     /**
  95. 95:      * Not supported (only appending).
  96. 96:      */
  97. 97:     public function offsetSet($key$item)
  98. 98:     {
  99. 99:         if ($key === NULL{
  100. 100:             $this->append($item);
  101. 101:         else {
  102. 102:             throw new NotSupportedException;
  103. 103:         }
  104. 104:     }
  105. 105:  
  106. 106:  
  107. 107:  
  108. 108:     /**
  109. 109:      * Not supported.
  110. 110:      */
  111. 111:     public function offsetGet($key)
  112. 112:     {
  113. 113:         throw new NotSupportedException;
  114. 114:     }
  115. 115:  
  116. 116:  
  117. 117:  
  118. 118:     /**
  119. 119:      * Not supported.
  120. 120:      */
  121. 121:     public function offsetExists($key)
  122. 122:     {
  123. 123:         throw new NotSupportedException;
  124. 124:     }
  125. 125:  
  126. 126:  
  127. 127:  
  128. 128:     /**
  129. 129:      * Not supported.
  130. 130:      */
  131. 131:     public function offsetUnset($key)
  132. 132:     {
  133. 133:         throw new NotSupportedException;
  134. 134:     }
  135. 135: