/** * Quote all values of an array according to their type * * @param array $array * @return array */ public function quote(array $array) { foreach ($array as &$value) { $value = is_array($value) ? self::quote($value) : StringUtils::quote($value); } return $array; }
/** * Encode an array into INI format * * <code> * // Default $options * $options = [ * 'default_section' => 'settings', * 'allow_fake_first' => true * ] * </code> * * @param mixed $resource * @param array $options */ public function fromArray(array $resource, array $options = []) { $options = array_merge(['default_section' => 'settings', 'allow_fake_first' => true], $options); $quoteArgs = ['enclosure' => '"', 'escape' => '\\']; // fake a section in case the array is not deep enough if (count($resource) && $options['allow_fake_first'] && !is_array(current($resource))) { $resource = [$options['default_section'] => $resource]; } $ini = ''; foreach ($resource as $l1Key => $l1Val) { if (is_array($l1Val)) { $ini .= "\n" . '[' . $l1Key . ']' . "\n"; foreach ($l1Val as $l2Key => $l2Val) { if (is_array($l2Val)) { foreach ($l2Val as $l3Key => $l3Val) { if (is_array($l3Val)) { throw new JigException('$resource must not have more than three levels'); } $ini .= $l2Key . '[' . $l3Key . '] = ' . StringUtils::quote($l3Val, $quoteArgs) . "\n"; } } else { $ini .= $l2Key . ' = ' . StringUtils::quote($l2Val, $quoteArgs) . "\n"; } } } else { $ini .= $l1Key . ' = ' . StringUtils::quote($l1Val, $quoteArgs) . "\n"; } } return trim($ini); }
public static function renderHtmlUser($userId) { // assume generis user $user = new core_kernel_classes_Resource($userId); $props = $user->getPropertiesValues(array(RDFS_LABEL, PROPERTY_USER_MAIL)); $label = isset($props[RDFS_LABEL]) && !empty($props[RDFS_LABEL]) ? (string) reset($props[RDFS_LABEL]) : '(' . $userId . ')'; $label = StringUtils::wrapLongWords($label); $mail = isset($props[PROPERTY_USER_MAIL]) && !empty($props[PROPERTY_USER_MAIL]) ? (string) reset($props[PROPERTY_USER_MAIL]) : ''; return !empty($mail) ? '<a href="mailto:' . $mail . '">' . $label . '</a>' : $label; }
/** * Serialize data and duration * * @param mixed $data * @param mixed $duration can be a number of seconds or any argument accepted by strtotime() */ protected static function serialize($data, $duration = 0) { switch (true) { case !$duration: $duration = strtotime('+1 week'); break; case !is_numeric($duration): $duration = strtotime($duration); break; case is_numeric($duration): $duration = intval($duration); break; default: throw new JigException('Invalid duration ' . $duration . ', caching not possible'); } $base64 = false; if (is_string($data) && StringUtils::isBinary($data)) { $data = base64_encode($data); $base64 = true; } $data = array('expires' => $duration, 'content' => $data, 'base64' => $base64); return json_encode($data); }
/** * Configure a hyperlink * * @param array $settings * @return string the linked text */ protected static function aElement($attributes, $settings) { if (empty($attributes['href'])) { $attributes['href'] = strip_tags($settings['content']); } if (empty($settings['content'])) { $settings['content'] = $attributes['href']; } $href_content_eq = $settings['content'] === $attributes['href']; // emails but not urls with password if (strpos($attributes['href'], '@') !== false && strpos($attributes['href'], '//') === false) { $address = str_replace('mailto:', '', $attributes['href']); $attributes['href'] = StringUtils::encodeText('mailto:' . $address); if ($href_content_eq) { $settings['content'] = StringUtils::encodeText($address); } } else { if ($href_content_eq && strpos($settings['content'], '//') !== false) { $settings['content'] = substr($settings['content'], strpos($settings['content'], '//') + 2); if (substr_count($settings['content'], '/') === 1) { $settings['content'] = rtrim($settings['content'], '/'); } } else { if (preg_match('~^(\\(|\\+){0,2}[\\d\\)\\- ]{6,}+$~', $settings['content'])) { $protocol = 'tel'; $attributes['href'] = str_replace(array('tel:', ' ', ')', '(', '-'), '', $attributes['href']); if (strpos($attributes['href'], '00') !== false) { $attributes['href'] = '+' . substr($attributes['href'], 2); } $attributes['href'] = $protocol . ':' . str_replace(array('tel:', ' ', ')', '('), '', $attributes['href']); } } } return self::build($attributes, $settings); }
/** * Generate all required data from _POST * * @return array * @throws \Exception */ protected function getMappedArguments() { $requiredArgs = array('client' => 'Prefix', 'tool-title' => 'Tool title', 'transparent' => '(1 or 0)', 'rotatable' => '(1 or 0)', 'movable' => '(1 or 0)', 'adjustable-x' => '(1 or 0)', 'adjustable-y' => '(1 or 0)'); $argHelp = "<p>Required arguments are:</p><ul>"; foreach ($requiredArgs as $key => $value) { $argHelp .= '<li>' . $key . ': ' . $value . '</li>'; } $argHelp .= '</ul>'; foreach ($requiredArgs as $key => $value) { // !string '0' is a valid entry! if (!isset($_POST[$key]) || $_POST[$key] === '') { throw new \Exception($argHelp); break; } // trim all, cast 0|1 to bool $_POST[$key] = trim($_POST[$key]); if (in_array($_POST[$key], array('0', '1'))) { $_POST[$key] = (bool) $_POST[$key]; } } $_POST['client'] = strtoupper($_POST['client']); $_POST['tool-base'] = StringUtils::removeSpecChars($_POST['tool-title']); $_POST['tool-fn'] = StringUtils::camelize($_POST['tool-base']); $_POST['tool-obj'] = ucfirst($_POST['tool-fn']); $_POST['tool-id'] = strtolower($_POST['client']) . $_POST['tool-obj']; $_POST['is-transparent'] = json_encode($_POST['transparent']); $_POST['is-rotatable-tl'] = json_encode($_POST['rotatable']); // default position of rotator // only visible when not adjustable $_POST['is-rotatable-tr'] = json_encode($_POST['rotatable'] && (!$_POST['adjustable-x'] && !$_POST['adjustable-y'])); $_POST['is-rotatable-br'] = json_encode($_POST['rotatable'] && (!$_POST['adjustable-x'] && !$_POST['adjustable-y'])); $_POST['is-rotatable-bl'] = json_encode($_POST['rotatable']); // also default position of rotator // alternative positions, need to be configured manually $_POST['is-rotatable-t'] = json_encode(false); $_POST['is-rotatable-r'] = json_encode(false); $_POST['is-rotatable-b'] = json_encode(false); $_POST['is-rotatable-l'] = json_encode(false); $_POST['is-movable'] = json_encode($_POST['movable']); $_POST['is-adjustable-x'] = json_encode($_POST['adjustable-x']); $_POST['is-adjustable-y'] = json_encode($_POST['adjustable-y']); $_POST['is-adjustable-xy'] = json_encode($_POST['adjustable-x'] && $_POST['adjustable-y']); unset($_POST['transparent']); unset($_POST['rotatable']); unset($_POST['movable']); unset($_POST['adjustable-x']); unset($_POST['adjustable-y']); return $_POST; }
/** * {@inheritdoc} * * Source format: CSV * * <code> * $options = [ * 'delimiter' => ',', // typically ,|;|\t * 'enclosure' => '"', // typically "|' * 'escape' => '\\', // how the above is to be escaped * 'pivot' => false // pivot input array * ]; * </code> * * @param mixed $resource * @param array $options */ public function fromArray($resource, array $options = []) { $options = array_merge(['delimiter' => ',', 'enclosure' => '"', 'escape' => '\\', 'pivot' => false], $options); $resource = parent::getRealResource($resource, $options); if ($options['pivot']) { $resource = ArrayUtils::pivot($resource); } $dataDepth = self::verifyDataDepthLimit($resource, 2); if ($dataDepth === 1) { $resource = [$resource]; } else { if ($dataDepth > 2) { $msg = '$resource must have one or two levels'; if ($options['pivot']) { $msg .= ' after it has been pivoted'; } throw new JigException($msg); } } $csv = ''; foreach ($resource as $valueArr) { $line = ''; foreach ($valueArr as $value) { $line .= StringUtils::quote($value, $options) . $options['delimiter']; } $csv .= rtrim($line, $options['delimiter']) . PHP_EOL; } return $csv; }
/** * Adds sample code for theme support */ protected function addSampleTheme() { // replacements $values = array('{themeLabel}' => $this->label . ' default theme', '{themeId}' => StringUtils::camelize($this->label . ' default theme'), '{platformTheme}' => StringUtils::camelize($this->label . ' default theme', true)); $pathValues = array(); foreach ($values as $key => $value) { $pathValues[trim($key, '{}')] = $value; } // copy templates $samplePath = \common_ext_ExtensionsManager::singleton()->getExtensionById('taoDevTools')->getDir() . 'models' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR; $paths = array(array('model', 'theme', '*.sample'), array('scripts', 'install', '*.sample'), array('views', 'templates', 'themes', 'platform', 'themeId', '*.sample'), array('views', 'scss', 'themes', 'items', '*.sample'), array('views', 'scss', 'themes', 'platform', 'themeId', '*.sample')); $templates = array(); foreach ($paths as $path) { $templates = array_merge($templates, glob($samplePath . implode(DIRECTORY_SEPARATOR, $path))); } foreach ($templates as $template) { $template = \tao_helpers_File::getRelPath($samplePath, $template); $template = substr($template, 0, strrpos($template, '.')); $this->copyFile($template, str_replace(array_keys($pathValues), $pathValues, $template), $values); } }
/** * Encode content to base64 if binary * * @param mixed $content * @return mixed|string */ protected static function encodeContent($content) { return static::$checkForBinary && is_string($content) && StringUtils::isBinary($content) ? base64_encode($content) : $content; }