CollectorAbstract::match()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Route
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Route\Collector;
16
17
use Phossa2\Route\Route;
18
use Phossa2\Route\Message\Message;
19
use Phossa2\Route\Traits\AddRouteTrait;
20
use Phossa2\Shared\Debug\DebuggableTrait;
21
use Phossa2\Route\Traits\PrefixAwareTrait;
22
use Phossa2\Route\Traits\HandlerAwareTrait;
23
use Phossa2\Shared\Debug\DebuggableInterface;
24
use Phossa2\Route\Interfaces\ResultInterface;
25
use Phossa2\Route\Interfaces\CollectorInterface;
26
use Phossa2\Route\Interfaces\PrefixAwareInterface;
27
use Phossa2\Route\Interfaces\HandlerAwareInterface;
28
use Phossa2\Event\EventableExtensionCapableAbstract;
29
30
/**
31
 * CollectorAbstract
32
 *
33
 * @package Phossa2\Route
34
 * @author  Hong Zhang <[email protected]>
35
 * @see     EventableExtensionCapableAbstract
36
 * @see     CollectorInterface
37
 * @see     HandlerAwareInterface
38
 * @see     DebuggableInterface
39
 * @version 2.0.1
40
 * @since   2.0.0 added
41
 * @since   2.0.1 added PrefixAware* stuff
42
 */
43
abstract class CollectorAbstract extends EventableExtensionCapableAbstract implements CollectorInterface, PrefixAwareInterface, HandlerAwareInterface, DebuggableInterface
44
{
45
    use HandlerAwareTrait, PrefixAwareTrait, DebuggableTrait, AddRouteTrait;
46
47
    /**#@+
48
     * Collector level events
49
     *
50
     * @const
51
     */
52
53
    // before match in this collector
54
    const EVENT_BEFORE_MATCH = 'collector.match.before';
55
56
    // after a successful match in this collector
57
    const EVENT_AFTER_MATCH = 'collector.match.after';
58
59
    /**#@-*/
60
61
    /**
62
     * Constructor
63
     *
64
     * @param  array $properties
65
     * @access public
66
     */
67
    public function __construct(array $properties = [])
68
    {
69
        $this->setProperties($properties);
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     *
75
     * @since 2.0.1 added patch prefix checking
76
     */
77
    public function matchRoute(ResultInterface $result)/*# : bool */
78
    {
79
        // skip if path prefix not match
80
        if (!$this->matchPathPrefix($result->getPath())) {
81
            return false;
82
        }
83
84
        // match with each routes in the collector
85
        return $this->realMatchRoute($result);
86
    }
87
88
    /**
89
     * Matching
90
     *
91
     * @param  ResultInterface $result
92
     * @return boolean
93
     * @access protected
94
     */
95
    protected function realMatchRoute(ResultInterface $result)/*# : bool */
96
    {
97
        $res = false;
98
        $param = ['result' => $result];
99
        if ($this->trigger(self::EVENT_BEFORE_MATCH, $param) &&
100
            $this->match($result) &&
101
            $this->trigger(self::EVENT_AFTER_MATCH, $param)
102
        ) {
103
            $res = true;
104
        }
105
106
        $this->setCollectorHandler($result);
107
        return $res;
108
    }
109
110
    /**
111
     * Set collector level handler if result has no handler yet
112
     *
113
     * @param  ResultInterface $result
114
     * @return $this
115
     * @access protected
116
     */
117
    protected function setCollectorHandler(ResultInterface $result)
118
    {
119
        $status = $result->getStatus();
120
        if (is_null($result->getHandler()) &&
121
            $this->getHandler($status)
122
        ) {
123
            // debug message
124
            $this->debug(Message::get(
125
                Message::RTE_COLLECTOR_HANDLER,
126
                $this->getHandler($status),
127
                get_class($this)
128
            ));
129
            $result->setHandler($this->getHandler($status));
130
        }
131
        return $this;
132
    }
133
134
    /**
135
     * Child class must implement this method
136
     *
137
     * MUST set $result status and handler in this method
138
     *
139
     * @param  ResultInterface $result result object
140
     * @return bool
141
     * @access protected
142
     */
143
    abstract protected function match(ResultInterface $result)/*# : bool */;
144
}
145