dypath.class.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Juju\App;
  3. require_once('arguments.class.php');
  4. class DyPath implements \JsonSerializable{
  5. public $path;
  6. public function __construct(string $path){
  7. $this->path = $path;
  8. }
  9. public function jsonSerialize(){
  10. return [
  11. 'path'=>$this->path,
  12. 'regex'=>$this->regex,
  13. 'arguments'=>$this->arguments
  14. ];
  15. }
  16. public function __toString(){
  17. return $this->path;
  18. }
  19. public function __get($name){
  20. switch($name){
  21. case 'arguments':
  22. preg_match_all('/\{([^#\/][^}\n]+?)\}/',$this->path,$m,PREG_SET_ORDER);
  23. return $m;
  24. break;
  25. case 'regex':
  26. return '/^'.preg_replace('/\\\{[^#\/][^}\n]+?\\\}/','([^\/]*)', preg_quote($this->path,'/')).'\/?$/';
  27. break;
  28. }
  29. }
  30. public function matches(string $url){
  31. return preg_match($this->regex, $url);
  32. }
  33. public function args(string $url){
  34. $ret = [];
  35. preg_match_all($this->regex,$url,$m,PREG_SET_ORDER);
  36. foreach($this->arguments as $k => $arg){
  37. $ret[$arg[1]] = $m[0][$k+1];
  38. }
  39. return new Arguments($ret);
  40. }
  41. }
  42. ?>