PHP utility collection with hybrid and fluent APIs.

⌈⌋ branch:  hybrid7 libraries


Check-in [23a6b15a36]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Simplified __filter list back to also allow plain callback names instead of [name, args[]]. Restructured filter() varname checks.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:23a6b15a3626ab82aef84db6b2e41b7a949edf91
User & Date: mario 2014-06-18 15:54:40
Context
2014-06-27
17:38
Permit static filter invocation `input::text($str)` without underscore prefix, allow ->has() and ->no() to probe a list of key names, extra parameter/argument support for bound global functions, fixed _disallow Exception syntax, and make _length() now not only to slice but also declare a minimum string length. check-in: e42563785e user: mario tags: trunk
2014-06-18
15:54
Simplified __filter list back to also allow plain callback names instead of [name, args[]]. Restructured filter() varname checks. check-in: 23a6b15a36 user: mario tags: trunk
2014-06-17
02:27
Permit $_SERVER->method(["name"]) syntax as well (varnames get unpacked from single-entry array). check-in: d63339146a user: mario tags: trunk
Changes

Changes to php7/input.php.

756
757
758
759
760
761
762
763
764
765
766
767



768
769
770


771
772

773
774
775
776
777



778
779

780
781
782
783
784
785

786
787
788
789
790
791
792

