Skip to content

Commit 5a0c2da

Browse files
committed
Add fn not.
1 parent 3707fca commit 5a0c2da

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

cw_tool.module

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,14 @@ function cw_tool_library() {
359359

360360
return $libraries;
361361
}
362+
363+
/**
364+
* Check if the form ID is associated to the form class.
365+
*
366+
* @param string $class
367+
* @param string $form_id
368+
* @return bool
369+
*/
370+
function cw_tool_is_form_id_of_form_class($class, $form_id) {
371+
return strpos($form_id, $class) !== FALSE;
372+
}

src/CW/Util/FieldUtil.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class FieldUtil {
2121
const KEY_SAFE_VALUE = 'safe_value';
2222
const KEY_NODEREFERENCE_ID = 'nid';
2323
const KEY_URI = 'uri';
24+
const KEY_DATE_START = 'value';
25+
const KEY_DATE_END = 'value2';
2426
const KEY_RICH_TEXT_FORMAT = 'format';
2527

2628
}

src/CW/Util/Functional.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public static function first(array $list, $callable, $defaultValue = NULL) {
142142
}
143143

144144
/**
145-
* Mimics the functional dot: F1 . F2 . F3 .. -> F1(F2(F3(...))).
145+
* Mimics the functional dot: F1 . F2 ... Fn-1 . Fn -> Fn(Fn-1(...F2(F1())...)).
146146
*
147147
* @param callable[] $callbacks
148148
* @param mixed $value
@@ -189,4 +189,22 @@ public static function curry($callback) {
189189
};
190190
}
191191

192+
/**
193+
* Generates a negating callback.
194+
*
195+
* @param callable $callable
196+
* @return \Closure
197+
*/
198+
public static function fnNot($callable) {
199+
$staticArgs = func_get_args();
200+
array_shift($staticArgs);
201+
202+
return function () use ($callable, $staticArgs) {
203+
$dynArgs = func_get_args();
204+
$args = array_merge($staticArgs, $dynArgs);
205+
206+
return !call_user_func_array($callable, $args);
207+
};
208+
}
209+
192210
}

tests/phpunit/Util/FunctionalTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,32 @@ public function testCurry() {
148148
$this->assertEquals(22, $staticSum());
149149
}
150150

151+
public function testNotSimple() {
152+
$fnNotString = Functional::fnNot('is_string');
153+
154+
$this->assertTrue($fnNotString(1));
155+
$this->assertTrue($fnNotString(NULL));
156+
$this->assertTrue($fnNotString(FALSE));
157+
$this->assertTrue($fnNotString(TRUE));
158+
159+
$this->assertFalse($fnNotString("hello"));
160+
$this->assertFalse($fnNotString(''));
161+
}
162+
163+
public function testNotComplex() {
164+
$fnIsMod = function ($mod, $n) {
165+
return $n % $mod === 0;
166+
};
167+
168+
$fnNotIsMod10 = Functional::fnNot($fnIsMod, 10);
169+
170+
$this->assertEquals(TRUE, $fnNotIsMod10(1));
171+
$this->assertEquals(TRUE, $fnNotIsMod10(3));
172+
$this->assertEquals(FALSE, $fnNotIsMod10(10));
173+
$this->assertEquals(TRUE, $fnNotIsMod10(1043));
174+
$this->assertEquals(FALSE, $fnNotIsMod10(35298320));
175+
}
176+
151177
}
152178

153179
class FunctionalTest__SimpleClassDummy {

0 commit comments

Comments
 (0)