GithubHelp home page GithubHelp logo

volcanus-routing's People

Contributors

k-holy avatar

Watchers

 avatar

volcanus-routing's Issues

ディレクトリパラメータの型指定サポート

現在の仕様

__gateway.php

    <?php
    $router = Router::instance([
        'parameterDirectoryName' => '%VAR%',
    ])>execute();
    // RequestURI /items/1 … include /items/%VAR%/index.php

/items/%VAR%/index.php

    <?php
    $item_id = $router->parameter(0); // (string)'1'

こういうのに対応

__gateway.php

    <?php
    $router = Router::instance([
        'parameterDirectoryLeftDelimiter' => '{%',
        'parameterDirectoryRightDelimiter' => '%}',
        'parameterFilters' => [
            'digit' => function($var) {
                if (!ctype_digit($var)) {
                    throw new InvalidParameterException('oh...');
                }
                return intval($var);
            },
        ],
    ])->execute();
    // RequestURI /items/1 … include /items/{%digit%}/index.php
    // RequestURI /items/a … InvalidParameterException

/items/{%digit%}/index.php

    <?php
    $item_id = $router->parameter(0); // (int)1

デリミタ導入前の仕様では実現できなかった以下のパターンにも対応できるか?

__gateway.php

<?php
$router = Router::instance([
    'parameterDirectoryLeftDelimiter' => '{%',
    'parameterDirectoryRightDelimiter' => '%}',
    'parameterFilters' => [
        'digit' => function($var) {
            if (!ctype_digit($var)) {
                throw new InvalidParameterException('oh...');
            }
            return intval($var);
        },
        'alpha' => function($var) {
            if (!ctype_alpha($var) {
                throw new InvalidParameterException('ahh...');
            }
            return $var;
        },
    ],
])->execute();
// RequestURI /users/1 … include /users/{%digit%}/index.php
// RequestURI /users/kholy … include /users/{%alpha%}/index.php
// RequestURI /users/k-holy ... InvalidParameterException

ただし、先にマッチしたものが適用されるので、ルールは相互に影響しないものとする必要あり?

/users/{%digit%}/index.php

    <?php
    $user_id = $router->parameter(0); // (int) 1

/users/{%alpha%}/index.php

    <?php
    $account_id = $router->parameter(0); // (string) 'kholy'

あと、Ctype関数はデフォルトで対応しても良いかも。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.