PageRenderTime 79ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/sally/core/lib/sly/Slice/Values.php

https://bitbucket.org/SallyCMS/0.6
PHP | 228 lines | 86 code | 29 blank | 113 comment | 9 complexity | afe32f9c58d3d7555900ee92587404c9 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
  4. *
  5. * This file is released under the terms of the MIT license. You can find the
  6. * complete text in the attached LICENSE file or online at:
  7. *
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. /**
  11. * @since 0.6
  12. * @author zozi@webvariants.de
  13. * @author christoph@webvariants.de
  14. */
  15. class sly_Slice_Values {
  16. private $data; ///< array
  17. /**
  18. * Constructor
  19. *
  20. * @param array $data the slice data
  21. */
  22. public function __construct(array $data) {
  23. $this->data = $data;
  24. }
  25. /**
  26. * Get a single slice value
  27. *
  28. * @param string $id the value's id (form element's name)
  29. * @param string $default value to use if the $id was not found
  30. * @return mixed the value or the default
  31. */
  32. public function get($id, $default = null) {
  33. if (!array_key_exists($id, $this->data)) return $default;
  34. return $this->data[$id];
  35. }
  36. /**
  37. * Get a list of values
  38. *
  39. * @param string $id
  40. * @param string $type variable type like 'int' or 'string'
  41. * @param boolean $unique whether or not to remove duplicates
  42. * @param boolean $separators string of separator characters
  43. * @return array [<value>, <value>, ...]
  44. */
  45. public function getMany($id, $type, $unique = false, $separators = ',') {
  46. $value = $this->get($id, '');
  47. if (mb_strlen($value) === 0) return array();
  48. $elements = preg_split('/['.preg_quote($separators, '/').']/', $value, null, PREG_SPLIT_NO_EMPTY);
  49. foreach ($elements as $idx => $element) {
  50. $elements[$idx] = sly_settype($element, $type);
  51. }
  52. if ($unique) {
  53. $elements = array_values(array_unique($elements));
  54. }
  55. return $elements;
  56. }
  57. /**
  58. * Get a list of articles
  59. *
  60. * @param string $id
  61. * @param boolean $unique whether or not to remove duplicates
  62. * @param boolean $separators string of separator characters
  63. * @return array [sly_Model_Article, sly_Model_Article, ...]
  64. */
  65. public function getArticles($id, $unique = false, $separators = ',') {
  66. $ids = $this->getMany($id, 'int', $unique, $separators);
  67. $res = array();
  68. foreach ($ids as $id) {
  69. $article = sly_Util_Article::findById($id);
  70. if ($article) $res[] = $article;
  71. }
  72. return $res;
  73. }
  74. /**
  75. * Get a list of categories
  76. *
  77. * @param string $id
  78. * @param boolean $unique whether or not to remove duplicates
  79. * @param boolean $separators string of separator characters
  80. * @return array [sly_Model_Category, sly_Model_Category, ...]
  81. */
  82. public function getCategories($id, $unique = false, $separators = ',') {
  83. $ids = $this->getMany($id, 'int', $unique, $separators);
  84. $res = array();
  85. foreach ($ids as $id) {
  86. $category = sly_Util_Category::findById($id);
  87. if ($category) $res[] = $category;
  88. }
  89. return $res;
  90. }
  91. /**
  92. * Get a list of filenames
  93. *
  94. * @param string $id
  95. * @param boolean $skipMissing
  96. * @param boolean $unique whether or not to remove duplicates
  97. * @param boolean $separators string of separator characters
  98. * @return array [test.jpg, dummy.png, ...]
  99. */
  100. public function getFilenames($id, $skipMissing = true, $unique = false, $separators = ',') {
  101. $filenames = $this->getMany($id, 'string', $unique, $separators);
  102. $res = array();
  103. if (!$skipMissing) {
  104. return $filenames;
  105. }
  106. foreach ($filenames as $name) {
  107. if (file_exists(SLY_MEDIAFOLDER.'/'.$name)) {
  108. $res[] = $name;
  109. }
  110. }
  111. return $res;
  112. }
  113. /**
  114. * Get a list of sly_Model_Medium instances
  115. *
  116. * @param string $id
  117. * @param boolean $skipMissing
  118. * @param boolean $unique whether or not to remove duplicates
  119. * @param boolean $separators string of separator characters
  120. * @return array [sly_Model_Medium, sly_Model_Medium, ...]
  121. */
  122. public function getMedia($id, $skipMissing = true, $unique = false, $separators = ',') {
  123. $filenames = $this->getFilenames($id, $skipMissing, $unique, $separators);
  124. $res = array();
  125. foreach ($filenames as $name) {
  126. $medium = sly_Util_Medium::findByFilename($name);
  127. if ($medium) $res[] = $medium;
  128. }
  129. return $res;
  130. }
  131. /**
  132. * Get an article instance
  133. *
  134. * @param string $id
  135. * @param mixed $default
  136. * @return sly_Model_Article
  137. */
  138. public function getArticle($id, $default = null) {
  139. return sly_Util_Article::findById($this->get($id, null), $default);
  140. }
  141. /**
  142. * Get a category instance
  143. *
  144. * @param string $id
  145. * @param mixed $default
  146. * @return sly_Model_Category
  147. */
  148. public function getCategory($id, $default = null) {
  149. return sly_Util_Category::findById($this->get($id, null), $default);
  150. }
  151. /**
  152. * Get a medium instance
  153. *
  154. * @param string $id
  155. * @param mixed $default
  156. * @return sly_Model_Medium
  157. */
  158. public function getMedium($id, $default = null) {
  159. return sly_Util_Medium::findByFilename($this->get($id, null), $default);
  160. }
  161. /**
  162. * Get a URL to an article
  163. *
  164. * @param string $id
  165. * @param array $params
  166. * @param string $divider
  167. * @param boolean $absolute
  168. * @param boolean $secure
  169. * @return string
  170. */
  171. public function getUrl($id, $params = array(), $divider = '&amp;', $absolute = false, $secure = null) {
  172. return sly_Util_Article::getUrl($this->get($id, sly_Util_Article::CURRENT_ARTICLE), null, $params, $divider, $absolute, $secure);
  173. }
  174. /**
  175. * Get a URL to an article
  176. *
  177. * @param string $id
  178. * @param array $attributes
  179. * @param boolean $forceUri
  180. * @return string
  181. */
  182. public function getImageTag($id, array $attributes = array(), $forceUri = false) {
  183. return sly_Util_HTML::getImageTag($this->get($id, ''), $attributes, $forceUri);
  184. }
  185. /**
  186. * @throws sly_Exception
  187. * @param string $method
  188. * @param array $arguments
  189. * @return mixed
  190. */
  191. public function __call($method, $arguments) {
  192. $event = strtoupper('SLY_SLICEVALUES_'.$method);
  193. $dispatcher = sly_Core::dispatcher();
  194. if (!$dispatcher->hasListeners($event)) {
  195. throw new sly_Exception('Call to undefined method '.get_class($this).'::'.$method.'()');
  196. }
  197. return $dispatcher->filter($event, null, array('method' => $method, 'arguments' => $arguments, 'object' => $this));
  198. }
  199. }