76 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			76 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php namespace SokoWeb\Routing;
 | 
						|
 | 
						|
use SokoWeb\Interfaces\Routing\IRoute;
 | 
						|
 | 
						|
class Route implements IRoute
 | 
						|
{
 | 
						|
    private string $id;
 | 
						|
 | 
						|
    private array $pattern;
 | 
						|
 | 
						|
    private array $handler;
 | 
						|
 | 
						|
    public function __construct(string $id, array $pattern, array $handler)
 | 
						|
    {
 | 
						|
        $this->id = $id;
 | 
						|
        $this->pattern = $pattern;
 | 
						|
        $this->handler = $handler;
 | 
						|
    }
 | 
						|
 | 
						|
    public function getId(): string
 | 
						|
    {
 | 
						|
        return $this->id;
 | 
						|
    }
 | 
						|
 | 
						|
    public function getHandler(): array
 | 
						|
    {
 | 
						|
        return $this->handler;
 | 
						|
    }
 | 
						|
 | 
						|
    public function generateLink(array $parameters = []): string
 | 
						|
    {
 | 
						|
        $link = [];
 | 
						|
 | 
						|
        foreach ($this->pattern as $fragment) {
 | 
						|
            if (preg_match('/^{(\\w+)(\\?)?}$/', $fragment, $matches) === 1) {
 | 
						|
                if (isset($parameters[$matches[1]])) {
 | 
						|
                    $link[] = $parameters[$matches[1]];
 | 
						|
                    unset($parameters[$matches[1]]);
 | 
						|
                } elseif (!isset($matches[2])) {//TODO: why? parameter not found but not optional
 | 
						|
                    $link[] = $fragment;
 | 
						|
                }
 | 
						|
            } else {
 | 
						|
                $link[] = $fragment;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        $queryParams = [];
 | 
						|
        foreach ($parameters as $key => $value) {
 | 
						|
            if ($value === null) {
 | 
						|
                continue;
 | 
						|
            }
 | 
						|
 | 
						|
            $queryParams[$key] = $value;
 | 
						|
        }
 | 
						|
 | 
						|
        $query = count($queryParams) > 0 ? '?' . http_build_query($queryParams) : '';
 | 
						|
 | 
						|
        return '/' . implode('/', $link) . $query;
 | 
						|
    }
 | 
						|
 | 
						|
    public function testAgainst(array $path): ?array
 | 
						|
    {
 | 
						|
        $parameters = [];
 | 
						|
 | 
						|
        foreach ($path as $i => $fragment) {
 | 
						|
            if (preg_match('/^{(\\w+)(?:\\?)?}$/', $this->pattern[$i], $matches) === 1) {
 | 
						|
                $parameters[$matches[1]] = $fragment;
 | 
						|
            } elseif ($fragment != $this->pattern[$i]) {
 | 
						|
                return null;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        return $parameters;
 | 
						|
    }
 | 
						|
}
 |