PageRenderTime 35ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/jasonlewis/basset/src/Basset/Collection.php

https://bitbucket.org/larryg/powerhut
PHP | 174 lines | 69 code | 22 blank | 83 comment | 3 complexity | 5c658c217ef90615dbde6249717bba5f MD5 | raw file
  1. <?php namespace Basset;
  2. class Collection {
  3. /**
  4. * The collection identifier.
  5. *
  6. * @var string
  7. */
  8. protected $identifier;
  9. /**
  10. * The default directory of the collection.
  11. *
  12. * @var \Basset\Directory
  13. */
  14. protected $directory;
  15. /**
  16. * Create a new collection instance.
  17. *
  18. * @param string $identifier
  19. * @param \Basset\Directory $directory
  20. * @return void
  21. */
  22. public function __construct($identifier, Directory $directory)
  23. {
  24. $this->identifier = $identifier;
  25. $this->directory = $directory;
  26. }
  27. /**
  28. * Get all the assets filtered by a group and without the excluded assets.
  29. *
  30. * @param string $group
  31. * @return \Illuminate\Support\Collection
  32. */
  33. public function getAssetsWithoutExcluded($group = null)
  34. {
  35. return $this->getAssets($group, false);
  36. }
  37. /**
  38. * Get all the assets filtered by a group and with the excluded assets.
  39. *
  40. * @param string $group
  41. * @return \Illuminate\Support\Collection
  42. */
  43. public function getAssetsWithExcluded($group = null)
  44. {
  45. return $this->getAssets($group, true);
  46. }
  47. /**
  48. * Get all the assets filtered by a group but only if the assets are excluded.
  49. *
  50. * @param string $group
  51. * @return \Illuminate\Support\Collection
  52. */
  53. public function getAssetsOnlyExcluded($group = null)
  54. {
  55. // Get all the assets for the given group and filter out assets that aren't listed
  56. // as being excluded.
  57. $assets = $this->getAssets($group, true)->filter(function($asset)
  58. {
  59. return $asset->isExcluded();
  60. });
  61. return $assets;
  62. }
  63. /**
  64. * Get all the assets filtered by a group and if to include the excluded assets.
  65. *
  66. * @param string $group
  67. * @param bool $excluded
  68. * @return \Illuminate\Support\Collection
  69. */
  70. public function getAssets($group = null, $excluded = true)
  71. {
  72. // Spin through all of the assets that belong to the given group and push them on
  73. // to the end of the array.
  74. $assets = clone $this->directory->getAssets();
  75. foreach ($assets as $key => $asset)
  76. {
  77. if ( ! $excluded and $asset->isExcluded() or ! is_null($group) and ! $asset->{'is'.ucfirst(str_singular($group))}())
  78. {
  79. $assets->forget($key);
  80. }
  81. }
  82. // Spin through each of the assets and build an ordered array of assets. Once
  83. // we have the ordered array we'll transform it into a collection and apply
  84. // the collection wide filters to each asset.
  85. $ordered = array();
  86. foreach ($assets as $asset)
  87. {
  88. $this->orderAsset($asset, $ordered);
  89. }
  90. return new \Illuminate\Support\Collection($ordered);
  91. }
  92. /**
  93. * Orders the array of assets as they were defined or on a user ordered basis.
  94. *
  95. * @param \Basset\Asset $asset
  96. * @param array $assets
  97. * @return void
  98. */
  99. protected function orderAsset(Asset $asset, array &$assets)
  100. {
  101. $order = $asset->getOrder() and $order--;
  102. // If an asset already exists at the given order key then we'll add one to the order
  103. // so the asset essentially appears after the existing asset. This makes sense since
  104. // the array of assets has been reversed, so if the last asset was told to be first
  105. // then when we finally get to the first added asset it's added second.
  106. if (array_key_exists($order, $assets))
  107. {
  108. array_splice($assets, $order, 0, array(null));
  109. }
  110. $assets[$order] = $asset;
  111. ksort($assets);
  112. }
  113. /**
  114. * Get the identifier of the collection.
  115. *
  116. * @return string
  117. */
  118. public function getIdentifier()
  119. {
  120. return $this->identifier;
  121. }
  122. /**
  123. * Get the default directory.
  124. *
  125. * @return \Basset\Directory
  126. */
  127. public function getDefaultDirectory()
  128. {
  129. return $this->directory;
  130. }
  131. /**
  132. * Determine an extension based on the group.
  133. *
  134. * @param string $group
  135. * @return string
  136. */
  137. public function getExtension($group)
  138. {
  139. return str_plural($group) == 'stylesheets' ? 'css' : 'js';
  140. }
  141. /**
  142. * Dynamically call methods on the default directory.
  143. *
  144. * @param string $method
  145. * @param array $parameters
  146. * @return mixed
  147. */
  148. public function __call($method, $parameters)
  149. {
  150. return call_user_func_array(array($this->directory, $method), $parameters);
  151. }
  152. }