1
0

dypath.class.php 1010 B

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