lodash-php / lodash-php Goto Github PK
View Code? Open in Web Editor NEWEasy to use utility functions for everyday PHP projects. This is a port of the Lodash JS library to PHP
Home Page: https://lodash-php.github.io
License: MIT License
Easy to use utility functions for everyday PHP projects. This is a port of the Lodash JS library to PHP
Home Page: https://lodash-php.github.io
License: MIT License
I noted the lack of a set function (as here). I built a sort of set function in a project of mine. At the moment it works only on array, what do you think if I make a porting of my function on your project?
What are your general guidelines to make a pull request? Do you have any suggest?
Best Regards
Domenico Rizzo
Was looking for has
today and couldn't find it. I can implement has
and hasIn
(Seems to be the exact same to me) using the Symfony Property Access component (SPAC after) but will have one slight difference from the lodash support because SPAC doesn't support one of the suggested approach in the lodash docs. Also, it will obviously require the addition of a small dependency. Are you ok with it?
References:
The differences would be:
var object = { 'a': { 'b': 2 } };
var other = _.create({ 'a': _.create({ 'b': 2 }) });
_.has(object, ['a', 'b']);
// => broken, SPAC cannot resolve paths using arrays of keys
I will wait for your approval to start work on this.
Using the library with Laravel
or Lumen
cause the folowing issue since __ is already being used by the core framework.
Fatal error: Cannot redeclare __() (previously declared in ..../vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:947) in ..../vendor/lodash-php/lodash-php/src/compiled.php on line 5
Any idea how to fix this issue? Creating a different class name perhaps?
Thank you
As an extension to #8 , I'd like to either document the magic methods with @method
or replace them with real method calls. My IDE refuses to do code completion and other features until they're there or in the docblock. So my question is which of the two would @pierredup prefer. I always prefer real methods, even if auto-generated by a script, than more magic.
Release 0.0.7 was over a year ago and uses now abandoned packages.
(Package symfony/inflector is abandoned, you should avoid using it. Use EnglishInflector from the String component instead.)
when I pass _\flatten
an array which contains empty array, _\flatten
does not expand the empty array.
ex.
use function _\flatten;
$arr = [
[1, 2, 3],
[],
[4, 5, 6],
];
$result = flatten($arr);
I expected that $result
is [1, 2, 3, 4, 5, 6]
, but actually [1, 2, 3, [], 4, 5, 6]
.
this behavior is expected?
You release 0.09 instead of 0.0.9 @pierredup ...
Why isn't it a 1.x tag anyway. This has been out for multiple years and works no?
I'm struggling to get this library to work.
I've required it via composer. I'm trying to include it with a class (none of the examples in the docs show how to work within classes).
composer.json
{
"name": "cakephp/app",
"description": "CakePHP skeleton app",
"homepage": "https://cakephp.org",
"type": "project",
"license": "MIT",
"require": {
"php": ">=5.6",
"lodash-php/lodash-php": "^0.0.6",
php code
<?php
namespace App\Controller\Api;
use function _\get;
class Controller{
public function __construct()
{
get('one.two.three');
}
error:
{
"status": "500",
"title": "Internal Server Error",
"detail": "Call to undefined function _\\get()"
}
I tried this without using classes and I get the same error:
<?php
namespace App\Controller\Api;
use function _\get;
get('obj.one.two.three');
die();
error: Call to undefined function _\\get()
Update: it seems other functions are working, it's only get
that doesn't (Murphy's law because that's the only function I'm interested in!).
Looking through the vendor src folder I can see that the get function is not included. The README seems to indicate that it would be expected in the Objects
folder but it is not present there.
I also tried composer remove
and then require
again. Same deal, src/Objects/get.php
is missing. I'm on Mac OS X Catalina and Composer version 1.8.6
I also notice that the tests for get
are missing from the tests folder.
You're missing one of the important features of lodash
which is the _.chain() method.
e.g:
_::chain(array)->map(function(item) {
return item['price'];
})->sum();
Nesting these functions instead of chaining would be a real pain in the ass. Consider this example:
_::sum(_::map(array, function(item) {
return item['price'];
}));
This example only involves two functions, but in real world we use more than that.
composer require lodash-php/lodash-php
Error: Package lodash-php/lodash-php at version has a PHP requirement incompatible with your PHP version (7.0.33)
suggestion 1:
implement all functions as \lodash\_
class static methods
this way _ class will be loaded on-demand only.
so far php missing namespaced functions on-demand autoload mechanics
use lodash\_;
_::$method();
suggestion 2:
split class in two or more parts:
implement
public static function __callStatic($method, $args) {
foreach (static::$KNOWN_EXTENSIONS as $ext)
if (class_exists($ext) && method_exists($ext, $method))
return $ext::$method(...$args);
}
I'm getting those warnings when using lodash-php with PHP 7.4:
PHP Warning: Use of undefined constant rsZWJ - assumed 'rsZWJ' (this will throw an Error in a future version of PHP) in /var/www/symfony/vendor/lodash-php/lodash-php/src/internal/hasUnicode.php on line 15
PHP Stack trace:
PHP 1. {main}() /var/www/symfony/bin/console:0
PHP 2. require() /var/www/symfony/bin/console:16
PHP 3. ComposerAutoloaderInitf1b79703f32f0edf938f45d6c5aba2e0::getLoader() /var/www/symfony/vendor/autoload.php:12
PHP 4. composerRequiref1b79703f32f0edf938f45d6c5aba2e0() /var/www/symfony/vendor/composer/autoload_real.php:59
PHP 5. require() /var/www/symfony/vendor/composer/autoload_real.php:69
PHP 6. require_once() /var/www/symfony/vendor/lodash-php/lodash-php/src/bootstrap.php:19
Warning: Use of undefined constant rsZWJ - assumed 'rsZWJ' (this will throw an Error in a future version of PHP) in /var/www/symfony/vendor/lodash-php/lodash-php/src/internal/hasUnicode.php on line 15
Call Stack:
0.0060 402680 1. {main}() /var/www/symfony/bin/console:0
0.0070 404480 2. require('/var/www/symfony/vendor/autoload.php') /var/www/symfony/bin/console:16
0.0073 418048 3. ComposerAutoloaderInitf1b79703f32f0edf938f45d6c5aba2e0::getLoader() /var/www/symfony/vendor/autoload.php:12
0.0161 1018976 4. composerRequiref1b79703f32f0edf938f45d6c5aba2e0() /var/www/symfony/vendor/composer/autoload_real.php:59
0.0162 1021176 5. require('/var/www/symfony/vendor/lodash-php/lodash-php/src/bootstrap.php') /var/www/symfony/vendor/composer/autoload_real.php:69
0.0274 1419224 6. require_once('/var/www/symfony/vendor/lodash-php/lodash-php/src/internal/hasUnicode.php') /var/www/symfony/vendor/lodash-php/lodash-php/src/bootstrap.php:19
PHP Warning: Use of undefined constant rsAstralRange - assumed 'rsAstralRange' (this will throw an Error in a future version of PHP) in /var/www/symfony/vendor/lodash-php/lodash-php/src/internal/hasUnicode.php on line 15
PHP Stack trace:
PHP 1. {main}() /var/www/symfony/bin/console:0
PHP 2. require() /var/www/symfony/bin/console:16
PHP 3. ComposerAutoloaderInitf1b79703f32f0edf938f45d6c5aba2e0::getLoader() /var/www/symfony/vendor/autoload.php:12
PHP 4. composerRequiref1b79703f32f0edf938f45d6c5aba2e0() /var/www/symfony/vendor/composer/autoload_real.php:59
PHP 5. require() /var/www/symfony/vendor/composer/autoload_real.php:69
PHP 6. require_once() /var/www/symfony/vendor/lodash-php/lodash-php/src/bootstrap.php:19
Warning: Use of undefined constant rsAstralRange - assumed 'rsAstralRange' (this will throw an Error in a future version of PHP) in /var/www/symfony/vendor/lodash-php/lodash-php/src/internal/hasUnicode.php on line 15
Call Stack:
0.0060 402680 1. {main}() /var/www/symfony/bin/console:0
0.0070 404480 2. require('/var/www/symfony/vendor/autoload.php') /var/www/symfony/bin/console:16
0.0073 418048 3. ComposerAutoloaderInitf1b79703f32f0edf938f45d6c5aba2e0::getLoader() /var/www/symfony/vendor/autoload.php:12
0.0161 1018976 4. composerRequiref1b79703f32f0edf938f45d6c5aba2e0() /var/www/symfony/vendor/composer/autoload_real.php:59
0.0162 1021176 5. require('/var/www/symfony/vendor/lodash-php/lodash-php/src/bootstrap.php') /var/www/symfony/vendor/composer/autoload_real.php:69
0.0274 1419224 6. require_once('/var/www/symfony/vendor/lodash-php/lodash-php/src/internal/hasUnicode.php') /var/www/symfony/vendor/lodash-php/lodash-php/src/bootstrap.php:19
PHP Warning: Use of undefined constant rsComboRange - assumed 'rsComboRange' (this will throw an Error in a future version of PHP) in /var/www/symfony/vendor/lodash-php/lodash-php/src/internal/hasUnicode.php on line 15
PHP Stack trace:
PHP 1. {main}() /var/www/symfony/bin/console:0
PHP 2. require() /var/www/symfony/bin/console:16
PHP 3. ComposerAutoloaderInitf1b79703f32f0edf938f45d6c5aba2e0::getLoader() /var/www/symfony/vendor/autoload.php:12
PHP 4. composerRequiref1b79703f32f0edf938f45d6c5aba2e0() /var/www/symfony/vendor/composer/autoload_real.php:59
PHP 5. require() /var/www/symfony/vendor/composer/autoload_real.php:69
PHP 6. require_once() /var/www/symfony/vendor/lodash-php/lodash-php/src/bootstrap.php:19
Warning: Use of undefined constant rsComboRange - assumed 'rsComboRange' (this will throw an Error in a future version of PHP) in /var/www/symfony/vendor/lodash-php/lodash-php/src/internal/hasUnicode.php on line 15
Call Stack:
0.0060 402680 1. {main}() /var/www/symfony/bin/console:0
0.0070 404480 2. require('/var/www/symfony/vendor/autoload.php') /var/www/symfony/bin/console:16
0.0073 418048 3. ComposerAutoloaderInitf1b79703f32f0edf938f45d6c5aba2e0::getLoader() /var/www/symfony/vendor/autoload.php:12
0.0161 1018976 4. composerRequiref1b79703f32f0edf938f45d6c5aba2e0() /var/www/symfony/vendor/composer/autoload_real.php:59
0.0162 1021176 5. require('/var/www/symfony/vendor/lodash-php/lodash-php/src/bootstrap.php') /var/www/symfony/vendor/composer/autoload_real.php:69
0.0274 1419224 6. require_once('/var/www/symfony/vendor/lodash-php/lodash-php/src/internal/hasUnicode.php') /var/www/symfony/vendor/lodash-php/lodash-php/src/bootstrap.php:19
PHP Warning: Use of undefined constant rsVarRange - assumed 'rsVarRange' (this will throw an Error in a future version of PHP) in /var/www/symfony/vendor/lodash-php/lodash-php/src/internal/hasUnicode.php on line 15
PHP Stack trace:
PHP 1. {main}() /var/www/symfony/bin/console:0
PHP 2. require() /var/www/symfony/bin/console:16
PHP 3. ComposerAutoloaderInitf1b79703f32f0edf938f45d6c5aba2e0::getLoader() /var/www/symfony/vendor/autoload.php:12
PHP 4. composerRequiref1b79703f32f0edf938f45d6c5aba2e0() /var/www/symfony/vendor/composer/autoload_real.php:59
PHP 5. require() /var/www/symfony/vendor/composer/autoload_real.php:69
PHP 6. require_once() /var/www/symfony/vendor/lodash-php/lodash-php/src/bootstrap.php:19
Warning: Use of undefined constant rsVarRange - assumed 'rsVarRange' (this will throw an Error in a future version of PHP) in /var/www/symfony/vendor/lodash-php/lodash-php/src/internal/hasUnicode.php on line 15
Call Stack:
0.0060 402680 1. {main}() /var/www/symfony/bin/console:0
0.0070 404480 2. require('/var/www/symfony/vendor/autoload.php') /var/www/symfony/bin/console:16
0.0073 418048 3. ComposerAutoloaderInitf1b79703f32f0edf938f45d6c5aba2e0::getLoader() /var/www/symfony/vendor/autoload.php:12
0.0161 1018976 4. composerRequiref1b79703f32f0edf938f45d6c5aba2e0() /var/www/symfony/vendor/composer/autoload_real.php:59
0.0162 1021176 5. require('/var/www/symfony/vendor/lodash-php/lodash-php/src/bootstrap.php') /var/www/symfony/vendor/composer/autoload_real.php:69
0.0274 1419224 6. require_once('/var/www/symfony/vendor/lodash-php/lodash-php/src/internal/hasUnicode.php') /var/www/symfony/vendor/lodash-php/lodash-php/src/bootstrap.php:19
I encountered this error after installing this library within a Bedrock/Sage WordPress application. This issue seems to be similar to #12.
Fatal error: Cannot redeclare __() (previously declared in /app/vendor/lodash-php/lodash-php/src/compiled.php:5) in /app/web/wp/wp-includes/l10n.php on line 249
/**
* Retrieve the translation of $text.
*
* If there is no translation, or the text domain isn't loaded, the original text is returned.
*
* @since 2.1.0
*
* @param string $text Text to translate.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
* Default 'default'.
* @return string Translated text.
*/
function __( $text, $domain = 'default' ) {
return translate( $text, $domain );
}
Are there any plans to implement the toPairs function from lodashJS?
Right now your autoload iterate over directory tree and manually includes every file (one function per file)
find . -name "*.php" | wc -l
288 FILES = 288 includes
This approach makes this library unusable for any serious system.
Please create one file with all functions.
You can create build script to generate this file:
suggestion:
vendor/lodash-php/lodash-php/src:
echo "declare(strict_types=1); namespace _;" > loadash-one-file.php
find . -name ".php" | grep -v internal | xargs -n 1 php -w | sed "s/<?php/\r/g" | sed "s/declare(strict_types=1); namespace _;//g" >> loadash-one-file.php
.. same for internal/.php
^^^ this script + minor code fixes will create you one file with all your functions
Code:
$visibleColumns = \_::chain($columnSettings)
->filter(function($column) {
return
null == $columns['ratioCalculations'] &&
null == $columns['averageCalculation'] &&
null == $columns['percentageOf']
;
})
->map(function($column) {
return [
$columns['name'],
$columns['alt_name'],
];
})
->value();
Error:
Attempted to call function "chain" from namespace "_".
Symfony\Component\Debug\Exception
UndefinedFunctionException
in vendor/lodash-php/lodash-php/src/Lodash.php (line 56)
* @return mixed
* @throws Exception
*/
public static function __callStatic(string $method, array $args)
{
return ("_\\$method")(...$args);
}
Id like to just use the old school require statements, and download the zip here.
Is that possible?
I couldn't find the defaultTo function present in the lodash JS
I could create a pull request for the same
The defafultTo function signature would be as follows:
_::defaultTo(object|associative array, string|array, defaultValue[, defaultToOnEmpty])
lodash-php/src/Collection/filter.php l.65
function filter(iterable $array, $predicate = null): array
{
$iteratee = baseIteratee($predicate);
$result = \array_filter(
\is_array($array) ? $array : \iterator_to_array($array),
function ($value, $key) use ($array, $iteratee) {
return $iteratee($value, $key, $array);
},
\ARRAY_FILTER_USE_BOTH
);
\sort($result); // <- why ?
return $result;
}
why _\filter
also sort list not only filter?
it confuse me because I don't expected "filtered list is sorted".
when i need the sorted, i want to do by myself.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.