12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <?php
- namespace Juju\App;
- require_once('arguments.class.php');
- class DyPath implements \JsonSerializable{
- public $path;
- public function __construct(string $path){
- $this->path = $path;
- }
- public function jsonSerialize(){
- return [
- 'path'=>$this->path,
- 'regex'=>$this->regex,
- 'arguments'=>$this->arguments
- ];
- }
- public function __toString(){
- return $this->path;
- }
- public function __get($name){
- switch($name){
- case 'arguments':
- preg_match_all('/\{([^#\/][^}\n]+?)\}/',$this->path,$m,PREG_SET_ORDER);
- return $m;
- break;
- case 'regex':
- return '/^'.preg_replace('/\\\{[^#\/][^}\n]+?\\\}/','([^\/]*)', preg_quote($this->path,'/')).'\/?$/';
- break;
- }
- }
- public function matches(string $url){
- return preg_match($this->regex, $url);
- }
- public function args(string $url){
- $ret = [];
- preg_match_all($this->regex,$url,$m,PREG_SET_ORDER);
- foreach($this->arguments as $k => $arg){
- $ret[$arg[1]] = $m[0][$k+1];
- }
- return new Arguments($ret);
- }
- }
- ?>
|