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