public function testFindNthOccurence() { $this->assertNull(StringUtil::findNthOccurence('aaaa', 'a', 5)); $this->assertNull(StringUtil::findNthOccurence('ciao ciao ciao', 'ciao', 4)); $this->assertNull(StringUtil::findNthOccurence('ciao', 'b', 1)); $this->assertEquals(2, StringUtil::findNthOccurence('aaaa', 'a', 3)); $this->assertEquals(3, StringUtil::findNthOccurence('aaaa', 'a', 4)); $this->assertEquals(5, StringUtil::findNthOccurence('ciao ciao ciao', 'ciao', 2)); $this->assertEquals(10, StringUtil::findNthOccurence('/home/user/dir/dir', '/', 3)); }
public function __call($name, $args) { if (StringUtil::startsWith($name, 'set') && !StringUtil::endsWith($name, 'Id')) { $property = strtolower(preg_replace('/([a-z])([A-Z])/', '${1}_${2}', substr($name, 3))); if (property_exists($this, $property) && count($args) == 1) { $this->{$property} = $args[0]; return $this; } } throw new ModelException(sprintf('The method %s does not exist on the model %s, and cannot be magically created', $name, get_class($this))); }
public function testGetCallStack() { try { $this->instanceFunction(); $this->fail(); } catch (Exception $e) { $formatter = new ExceptionFormatter($e); } $calls = $formatter->getCallStack(); $this->assertTrue(StringUtil::startsWith($calls[0], 'function genericFuncion()')); $this->assertTrue(StringUtil::startsWith($calls[1], 'ExceptionFormatterTest::{closure}()')); $this->assertTrue(StringUtil::startsWith($calls[2], 'ExceptionFormatterTest::staticFunction()')); $this->assertTrue(StringUtil::startsWith($calls[3], 'ExceptionFormatterTest->instanceFunction()')); }
/** * Use the route defined by $routeName to encode $params and return a valid path * * @param string $routeName The name of the route to use * @param array $params The params to use in the path * @return string The path * @throws InvalidParamException If there is something wrong with the params * @throws MalformedPathException If the generated path is malformed * @throws MissingParamException If one or more required params are missing * @throws UnknownRouteException If there's no route for $routeName */ public function encode($routeName, array $params = array()) { if (!isset($this->routes[$routeName])) { throw new UnknownRouteException(sprintf('Unknown route %s', $routeName)); } /** @var $route IRoute */ $route = $this->routes[$routeName]; $path = $route->encode($params); // Making sure that the path starts with / if (!StringUtil::startsWith($path, '/')) { throw new MalformedPathException(sprintf('The generated path must start with /, instead it was: %s', $path)); } return $path; }
/** * Scan all the given folders (not recursive), and extract models * This function expects all the php files in the folders to be models, * if this is not true for your application generate the list of models * and call createFromModels() instead */ public function createFromModelFolders(array $folders) { $models = array(); foreach ($folders as $folder) { $files = scandir($folder); foreach ($files as $file) { $path = "{$folder}/{$file}"; if (is_file($path) && StringUtil::endsWith($file, ".php")) { require_once $path; $models = array_merge($models, self::extractClassFromFile($path)); } } } $this->createFromModels($models); }
/** * @see IRoute */ public function decode($path) { // Removing the first token, which is always empty $tokens = array_slice(explode('/', $path), 1); $controller = $this->controller; $action = $this->action; $params = array(); foreach (array_values($this->parsedRule['components']) as $i => $component) { if ($component['isVar']) { if (!isset($tokens[$i])) { return null; } if ($component['choices'] && !in_array($tokens[$i], $component['choices'])) { return null; } $paramName = $component['name']; $paramValue = $tokens[$i]; $params[$paramName] = urldecode($paramValue); $controller = str_replace(":{$paramName}", ucfirst($paramValue), $controller); $action = str_replace(":{$paramName}", $paramValue, $action); } else { if ($component['name'] !== $tokens[$i]) { return null; } } unset($tokens[$i]); } if (!empty($tokens)) { if ($this->parsedRule['varargs']) { $varargsStartsAt = StringUtil::findNthOccurence($path, '/', count($this->parsedRule['components']) + 1); $params = array_merge($params, VarargsHelper::deserialize(substr($path, $varargsStartsAt))); } else { return null; } } return new ControllerActionParams($controller, $action, $params); }