1: <?php
2:
3: namespace PHPixie\Route\Resolvers\Resolver;
4:
5: abstract class Pattern implements \PHPixie\Route\Resolvers\Resolver
6: {
7: protected $builder;
8: protected $configData;
9:
10: protected $processed = array();
11:
12: protected $hostPattern;
13: protected $pathPattern;
14: protected $methods;
15:
16: public function __construct($builder, $configData)
17: {
18: $this->builder = $builder;
19: $this->configData = $configData;
20: }
21:
22: public function hostPattern()
23: {
24: return $this->pattern('host','.+?');
25: }
26:
27: public function pathPattern()
28: {
29: return $this->pattern('path','[^/]+?');
30: }
31:
32: public function defaults()
33: {
34: return $this->config('defaults', array());
35: }
36:
37: public function attributePatterns()
38: {
39: return $this->config('attributePatterns', array());
40: }
41:
42: public function methods()
43: {
44: return $this->config('methods', array());
45: }
46:
47: protected function pattern($name, $defaultAttributePattern)
48: {
49: $key = $name.'Pattern';
50: if(!array_key_exists($key, $this->processed)) {
51: $pattern = $this->configData->get($name, null);
52: if($pattern !== null) {
53: $pattern = $this->builder->matcherPattern(
54: $pattern,
55: $defaultAttributePattern,
56: $this->attributePatterns()
57: );
58: }
59: $this->processed[$key] = $pattern;
60: }
61: return $this->processed[$key];
62: }
63:
64: protected function config($key, $default)
65: {
66: if(!array_key_exists($key, $this->processed)) {
67: $this->processed[$key] = $this->configData->get($key, $default);
68: }
69:
70: return $this->processed[$key];
71: }
72:
73: protected function isMethodValid($fragment)
74: {
75: $methods = $this->methods();
76: if(empty($methods)) {
77: return true;
78: }
79:
80: $method = $fragment->serverRequest()->getMethod();
81: return in_array($method, $methods, true);
82: }
83:
84: protected function generatePatternString($pattern, $attributes)
85: {
86: if($pattern === null) {
87: return '';
88: }
89:
90: return $pattern->generate($attributes);
91: }
92:
93: protected function mergeAttributes($match)
94: {
95: return array_merge(
96: $this->defaults(),
97: $match->attributes()
98: );
99: }
100:
101: abstract public function generate($match, $withHost = false);
102: }