{"id":24976896,"url":"https://github.com/rezigned/labs","last_synced_at":"2025-03-29T08:24:22.599Z","repository":{"id":1714408,"uuid":"2444191","full_name":"rezigned/Labs","owner":"rezigned","description":"For test something out","archived":false,"fork":false,"pushed_at":"2012-04-25T17:10:09.000Z","size":100,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-03T22:59:19.663Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rezigned.png","metadata":{"files":{"readme":"README","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-09-23T13:49:02.000Z","updated_at":"2024-12-25T23:43:41.000Z","dependencies_parsed_at":"2022-09-04T15:23:51.329Z","dependency_job_id":null,"html_url":"https://github.com/rezigned/Labs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rezigned%2FLabs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rezigned%2FLabs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rezigned%2FLabs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rezigned%2FLabs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rezigned","download_url":"https://codeload.github.com/rezigned/Labs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246158284,"owners_count":20732759,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-03T22:59:32.634Z","updated_at":"2025-03-29T08:24:22.583Z","avatar_url":"https://github.com/rezigned.png","language":"PHP","readme":"TEST\n\n\nabstract class Models_Managers_Abstract {\n    \n    protected $filters = array(),\n            \n              $params  = array();\n    \n    public function filter($key, $op = '=', $val = null, $type = 'AND') {\n        \n        $args = func_get_args();\n\n        switch(func_num_args()) {\n            \n            # custom sql \n            case 1:\n                $this-\u003efilters[$key][] = compact('type');\n                break;\n            \n            # normal operand\n            case 2:\n                $val = $op;\n                $this-\u003efilters[$key][] = array('op' =\u003e '=') + compact('op', 'val', 'type');\n                break;\n            \n            default:\n                $this-\u003efilters[$key][] = compact('op', 'val', 'type');\n                \n        }\n\n        return $this;\n    }\n    \n    public function or_filter($key, $op = '=', $val = null) {\n        $this-\u003efilter($key, $op, $val, 'OR');\n        \n        return $this;\n    }\n    \n    public function condition($conditions = array()) {\n        \n    }\n    \n    public function compile() {\n\n        $sql = array();\n        foreach($this-\u003efilters as $col =\u003e $f) {\n                \n            # group it\n            if (sizeof($f) \u003e 1) {\n                \n                $t1 = $f[0]['type'];\n                $t2 = array();\n\n                # unset it\n                $f[0]['type'] = null;\n                \n                foreach($f as $k =\u003e $data) {\n\n                    $t2[] = \"{$col} {$data['op']} ? \" . (isset($f[$k + 1]['type']) ? $f[$k + 1]['type'] : '');\n                    $this-\u003eparams[] = $data['val'];\n                }\n                \n                $t1 .= ' (' . join(' ', $t2) . ')';\n                \n                $sql[] = $t1;\n            }\n            \n            # single condition\n            else {\n                \n                $data = $f[0];\n                $sql[] = \"{$data['type']} {$col} {$data['op']} ?\";\n                $this-\u003eparams[] = $data['val'];\n            }\n\n        }\n        \n        echo join(' ', $sql);\n        # p($this-\u003efilters);\n        p($this-\u003eparams);\n\n    }\n    \n    public function parse_condition($conditions = array()) {\n        \n        # recusive styles (find alternate way without recursive\n        $conds = $params = array();\n        \n        foreach($conditions as $type =\u003e $data) {\n\n            # for numeric key\n            if (is_numeric($type)) {\n\n                $conds[]  = \"{$data[0]} $data[1] ?\";\n                $params[] = $data[2];\n            }\n\n            # for 'boolean' e.g. AND, OR\n            else {\n                list($c, $p) = call_user_func(array($this, 'parse_condition'), $data);\n                $conds[] = '(' . join(\" $type \", $c) . ')';\n                $params  = array_merge($params, $p);\n            }\n        }\n\n        return array($conds, $params);\n    }\n    \n    public function parse_param() {\n        \n    }\n    \n    /**\n     *\n     * @param type $conditions \n     */\n    public function find($conditions = array()) {\n\n        p($this-\u003eparse_condition($conditions));\n        return $this;\n    }\n    \n    public function indexed() {\n        $this-\u003efind();\n    }\n\n    public function threaded() {\n        \n    }\n    \n    public function neighbors() {\n        \n    }\n    \n    public function count() {\n        \n    }\n}\n\nclass BC extends Models_Managers_Abstract {\n    \n}\n\n$a = new BC;\n//$a-\u003efilter('username', 'kung')\n//  -\u003efilter('key', 'yes')\n//  -\u003eor_filter('key', '=', 'ma')\n//  -\u003efilter('key', 'bac')\n//  -\u003efilter('date_published', '\u003e', '23')\n//  -\u003eor_filter('firstname', 'LIKE', 'a%');\n\n//$a-\u003efind(array(\n//    'OR' =\u003e array(\n//        array('test', '=', 'test'),\n//        array('test', '=', 'test'),\n//    ),\n//    'AND' =\u003e array(\n//        array('email', '=', 'marut@landaureece.com'),\n//    ),\n//));\n\n\nfunction test_find_2($conditions) {\n\n    $conds = array();\n    $level = 0;\n    while(list($type, $data) = each($conditions)) {\n        \n        if ($level \u003e 0) {\n            echo '(';\n        }\n        \n        echo ' [type: ', $type, '] ';\n        if (is_numeric($type)) {\n            echo \"{$data[0]} $data[1] ?\";\n        }\n        \n        else {\n            $level++;\n            $conditions = $data;\n        }\n        \n        if ($level \u003e 0) {\n            echo ')';\n        }\n    }\n    \n    p($conds);\n    die;\n}\n\n$condition = array(\n   array('User.firstname', '=', 'marut'),\n   'OR' =\u003e array(\n      array('Company.name', '=', 'Future Holdings'),\n      array('Company.city', '=', 'CA'),\n      'AND' =\u003e array(\n          array('Company.postcode', '=', '10234'),\n          array('Company.postcode', '=', '10234'),\n      )\n   ),\n   'AND' =\u003e array(\n       array('User.name', '=', 'Kung'),\n   ),\n   'AND' =\u003e array(\n       array('User.id', 'IN', '1234')\n   ),\n);\n\n$a-\u003efind($condition);\n\n\nclass Nobify_Router_Route {\n    \n    public $REG_OPTIONAL  = '|\\(([^:]*):([^\\)]+)\\)|',\n           $REG_TRAILING  = '[^/]*',\n           $REG_DELIMETER = '|',\n           $REG_DYNAMIC   = '|:([\\w-]+)|',\n           $REG_FORMAT    = '|(?\\.:format\\)?|',\n           $REG_NORMALIZE_NAME = '|[^\\w+-]+|',\n           $PART_FORMAT   = '(.:format)';\n           \n    public $routes = array(),\n           $routes_names    = array(),\n           $match  = array(),\n           $default_options = array(\n               'controller'  =\u003e null,\n               'route'  =\u003e null,\n               'route_regex'       =\u003e null,\n               'route_with_format' =\u003e null,\n               'module' =\u003e 'default',\n               'name'   =\u003e null,\n               'format' =\u003e true,\n               'method' =\u003e array('GET', 'POST', 'PUT', 'DELETE', 'HEAD'),\n           );\n    \n    public $resource_routes = array(\n        array(\n            '',\n            '#index',\n            'method' =\u003e 'GET'\n        ),\n        array(\n            '',\n            '#create',\n            'method' =\u003e 'POST',\n        ),\n        array(\n            '/new',\n            '#new',\n            'method' =\u003e 'GET',\n        ),\n        array(\n            '/:id/edit', \n            '#edit',\n            'method' =\u003e 'GET',\n        ),\n        array(\n            '/:id',\n            '#show',\n            'method' =\u003e 'GET',\n        ),\n        array(\n            '/:id', \n            '#update',\n            'method' =\u003e 'PUT',\n        ),\n        array(\n            '/:id', \n            '#destroy',\n            'method' =\u003e 'DELETE',\n        ),\n    );\n    /**\n     * Convert route string to regular expression\n     * \n     * @param type $route\n     * @param type $options\n     * @return type \n     * \n     * (:controller(/:action))\n     */\n    protected function route_to_regex($route, \u0026$options = array()) {\n\n        $routes   = array();\n        $replaces = array();\n        \n        # convert optional part (id) to non-capturing sub group (?:id)\n        $route    = str_replace(array('(', '.'), array('(?:', '\\.'), $route);\n        \n        # dynamic part exists? e.g. :id, :user\n        if (strpos($route, ':') !== FALSE) {\n            \n            # find all dynamic part\n            preg_match_all($this-\u003eREG_DYNAMIC, $route, $matches);\n            \n            if ($matches) {\n                \n                # matches[0] include `:`  e.g. `:id`\n                # matches[1] didn't include `:` e.g. `id`\n                $parts = $matches[1];\n\n                foreach($parts as $part) {\n\n                    $rule = $this-\u003eREG_TRAILING;\n\n                    # constraint exists?\n                    if (isset($options['constraints'][$part]))\n                        $rule = $options['constraints'][$part];\n\n                    $part = '(?P\u003c' . $part . '\u003e' . $rule . ')';\n                    $replaces[] = $part;\n                }\n\n                $options['matched_parts'] = $parts;\n                return str_replace($matches[0], $replaces, $route) . '?';\n            }\n        }\n        \n        return $route;\n    }\n\n    /**\n     * Register a route\n     * \n     * @param string       $routes      route style\n     * @param string|array $controller  controller name or route option array\n     * \n     * @example\n        \n         add('sessions/login', 'Session#login'\n         add('users(.:format)', '\n        'sessions/login'      =\u003e 'User#login',\n        'sessions/(.:format)' =\u003e 'User#logout',\n\n        'dashboard/:user/account/orders/:id' =\u003e array(\n            'User#method',\n            'constraints' =\u003e array(\n                ':user' =\u003e '\\w+',\n            ),\n        )\n     * \n     */\n    public function add($route, $controller) {\n\n        $parsed_routes = array();\n        $options       = array();\n\n        # handle options\n        if (is_array($controller)) {\n\n            $options    = $controller;\n            $controller = $options[0];\n            \n            # normalize request method i.e. convert to all caps\n            $options['method'] = array_map('strtoupper', (array)$options['method']);\n        }\n\n        $options = $options + $this-\u003edefault_options;\n        \n        # normalize route name\n        if (!$options['name'])\n            $options['name'] = str_replace('_format', '', trim(preg_replace($this-\u003eREG_NORMALIZE_NAME, '_', $route), '_'));\n        \n        # automatic append ':format'\n        $route_with_format = $route;\n        \n        if ($options['format'] \u0026\u0026 strpos($route, ':format') === FALSE)\n            $route_with_format .= $this-\u003ePART_FORMAT;\n        \n        # merge new values\n        $parsed_routes = array(\n            'controller'  =\u003e $controller,\n            'route'       =\u003e $route,\n            'route_regex'       =\u003e $this-\u003eroute_to_regex($route_with_format, $options),\n            'route_with_format' =\u003e $route_with_format,\n        ) + $options;\n\n        # for reverse url lookup\n        $this-\u003eroutes_names[$options['name']][] = sizeof($this-\u003eroutes);\n        $this-\u003eroutes[] = $parsed_routes;   \n    }\n    \n    /**\n     * Add RESTful resource \n     * \n     * @param type $resource\n     * @param type $options   only allow some options e.g. 'constraints'\n     */\n    public function add_resource($resource, $options = array()) {\n        \n        foreach($this-\u003eresource_routes as $r) {\n            \n            # set default options for controller and http method\n            $opts = array(\n                $resource . $r[1],\n                'method' =\u003e $r['method']\n            );\n            \n            $this-\u003eadd($resource . $r[0], $opts + $options);\n        }\n    }\n    \n    /**\n     * Match given uri with existing routes\n     * \n     * @param type $uri \n     * @example\n     *     match('account/signup')\n     */\n    public function match($uri) {\n\n        $params = array();\n        $match  = array();\n        $method = $_SERVER['REQUEST_METHOD'];\n        \n        # hidden input take more precedence\n        if (isset($_REQUEST['_method']) and $_method = strtoupper($_REQUEST['_method']) \n                                        and in_array($_method, $this-\u003edefault_options['method'])) {\n            $method = $_method;\n        }\n                                        \n        foreach($this-\u003eroutes as $k =\u003e $r) {\n            \n            # filter out non-matched method (save regex overhead)\n            if (!in_array($method, $r['method']))\n                continue;\n\n            if (preg_match('|^' . $r['route_regex'] . '$|', $uri, $matches)) {\n\n                # merge params\n                $params = array_slice($matches, 1);\n                $match  = $r;\n                break;\n            }\n        }\n\n        # validate \n        if ($match) {\n            $match['params'] = $params;\n        }\n        \n        $match['uri'] = $uri;\n\n        return $this-\u003ematch = $match;\n    }\n    \n    /**\n     * Reverse url for named route\n     * \n     * @param  string $name\n     * @param  array  $params\n     * @return string\n     * @example \n     *     \n     *     $this-\u003eurl('posts', array('id' =\u003e 5)) =\u003e 'posts/5'\n     *     $this-\u003eurl('posts', array('id' =\u003e 5, 'format' =\u003e 'xml')) =\u003e 'posts/5.xml'\n     */\n    public function url($name, $params = array()) {\n        \n        $r = array();\n        \n        # get matched route\n        foreach($this-\u003eroutes_names[$name] as $id) {\n            if (isset($this-\u003eroutes[$id])) {\n                $r = $this-\u003eroutes[$id];\n                break;\n            }\n        }\n\n        # reverse vars\n        $route    = $r['route_with_format'];\n        $parts    = $r['matched_parts'];        \n        $searches = $replaces = array();\n        \n        foreach($parts as $key) {\n\n            # if param doesn't exist we use `null` as default value\n            if (isset($params[$key]))\n                $val = $params[$key];\n            else\n                $val = null;\n            \n            # prepend ':' to key\n            $key = ':' . $key;\n            \n            # is there an optional part e.g. (.:format) we have to do an extra work here.\n            if (strpos($route, $key . ')') !== FALSE) {\n                \n                # grap the whole dynamic part include `(,)`\n                preg_match('|\\(([^:]*)' . $key . '\\)|', $route, $match);\n                \n                if ($match) {\n                    \n                    # replace key with dynamic part style\n                    $key = $match[0];\n                    \n                    # prepend any extra char e.g. '.', '/' if exists\n                    if (isset($val, $match[1]))\n                        $val = $match[1] . $val;\n                }\n            }\n            \n            # add search part \n            $searches[] = $key;\n            $replaces[] = $val;\n        }\n        \n        # lookup for :host, :port params\n        \n        # convert unknown :key to query string\n        $qs = '';\n        if ($diff = array_diff_key($params, array_flip($parts))) {\n            $qs = '?' . str_replace('%3A', '', http_build_query($diff));\n        }\n\n        return str_replace($searches, $replaces, $route) . $qs;\n    }\n}","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frezigned%2Flabs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frezigned%2Flabs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frezigned%2Flabs/lists"}