mustache use current array value to index an array - php

In mustache:
I have 'matches'=>['foo', 'bar']. I also have:
[
'deals'=> [
'foo' => new Deal('name1'),
'bar' => new Deal('name2'),
'baz' => new Deal('name3')
]
]
What I am trying to do is this:
{{#matches}}
{{deals}}.{{.}}.{{name}}
{{/matches}}
Which doesn't work.
This works, except it isn't dynamic like I need:
{{#matches}}
{{deals.bar.name}}
{{/matches}}
Any thoughts or suggestions?

You may want to make a projection ahead of time that filters deals on matches in code before applying it to the template. If I'm understanding right, you're attempting to embed matching logic in the template which Mustache doesn't generally support.
You could either filter matches, or apply a Boolean property to each describing whether it has a match.

Related

Efficient way to capture "variations" or "combinations" ore "aliases" for switch-case argument(s) in PHP

I am pretty sure this challenge has been solved by someone already but even searching with different words, I could not find a solution for this problem:
I try to give users the possibility to run certain functions of a class based on an argument like
service_class::do_this( "selection-argument" );
but the user shall be able to use "clear words" as well as "aliases" and even "well known" abbreviations or synonyms.
I use switch-case construction to call the "real" function.
Example: To get the contens of a folder, The user can use "getdir", "dir", "Directory", "getfolder", "getcontent", "content", "d-cont" and a number of more other "matching words" to start the function(s) underlaying and getting back the very same result.
Capture-ing lowercase/uppercase is simple. What I search for is an efficient way to capture all possible "variations" - that are, of course different number of variations for different functions called.
At the moment I use multiple "case "": lines after each other, but that makes the code quite long, and further I would like the user to be able to "enahnce" the recognition set for a certain function.
That's why I thought about "stripos" to determine first what "internal word" to use and only then run into the switch-case construction.
Anyone had that issue and can direct me to a "good and efficient" solution?
Seems that Stck-exchange itself had a similar challenge (https://codereview.stackexchange.com/tags/php/synonyms) ... maybe I can simply re-use the underlying code?
Thanks in advance and sorry if I overlooked a solution already posted.
You could use a database or array. Let's do the latter. So to determine whether an user wants to get a directory you would define an array like this:
$getDirVariants = ['getdir',
'dir',
'directory',
'getfolder',
'getcontent',
'content',
'd-cont'];
It is easy to add more of these arrays. To test the query word you would do:
$queryWord = strtolower($queryWord);
if (in_array($queryWord, $getDirVariants)) service_class::getDir(<arguments>);
elseif (in_array($queryWord, $deleteVariants)) service_class::delete(<arguments>);
You can easily add to the arrays or make it a 2D array to contain more commands. That array could also be placed in a database.
Especially when there are many commands, with many variants, a database will be the better solution, because you can find the query word with one database query.
There's a variation I can think of that will also simplify the code when there are many commands. You could use an associative array to find the command:
$commandVariants = ['getdir' => 'getdir',
'dir' => 'getdir',
'directory' => 'getdir',
'getfolder' => 'getdir',
'getcontent' => 'getdir',
'content' => 'getdir',
'd-cont' => 'getdir',
'delete' => 'delete',
'del' => 'delete',
'remove' => 'delete',
'unlink' => 'delete'];
$queryWord = strtolower($queryWord);
if (isset($commandVariants[$queryWord])) {
$command = $commandVariants[$queryWord];
service_class::$command(<arguments>);
}
else echo "I don't recognize that command.";
This uses a variable identifier.

regex for changing array() to [] using atom

I am wanting to update my php script as a project (globally) and change array(elements); to [elements];
Using atom i have already ran the update for just the init of arrays and have already changed array(); to []; however this just takes care of the empty array initialization codes.
The challenge now is to change arrays that actually have elements in them from () to []
For example the arrays could be in several formats
array('content',number content,'content');
or
array(
'content',
array(
number content,'content'
)
);
or any variation of the number of levels.
I have broken it down this way but i dont know how to do the regex.,
Find every occurance of array(
- replace with [
Find the associated close of that array );
-replace with ];
Find every array( inside another which is array(
-replace with [
Find the associated close of each of those arrays that are inside another array ); or just ),
- replace with ]; or ],
I think that should cover it.... is the regex going to be impossible to do?
thanks
Seeing, that you are willing to do some manual repairs afterwards and that the solution suggested in my comment is of interest to you I now put it in "proper" answer form. This is easier to edit and to discuss. As said before, this is only a "workaround", as we are not really parsing the text in the sense of really understanding it syntactically. However, when you apply the following Regexp
\barray\(([^()]*)\)
repeatedly and replace it with
[$1]
(yes, I left out the "array" in front of "["!) you might have a chance of doing what you want.
I put a \b in front of the Regexp to make it look for a word boundary. This way we exclude text fragments like is_array() or myarray() from being changed.

How to get around PHP array forcing a key

I have a very simple array where I want to be able to add settings as simple as possible. In my case, if I don't need a second value, I don't need to add it.
Code
$settings = [
'my_string',
'my_array' => 'hello'
];
print_r($settings);
Result
The problem with my approach is that my_string is seen as a value and the key 0 is added.
Array
(
[0] => my_string
[my_array] => hello
)
I could check if the key is a number and in that case figure out that I don't use a second value.
Is there a better approach?
I'm aware of that I can use multiple nested arrays but then simplicity is gone.
The way you've written it, you wouldn't be able to access the value 'my_string' without some kind of key. There is always a key for any value, and if you don't specify values, PHP generates them as though they were indices in an actual array (PHP 'arrays' are actually dictionaries/maps/etc. that have default behavior in the absence of a specified key).
$settings = [
'name' => 'my_string',
'my_array' => 'hello'
];
If you don't want to use $settings[0] -- and I wouldn't want to either -- you should probably decide on a name for whatever 'my_string' is supposed to be and use that as its key.

Get input name from request, Laravel

In the laravel how can we made something like this?
For example if we write dd($request); in the controller:
"slug_en" => "english_slug_here"
"lang_en" => "english"
"slug_es" => "spanish_slug_here"
"lang_es" => "spanish"
If i must use "english", simply i can use $request->lang_en;
But what if i know just "english" and want to know input name?
$request->X = "english";
I want to X right here. I need to set language system with dynamically, but i stuck right here. If can anybody help me, i'll glad so much. Thanks in advance.
Try this
$x = array_search ('english', $request->all());
use
$x = array_keys ($request->all(),'english');
returns all the keys having value english
if given only the array returns all the keys. The search field is included as a second parameter to get keys for the given search value and there is a optional third parameter strict which may be either true or false, if set strict validation occurstrue

MongoDB PHP query using $and operator on the same object

I've got a fairly simple query that I have working in command line, and am trying to execute using php.
It's looking for documents that match all of the given "tags" entered in a search box:
db.collection.find( { $and: [ { tags: "cats" }, { tags: "video" } ] } )
I can't seem to figure out how to translate this to php. I've been using codeigniter for everything up to this point (Alex Bilbie's library), but have looked into building my own queries with no luck. Most of the methods I've tried eliminate the first tag (cats), since it is looking at the same field name (tags).
any thoughts?
PHP can be a bit tricky with how you need to format the arrays. What I've found to be the best way to create the queries is through doing things like:
json_encode($myQuery);
then comparing that to what actually works directly on the console of the app. In this case you're looking for:
$item = array('$and' => array(array('tags' => 'cats'), array('tags' => 'videos')))
which you can confirm by doing:
echo(json_encode(array('$and' => array(array('tags' => 'cats'), array('tags' => 'videos')))));
Good Luck!

Categories