summaryrefslogtreecommitdiff
path: root/app/frontend/url/UrlManager.php
blob: a33d98ef62b27affda3a20880393e3aaff75ee22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

Prado::using('System.Web.TUrlMapping');

class UrlManager extends TUrlMapping {

    public function constructUrl($serviceID, $serviceParam, $getItems, $encodeAmpersand, $encodeGetItems) {
        $url = parent::constructUrl(
            $serviceID,
            $serviceParam,
            $getItems,
            $encodeAmpersand,
            $encodeGetItems
        );
        return rtrim(
            preg_replace(
                '#^/' . $serviceParam . '#',
                '/' . $this->_convertServiceParam($serviceParam),
                preg_replace('#^/' . $serviceID . '#', '', $url)
            ),
            '/'
        ) . '/';
    }

    public function parseUrl() {
        $params = parent::parseUrl();
        if ($this->MatchingPattern) {
            $serviceID = $this->MatchingPattern->ServiceID;
            if (isset($params[$serviceID])) {
                $params[$serviceID] = $this->_parseServiceParam($params[$serviceID]);
            }
        }
        return $params;
    }

    /**
     * Convert service param from camelCase to hyphenated-form.
     **/
    private function _convertServiceParam($param) {
        return implode(
            '-',
            array_map('mb_strtolower', array_filter(preg_split('/(?=[A-Z])/', $param)))
        );
    }

    /**
     * Convert service param from hyphenated-form to camelCase.
     **/
    private function _parseServiceParam($param) {
        return implode(
            '',
            array_map('ucfirst', explode('-', $param))
        );
    }
}

?>