793
794
795
796
797
798
799
800
...
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
...
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
....
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
    
    /**
     * Executes current filter or filter chain on given $varname.
     *
     */
    function filter($varname, $reset_filter_afterwards=NULL) {

        // direct/raw access can occour if invoked via offsetGet[] or filter() method called directly
        if (empty($this->__filter)) {
            $this->__filter[] = array(INPUT_DIRECT, array());

            // or apply a pre-defined filter chain



            if (isset($this->__rules[$varname])) {
                // must be in internal nested format
                $this->__filter = $this->rules[$varname];


            }
        }


        // retrieve value for selected input variable
        $this->__varname = $varname;
        if (is_array($varname) and count($varname)===1) {  // single array [["name"]] becomes varname
            $varname = current($varname);



        }
        if (isset($this->__vars[$varname])) {   // entry exists

            $data = $this->__vars[$varname];
        }
        elseif (count($this->__filter) and ("list" == $this->__filter[0][0])) {
            $data = NULL;    // do nothing, as values will be fetched by a multiplex handler
        }
        else {

            INPUT_QUIET or trigger_error("Undefined input variable \${$this->__title}['{$this->__varname}']", E_USER_NOTICE);   // undecorative
            $data = NULL;    // run through filters anyway (log)
        }
        
        // implicit ->array filter handling for lists (= if value is an array)
        $ARRAY = (ARRAY("array",(array)NULL));  # complex trigger token
        if (is_array($data) && !in_array($ARRAY, $this->__filter)) {

            array_unshift($this->__filter, array("array", array()));
        }
        
        // apply filters (we're building an ad-hoc merged array here, because ->apply works on the reference, and some filters expect ->__filter to contain the complete current list)
        $this->__filter = array_merge($this->__filter, $this->__always);
        $data = $this->apply($data, $this->__filter);
        
        // the Traversable array interface resets the filter list after each request, see ->current()
................................................................................
     *
     * It works on an array reference and with array_shift() because filters are allowed to
     * modify the list at runtime (->array requires to).
     *
     */
    function apply($data, &$filterchain) {
        while ($f = array_shift($filterchain)) {
            list($filtername, $args) = $f;
            
            // an override function name or closure was set
            if (isset($this->{"_$filtername"})) {
                $filtername = $this->{"_$filtername"};
            }
            // call _filter method
            if (is_string($filtername) && method_exists($this, "_$filtername")) {
                $data = call_user_func_array(array($this, "_$filtername"), array_merge(array($data), (array)$args));
            }
................................................................................
            $filtername = array_shift($args);
        }
        else {
            $args=array();
        }

        // Add filter to list
        $this->__filter[] = array($filtername, $args);

        // fluent interface
        return $this;
    }
    


................................................................................
$_REQUEST = new input($_REQUEST, "_REQUEST");
$_GET = new input($_GET, "_GET");
$_POST = new input($_POST, "_POST");
$_COOKIE = new input($_COOKIE, "_COOKIE");
#$_SESSION
#$_ENV
#$_FILES required a special handler

//          print_r(  $_SERVER->list->text[[USER,HOME]]  );



?>







|
|
|
|
|
>
>
>

<
|
>
>


>


<
<
<
>
>
>

|
>


<
<
<
<
>
|
|


|
<
<
>
|







 







|
|
|







 







|







 







<
<
<



756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771

772
773
774
775
776
777
778
779



780
781
782
783
784
785
786
787




788
789
790
791
792
793


794
795
796
797
798
799
800
801
802
...
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
...
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
....
1124
1125
1126
1127
1128
1129
1130



1131
1132
1133
    
    /**
     * Executes current filter or filter chain on given $varname.
     *
     */
    function filter($varname, $reset_filter_afterwards=NULL) {

        // single array [["name"]] becomes varname
        if (is_array($varname) and count($varname)===1) {
            $varname = current($varname);
        }
        $this->__varname = $varname;

        // direct/raw access without any ->filtername prior offsetGet[] or plain filter() call
        if (empty($this->__filter)) {
            if (isset($this->__rules[$varname])) {

                $this->__filter = $this->rules[$varname];  // use a predefined filterset
            } else {
                $this->__filter = array( INPUT_DIRECT );  // direct access handler
            }
        }
        $first_handler = reset($this->__filter);

        // retrieve value for selected input variable



        $data = NULL;
        if (!is_scalar($varname) or $first_handler == "list" or $first_handler == "multi") {
            // one of the multiplex handlers supposedly picks it up
        }
        elseif (isset($this->__vars[$varname])) {
            // entry exists
            $data = $this->__vars[$varname];
        }




        elseif (!INPUT_QUIET) {
            trigger_error("Undefined input variable \${$this->__title}['{$this->__varname}']", E_USER_NOTICE);
            // run through filters anyway (for ->log)
        }
        
        // implicit ->array filter handling


        if (is_array($data) and $first_handler != "array") {
            array_unshift($this->__filter, "array");
        }
        
        // apply filters (we're building an ad-hoc merged array here, because ->apply works on the reference, and some filters expect ->__filter to contain the complete current list)
        $this->__filter = array_merge($this->__filter, $this->__always);
        $data = $this->apply($data, $this->__filter);
        
        // the Traversable array interface resets the filter list after each request, see ->current()
................................................................................
     *
     * It works on an array reference and with array_shift() because filters are allowed to
     * modify the list at runtime (->array requires to).
     *
     */
    function apply($data, &$filterchain) {
        while ($f = array_shift($filterchain)) {
            list($filtername, $args) = is_array($f) ? $f : array($f, array());

            // an override function name or closure exists
            if (isset($this->{"_$filtername"})) {
                $filtername = $this->{"_$filtername"};
            }
            // call _filter method
            if (is_string($filtername) && method_exists($this, "_$filtername")) {
                $data = call_user_func_array(array($this, "_$filtername"), array_merge(array($data), (array)$args));
            }
................................................................................
            $filtername = array_shift($args);
        }
        else {
            $args=array();
        }

        // Add filter to list
        $this->__filter[] = count($args) ? array($filtername, $args) : $filtername;

        // fluent interface
        return $this;
    }
    


................................................................................
$_REQUEST = new input($_REQUEST, "_REQUEST");
$_GET = new input($_GET, "_GET");
$_POST = new input($_POST, "_POST");
$_COOKIE = new input($_COOKIE, "_COOKIE");
#$_SESSION
#$_ENV
#$_FILES required a special handler





?>