Файловый менеджер - Редактировать - /var/www/xthruster/html/wp-content/uploads/flags/fp.tar
Назад
core/system.php 0000755 00000003240 14717633353 0007547 0 ustar 00 <?php namespace WPML\INSTALLER\FP\System; /** * Returns a filter function to filter a collection by the given key * Use like: * $theCollection->map( getFilterFor( 'my-key' )->using( santatizeString() )->defaultTo( '' ) ) * This will filter the collection item with a key of 'my-key' using the 'FILTER_SANITIZE_STRING'. * If the key doesn't exist it defaults to an empty string. * * defaultTo can be a value or a callable that returns a value * * @param string $key * * @return _Filter */ function getFilterFor( $key ) { return new _Filter( $key ); } /** * Returns a function of the defined type that can then be used to map * over a variable. * * @param int $filter - Filter type same as for php filter_var function * * @return \Closure */ function filterVar( $filter ) { return function ( $var ) use ( $filter ) { return filter_var( $var, $filter ); }; } /** * returns a function that will sanitize string. * @return \Closure */ function sanitizeString() { return function ( $var ) { return htmlspecialchars( strip_tags( $var ) ); }; } /** * Returns a validator function to filter a collection by the given key * Use like: * map( getValidatorFor( 'my-key' )->using( Logic::isNotNull() )->error( 'It was false' ) ), $myCollection) * This will run the validator on the collection item with a key of 'my-key' and return Either::Right * containing the given collection or Either::Left containing the error depending if the supplied * using function returns true or false * * error can be a value or a callable that returns a value * * @param string $key * * @return _Validator */ function getValidatorFor( $key ) { return new _Validator( $key ); } core/traits/Curryable.php 0000755 00000004605 14717633353 0011467 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use BadMethodCallException; use Closure; trait Curryable { /** * The registered string curried methods. * * @var string[] */ protected static $curried = []; /** * Register a custom curried function. * * @param string $name * @param int $argCount * @param callable $fn * * @return void */ public static function curryN( $name, $argCount, callable $fn ) { static::$curried[ $name ] = [ $argCount, $fn ]; } /** * Checks if curried function is registered. * * @param string $name * * @return bool */ public static function hasCurry( $name ) { return isset( static::$curried[ $name ] ); } /** * Dynamically handle calls to the class. * * @param string $method * @param mixed[] $parameters * * @return mixed * * @throws \BadMethodCallException */ public static function __callStatic( $method, $parameters ) { if ( ! static::hasCurry( $method ) ) { throw new BadMethodCallException( "Method {$method} does not exist." ); } if ( static::$curried[ $method ][1] instanceof Closure ) { return call_user_func_array( self::curryItStaticCall( ...static::$curried[ $method ] ), $parameters ); } return call_user_func_array( static::$curried[ $method ][1], $parameters ); } /** * Dynamically handle calls to the class. * * @param string $method * @param mixed[] $parameters * * @return mixed * * @throws \BadMethodCallException */ public function __call( $method, $parameters ) { throw new BadMethodCallException( "Curryable does not support methods in object scope. This is a limitation of PHP 5.x." ); if ( ! static::hasCurry( $method ) ) { throw new BadMethodCallException( "Method {$method} does not exist." ); } if ( static::$curried[ $method ][1] instanceof Closure ) { return call_user_func_array( $this->curryItCall( ...self::$curried[ $method ] ), $parameters ); } return call_user_func_array( static::$curried[ $method ][1], $parameters ); } /** * @param int $count * @param \Closure $fn * * @return \Closure */ private function curryItCall( $count, Closure $fn ) { return curryN( $count, $fn->bindTo( $this, static::class ) ); } /** * @param int $count * @param \Closure $fn * * @return \Closure */ private static function curryItStaticCall( $count, Closure $fn ) { return curryN( $count, Closure::bind( $fn, null, static::class ) ); } } core/Either.php 0000755 00000012520 14717633353 0007444 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\Collect\Support\Traits\Macroable; use WPML\INSTALLER\FP\Functor\Functor; use WPML\INSTALLER\FP\Functor\Pointed; /** * Class Either * @package WPML\INSTALLER\FP * * @method static callable|Right of( ...$value ) - Curried :: a → Right a * * @method static callable|Left left( ...$value ) - Curried :: a → Left a * * @method static callable|Right right( ...$value ) - Curried :: a → Right a * * @method static callable|Left|Right fromNullable( ...$value ) - Curried :: a → Either a * * @method static callable|Left|Right fromBool( ...$value ) - Curried :: a → Either a * * @method static Either tryCatch( ...$fn ) - Curried :: a → Either a * * @method static mixed getOrElse( ...$other ) */ abstract class Either { use Functor; use Macroable; /** * @return void */ public static function init() { self::macro( 'of', Right::of() ); self::macro( 'left', Left::of() ); self::macro( 'right', Right::of() ); self::macro( 'fromNullable', curryN( 1, function ( $value ) { return is_null( $value ) ? self::left( $value ) : self::right( $value ); } ) ); self::macro( 'fromBool', curryN( 1, function ( $value ) { return (bool) $value ? self::right( $value ) : self::left( $value ); } ) ); } /** * @return Either */ public function join() { if ( ! $this->value instanceof Either ) { return $this; } return $this->value->join(); } /** * @param callable $fn * * @return mixed */ abstract public function chain( callable $fn ); /** * @param callable $leftFn * @param callable $rightFn * * @return Either|Left|Right */ abstract public function bichain( callable $leftFn, callable $rightFn ); /** * @param callable $fn * * @return mixed */ abstract public function orElse( callable $fn ); abstract public function bimap( callable $leftFn, callable $rightFn ); abstract public function coalesce( callable $leftFn, callable $rightFn ); abstract public function alt( Either $alt ); abstract public function filter( callable $fn ); } class Left extends Either { use ConstApplicative; use Pointed; /** * @param callable $fn * * @return Either */ public function map( callable $fn ) { return $this; // noop } public function bimap( callable $leftFn, callable $rightFn ) { return Either::left( $leftFn( $this->value ) ); } /** * @param callable $leftFn * @param callable $rightFn * * @return Right */ public function coalesce( callable $leftFn, callable $rightFn ) { return Either::of( $leftFn( $this->value ) ); } /** * @return void * @throws \Exception */ public function get() { throw new \Exception( "Can't extract the value of Left" ); } /** * @param mixed $other * * @return mixed */ public function getOrElse( $other ) { return $other; } /** * @param callable $fn * * @return Right */ public function orElse( callable $fn ) { return Either::right( $fn( $this->value ) ); } /** * @param callable $fn * * @return Either */ public function chain( callable $fn ) { return $this; } /** * @param callable $leftFn * @param callable $rightFn * * @return Either|Left|Right */ public function bichain( callable $leftFn, callable $rightFn ) { return $leftFn( $this->value ); } /** * @param mixed $value * * @return void * @throws \Exception */ public function getOrElseThrow( $value ) { throw new \Exception( $value ); } /** * @param callable $fn * * @return Either */ public function filter( callable $fn ) { return $this; } /** * @param callable $fn * * @return Either */ public function tryCatch( callable $fn ) { return $this; // noop } public function alt( Either $alt ) { return $alt; } } class Right extends Either { use Applicative; use Pointed; /** * @param callable $fn * * @return Either */ public function map( callable $fn ) { return Either::of( $fn( $this->value ) ); } public function bimap( callable $leftFn, callable $rightFn ) { return $this->map( $rightFn ); } /** * @param callable $leftFn * @param callable $rightFn * * @return Right */ public function coalesce( callable $leftFn, callable $rightFn ) { return $this->map( $rightFn ); } /** * @param Either $other * * @return mixed */ public function getOrElse( $other ) { return $this->value; } /** * @param callable $fn * * @return Either */ public function orElse( callable $fn ) { return $this; } /** * @param mixed $value * * @return mixed */ public function getOrElseThrow( $value ) { return $this->value; } /** * @param callable $fn * * @return Either */ public function chain( callable $fn ) { return $this->map( $fn )->join(); } /** * @param callable $leftFn * @param callable $rightFn * * @return Either|Left|Right */ public function bichain( callable $leftFn, callable $rightFn ) { return $rightFn( $this->value ); } /** * @param callable $fn * * @return Either */ public function filter( callable $fn ) { return Logic::ifElse( $fn, Either::right(), Either::left(), $this->value ); } /** * @param callable $fn * * @return Either */ public function tryCatch( callable $fn ) { return tryCatch( function () use ( $fn ) { return $fn( $this->value ); } ); } public function alt( Either $alt ) { return $this; } } Either::init(); core/Undefined.php 0000755 00000000350 14717633353 0010123 0 ustar 00 <?php namespace WPML\INSTALLER\FP; /** * Class Undefined * @package WPML\INSTALLER\FP * * Class represents Undefined value. It let us handle correctly expected, but falsy values like null, 0 or false. */ class Undefined { } core/Cast.php 0000755 00000000761 14717633353 0007122 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\Collect\Support\Traits\Macroable; /** * @method static callable|bool toBool( mixed ...$v ) - Curried :: mixed->bool * @method static callable|int toInt( mixed ...$v ) - Curried :: mixed->int */ class Cast { use Macroable; public static function init() { self::macro( 'toBool', curryN( 1, function ( $v ) { return (bool) $v; } ) ); self::macro( 'toInt', curryN( 1, function ( $v ) { return intval( $v ); } ) ); } } Cast::init(); core/Logic.php 0000755 00000012054 14717633353 0007263 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\Collect\Support\Traits\Macroable; /** * @method static callable|bool not( mixed ...$v ) - Curried :: mixed->bool * @method static callable|bool isNotNull( mixed ...$v ) - Curried :: mixed->bool * @method static callable|mixed ifElse( ...$predicate, ...$first, ...$second, ...$data ) - Curried :: ( a->bool )->callable->callable->callable * @method static callable when( ...$predicate, ...$fn ) - Curried :: ( a->bool )->callable->callable * @method static callable unless( ...$predicate, ...$fn ) - Curried :: ( a->bool )->callable->callable * @method static callable cond( ...$conditions, ...$fn ) - Curried :: [( a->bool ), callable]->callable * @method static callable both( ...$a, ...$b, ...$data ) - Curried :: ( a → bool ) → ( a → bool ) → a → bool * @method static callable|bool allPass( ...$predicates, ...$data ) - Curried :: [( *… → bool )] → ( *… → bool ) * @method static callable|bool anyPass( ...$predicates, ...$data ) - Curried :: [( *… → bool )] → ( *… → bool ) * @method static callable complement( ...$fn ) - Curried :: ( *… → * ) → ( *… → bool ) * @method static callable|mixed defaultTo( ...$a, ...$b ) - Curried :: a → b → a | b * @method static callable|bool either( ...$a, ...$b ) - Curried :: ( *… → bool ) → ( *… → bool ) → ( *… → bool ) * @method static callable|mixed until ( ...$predicate, ...$transform, ...$data ) - Curried :: ( a → bool ) → ( a → a ) → a → a * @method static callable|bool propSatisfies( ...$predicate, ...$prop, ...$data ) - Curried :: ( a → bool ) → String → [String => a] → bool * @method static callable|bool isArray ( ...$a ) - Curried :: a → bool * @method static callable|bool isMappable ( ...$a ) - Curried :: a → bool * @method static callable|bool isEmpty( ...$a ) - Curried:: a → bool * @method static callable|mixed firstSatisfying( ...$predicate, ...$functions, ...$data ) - Curried:: callable->callable[]->mixed->mixed * @method static callable|bool isTruthy( ...$data ) - Curried:: mixed->bool */ class Logic { use Macroable; /** * @return void */ public static function init() { self::macro( 'not', curryN( 1, function ( $v ) { return ! Fns::value( $v ); } ) ); self::macro( 'isNotNull', curryN( 1, pipe( 'is_null', self::not() ) ) ); self::macro( 'ifElse', curryN( 4, function ( callable $predicate, callable $first, callable $second, $data ) { return $predicate( $data ) ? $first( $data ) : $second( $data ); } ) ); self::macro( 'when', curryN( 3, function ( callable $predicate, callable $fn, $data ) { return $predicate( $data ) ? $fn( $data ) : $data; } ) ); self::macro( 'unless', curryN( 3, function ( callable $predicate, callable $fn, $data ) { return $predicate( $data ) ? $data : $fn( $data ); } ) ); self::macro( 'cond', curryN( 2, function ( array $conditions, $data ) { foreach ( $conditions as $condition ) { if ( $condition[0]( $data ) ) { return $condition[1]( $data ); } } } ) ); self::macro( 'both', curryN( 3, function ( callable $a, callable $b, $data ) { return $a( $data ) && $b( $data ); } ) ); self::macro( 'allPass', curryN( 2, function ( array $predicates, $data ) { foreach ( $predicates as $predicate ) { if ( ! $predicate( $data ) ) { return false; } } return true; } ) ); self::macro( 'anyPass', curryN( 2, function ( array $predicates, $data ) { foreach ( $predicates as $predicate ) { if ( $predicate( $data ) ) { return true; } } return false; } ) ); self::macro( 'complement', curryN( 1, function ( $fn ) { return pipe( $fn, self::not() ); } ) ); self::macro( 'defaultTo', curryN( 2, function ( $default, $v ) { return is_null( $v ) ? $default : $v; } ) ); self::macro( 'either', curryN( 3, function ( callable $a, callable $b, $data ) { return $a( $data ) || $b( $data ); } ) ); self::macro( 'until', curryN( 3, function ( $predicate, $transform, $data ) { while ( ! $predicate( $data ) ) { $data = $transform( $data ); } return $data; } ) ); self::macro( 'propSatisfies', curryN( 3, function ( $predicate, $prop, $data ) { return $predicate( Obj::prop( $prop, $data ) ); } ) ); self::macro( 'isArray', curryN( 1, function ( $a ) { return is_array( $a ); } ) ); self::macro( 'isMappable', curryN( 1, function ( $a ) { return self::isArray( $a ) || ( is_object( $a ) && method_exists( $a, 'map' ) ); } ) ); self::macro( 'isEmpty', curryN( 1, function ( $arg ) { if ( is_array( $arg ) || $arg instanceof \Countable ) { return count( $arg ) === 0; } return empty( $arg ); } ) ); self::macro( 'firstSatisfying', curryN( 3, function ( $predicate, array $conditions, $data ) { foreach ( $conditions as $condition ) { $res = $condition( $data ); if ( $predicate( $res ) ) { return $res; } } return null; } ) ); self::macro( 'isTruthy', curryN( 1, function ( $data ) { return $data instanceof \Countable ? count( $data ) > 0 : (bool) $data; } ) ); } } Logic::init(); core/functions.php 0000755 00000014512 14717633353 0010237 0 ustar 00 <?php namespace WPML\INSTALLER\FP; /** * Wraps the given function and returns a function that can take arguments as an array and invokes * the wrapped function with individual arguments * * @param callable $fn * * @return \Closure */ function spreadArgs( callable $fn ) { return function ( $args ) use ( $fn ) { return $fn( ...$args ); }; } /** * Wraps the given function and returns a function that can take individual arguments and invokes * the wrapped function with individual arguments gathered into an array * * @param callable $fn * * @return \Closure */ function gatherArgs( callable $fn ) { return function ( ...$args ) use ( $fn ) { return $fn( $args ); }; } /** * Returns new function which applies each given function to the result of another from right to left * compose(f, g, h)(x) is the same as f(g(h(x))) * * @param callable $f * @param callable $g * * @return callable */ function compose( callable $f, callable $g ) { $functions = func_get_args(); return function () use ( $functions ) { $args = func_get_args(); foreach ( array_reverse( $functions ) as $function ) { $args = array( call_user_func_array( $function, $args ) ); } return current( $args ); }; } /** * Returns new function which applies each given function to the result of another from left to right * pipe(f, g, h)(x) is the same as h(g(f(x))) * * @param callable $f * @param callable $g * * @return callable */ function pipe( callable $f, callable $g ) { return call_user_func_array( 'WPML\INSTALLER\FP\compose', array_reverse( func_get_args() ) ); } /** * Returns new function which will behave like $function with * predefined left arguments passed to partial * * @param callable $function * @param mixed $arg1 * * @return callable */ function partial( callable $function, $arg1 ) { $args = array_slice( func_get_args(), 1 ); return function () use ( $function, $args ) { return call_user_func_array( $function, array_merge( $args, func_get_args() ) ); }; } /** * Returns new partial function which will behave like $function with * predefined right arguments passed to partialRight * * @param callable $function * @param mixed $arg1 * * @return callable */ function partialRight( callable $function, $arg1 ) { $args = array_slice( func_get_args(), 1 ); return function () use ( $function, $args ) { return call_user_func_array( $function, array_merge( func_get_args(), $args ) ); }; } /** * @param callable $fn * * @return \Closure */ function tap( callable $fn ) { return function ( $value ) use ( $fn ) { $fn( $value ); return $value; }; } /** * @param callable $f * @param callable $g * * @return \Closure */ function either( callable $f, callable $g ) { return function ( $value ) use ( $f, $g ) { return $f( $value ) || $g( $value ); }; } /** * @param int $count * @param callable $fn * * @return \Closure */ function curryN( $count, callable $fn ) { $accumulator = function ( array $arguments ) use ( $count, $fn, &$accumulator ) { return function () use ( $count, $fn, $arguments, $accumulator ) { $oldArguments = $arguments; $arguments = array_merge( $arguments, func_get_args() ); $replacementIndex = count( $oldArguments ); for ( $i = 0; $i < $replacementIndex; $i ++ ) { if ( count( $arguments ) <= $replacementIndex ) { break; } if ( $arguments[ $i ] === Fns::__ ) { $arguments[ $i ] = $arguments[ $replacementIndex ]; unset( $arguments[ $replacementIndex ] ); $arguments = array_values( $arguments ); } } if ( ! in_array( Fns::__, $arguments, true ) && $count <= count( $arguments ) ) { return call_user_func_array( $fn, $arguments ); } return $accumulator( $arguments ); }; }; return $accumulator( [] ); } /** * @param callable $fn * @param bool $required * * @return \Closure * @throws \ReflectionException */ function curry( callable $fn, $required = true ) { if ( is_string( $fn ) && strpos( $fn, '::', 1 ) !== false ) { $reflection = new \ReflectionMethod( $fn ); } else if ( is_array( $fn ) && count( $fn ) == 2 ) { $reflection = new \ReflectionMethod( $fn[0], $fn[1] ); } else if ( is_object( $fn ) && method_exists( $fn, '__invoke' ) ) { $reflection = new \ReflectionMethod( $fn, '__invoke' ); } else { $reflection = new \ReflectionFunction( $fn ); } $count = $required ? $reflection->getNumberOfRequiredParameters() : $reflection->getNumberOfParameters(); return curryN( $count, $fn ); } /** * @param string $fnName * * @return \Closure */ function apply( $fnName ) { $args = array_slice( func_get_args(), 1 ); return function ( $container ) use ( $fnName, $args ) { return call_user_func_array( [ $container, $fnName ], $args ); }; } /** * Returns an Invoker that runs the member function. Use `with` to set the arguments * of the member function and then invoke with `()` * * eg. give Test class: * class Test { * * private $times; * * public function __construct( $times ) { * $this->times = $times; * } * * public function multiply( $x ) { * return $x * $this->times; * } * } * * $invoker = invoke( 'multiply' )->with( 10 ); * $result = $invoker( new Test( 2 ) ); // 20 * * * @param string $fnName * * @return _Invoker */ function invoke( $fnName ) { return new _Invoker( $fnName ); } /** * @param callable $fn * * @return \Closure */ function chain( callable $fn ) { return function ( $container ) use ( $fn ) { if ( method_exists( $container, 'chain' ) ) { return $container->chain( $fn ); } elseif ( method_exists( $container, 'flatMap' ) ) { return $container->flatMap( $fn ); } elseif ( method_exists( $container, 'join' ) ) { return $container->map( $fn )->join(); } else { throw new \Exception( 'chainable method not found' ); } }; } /** * @param callable $fn * * @return \Closure */ function flatMap( callable $fn ) { return chain( $fn ); } /** * @param callable $fn * * @return Either */ function tryCatch( callable $fn ) { try { return Right::of( $fn() ); } catch ( \Exception $e ) { return Either::left( $e ); } } /** * @param callable $fn * * @return \Closure */ function flip( callable $fn ) { return function () use ( $fn ) { $args = func_get_args(); if ( count( $args ) > 1 ) { $temp = $args[0]; $args[0] = $args[1]; $args[1] = $temp; } return call_user_func_array( $fn, $args ); }; } core/Obj.php 0000755 00000033524 14717633353 0006745 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\Collect\Support\Collection; use WPML\Collect\Support\Traits\Macroable; use WPML\INSTALLER\FP\Functor\ConstFunctor; use WPML\INSTALLER\FP\Functor\IdentityFunctor; /** * @method static callable|mixed prop( ...$key, ...$obj ) - Curried :: string->Collection|array|object->mixed|null * @method static callable|mixed propOr( ...$default, ...$key, ...$obj ) - Curried :: mixed->string->Collection|array|object->mixed|null * @method static callable|array props( ...$keys, ...$obj ) - Curried :: [keys] → Collection|array|object → [v] * @method static callable|array|\stdClass addProp( ...$key, ...$transformation, ...$obj ) - Curried :: string->callable->object|array->object->array * @method static callable|array|\stdClass removeProp( ...$key, ...$obj ) - Curried :: string->object|array->object->array * @method static callable|array|\stdClass renameProp( ...$key, ...$newKey, ...$obj ) - Curried :: string->string->object|array->object->array * @method static callable|mixed path( ...$path, ...$obj ) - Curried :: array->Collection|array|object->mixed|null * @method static callable|mixed pathOr( ...$default, ...$path, ...$obj ) - Curried :: mixed → array → Collection|array|object → mixed * @method static callable assoc( ...$key, ...$value, ...$item ) - Curried :: string->mixed->Collection|array|object->mixed|null * @method static callable assocPath( ...$path, ...$value, ...$item ) - Curried :: array->mixed->Collection|array|object->mixed|null * @method static callable lens( ...$getter, ...$setter ) - Curried :: callable->callable->callable * @method static callable lensProp( ...$prop ) - Curried :: string->callable * @method static callable lensPath( ...$path ) - Curried :: array->callable * @method static callable lensMapped( ...$toFunctorFn ) - Curried :: callable->callable * @method static callable lensMappedProp( ...$prop ) - Curried :: string->callable * @method static callable view( ...$lens, ...$obj ) - Curried :: callable->Collection|array|object->mixed * @method static callable set( ...$lens, ...$value, ...$obj ) - Curried :: callable->mixed->Collection|array|object->mixed * @method static callable over( ...$lens, ...$transformation, ...$obj ) - Curried :: callable->callable->Collection|array|object->mixed * @method static callable pick( ...$props, ...$obj ) - Curried :: array->Collection|array->Collection|array * @method static callable pickAll( ...$props, ...$obj ) - Curried :: array->Collection|array->Collection|array * @method static callable pickBy( ...$predicate, ...$obj ) - Curried :: ( ( v, k ) → bool ) → Collection|array->Collection|array * @method static callable project( ...$props, ...$target ) - Curried :: array->Collection|array->Collection|array * @method static callable where( array $condition ) - Curried :: [string → ( * → bool )] → bool * @method static callable|bool has( ...$prop, ...$item ) - Curried :: string → a → bool * @method static callable|mixed evolve( ...$transformations, ...$item ) - Curried :: array → array → array * * @method static callable|array objOf( ...$key, ...$value ) - Curried :: string->mixed->array * * Creates an object containing a single key:value pair. * * @method static callable|array keys( ...$obj ) - Curried :: object|array->array * * Returns * - keys if argument is an array * - public properties' names if argument is an object * - keys if argument is Collection * * ``` * $this->assertEquals( [ 0, 1, 2 ], Obj::keys( [ 'a', 'b', 'c' ] ) ); * $this->assertEquals( [ 'a', 'b', 'c' ], Obj::keys( [ 'a' => 1, 'b' => 2, 'c' => 3 ] ) ); * * $this->assertEquals( [ 0, 1, 2 ], Obj::keys( \wpml_collect( [ 'a', 'b', 'c' ] ) ) ); * $this->assertEquals( [ 'a', 'b', 'c' ], Obj::keys( \wpml_collect( [ 'a' => 1, 'b' => 2, 'c' => 3 ] ) ) ); * * $this->assertEquals( [ 'a', 'b', 'c' ], Obj::keys( (object) [ 'a' => 1, 'b' => 2, 'c' => 3 ] ) ); * ``` * * @method static callable|array values( ...$obj ) - Curried :: object|array->array * * Returns * - values if argument is an array * - public properties' values if argument is an object * - values if argument is Collection * * ``` * $this->assertEquals( [ 'a', 'b', 'c' ], Obj::values( [ 'a', 'b', 'c' ] ) ); * $this->assertEquals( [ 1, 2, 3 ], Obj::values( [ 'a' => 1, 'b' => 2, 'c' => 3 ] ) ); * * $this->assertEquals( [ 'a', 'b', 'c' ], Obj::values( \wpml_collect( [ 'a', 'b', 'c' ] ) ) ); * $this->assertEquals( [ 1, 2, 3 ], Obj::values( \wpml_collect( [ 'a' => 1, 'b' => 2, 'c' => 3 ] ) ) ); * * $this->assertEquals( [ 1, 2, 3 ], Obj::values( (object) [ 'a' => 1, 'b' => 2, 'c' => 3 ] ) ); * ``` * * @method static callable|array replaceRecursive( array ...$newValue, ...$target ) - Curried :: array->array->array * * @method static callable|array toArray( Collection|Object ...$item ) - Curried :: Collection|Object->array */ class Obj { use Macroable; /** * @return void */ public static function init() { self::macro( 'prop', curryN( 2, function ( $key, $item ) { return self::propOr( null, $key, $item ); } ) ); self::macro( 'propOr', curryN( 3, function ( $default, $key, $item ) { if ( $item instanceof Collection ) { return $item->get( $key, $default ); } if ( is_array( $item ) ) { return array_key_exists( $key, $item ) ? $item[ $key ] : $default; } if ( is_object( $item ) ) { if ( property_exists( $item, $key ) || isset( $item->$key ) ) { return $item->$key; } elseif ( is_numeric( $key ) ) { return self::propOr( $default, $key, (array) $item ); } else { return $default; } } if ( is_null( $item ) ) { return null; } throw( new \InvalidArgumentException( 'item should be a Collection or an array or an object' ) ); } ) ); self::macro( 'props', curryN( 2, function ( array $keys, $item ) { return Fns::map( Obj::prop( Fns::__, $item ), $keys ); } ) ); self::macro( 'addProp', curryN( 3, function ( $key, $transformation, $data ) { return Obj::assoc( $key, $transformation( $data ), $data ); } ) ); self::macro( 'removeProp', curryN( 2, function ( $key, $data ) { if ( is_array( $data ) ) { unset( $data[ $key ] ); return $data; } elseif ( is_object( $data ) ) { $newData = clone $data; unset( $newData->$key ); return $newData; } return $data; } ) ); self::macro( 'renameProp', curryN( 3, function ( $key, $newKey, $data ) { $data = self::addProp( $newKey, self::prop( $key ), $data ); return self::removeProp( $key, $data ); } ) ); self::macro( 'path', curryN( 2, function ( $path, $item ) { return array_reduce( $path, flip( self::prop() ), $item ); } ) ); self::macro( 'pathOr', curryN( 3, function ( $default, $path, $item ) { $result = Either::of( $item ) ->tryCatch( Obj::path( $path ) ) ->getOrElse( null ); return is_null( $result ) ? $default : $result; } ) ); self::macro( 'assocPath', curryN( 3, function ( $path, $val, $item ) { $split = [ $item ]; for ( $i = 0; $i < count( $path ) - 1; $i ++ ) { $split[] = self::prop( $path[ $i ], $split[ $i ] ); } $split = array_reverse( $split ); $reversePath = array_reverse( $path ); $split[0] = self::assoc( $reversePath[0], $val, $split[0] ); for ( $i = 1; $i < count( $reversePath ); $i ++ ) { $key = $reversePath[ $i ]; $split[ $i ] = self::assoc( $key, $split[ $i - 1 ], $split[ $i ] ); } return array_pop( $split ); } ) ); self::macro( 'assoc', curryN( 3, function ( $key, $value, $item ) { if ( $item instanceof Collection ) { $item = clone $item; return $item->put( $key, $value ); } if ( is_array( $item ) ) { $item[ $key ] = $value; return $item; } if ( is_object( $item ) ) { $item = clone $item; $item->$key = $value; return $item; } if ( is_null( $item ) ) { return null; } throw( new \InvalidArgumentException( 'item should be a Collection or an array or an object' ) ); } ) ); self::macro( 'lens', curryN( 2, function ( $getter, $setter ) { return function ( $toFunctorFn ) use ( $getter, $setter ) { return function ( $target ) use ( $toFunctorFn, $getter, $setter ) { $result = $getter( $target ); return Fns::map( function ( $focus ) use ( $setter, $target ) { return $setter( $focus, $target ); }, $toFunctorFn( $result ) ); }; }; } ) ); self::macro( 'lensProp', curryN( 1, function ( $prop ) { return self::lens( self::prop( $prop ), self::assoc( $prop ) ); } ) ); self::macro( 'lensPath', curryN( 1, function ( $path ) { return self::lens( self::path( $path ), self::assocPath( $path ) ); } ) ); self::macro( 'lensMapped', curryN( 1, function ( $toFunctorFn ) { return function ( $target ) use ( $toFunctorFn ) { return IdentityFunctor::of( Logic::isMappable( $target ) ? Fns::map( pipe( $toFunctorFn, invoke( 'get' ) ), $target ) : $target ); }; } ) ); self::macro( 'lensMappedProp', curryN( 1, function ( $prop ) { return compose( Obj::lensProp( $prop ), Obj::lensMapped() ); } ) ); self::macro( 'view', curryN( 2, function ( $lens, $obj ) { $view = $lens( [ ConstFunctor::class, 'of' ] ); return $view( $obj )->get(); } ) ); self::macro( 'set', curryN( 3, function ( $lens, $value, $obj ) { return self::over( $lens, Fns::always( $value ), $obj ); } ) ); self::macro( 'over', curryN( 3, function ( $lens, $transformation, $obj ) { $over = $lens( function ( $value ) use ( $transformation ) { return IdentityFunctor::of( $transformation( $value ) ); } ); return $over( $obj )->get(); } ) ); self::macro( 'pick', curryN( 2, function ( array $props, $item ) { $find = curryN( 3, function ( $item, $result, $prop ) { $value = self::propOr( new Undefined(), $prop, $item ); if ( ! $value instanceof Undefined ) { $result[ $prop ] = $value; } return $result; } ); $result = Fns::reduce( $find( $item ), [], $props ); return self::matchType( $result, $item ); } ) ); self::macro( 'pickAll', curryN( 2, function ( array $props, $item ) { $find = curryN( 3, function ( $item, $result, $prop ) { $result[ $prop ] = self::prop( $prop, $item ); return $result; } ); $result = Fns::reduce( $find( $item ), [], $props ); return self::matchType( $result, $item ); } ) ); self::macro( 'project', curryN( 2, function ( array $props, $items ) { return Fns::map( Obj::pick( $props ), $items ); } ) ); self::macro( 'where', curryN( 2, function ( array $conditions, $items ) { foreach ( $conditions as $prop => $condition ) { $filter = pipe( Obj::prop( $prop ), Logic::both( Logic::isNotNull(), $condition ) ); $items = Fns::filter( $filter, $items ); } return $items; } ) ); self::macro( 'pickBy', curryN( 2, function ( callable $predicate, $item ) { $result = array_filter( self::toArray( $item ), $predicate, ARRAY_FILTER_USE_BOTH ); return self::matchType( $result, $item ); } ) ); self::macro( 'has', curryN( 2, function ( $prop, $item ) { if ( $item instanceof Collection ) { return $item->has( $prop ); } if ( is_array( $item ) ) { return isset( $item[ $prop ] ); } if ( is_object( $item ) ) { return property_exists( $item, $prop ); } throw( new \InvalidArgumentException( 'item should be a Collection or an array or an object' ) ); } ) ); self::macro( 'evolve', curryN( 2, function ( $transformations, $item ) { $temp = self::toArray( $item ); foreach ( $transformations as $prop => $transformation ) { if ( isset( $temp[ $prop ] ) ) { if ( is_callable( $transformation ) ) { $temp[ $prop ] = $transformation( $temp[ $prop ] ); } elseif ( is_array( $transformation ) ) { $temp[ $prop ] = self::evolve( $transformation, $temp[ $prop ] ); } } } return self::matchType( $temp, $item ); } ) ); self::macro( 'keys', curryN( 1, function ( $obj ) { if ( is_array( $obj ) ) { return array_keys( $obj ); } elseif ( $obj instanceof Collection ) { return $obj->keys()->toArray(); } elseif ( is_object( $obj ) ) { return array_keys( get_object_vars( $obj ) ); } throw new \InvalidArgumentException( 'obj should be either array or object' ); } ) ); self::macro( 'values', curryN( 1, function ( $obj ) { if ( is_array( $obj ) ) { return array_values( $obj ); } elseif ( $obj instanceof Collection ) { return $obj->values()->toArray(); } elseif ( is_object( $obj ) ) { return array_values( get_object_vars( $obj ) ); } throw new \InvalidArgumentException( 'obj should be either array or object' ); } ) ); self::macro( 'objOf', curryN( 2, function ( $key, $value ) { return [ $key => $value ]; } ) ); self::macro( 'replaceRecursive', curryN( 2, flip( 'array_replace_recursive' ) ) ); self::macro( 'toArray', curryN( 1, function ( $item ) { $temp = $item; if ( $temp instanceof Collection ) { $temp = $temp->toArray(); } if ( is_object( $temp ) ) { $temp = (array) $temp; } return $temp; } ) ); } /** * @param object|Collection $item * @param object|Collection $reference * * @return object|Collection */ private static function matchType( $item, $reference ) { if ( $reference instanceof Collection ) { return wpml_collect( $item ); } if ( is_object( $reference ) ) { return (object) $item; } return $item; } /** * Curried :: mixed → array|object|Collection → array|object|Collection * function to remove an item by key from an array. * * @param string|int $key * @param array|object|Collection|null $item * * @return callable|array|object|Collection */ static function without( $key = null, $item = null ) { $without = function ( $key, $item ) { $temp = self::toArray( $item ); unset( $temp[ $key ] ); return self::matchType( $temp, $item ); }; return call_user_func_array( curryN( 2, $without ), func_get_args() ); } } Obj::init(); core/Debug.php 0000755 00000002623 14717633353 0007255 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\Collect\Support\Traits\Macroable; /** * @method static callable|mixed inspect( mixed ...$input ) * @method static callable|mixed log( string ...$label ) * @method static callable|mixed logDump( string ...$label, mixed ...$input ) * @method static callable|mixed logPrintR( string ...$label, mixed ...$input ) * @method static callable|mixed logBacktrace( string ...$label, mixed ...$input ) */ class Debug { use Macroable; public static function init() { self::macro( 'inspect', curryN( 1, function ( $input ) { $allParameters = func_get_args(); function_exists( 'xdebug_break' ) && xdebug_break(); return $input; } ) ); self::macro( 'log', curryN( 2, function ( $string, $input ) { error_log( $string ); return $input; } ) ); self::macro( 'logDump', curryN( 2, function ( $label, $input ) { ob_start(); var_dump( $input ); $dumpInput = ob_get_clean(); error_log( $label . ': ' . $dumpInput ); return $input; } ) ); self::macro( 'logPrintR', curryN( 2, function ( $label, $input ) { error_log( $label . ': ' . print_r( $input, true ) ); return $input; } ) ); self::macro( 'logBacktrace', curryN( 2, function ( $label, $input ) { ob_start(); debug_print_backtrace(); $backtrace = ob_get_clean(); error_log( $label . ': ' . PHP_EOL . $backtrace ); return $input; } ) ); } } Debug::init(); core/strings_functions.php 0000755 00000002231 14717633353 0012003 0 ustar 00 <?php namespace WPML\INSTALLER\FP\Strings; use function WPML\INSTALLER\FP\partial; use function WPML\INSTALLER\FP\partialRight; use function WPML\INSTALLER\FP\pipe; /** * ltrimWith :: string -> ( string -> string ) * * @param string $trim * * @return callable */ function ltrimWith( $trim ) { return partialRight( 'ltrim', $trim ); } /** * rtrimWith :: string -> ( string -> string ) * * @param string $trim * * @return callable */ function rtrimWith( $trim ) { return partialRight( 'rtrim', $trim ); } /** * explodeToCollection :: string -> ( string -> Collection ) * * @param string $delimiter * * @return callable */ function explodeToCollection( $delimiter ) { return pipe( partial( 'explode', $delimiter ), 'wpml_collect' ); } /** * replace :: string -> string -> ( string -> string ) * * @param string $search * @param string $replace * * @return callable */ function replace( $search, $replace ) { return partial( 'str_replace', $search, $replace ); } /** * remove :: string -> ( string -> string ) * * @param string $remove * * @return callable */ function remove( $remove ) { return partial( 'str_replace', $remove, '' ); } core/Functor/Pointed.php 0000755 00000000715 14717633353 0011251 0 ustar 00 <?php namespace WPML\INSTALLER\FP\Functor; use function WPML\INSTALLER\FP\curryN; trait Pointed { /** * of :: a -> M a * * Curried function that returns an instance of the derived class * * @param mixed $value (optional) * * @return mixed|callable */ public static function of( $value = null ) { $of = function ( $value ) { return new static( $value ); }; return call_user_func_array( curryN( 1, $of ), func_get_args() ); } } core/Functor/ConstFunctor.php 0000755 00000000335 14717633353 0012274 0 ustar 00 <?php namespace WPML\INSTALLER\FP\Functor; class ConstFunctor { use Functor; use Pointed; /** * @param callable $callback * * @return ConstFunctor */ public function map( $callback ) { return $this; } } core/Functor/Functor.php 0000755 00000000647 14717633353 0011273 0 ustar 00 <?php namespace WPML\INSTALLER\FP\Functor; trait Functor { /** @var mixed */ protected $value; /** * @param mixed $value */ public function __construct( $value ) { $this->value = $value; } /** * @return mixed */ public function get() { return $this->value; } /** * @param callable $callback * * @return \WPML\INSTALLER\FP\Either */ abstract public function map( callable $callback ); } core/Functor/ConstApplicative.php 0000755 00000000276 14717633353 0013121 0 ustar 00 <?php namespace WPML\INSTALLER\FP; trait ConstApplicative { /** * @param mixed $otherContainer * * @return mixed */ public function ap( $otherContainer ) { return $this; } } core/Functor/IdentityFunctor.php 0000755 00000000403 14717633353 0012773 0 ustar 00 <?php namespace WPML\INSTALLER\FP\Functor; class IdentityFunctor { use Functor; use Pointed; /** * @param callable $callback * * @return IdentityFunctor */ public function map( $callback ) { return new self( $callback( $this->get() ) ); } } core/Functor/Applicative.php 0000755 00000000330 14717633353 0012101 0 ustar 00 <?php namespace WPML\INSTALLER\FP; trait Applicative { /** * @param mixed $otherContainer * * @return mixed */ public function ap( $otherContainer ) { return $otherContainer->map( $this->value ); } } core/Strings.php 0000755 00000007431 14717633353 0007662 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\Collect\Support\Traits\Macroable; /** * @method static string tail( string ...$str ) - Curried :: string->string * @method static array split( ...$delimiter, ...$str ) - Curried :: string->string->string * @method static callable|bool includes( ...$needle, ...$str ) - Curried :: string → string → bool * @method static callable|string trim( ...$trim, ...$str ) - Curried :: string → string → string * @method static callable|string concat( ...$a, ...$b ) - Curried :: string → string → string * @method static callable|string sub( ...$start, ...$str ) - Curried :: int → string → string * @method static callable|string startsWith( ...$test, ...$str ) - Curried :: string → string → bool * @method static callable|string endsWith( ...$test, ...$str ) - Curried :: string → string → bool * @method static callable|string pos( ...$test, ...$str ) - Curried :: string → string → int * @method static callable|string len( ...$str ) - Curried :: string → int * @method static callable|string replace( ...$find, ...$replace, ...$str ) - Curried :: string → string → string → string * @method static callable|string pregReplace( ...$pattern, ...$replace, ...$str ) - Curried :: string → string → string → string * @method static callable|string match( ...$pattern, ...$str ) - Curried :: string → string → array * @method static callable|string matchAll( ...$pattern, ...$str ) - Curried :: string → string → array * @method static callable|string wrap( ...$before, ...$after, ...$str ) - Curried :: string → string → string * @method static callable|string toUpper( string ...$str ) - Curried :: string → string * @method static callable|string toLower( string ...$str ) - Curried :: string → string * * Wraps a string inside 2 other strings * * ``` * $wrapsInDiv = Str::wrap( '<div>', '</div>' ); * $wrapsInDiv( 'To be wrapped' ); // '<div>To be wrapped</div>' * ``` * */ class Str { use Macroable; /** * @return void */ public static function init() { self::macro( 'split', curryN( 2, 'explode' ) ); self::macro( 'trim', curryN( 2, flip( 'trim' ) ) ); self::macro( 'concat', curryN( 2, function ( $a, $b ) { return $a . $b; } ) ); self::macro( 'sub', curryN( 2, flip( function_exists( 'mb_substr' ) ? 'mb_substr' : 'substr' ) ) ); self::macro( 'tail', self::sub( 1 ) ); self::macro( 'pos', curryN( 2, flip( function_exists( 'mb_strpos' ) ? 'mb_strpos' : 'strpos' ) ) ); self::macro( 'startsWith', curryN( 2, pipe( self::pos(), Relation::equals( 0 ) ) ) ); self::macro( 'endsWith', curryN( 2, function ( $find, $s ) { return self::sub( - self::len( $find ), $s ) === $find; } ) ); self::macro( 'includes', curryN( 2, pipe( self::pos(), Logic::complement( Relation::equals( false ) ) ) ) ); self::macro( 'len', curryN( 1, function_exists( 'mb_strlen' ) ? 'mb_strlen' : 'strlen' ) ); self::macro( 'replace', curryN( 3, function ( $search, $replace, $subject ) { return str_replace( $search, $replace, $subject ); } ) ); self::macro( 'pregReplace', curryN( 3, function ( $pattern, $replace, $subject ) { return preg_replace( $pattern, $replace, $subject ); } ) ); self::macro( 'match', curryN( 2, function ( $pattern, $subject ) { $matches = []; return preg_match( $pattern, $subject, $matches ) ? $matches : []; } ) ); self::macro( 'matchAll', curryN( 2, function ( $pattern, $subject ) { $matches = []; return preg_match_all( $pattern, $subject, $matches, PREG_SET_ORDER ) ? $matches : []; } ) ); self::macro( 'wrap', curryN( 3, function ( $before, $after, $string ) { return $before . $string . $after; } ) ); self::macro( 'toUpper', curryN( 1, 'strtoupper' ) ); self::macro( 'toLower', curryN( 1, 'strtolower' ) ); } } Str::init(); core/Invoker.php 0000755 00000001142 14717633353 0007637 0 ustar 00 <?php namespace WPML\INSTALLER\FP; class _Invoker { /** * @var string */ private $fnName; /** * @var mixed[] */ private $args = []; /** * _Invoker constructor. * * @param string $fnName */ public function __construct( $fnName ) { $this->fnName = $fnName; } /** * @param mixed ...$args * * @return _Invoker */ public function with( ...$args ) { $this->args = $args; return $this; } /** * @param mixed $instance * * @return mixed */ public function __invoke( $instance ) { return call_user_func_array( [ $instance, $this->fnName ], $this->args ); } } core/Wrapper.php 0000755 00000001655 14717633353 0007653 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\INSTALLER\FP\Functor\Functor; use WPML\INSTALLER\FP\Functor\Pointed; class Wrapper { use Functor; use Pointed; /** * @param callable $fn * * @return Wrapper */ public function map( callable $fn ) { return self::of( $fn( $this->value ) ); } /** * @param callable $fn * * @return mixed|null */ public function filter( $fn = null ) { $fn = $fn ?: Fns::identity(); return $fn( $this->value ) ? $this->value : null; } /** * @return mixed */ public function join() { if ( ! $this->value instanceof Wrapper ) { return $this->value; } return $this->value->join(); } /** * @param mixed $value * * @return Wrapper */ public function ap( $value ) { return self::of( call_user_func( $this->value, $value ) ); // $this->value should be callable and curried } /** * @return mixed */ public function get() { return $this->value; } } core/Validator.php 0000755 00000001754 14717633353 0010160 0 ustar 00 <?php namespace WPML\INSTALLER\FP\System; use WPML\Collect\Support\Collection; use WPML\INSTALLER\FP\Either; class _Validator { /** * @var string */ private $key; /** * @var callable */ private $fn; /** * @var string */ private $error; /** * _Validator constructor. * * @param string $key */ public function __construct( $key ) { $this->key = $key; } /** * @param callable $fn * * @return _Validator */ public function using( callable $fn ) { $this->fn = $fn; return $this; } /** * @param string $error * * @return _Validator */ public function error( $error ) { $this->error = $error; return $this; } /** * @param \WPML\Collect\Support\Collection<mixed> $collection * * @return callable|\WPML\INSTALLER\FP\Either */ public function __invoke( Collection $collection ) { return call_user_func( $this->fn, $collection->get( $this->key ) ) ? Either::right( $collection ) : Either::left( value( $this->error ) ); } } core/Maybe.php 0000755 00000011015 14717633353 0007257 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\Collect\Support\Traits\Macroable; use WPML\INSTALLER\FP\Functor\Functor; use WPML\INSTALLER\FP\Functor\Pointed; /** * Class Maybe * @package WPML\INSTALLER\FP * @method static callable|Just|Nothing fromNullable( ...$value ) - Curried :: a → Nothing | Just a * * if $value is null or false it returns a Nothing otherwise returns a Just containing the value * * @method static callable safe( ...$fn ) - Curried :: ( a → b ) → ( a → Maybe b ) * * returns a function that when called will run the passed in function and put the result into a Maybe * * @method static callable safeAfter( ...$predicate, ...$fn ) - Curried :: ( b → bool ) → ( a → b ) → ( a → Maybe b ) * * returns a function that when called will run the passed in function and pass the result of the function * to the predicate. If the predicate returns true the result will be a Just containing the result of the function. * Otherwise it returns a Nothing if the predicate returns false. * * @method static callable safeBefore( ...$predicate, ...$fn ) - Curried :: ( a → bool ) → ( a → b ) → ( a → Maybe b ) * * returns a function that when called will pass the given value to the predicate. * If the predicate returns true the value will be lifted into a Just instance and * the passed in function will then be mapped. * Otherwise it returns a Nothing if the predicate returns false. * * @method static callable|Just just( ...$value ) - Curried :: a → Just a * * returns a Just containing the value. * * @method static callable|Just of( ...$value ) - Curried :: a → Just a * * returns a Just containing the value. * */ class Maybe { use Macroable; /** * @return void */ public static function init() { self::macro( 'just', Just::of() ); self::macro( 'of', Just::of() ); self::macro( 'fromNullable', curryN( 1, function ( $value ) { return is_null( $value ) || $value === false ? self::nothing() : self::just( $value ); } ) ); Maybe::macro( 'safe', curryN( 1, function ( $fn ) { return pipe( $fn, self::fromNullable() ); } ) ); Maybe::macro( 'safeAfter', curryN( 2, function ( $predicate, $fn ) { return pipe( $fn, Logic::ifElse( $predicate, self::just(), [ self::class, 'nothing' ] ) ); } ) ); Maybe::macro( 'safeBefore', curryN( 2, function ( $predicate, $fn ) { return pipe( Logic::ifElse( $predicate, self::just(), [ self::class, 'nothing' ] ), Fns::map( $fn ) ); } ) ); } /** * @return Nothing */ public static function nothing() { return new Nothing(); } /** * @return bool */ public function isNothing() { return false; } /** * @return bool */ public function isJust() { return false; } } class Just extends Maybe { use Functor; use Pointed; use Applicative; /** * @param callable $fn * * @return Just|Nothing */ public function map( callable $fn ) { return Maybe::fromNullable( $fn( $this->value ) ); } /** * @param mixed $other * * @return mixed */ public function getOrElse( $other ) { return $this->value; } /** * @param callable $fn * * @return Just|Nothing */ public function filter( $fn = null ) { $fn = $fn ?: Fns::identity(); return Maybe::fromNullable( $fn( $this->value ) ? $this->value : null ); } /** * @param callable $fn * * @return Just|Nothing */ public function reject( $fn = null ) { $fn = $fn ?: Fns::identity(); return $this->filter( Logic::complement( $fn ) ); } /** * @param callable $fn * * @return mixed */ public function chain( callable $fn ) { return $fn( $this->value ); } /** * @return bool */ public function isJust() { return true; } } class Nothing extends Maybe { use ConstApplicative; /** * @param callable $fn * * @return Nothing */ public function map( callable $fn ) { return $this; } /** * @return void * @throws \Exception */ public function get() { throw new \Exception( "Can't extract the value of Nothing" ); } /** * @param mixed|callable $other * * @return mixed */ public function getOrElse( $other ) { return value( $other ); } /** * @param callable $fn * * @return Nothing */ public function filter( callable $fn ) { return $this; } /** * @param callable $fn * * @return Nothing */ public function reject( callable $fn ) { return $this; } /** * @param callable $fn * * @return Nothing */ public function chain( callable $fn ) { return $this; } /** * @return bool */ public function isNothing() { return true; } } Maybe::init(); core/Math.php 0000755 00000001335 14717633353 0007117 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\Collect\Support\Traits\Macroable; /** * @method static callable|mixed multiply( ...$a, ...$b ) - Curried :: Number → Number → Number * @method static callable|mixed add( ...$a, ...$b ) - Curried :: Number → Number → Number * @method static callable|mixed product( ...$array ) - Curried :: [Number] → Number */ class Math { use Macroable; /** * @return void */ public static function init() { self::macro( 'multiply', curryN( 2, function ( $a, $b ) { return $a * $b; } ) ); self::macro( 'add', curryN( 2, function ( $a, $b ) { return $a + $b; } ) ); self::macro( 'product', curryN( 1, Fns::reduce( self::multiply(), 1 ) ) ); } } Math::init(); core/FP.php 0000755 00000003532 14717633353 0006534 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\Collect\Support\Traits\Macroable; /** * @deprecated Use Fn instead * * @method static callable|mixed map( callable ...$fn, mixed ...$target ) - Curried :: ( a->b )->f a->f b * @method static callable|mixed identity( mixed ...$data ) - Curried :: a->a * @method static callable|mixed always( ...$a, ...$b ) - Curried :: a->b->a * @method static callable|mixed reduce( ...$fn, ...$initial, ...$target ) - Curried :: ( ( a, b ) → a ) → a → [b] → a * @method static callable\mixed converge( ...$convergingFn, ...$branchingFns, ...$data ) - Curried :: callable->[callable]->mixed->callable */ class FP { use Macroable; /** * @return void */ public static function init() { self::macro( 'map', curryN( 2, function ( $fn, $target ) { if ( is_object( $target ) ) { return $target->map( $fn ); } if ( is_array( $target ) ) { return array_map( $fn, $target ); } throw( new \InvalidArgumentException( 'target should be an object with map method or an array' ) ); } ) ); self::macro( 'identity', curryN( 1, function ( $value ) { return $value; } ) ); self::macro( 'always', curryN( 2, function ( $value, $_ ) { return $value; } ) ); self::macro( 'reduce', curryN( 3, function ( $fn, $initial, $target ) { if ( is_object( $target ) ) { return $target->reduce( $fn, $initial ); } if ( is_array( $target ) ) { return array_reduce( $target, $fn, $initial ); } throw( new \InvalidArgumentException( 'target should be an object with reduce method or an array' ) ); } ) ); self::macro( 'converge', curryN( 3, function ( $convergingFn, array $branchingFns, $data ) { $apply = function ( $fn ) use ( $data ) { return $fn( $data ); }; return call_user_func_array( $convergingFn, self::map( $apply, $branchingFns ) ); } ) ); } } FP::init(); core/Fns.php 0000755 00000036342 14717633353 0006762 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\Collect\Support\Traits\Macroable; /** * @method static callable always( ...$a ) Curried :: a → ( * → a ) * * Returns a function that always returns the given value. * * ```php * $t = Fns::always( 'Tee' ); * $t(); //=> 'Tee' * ``` * * @method static callable converge( ...$convergingFn, ...$branchingFns ) - Curried :: ( ( x1, x2, … ) → z ) → [( ( a, b, … ) → x1 ), ( ( a, b, … ) → x2 ), …] → ( a → b → … → z ) * * Accepts a converging function and a list of branching functions and returns a new function. The arity of the new function is the same as the arity of the longest branching function. When invoked, this new function is applied to some arguments, and each branching function is applied to those same arguments. The results of each branching function are passed as arguments to the converging function to produce the return value. * * ```php * $divide = curryN( 2, function ( $num, $dom ) { return $num / $dom; } ); * $sum = function ( Collection $collection ) { return $collection->sum(); }; * $length = function ( Collection $collection ) { return $collection->count(); }; * * $average = Fns::converge( $divide, [ $sum, $length ] ); * $this->assertEquals( 4, $average( wpml_collect( [ 1, 2, 3, 4, 5, 6, 7 ] ) ) ); * ``` * * @method static callable|mixed map( ...$fn, ...$target ) - Curried :: ( a→b )→f a→f b * * Takes a function and a *functor*, applies the function to each of the functor's values, and returns a functor of the same shape. * * And array is considered a *functor* * * Dispatches to the *map* method of the second argument, if present * * @method static callable|mixed each ( ...$fn, ...$target ) - Curried :: ( a→b )→f a→f b * @method static callable|mixed identity( mixed ...$data ) - Curried :: a->a * @method static callable|mixed tap( callable ...$fn, mixed ...$data ) - Curried :: fn->data->data * @method static callable|mixed reduce( ...$fn, ...$initial, ...$target ) - Curried :: ( ( a, b ) → a ) → a → [b] → a * @method static callable|mixed reduceRight( ...$fn, ...$initial, ...$target ) - Curried :: ( ( a, b ) → a ) → a → [b] → a * * Takes a function, an initial value and an array and returns the result. * * The function receives two values, the accumulator and the current value, and should return a result. * * The array values are passed to the function in the reverse order. * * ```php * $numbers = [ 1, 2, 3, 4, 5, 8, 19 ]; * * $append = function( $acc, $val ) { * $acc[] = $val; * return $acc; * }; * * $reducer = Fns::reduceRight( $append, [] ); * $result = $reducer( $numbers ); // [ 19, 8, 5, 4, 3, 2, 1 ] * * // Works on collections too. * $result = $reducer( wpml_collect( $numbers ) ); // [ 19, 8, 5, 4, 3, 2, 1 ] * ``` * * @method static callable|mixed filter( ...$predicate, ...$target ) - Curried :: ( a → bool ) → [a] → [a] * @method static callable|mixed reject( ...$predicate, ...$target ) - Curried :: ( a → bool ) → [a] → [a] * @method static callable|mixed value( mixed ...$data ) - Curried :: a|( *→a ) → a * @method static callable|object constructN( ...$argCount, ...$className ) - Curried :: int → string → object * @method static callable|int ascend( ...$fn, ...$a, ...$b ) - Curried :: ( a → b ) → a → a → int * @method static callable|int descend( ...$fn, ...$a, ...$b ) - Curried :: ( a → b ) → a → a → int * @method static callable useWith( ...$fn, ...$transformations ) - Curried :: ( ( x1, x2, … ) → z ) → [( a → x1 ), ( b → x2 ), …] → ( a → b → … → z ) * @method static callable nthArg( ...$n ) - Curried :: int → *… → * * @method static callable|mixed either( ...$f, ...$g, ...$e ) - Curried:: ( a → b ) → ( b → c ) → Either a b → c * @method static callable|mixed maybe( ...$v, ...$f, ...$m ) - Curried:: b → ( a → b ) → Maybe a → b * @method static callable|bool isRight( ...$e ) - Curried:: e → bool * @method static callable|bool isLeft( ...$e ) - Curried:: e → bool * @method static callable|bool isJust( ...$m ) - Curried:: e → bool * @method static callable|bool isNothing( ...$m ) - Curried:: e → bool * @method static callable|mixed T( ...$_ ) - Curried :: _ → bool * @method static callable|mixed F( ...$_ ) - Curried :: _ → bool * @method static callable|Maybe safe( ...$fn ) - Curried :: ( a → b ) → ( a → Maybe b ) * @method static callable|object make( ...$className ) - Curried :: string → object * @method static callable|object makeN( ...$argCount, ...$className ) - Curried :: int → string → object * @method static callable unary( ...$fn ) - Curried:: ( * → b ) → ( a → b ) * @method static callable|mixed memorizeWith( ...$cacheKeyFn, ...$fn ) - Curried :: ( *… → String ) → ( *… → a ) → ( *… → a ) * @method static callable|mixed memorize( ...$fn ) - Curried :: ( *… → a ) → ( *… → a ) * @method static callable|mixed once( ...$fn ) - Curried :: ( *… → a ) → ( *… → a ) * @method static callable|mixed withNamedLock( ...$name, ...$returnFn, ...$fn ) - Curried :: String → ( *… → String ) → ( *… → a ) → ( *… → a ) * * Creates a new function that is *locked* so that it wont be called recursively. Multiple functions can use the same lock so they are blocked from calling each other recursively * * ```php * $lockName = 'my-lock'; * $addOne = Fns::withNamedLock( * $lockName, * Fns::identity(), * function ( $x ) use ( &$addOne ) { return $addOne( $x + 1 ); } * ); * * $this->assertEquals( 13, $addOne( 12 ), 'Should not recurse' ); * * $addTwo = Fns::withNamedLock( * $lockName, * Fns::identity(), * function ( $x ) use ( $addOne ) { return pipe( $addOne, $addOne) ( $x ); } * ); * * $this->assertEquals( 10, $addTwo( 10 ), 'Should return 10 because $addOne is locked by the same name as $addTwo' ); * ``` * * @method static callable|mixed withoutRecursion( ...$returnFn, ...$fn ) - Curried :: ( *… → String ) → ( *… → a ) → ( *… → a ) * @method static callable|mixed liftA2( ...$fn, ...$monadA, ...$monadB ) - Curried :: ( a → b → c ) → m a → m b → m c * @method static callable|mixed liftA3( ...$fn, ...$monadA, ...$monadB, ...$monadC ) - Curried :: ( a → b → c → d ) → m a → m b → m c → m d * @method static callable|mixed liftN( ...$n, ...$fn, ...$monad ) - Curried :: Number->( ( * ) → a ) → ( *m ) → m a * * @method static callable|mixed until( ...$predicate, ...$fns ) - Curried :: ( b → bool ) → [( a → b )] → a → b * * Executes consecutive functions until their $predicate($fn(...$args)) is true. When a result fulfils predicate then it is returned. * * ``` * $fns = [ * $add(1), * $add(5), * $add(10), * $add(23), * ]; * * $this->assertSame( 20, Fns::until( Relation::gt( Fns::__, 18 ), $fns )( 10 ) ); * ``` * */ class Fns { use Macroable; const __ = '__CURRIED_PLACEHOLDER__'; /** * @return void */ public static function init() { self::macro( 'always', function ( $value ) { return function () use ( $value ) { return $value; }; } ); self::macro( 'converge', curryN( 2, function ( $convergingFn, array $branchingFns ) { return function ( $data ) use ( $convergingFn, $branchingFns ) { $apply = function ( $fn ) use ( $data ) { return $fn( $data ); }; return call_user_func_array( $convergingFn, self::map( $apply, $branchingFns ) ); }; } ) ); self::macro( 'map', curryN( 2, function ( $fn, $target ) { if ( ! Logic::isMappable( $target ) ) { throw( new \InvalidArgumentException( 'target should be an object with map method or an array' ) ); } if ( is_object( $target ) ) { return $target->map( $fn ); } else { $keys = array_keys( $target ); return array_combine( $keys, array_map( $fn, $target, $keys ) ); } } ) ); self::macro( 'each', curryN( 2, function ( $fn, $target ) { return self::map( self::tap( $fn ), $target ); } ) ); self::macro( 'identity', curryN( 1, function ( $value ) { return $value; } ) ); self::macro( 'tap', curryN( 2, function ( $fn, $value ) { $fn( $value ); return $value; } ) ); self::macro( 'reduce', curryN( 3, function ( $fn, $initial, $target ) { if ( is_object( $target ) ) { return $target->reduce( $fn, $initial ); } if ( is_array( $target ) ) { return array_reduce( $target, $fn, $initial ); } throw( new \InvalidArgumentException( 'target should be an object with reduce method or an array' ) ); } ) ); self::macro( 'reduceRight', curryN( 3, function ( $fn, $initial, $target ) { if ( is_object( $target ) ) { return $target->reverse()->reduce( $fn, $initial ); } if ( is_array( $target ) ) { return array_reduce( array_reverse( $target ), $fn, $initial ); } throw( new \InvalidArgumentException( 'target should be an object with reduce method or an array' ) ); } ) ); self::macro( 'filter', curryN( 2, function ( $predicate, $target ) { if ( is_object( $target ) ) { return $target->filter( $predicate ); } if ( is_array( $target ) ) { return array_values( array_filter( $target, $predicate ) ); } throw( new \InvalidArgumentException( 'target should be an object with filter method or an array' ) ); } ) ); self::macro( 'reject', curryN( 2, function ( $predicate, $target ) { return self::filter( pipe( $predicate, Logic::not() ), $target ); } ) ); self::macro( 'value', curryN( 1, function ( $value ) { return is_callable( $value ) ? $value() : $value; } ) ); self::macro( 'constructN', curryN( 2, function ( $argCount, $className ) { $maker = function () use ( $className ) { $args = func_get_args(); return new $className( ...$args ); }; return curryN( $argCount, $maker ); } ) ); self::macro( 'ascend', curryN( 3, function ( $fn, $a, $b ) { $aa = $fn( $a ); $bb = $fn( $b ); return $aa < $bb ? - 1 : ( $aa > $bb ? 1 : 0 ); } ) ); self::macro( 'descend', curryN( 3, function ( $fn, $a, $b ) { return self::ascend( $fn, $b, $a ); } ) ); self::macro( 'useWith', curryN( 2, function ( $fn, $transformations ) { return curryN( count( $transformations ), function () use ( $fn, $transformations ) { $apply = function ( $arg, $transform ) { return $transform( $arg ); }; $args = Fns::map( spreadArgs( $apply ), Lst::zip( func_get_args(), $transformations ) ); return $fn( ...$args ); } ); } ) ); self::macro( 'nthArg', curryN( 1, function ( $n ) { return function () use ( $n ) { return Lst::nth( $n, func_get_args() ); }; } ) ); self::macro( 'either', curryN( 3, function ( callable $f, callable $g, Either $e ) { if ( $e instanceof Left ) { return $e->orElse( $f )->get(); } return $e->map( $g )->get(); } ) ); self::macro( 'maybe', curryN( 3, function ( $v, callable $f, Maybe $m ) { if ( $m->isNothing() ) { return $v; } return $m->map( $f )->get(); } ) ); self::macro( 'isRight', curryN( 1, function ( $e ) { return $e instanceof Right; } ) ); self::macro( 'isLeft', curryN( 1, function ( $e ) { return $e instanceof Left; } ) ); self::macro( 'isJust', curryN( 1, function ( $m ) { return $m instanceof Just; } ) ); self::macro( 'isNothing', curryN( 1, function ( $m ) { return $m instanceof Nothing; } ) ); self::macro( 'safe', curryN( 1, function ( $fn ) { return pipe( $fn, Maybe::fromNullable() ); } ) ); self::macro( 'make', curryN( 1, function ( $className ) { return \WPML\Container\make( $className ); } ) ); self::macro( 'makeN', curryN( 2, function ( $argCount, $className ) { $maker = spreadArgs( curryN( $argCount, function () use ( $className ) { return \WPML\Container\make( $className, func_get_args() ); } ) ); return $maker( Lst::drop( 2, func_get_args() ) ); } ) ); self::macro( 'unary', curryN( 1, function ( $fn ) { return function ( $arg ) use ( $fn ) { return $fn( $arg ); }; } ) ); self::macro( 'memorizeWith', curryN( 2, function ( $cacheKeyFn, $fn ) { return function () use ( $cacheKeyFn, $fn ) { static $cache = []; $args = func_get_args(); $key = call_user_func_array( $cacheKeyFn, $args ); if ( array_key_exists( $key, $cache ) ) { return $cache[ $key ]; } $result = call_user_func_array( $fn, $args ); $cache[ $key ] = $result; return $result; }; } ) ); self::macro( 'memorize', self::memorizeWith( gatherArgs( pipe( Fns::map( 'json_encode' ), Lst::join( '|' ) ) ) ) ); self::macro( 'once', curryN( 1, function ( $fn ) { return function () use ( $fn ) { static $result = []; if ( array_key_exists( 'data', $result ) ) { return $result['data']; } $result['data'] = call_user_func_array( $fn, func_get_args() ); return $result['data']; }; } ) ); self::macro( 'withNamedLock', curryN( 3, function ( $name, $returnFn, $fn ) { static $inProgress = []; return function () use ( &$inProgress, $name, $returnFn, $fn ) { $args = func_get_args(); if ( Obj::prop( $name, $inProgress ) ) { return call_user_func_array( $returnFn, $args ); } $inProgress[ $name ] = true; $result = call_user_func_array( $fn, $args ); $inProgress[ $name ] = false; return $result; }; } ) ); self::macro( 'withoutRecursion', curryN( 2, function ( $returnFn, $fn ) { return function () use ( $returnFn, $fn ) { static $inProgress = false; $args = func_get_args(); if ( $inProgress ) { return call_user_func_array( $returnFn, $args ); } $inProgress = true; $result = call_user_func_array( $fn, $args ); $inProgress = false; return $result; }; } ) ); self::macro( 'liftA2', curryN( 3, function ( $fn, $monadA, $monadB ) { return $monadA->map( $fn )->ap( $monadB ); } ) ); self::macro( 'liftA3', curryN( 4, function ( $fn, $monadA, $monadB, $monadC ) { return $monadA->map( $fn )->ap( $monadB )->ap( $monadC ); } ) ); self::macro( 'liftN', function ( $n, $fn ) { $liftedFn = curryN( $n, function () use ( $n, $fn ) { $args = func_get_args(); $result = $args[0]->map( curryN( $n, $fn ) ); return Fns::reduce( function ( $result, $monad ) { return $result->ap( $monad ); }, $result, Lst::drop( 1, $args ) ); } ); return call_user_func_array( $liftedFn, Lst::drop( 2, func_get_args() ) ); } ); self::macro( 'until', curryN( 3, function ( $predicate, array $fns, ...$args ) { foreach ( $fns as $fn ) { $result = $fn( ...$args ); if ( $predicate( $result ) ) { return $result; } } return null; } ) ); } /** * @return \Closure */ public static function noop() { return function () { }; } /** * Curried function that transforms a Maybe into an Either. * * @param mixed|null $or * @param Maybe|null $maybe * * @return callable|Either */ public static function maybeToEither( $or = null, $maybe = null ) { $toEither = function ( $or, Maybe $maybe ) { return self::isJust( $maybe ) ? Either::right( $maybe->getOrElse( null ) ) : Either::left( $or ); }; return call_user_func_array( curryN( 2, $toEither ), func_get_args() ); } } Fns::init(); core/Relation.php 0000755 00000003460 14717633353 0010004 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\Collect\Support\Traits\Macroable; /** * @method static callable|bool equals( ...$a, ...$b ) - Curried :: a->b->bool * @method static callable|bool lt( ...$a, ...$b ) - Curried :: a->b->bool * @method static callable|bool lte( ...$a, ...$b ) - Curried :: a->b->bool * @method static callable|bool gt( ...$a, ...$b ) - Curried :: a->b->bool * @method static callable|bool gte( ...$a, ...$b ) - Curried :: a->b->bool * @method static callable|bool propEq( ...$prop, ...$value, ...$obj ) - Curried :: String → a → array → bool * @method static callable|array sortWith( ...$comparators, ...$array ) - Curried :: [( a, a ) → int] → [a] → [a] */ class Relation { use Macroable; /** * @return void */ public static function init() { self::macro( 'equals', curryN( 2, function ( $a, $b ) { return $a === $b; } ) ); self::macro( 'lt', curryN( 2, function ( $a, $b ) { if ( is_string( $a ) && is_string( $b ) ) { return strcmp( $a, $b ) < 0; } return $a < $b; } ) ); self::macro( 'gt', curryN( 2, function ( $a, $b ) { return self::lt( $b, $a ); } ) ); self::macro( 'lte', curryN( 2, function ( $a, $b ) { return ! self::gt( $a, $b ); } ) ); self::macro( 'gte', curryN( 2, function ( $a, $b ) { return ! self::lt( $a, $b ); } ) ); self::macro( 'propEq', curryN( 3, function ( $prop, $value, $obj ) { return Obj::prop( $prop, $obj ) === $value; } ) ); self::macro( 'sortWith', curryN( 2, function ( $comparators, $array ) { $compareFn = function ( $a, $b ) use ( $comparators ) { foreach ( $comparators as $comparator ) { $v = $comparator( $a, $b ); if ( $v !== 0 ) { return $v; } } return 0; }; return Lst::sort( $compareFn, $array ); } ) ); } } Relation::init(); core/Lst.php 0000755 00000026607 14717633353 0007001 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\Collect\Support\Collection; use WPML\Collect\Support\Traits\Macroable; use WPML\Collect\Support\Arr; /** * Lst class contains functions for working on ordered arrays indexed with numerical keys * * @method static callable|array append( mixed ...$newItem, array ...$data ) - Curried :: mixed->array->array * @method static callable|array fromPairs( array ...$array ) - Curried :: [[a, b]] → [a => b] * @method static callable|array toObj( array ...$array ) - Curried :: array → object * @method static callable|array pluck( ...$prop, ...$array ) - Curried :: string → array → array * @method static callable|array partition( ...$predicate, ...$target ) - Curried :: ( a → bool ) → [a] → [[a], [a]] * @method static callable|array sort( ...$fn, ...$target ) - Curried :: ( ( a, a ) → int ) → [a] → [a] * @method static callable|array unfold( ...$fn, ...$seed ) - Curried :: ( a → [b] ) → * → [b] * @method static callable|array zip( ...$a, ...$b ) - Curried :: [a] → [b] → [[a, b]] * @method static callable|array zipObj( ...$a, ...$b ) - Curried :: [a] → [b] → [a => b] * @method static callable|array zipWith( ...$f, ...$a, ...$b ) - Curried :: ( ( a, b ) → c ) → [a] → [b] → [c] * @method static callable|string join( ...$glue, ...$array ) - Curried :: string → [a] → string * @method static callable|string joinWithCommasAndAnd( ...$array ) - Curried :: [a] → string * @method static callable|array concat( ...$a, ...$b ) - Curried :: [a] → [a] → [a] * @method static callable|array|null find( ...$predicate, ...$array ) - Curried :: ( a → bool ) → [a] → a | null * @method static callable|array flattenToDepth( ...$depth, ...$array ) - Curried :: int → [[a]] → [a] * @method static callable|array flatten( ...$array ) - Curried :: [[a]] → [a] * @method static callable|bool includes( ...$val, ...$array ) - Curried :: a → [a] → bool * @method static callable|bool includesAll( ...$values, ...$array ) - Curried :: [a] → [a] → bool * * Determines if all the values are in the given array * * ``` * $includes10and20 = Lst::includesAll( [ 10, 20 ] ); * * $this->assertTrue( $includes10and20( [ 5, 10, 15, 20 ] ) ); * $this->assertFalse( $includes10and20( [ 5, 15, 20 ] ) ); * ``` * @method static callable|bool nth( ...$n, ...$array ) - Curried :: int → [a] → a | null * @method static callable|bool last( ...$array ) - Curried :: [a] → a | null * @method static callable|int length( ...$array ) - Curried :: [a] → int * @method static callable|array take( ...$n, ...$array ) - Curried :: int → [a] → [a] * @method static callable|array takeLast( ...$n, ...$array ) - Curried :: int → [a] → [a] * @method static callable|array slice( ...$offset, ...$limit, ...$array ) - Curried :: int → int->[a] → [a] * @method static callable|array drop( ...$n, ...$array ) - Curried :: int → [a] → [a] * @method static callable|array dropLast( ...$n, ...$array ) - Curried :: int → [a] → [a] * @method static callable|array makePair( ...$a, ...$b ) - Curried :: mixed → mixed → array * @method static callable|array make ( ...$a ) - Curried :: mixed → array * @method static callable|array insert( ...$index, ...$v, ...$array ) - Curried :: int → mixed → array → array * @method static callable|array range( ...$from, ...$to ) - Curried :: int → int → array * @method static callable|array xprod( ...$a, ...$b ) - Curried :: [a]->[b]->[a, b] * * Creates a new list out of the two supplied by creating each possible pair from the lists. * * ``` * $a = [ 1, 2, 3 ]; * $b = [ 'a', 'b', 'c' ]; * $expectedResult = [ * [ 1, 'a' ], [ 1, 'b' ], [ 1, 'c' ], * [ 2, 'a' ], [ 2, 'b' ], [ 2, 'c' ], * [ 3, 'a' ], [ 3, 'b' ], [ 3, 'c' ], * ]; * * $this->assertEquals( $expectedResult, Lst::xprod( $a, $b ) ); * ``` * @method static callable|array prepend( ...$val, ...$array ) - Curried:: a → [a] → [a] * * Returns a new array with the given element at the front, followed by the contents of the list. * * @method static callable|array reverse( ...$array ) - Curried:: [a] → [a] * * Returns a new array with the elements reversed. * */ class Lst { use Macroable; /** * @return void */ public static function init() { self::macro( 'append', curryN( 2, function ( $newItem, array $data ) { $data[] = $newItem; return $data; } ) ); self::macro( 'fromPairs', curryN( 1, function ( array $data ) { $fromPair = function ( array $result, array $pair ) { $result[ $pair[0] ] = $pair[1]; return $result; }; return Fns::reduce( $fromPair, [], $data ); } ) ); self::macro( 'toObj', curryN( 1, function ( array $data ) { return (object) $data; } ) ); self::macro( 'pluck', curryN( 2, function ( $prop, $data ) { return Fns::map( Obj::prop( $prop ), $data ); } ) ); self::macro( 'partition', curryN( 2, function ( $predicate, $data ) { return [ Fns::filter( $predicate, $data ), Fns::reject( $predicate, $data ) ]; } ) ); self::macro( 'sort', curryN( 2, function ( $compare, $data ) { if ( $data instanceof Collection ) { return wpml_collect( self::sort( $compare, $data->toArray() ) ); } usort( $data, $compare ); return $data; } ) ); self::macro( 'unfold', curryN( 2, function ( $fn, $seed ) { $result = []; do { $iteratorResult = $fn( $seed ); if ( is_array( $iteratorResult ) ) { $result[] = $iteratorResult[0]; $seed = $iteratorResult[1]; } } while ( $iteratorResult !== false ); return $result; } ) ); self::macro( 'zip', curryN( 2, function ( $a, $b ) { $result = []; for ( $i = 0; $i < min( count( $a ), count( $b ) ); $i ++ ) { $result[] = [ $a[ $i ], $b[ $i ] ]; } return $result; } ) ); self::macro( 'zipObj', curryN( 2, function ( $a, $b ) { $result = []; for ( $i = 0; $i < min( count( $a ), count( $b ) ); $i ++ ) { $result[ $a[ $i ] ] = $b[ $i ]; } return $result; } ) ); self::macro( 'zipWith', curryN( 3, function ( $fn, $a, $b ) { $result = []; for ( $i = 0; $i < min( count( $a ), count( $b ) ); $i ++ ) { $result[] = $fn( $a[ $i ], $b[ $i ] ); } return $result; } ) ); self::macro( 'join', curryN( 2, 'implode' ) ); self::macro( 'joinWithCommasAndAnd', curryN( 1, function ( $array ) { $last = Lst::last( $array ); if ( $last ) { if ( Lst::length( $array ) > 1 ) { return str_replace( ' ', ' ', Lst::join( ', ', Lst::dropLast( 1, $array ) ) . ' ' . __( ' and ', 'sitepress' ) . ' ' . $last ); // TODO Replace above with following (after 4.5.0 release to get 'and' translated): // return Lst::join( ', ', Lst::dropLast( 1, $array ) ) . ' ' . __( 'and', 'sitepress' ) . ' ' . $last; } else { return $last; } } else { return ''; } } ) ); self::macro( 'concat', curryN( 2, 'array_merge' ) ); self::macro( 'find', curryN( 2, function ( $predicate, $array ) { foreach ( $array as $value ) { if ( $predicate( $value ) ) { return $value; } } return null; } ) ); self::macro( 'flattenToDepth', curryN( 2, flip( Arr::class . '::flatten' ) ) ); self::macro( 'flatten', curryN( 1, Arr::class . '::flatten' ) ); self::macro( 'includes', curryN( 2, function ( $val, $array ) { return in_array( $val, $array, true ); } ) ); self::macro( 'includesAll', curryN( 2, function ( $values, $array ) { foreach ( $values as $val ) { if ( ! in_array( $val, $array, true ) ) { return false; } } return true; } ) ); self::macro( 'nth', curryN( 2, function ( $n, $array ) { $count = count( $array ); if ( $n < 0 ) { $n += $count; } return $n >= 0 && $n < $count ? $array[ $n ] : null; } ) ); self::macro( 'last', self::nth( - 1 ) ); self::macro( 'length', curryN( 1, 'count' ) ); self::macro( 'take', curryN( 2, function ( $n, $array ) { return array_slice( $array, 0, $n ); } ) ); self::macro( 'takeLast', curryN( 2, function ( $n, $array ) { return array_slice( $array, - $n, $n ); } ) ); self::macro( 'slice', curryN( 3, function ( $offset, $limit, $array ) { return array_slice( $array, $offset, $limit ); } ) ); self::macro( 'drop', curryN( 2, self::slice( Fns::__, null ) ) ); self::macro( 'dropLast', curryN( 2, function ( $n, $array ) { $len = count( $array ); return self::take( $n < $len ? $len - $n : 0, $array ); } ) ); self::macro( 'makePair', curryN( 2, function ( $a, $b ) { return [ $a, $b ]; } ) ); self::macro( 'make', curryN( 1, function ( ...$args ) { return $args; } ) ); self::macro( 'insert', curryN( 3, function ( $index, $v, $array ) { $values = array_values( $array ); array_splice( $values, $index, 0, [ $v ] ); return $values; } ) ); self::macro( 'range', curryN( 2, 'range' ) ); self::macro( 'xprod', curryN( 2, function ( $a, $b ) { $result = []; foreach ( $a as $el1 ) { foreach ( $b as $el2 ) { $result[] = [ $el1, $el2 ]; } } return $result; } ) ); self::macro( 'prepend', Lst::insert( 0 ) ); self::macro( 'reverse', curryN( 1, 'array_reverse' ) ); } /** * Curried function that keys the array by the given key * * keyBy :: string -> array -> array * * ``` * $data = [ * [ 'x' => 'a', 'y' => 123 ], * [ 'x' => 'b', 'y' => 456 ], * ]; * * Lst::keyBy( 'x', $data ); * [ * 'a' => [ 'x' => 'a', 'y' => 123 ], * 'b' => [ 'x' => 'b', 'y' => 456 ], * ], * ``` * * @param string $key * @param mixed[] $array * * @return mixed[]|callable */ public static function keyBy( $key = null, $array = null ) { $keyBy = function ( $key, $array ) { $apply = Fns::converge( Lst::zipObj(), [ Lst::pluck( $key ), Fns::identity() ] ); return $apply( $array ); }; return call_user_func_array( curryN( 2, $keyBy ), func_get_args() ); } /** * Curried function that wraps each item in array with pair: [$key => $item1] * * keyWith :: string -> array -> array * * ``` * $data = [ 1, 2.3, 'some data', - 2, 'a' ]; * * Lst::keyWith('myKey', $data); * [ [ 'myKey' => 1 ], [ 'myKey' => 2.3 ], [ 'myKey' => 'some data' ], [ 'myKey' => - 2 ], [ 'myKey' => 'a' ] ] * ``` * * @param string $key * @param mixed[] $array * * @return mixed[]|callable */ public static function keyWith( $key = null, $array = null ) { $keyWith = function ( $key, $array ) { return Fns::map( function ( $item ) use ( $key ) { return [ $key => $item ]; }, $array ); }; return call_user_func_array( curryN( 2, $keyWith ), func_get_args() ); } /** * This method will return the values in the original collection that are not present in the given collection: * * @param array|Collection $array1 * @param array|Collection $array2 * * @return callable|Collection|array */ public static function diff( $array1 = null, $array2 = null ) { $diff = function ( $array1, $array2 ) { if ( is_object( $array1 ) ) { return $array1->diff( $array2 ); } else { return array_diff( $array1, $array2 ); } }; return call_user_func_array( curryN( 2, $diff ), func_get_args() ); } /** * It returns array of $val elements repeated $times times. * * @param mixed $val * @param int $times * * @return callable|array[mixed] */ public static function repeat( $val = null, $times = null ) { $repeat = flip( partial( 'array_fill', 0 ) ); return call_user_func_array( curryN( 2, $repeat ), func_get_args() ); } } Lst::init(); core/Promise.php 0000755 00000002736 14717633353 0007652 0 ustar 00 <?php namespace WPML\INSTALLER\FP; class Promise { /** @var callable */ private $onResolved; /** @var callable */ private $onReject; /** @var Promise */ private $next; /** * @param mixed $data * * @return mixed */ public function resolve( $data ) { if ( $this->onResolved ) { if ( $data instanceof Either ) { $result = $data->chain( $this->onResolved ); } else { $result = call_user_func( $this->onResolved, $data ); } if ( $this->next ) { if ( $result && ! $result instanceof Left ) { return $this->next->resolve( $result ); } else { return $this->next->reject( $result ); } } else { return $result; } } else { return $data; } } /** * @param mixed $data * * @return mixed */ public function reject( $data ) { $result = $data; if ( $this->onReject ) { if ( $data instanceof Either ) { $result = $data->orElse( $this->onReject )->join(); } else { $result = call_user_func( $this->onReject, $data ); } } if ( $this->next ) { return $this->next->reject( $result ); } else { return $result; } } /** * @param callable $fn * * @return Promise */ public function then( callable $fn ) { $this->onResolved = $fn; $this->next = new Promise(); return $this->next; } /** * @param callable $fn * * @return Promise */ public function onError( callable $fn ) { $this->onReject = $fn; $this->next = new Promise(); return $this->next; } } core/SystemClass.php 0000755 00000000366 14717633353 0010503 0 ustar 00 <?php namespace WPML\INSTALLER\FP\System; use WPML\INSTALLER\FP\Either; class System { /** * @return \Closure */ public static function getPostData() { return function () { return Either::right( wpml_collect( $_POST ) ); }; } } core/Filter.php 0000755 00000001772 14717633353 0007460 0 ustar 00 <?php namespace WPML\INSTALLER\FP\System; use WPML\Collect\Support\Collection; class _Filter { /** * @var string */ private $key; /** * @var callable */ private $fn; /** * @var mixed */ private $default; /** * _Filter constructor. * * @param string $key */ public function __construct( $key ) { $this->key = $key; } /** * @param callable $fn * * @return _Filter */ public function using( callable $fn ) { $this->fn = $fn; return $this; } /** * @param mixed $default * * @return _Filter */ public function defaultTo( $default ) { $this->default = $default; return $this; } /** * @param \WPML\Collect\Support\Collection<mixed> $collection * * @return \WPML\Collect\Support\Collection<mixed> */ public function __invoke( Collection $collection ) { $value = $collection->has( $this->key ) ? call_user_func( $this->fn, $collection->get( $this->key ) ) : value( $this->default ); return $collection->put( $this->key, $value ); } } core/Json.php 0000755 00000001430 14717633353 0007133 0 ustar 00 <?php namespace WPML\INSTALLER\FP; use WPML\Collect\Support\Collection; use WPML\Collect\Support\Traits\Macroable; /** * @method static callable|array|null toArray( string ...$str ) - Curried :: json->array * @method static callable|Collection|null toCollection( string ...$str ) Curried :: json->null | Collection */ class Json { use Macroable; /** * @return void */ public static function init() { self::macro( 'toArray', curryN( 1, function ( $str ) { return json_decode( $str, true ); } ) ); self::macro( 'toCollection', curryN( 1, function ( $str ) { return Maybe::of( $str ) ->map( 'stripslashes' ) ->map( self::toArray() ) ->map( 'wpml_collect' ) ->getOrElse( null ); } ) ); } } Json::init();
| ver. 1.4 |
Github
|
.
| PHP 7.4.3-4ubuntu2.24 | Генерация страницы: 0.01 |
proxy
|
phpinfo
|
Настройка