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