PageRenderTime 313ms CodeModel.GetById 140ms app.highlight 8ms RepoModel.GetById 93ms app.codeStats 0ms

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

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