public function __construct() { // Initialize project namespace $projectName = \UObject::getClassName($this); \UString::doNotEndWith($projectName, 'Test'); $this->projectNamespace .= $projectName; }
public function is_redirection($url) { foreach (headers_list() as $header) { if (\UString::isStartWith($header, 'Location: ' . $url)) { return TRUE; } } return FALSE; }
protected function initialize() { parent::initialize(); if (\UString::isStartWith($this->title, '(')) { \UString::doSubstrAfter($this->title, '('); $this->preTitle = \UString::substrBefore($this->title, ')'); \UString::doSubstrAfter($this->title, ')'); } }
public function initialize() { parent::initialize(); $this->router = new \Stack\Router(); if (isset($_SERVER['REQUEST_URI'])) { $uri = \UString::substrBefore($_SERVER['REQUEST_URI'], '?'); $uri = rawurldecode($uri); \UString::doNotStartWith($uri, $this->baseUri); \UString::doStartWith($uri, '/'); $this->router->setUri($uri); } }
public function getBeginning($maximum = 300, $minimum = 200) { $seed = strip_tags($this->seed); $beginning = substr($seed, 0, $minimum); $margin = substr($seed, $minimum, $maximum); $part = trim(\UString::substrBeforeLast($margin, ['. ', '! ', '? '])); if (empty($part)) { $part = trim(\UString::substrBeforeLast($margin, [', ', '; ', ': '])); } if (strlen($seed) > strlen($beginning . $part)) { return $beginning . $part . '...'; } return $seed; }
public function execute($arguments = array()) { if (empty($this->request)) { throw new \Stack\Exception\Database('The SQL request is empty.'); } $result = []; try { // Prepare the request $this->connect(); $statement = $this->PDObject->prepare($this->request); foreach ($arguments as $parameter => $value) { $statement->bindValue($parameter, $value); } $result = $statement->execute(); // Execute the request if (!$result) { throw new \Stack\Exception\Database('Error with the SQL request : ' . $this->request, $statement->errorInfo()); } if (\UString::isStartWith($this->request, ['SELECT', 'SHOW', 'DESCRIBE', 'EXPLAIN'])) { $result = $statement->fetchAll(\PDO::FETCH_ASSOC); } else { if (\UString::isStartWith($this->request, "INSERT")) { $result = TRUE; $id = $this->PDObject->lastInsertId(); if ($id == '0') { // Error Case or a table without autoincrementation $id = FALSE; $result = FALSE; } $this->lastInsertId = $id; } } } catch (PDOException $exception) { throw new \Stack\Exception\Database($exception->getMessage()); } $this->disconnect(); return $result; }
public function bySetting($controller, $action, $setting) { if (!\UString::isStartWith($action, 'action')) { $action = 'action' . ucfirst($action); } $callable = [$controller, $action]; if (!is_callable($callable)) { $message = get_class($controller) . '::' . $action . ' is not callable'; throw new \Stack\Exception\NoCallable($message); } $uri = NULL; $exceptions = []; $aliases = []; if (is_array($setting)) { $uri = isset($setting['uri']) ? $setting['uri'] : $uri; $exceptions = isset($setting['exceptions']) ? $setting['exceptions'] : $exceptions; $aliases = isset($setting['aliases']) ? $setting['aliases'] : $aliases; } else { if (is_string($setting)) { $uri = $setting; } } return new $this($callable, $uri, $exceptions, $aliases); }
protected function getContentType($filePath) { $extension = \UString::substrAfterLast($filePath, '.'); if (in_array($extension, ['html', 'css'])) { $contentType = 'text/' . $extension; } else { if ($extension === 'js') { $contentType = 'text/javascript'; } else { if ($extension === 'ico') { $contentType = 'image/png'; } else { try { $finfo = finfo_open(FILEINFO_MIME_TYPE); $contentType = finfo_file($finfo, $filePath); finfo_close($finfo); } catch (\Exception $e) { $contentType = 'application/octet-stream'; } } } } return $contentType; }
public function setBaseUri($baseUri) { \UString::doStartWith($baseUri, '/'); \UString::doNotEndWith($baseUri, '/'); $this->baseUri = $baseUri; return $this; }
protected function doFormatExtensionNamespace(&$namespace) { $namespace = str_replace(DIRECTORY_SEPARATOR, '\\', $namespace); \UString::doNotStartWith($namespace, '\\'); \UString::doNotEndWith($namespace, '\\'); }
public static function getClassName($class) { \UObject::doConvertToClass($class); return \UString::substrAfterLast($class, '\\'); }
public function addInputFilter($inputName, $filterName, $filterCallback = NULL, $errorMessage = NULL) { if (is_null($errorMessage) && is_string($filterCallback) && \UString::has($filterCallback, ' ')) { $errorMessage = $filterCallback; $filterCallback = NULL; } $input = $this->getInput($inputName); $filterClass = $this->namespace . '\\FormInputFilter'; $filter = new $filterClass($filterName, $filterCallback); $input->filters[$filter->getName()] = $filter; if (!is_null($errorMessage)) { $filter->errorMessage = $errorMessage; } return $this; }
public function test_do_strip_special_char__replace() { $test = 'A page for $13'; \UString::doStripSpecialChar($test, 'a-zA-Z', ''); $this->assertEquals('Apagefor', $test); }
protected function getFileNames($fileName) { $fileNames = []; do { $fileNames[] = $fileName; $fileName = \UString::substrBeforeLast($fileName, '/'); } while (!empty($fileName)); return $fileNames; }
public function testLiveFildet_Validation_Nok() { $_POST['username'] = '******'; $form = $this->getLoginForm()->addInputFilter('username', 'with_a', function ($field) { return $field === '' || \UString::has($field, 'a'); }); $this->assertTrue($form->isActive(), 'Form must be active'); $this->assertFalse($form->isValid(), 'Form must be invalid'); $this->assertEquals($_POST['username'], $form->username, 'The given entry must be intact'); $this->assertEquals('with_a', $form->getInputError('username'), 'One error must be thrown, the username field must not validate the live filter'); }
protected function globDataFile($pattern, $asId = false) { $path = $this->folder; $folders = \Staq::App()->getExtensions(); array_walk($folders, function (&$a) use($path) { $a = realpath($a . $path); }); $folders = array_filter($folders, function ($a) { return !empty($a); }); $files = []; foreach ($folders as $folder) { $fullFolder = $folder . DIRECTORY_SEPARATOR . $pattern; foreach (glob($fullFolder . '\\.*') as $filename) { if ($asId) { $id = \UString::notStartWith($filename, $folder . '/'); \UString::doSubstrBeforeLast($id, '.'); $files[$id] = $id; } else { $files[] = $filename; } } } if ($asId) { array_values($files); } return array_reverse($files); }
public function diffUri($url) { if (is_object($url) && !empty($url->uri)) { $this->uri = \UString::substrAfter($this->uri, $url->uri); \UString::doStartWith($this->uri, '/'); } return $this; }
protected function rewriteJsonValues(&$values) { if (is_array($values)) { foreach ($values as $key => &$value) { if (!is_array($value) && \UString::isStartWith($value, array('[', '{'))) { $json = preg_replace(array('/([\\[\\]\\{\\}:,])\\s*(\\w)/', '/(\\w)\\s*([\\[\\]\\{\\}:,])/'), '\\1"\\2', $value); $array = json_decode($json, TRUE); if ($array !== FALSE) { $value = $array; } } $this->rewriteJsonValues($value); } } }
public static function isDeepSelector($selector) { return \UString::has($selector, '.'); }
protected function initialize() { /* Id */ if (substr_count($this->id, '/') == 2) { $this->section = \UString::substrBefore($this->id, '/'); $this->subsection = \UString::substrBeforeLast(\UString::substrAfter($this->id, '/'), '/'); $this->shortTitle = \UString::substrAfterLast($this->id, '/'); } /* Retreat content */ if (trim($this->content)) { $doc = new \DOMDocument(); @$doc->loadHTML('<?xml encoding="UTF-8">' . $this->content); /* H1 */ $h1List = $doc->getElementsByTagName('h1'); if ($h1List->length) { $this->title = $h1List->item(0)->textContent; } foreach ($h1List as $h1) { $h1->parentNode->removeChild($h1); } /* H2 */ $h2List = $doc->getElementsByTagName('h2'); foreach ($h2List as $h2) { $title = $h2->textContent; if ($h2->hasAttribute('id')) { $id = $h2->getAttribute('id'); } else { $id = \UString::stripSpecialChar($title); $h2->setAttribute('id', $id); } $this->summary[$id] = $title; $h2->nodeValue = ''; $link = $doc->createElement('a'); $link->nodeValue = htmlentities($title); $link->setAttribute('href', '#' . $id); $h2->appendChild($link); } /* Image */ $imageList = $doc->getElementsByTagName('img'); if ($imageList->length) { $image = $imageList->item(0); if ($image->hasAttribute('src')) { $this->image = $image->getAttribute('src'); } } /* Intro */ $ps = $doc->getElementsByTagName('p'); if ($ps->length) { $firstP = $ps->item(0); $this->intro = $firstP->textContent; $this->introHTML = $doc->saveHTML($firstP); $firstP->parentNode->removeChild($firstP); } /* Content */ $stripHTML = ['/^\\<\\!DOCTYPE.*?<html><body>/si', '!</body></html>$!si']; $this->content = preg_replace($stripHTML, '', $doc->saveHTML()); } if (!$this->label) { $this->label = $this->title; } }
protected function getClauseConditionList($request, &$parameters = false) { $whereList = []; if (is_array($request)) { foreach ($request as $fieldName => $fieldValue) { if (is_numeric($fieldName)) { if (is_string($fieldValue)) { $whereList[] = $fieldValue; } else { if (is_array($fieldValue) && isset($fieldValue[0]) && isset($fieldValue[1]) && isset($fieldValue[2])) { $fieldName = $fieldValue[0]; if (!\UString::has($fieldValue[0], '.')) { $fieldName = $this->table . '.' . $fieldName; } $whereList[] = $this->getClauseCondition($fieldName, $fieldValue[1], $fieldValue[2], $parameters); } } } else { if (!\UString::has($fieldName, '.')) { $fieldName = $this->table . '.' . $fieldName; } $whereList[] = $this->getClauseCondition($fieldName, '=', $fieldValue, $parameters); } } } else { $whereList[] = $request; } $whereList = array_filter($whereList, function ($a) { return !empty($a); }); return $whereList; }
public static function findTemplate($stack, $action = NULL) { $template = strtolower(\Staq\Util::getStackSubQuery($stack, '/')) . '.twig'; $template = str_replace('_', '/', $template); if (!empty($action)) { $template = $action . '/' . $template; } $folder = strtolower(\Staq\Util::getStackType($stack)); while (TRUE) { if (\Staq::App()->getFilePath('template/' . $folder . '/' . $template)) { break; } if (\UString::has($template, '/')) { $template = \UString::substrBeforeLast($template, '/') . '.twig'; } else { $template = 'index.twig'; break; } } return $folder . '/' . $template; }
public static function doStripSpecialChar(&$string, $chars = '-_a-zA-Z0-9', $replace = '-') { $string = \UString::stripSpecialChar($string, $chars, $replace); }
protected function groupArticles($articles, $index) { \UArray::doGroupBy($articles, $index); foreach ($articles as $key => $groupedArticles) { $articles[$key] = []; $articles[$key]['id'] = \UString::stripSpecialChar($key); $articles[$key]['name'] = $key; $articles[$key]['articles'] = $groupedArticles; } return $articles; }
public static function getPublicUrl($path, $absoluteURL = false) { \UString::doStartWith($path, '/'); $url = \Staq::App()->getBaseUri() . $path; if ($absoluteURL) { $url = \Staq\Util::getAbsoluteUrl($url); } return $url; }