From 23960bb72439d6d1112d7b38a9e9112fc809f3cb Mon Sep 17 00:00:00 2001
From: Penny Leach <penny@mjollnir.org>
Date: Thu, 17 Dec 2009 09:25:45 +0100
Subject: [PATCH] lib/moodlelib MDL-21130   added helper partial() function

this helps to create callbacks with partially bound arguments. woot!
---
 lib/moodlelib.php | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/lib/moodlelib.php b/lib/moodlelib.php
index e11b6f51b2..83fbd36392 100644
--- a/lib/moodlelib.php
+++ b/lib/moodlelib.php
@@ -9218,3 +9218,45 @@ function check_consecutive_identical_characters($password, $maxchars) {
     return true;
 }
 
+/**
+ * helper function to do partial function binding
+ * so we can use it for preg_replace_callback, for example
+ * this works with php functions, user functions, static methods and class methods
+ * it returns you a callback that you can pass on like so:
+ *
+ * $callback = partial('somefunction', $arg1, $arg2);
+ *     or
+ * $callback = partial(array('someclass', 'somestaticmethod'), $arg1, $arg2);
+ *     or even
+ * $obj = new someclass();
+ * $callback = partial(array($obj, 'somemethod'), $arg1, $arg2);
+ *
+ * and then the arguments that are passed through at calltime are appended to the argument list.
+ *
+ * @param mixed $function a php callback
+ * $param mixed $arg1.. $argv arguments to partially bind with
+ *
+ * @return callback
+ */
+function partial() {
+    if (!class_exists('partial')) {
+        class partial{
+            var $values = array();
+            var $func;
+
+            function __construct($func, $args) {
+                $this->values = $args;
+                $this->func = $func;
+            }
+
+            function method() {
+                $args = func_get_args();
+                return call_user_func_array($this->func, array_merge($this->values, $args));
+            }
+        }
+    }
+    $args = func_get_args();
+    $func = array_shift($args);
+    $p = new partial($func, $args);
+    return array($p, 'method');
+}
-- 
2.39.5