1: <?php
2:
3: namespace PHPixie\Route\Resolvers\Resolver\Pattern;
4:
5: class Prefix extends \PHPixie\Route\Resolvers\Resolver\Pattern
6: {
7: protected $resolverBuilder;
8: protected $resolver;
9:
10: public function __construct($builder, $resolverBuilder, $configData)
11: {
12: $this->resolverBuilder = $resolverBuilder;
13: parent::__construct($builder, $configData);
14: }
15:
16: public function match($fragment)
17: {
18: if(!$this->isMethodValid($fragment)) {
19: return null;
20: }
21:
22: list($hostAttributes, $host) = $this->matchPattern(
23: $this->hostPattern(),
24: $fragment->host()
25: );
26:
27: if($hostAttributes === null) {
28: return null;
29: }
30:
31: list($pathAttributes, $path) = $this->matchPattern(
32: $this->pathPattern(),
33: $fragment->path()
34: );
35:
36: if($pathAttributes === null) {
37: return null;
38: }
39:
40: $attributes = array_merge(
41: $this->defaults(),
42: $hostAttributes,
43: $pathAttributes
44: );
45:
46: $fragment = $fragment->copy();
47: $fragment->setPath($path);
48: $fragment->setHost($host);
49:
50: $match = $this->resolver()->match($fragment);
51: if($match !== null) {
52: $match->prependAttributes($attributes);
53: }
54:
55: return $match;
56: }
57:
58: protected function matchPattern($pattern, $string)
59: {
60: if($pattern === null) {
61: return array(array(), $string);
62: }
63:
64: return $this->builder->matcher()->matchPrefix($pattern, $string);
65: }
66:
67: public function generate($match, $withHost = false)
68: {
69: $fragment = $this->resolver()->generate($match, $withHost);
70: $attributes = $this->mergeAttributes($match);
71:
72: $path = $this->generatePatternString($this->pathPattern(), $attributes);
73: $path.= $fragment->path();
74: $fragment->setPath($path);
75:
76: if($withHost) {
77: $host = $this->generatePatternString($this->hostPattern(), $attributes);
78: $host.= $fragment->host();
79: $fragment->setHost($host);
80: }
81:
82: return $fragment;
83: }
84:
85: public function resolver()
86: {
87: if($this->resolver === null) {
88: $resolverConfig = $this->configData->slice('resolver');
89: $this->resolver = $this->resolverBuilder->buildFromConfig($resolverConfig);
90: }
91:
92: return $this->resolver;
93: }
94: }