/** * Returns all results as an array * * @return array an Array of all results, each one being an array */ public function getResultsAsArray() { $new = array(); foreach ((array) $this->results as $result) { if ($result instanceof Object) { $new[] = $result->toArray(); } else { if (is_array($result)) { return $this->results; } else { throw new Exception('Cannot convert to array: ' . ClassUtils::getQualifiedType($result)); } } } return $new; }
public function __construct($tagOrElement, $slug = '', $role = '', $value = '') { if ($tagOrElement instanceof TagPartial || $tagOrElement instanceof Tag) { $this->fields = $tagOrElement->toArray(); return; } $this->fields = array_merge($this->fields, array('TagElement' => '', 'TagAspect' => '', 'TagSlug' => '', 'TagRole' => '', 'TagValue' => '')); if (is_array($tagOrElement)) { $this->fields = array_merge($this->fields, $tagOrElement); } else { if (is_string($tagOrElement)) { // assume first param is element if (!empty($slug) || !empty($role) || !empty($value)) { if (substr($tagOrElement, 0, 1) == '@') { $this->fields['TagAspect'] = substr($tagOrElement, 1); } else { $this->fields['TagElement'] = $tagOrElement; } $this->fields['TagSlug'] = $slug; $this->fields['TagRole'] = $role; $this->fields['TagValue'] = $value; // assume first param is tag string } else { $expressions = StringUtils::smartSplit($tagOrElement, ".", '"', '\\"', 2); if (preg_match("/^(((?P<ai>@)?(?P<el>[a-z0-9-]+))?(:(?P<s>[a-z0-9\\/\\-]+)?)?)?(#(?P<r>[a-z0-9-]+)?)?(=(?P<v>.+?))?\$/", array_shift($expressions), $m)) { if (!empty($m['ai'])) { $this->fields['TagAspect'] = !empty($m['el']) ? $m['el'] : ''; } else { $this->fields['TagElement'] = !empty($m['el']) ? $m['el'] : ''; } $this->fields['TagSlug'] = !empty($m['s']) ? $m['s'] : ''; $this->fields['TagRole'] = !empty($m['r']) ? $m['r'] : ''; $this->fields['TagValue'] = !empty($m['v']) ? $m['v'] : ''; if (!empty($expressions)) { // if(count($expressions) > 1) $this->fields['ChildPartials'] = current($expressions); // else // $this->fields['ChildPartial'] = $expressions[0]; } } } } else { throw new Exception('Invalid parameter to TagPartial: ' . ClassUtils::getQualifiedType($tagOrElement)); } } if (empty($this->fields['TagElement']) && empty($this->fields['TagAspect']) && empty($this->fields['TagRole'])) { throw new TagException('Invalid partial: No element, aspect, or role was specified [' . print_r($tagOrElement, true) . ']'); } if (!empty($this->fields['TagAspect'])) { // $this->fields['TagAspect'] = strtolower($this->fields['TagAspect']); if (!preg_match("/[a-z0-9-]+/", $this->fields['TagAspect'])) { throw new TagException('Invalid partial: Aspect "' . $this->fields['TagAspect'] . '" must contain only characters or dash'); } } if (!empty($this->fields['TagSlug'])) { $this->fields['TagSlug'] = strtolower($this->fields['TagSlug']); if (!SlugUtils::isSlug($this->fields['TagSlug'], true)) { throw new TagException('Invalid tag: "' . $this->fields['TagSlug'] . '" must be valid slug'); } } if (!empty($this->fields['TagRole']) && !SlugUtils::isSlug($this->fields['TagRole'])) { $this->fields['TagRole'] = SlugUtils::createSlug($this->fields['TagRole']); } if (!empty($this->fields['TagValue']) && !SlugUtils::isSlug($this->fields['TagValue'])) { $this->fields['TagValue'] = SlugUtils::createSlug($this->fields['TagValue']); } // lowercase all parts foreach (array('TagElement', 'TagAspect', 'TagSlug', 'TagRole', 'TagValue') as $name) { $this->fields[$name] = strtolower($this->fields[$name]); } }
/** * Returns the associated value from the array based on the type of value given * * @param array $array_map An array of the type [1 => string, int => string] * @param string/int $value An integer or string to find the associated value for * * @return string or int If input was a string, the associated integer, * If was integer, the associated string */ public static function arrayKeyAssoc($array_map, $value) { if (is_string($value)) { if (($val = array_search($value, $array_map)) === false) { throw new Exception('Value string undefined: ' . $value); } return $val; } else { if (is_int($value)) { if (!array_key_exists('' . $value, $array_map)) { throw new Exception('Key value undefined: ' . $value); } return $array_map['' . $value]; } } throw new Exception('Invalid type for arrayKeyAssoc: ' . ClassUtils::getQualifiedType($value)); }