can we beautify the json output of laravel zero in terminal? - php

iam new in laravel zero. I dont know if it's possible, but i expected in laravel zero with my command can do like below:
$ php myapp myproduct:details 4
{
"id": 4,
"name": "adidas",
"size": 50
}
is it possible to use laravel zero display output json that also already beautified like above?

Related

php exec output is getting truncade truncated because of accents

I'm building a website on a linux server which can provide some informations about mkv file using mkvmerge command line but i'm facing a big issue when using the command $info = shell_exec("mkvmerge -J '".$chemin_fichier."'");
when the output of the command line contains accents, the output is being truncated :
exptected output :
{
"container": {
"properties": {
"is_providing_timestamps": true,
"title": "Le Bel Été 2019"
},
"type": "Matroska"
}
}
actual output :
{
"container": {
"properties": {
"is_providing_timestamps": true,
"title": "Le Bel
I did find on the web that we needed to modify the language of the environnement [using putenv() and setlocale() ] which i did but it didn t work. But i can define some variables using accents so this is quite strange.
anyway, when i run the same file on my computer using wamp server, or the same command line in my linux server terminal, i'm getting the correct output so i think the problem come from php(7.3) or apache(2.4).
do you have any idea ? Feel free to ask for extra details :)
Ok so i figured it out !
actually, the language "fr_FR.utf8" does not work for accents and special characters while "en_US.utf8" seems to be the only solution.
The default language on my server was POSIX which does not allow accents neither.
to make things work, place the following lines in your php script
$locale = 'en_US.utf8';
setlocale(LC_ALL, $locale);
putenv('LC_ALL='.$locale);

PHP json_decode() does not work with numbers starting with 0 (like 0123456)

In a JSON file I'd like to decode with PHP's json_decode() function I have items like this:
[{
"id_regione":3,
"regione":"Calabria",
"superficie":15221.9,
"num_residenti":1970521,
"num_comuni":409,
"num_provincie":5,
"presidente":"Gerardo Mario Oliverio",
"cod_istat":18,
"cod_fiscale":02205340793, /* problem here */
"piva":null,
"pec":"dipartimento.presidenza#pec.regione.calabria.it",
"sito":"www.regione.calabria.it",
"sede":"Regione Calabria / Via Massara 2, 88100 Catanzaro"
}]
You see, the part "cod_fiscale":02205340793 stops decoding with no apparent reason: but looking at PHP docs I found this in the changelog:
7.0.0 Rejected RFC 7159 incompatible number formats - top level (07, 0xff, .1, -.1) and all levels ([1.], [1.e1])
Oh, I see: 02205340793 seems an incompatible number format... Really it's not a number, it's a tax code (and I could potentially have problems also with phone numbers like 0935123456)
So, what's the best way to deal with these items? Convert everything to a string? And how?

Trim mongo object size

This is my document:
{
"_id": {
"$oid": "58e9a13999447d65550f4dd6"
},
"prices": {
"20170409": 701.09,
"20170408": 700.07
},
"stock": {
"20170409": 0,
"20170408": 0
}
}
I append a lot of fields in document objects (prices, stock) but over time it ends up being huge, and thats just one document. I have 200k documents and each have prices and stock objects.
Wondering if there's any way I could keep those object size to 30 fields max, that is, older entries purged on reaching the limit?
Take a look here! You can do it with a combination of $push and $slice, the example on the link sort the array too, don't know if that is your need, but I think you will have a good start from there.

PHP / Mongo geoJSON Loop is not valid

I'm passing in some coordinates to mongo to do a geo search. It works fine if the coordinates don't intersect (for instance a figure eight). But when two lines intersect it gives the loop is not valid. Is there any way to find the intersection and split all these loops up?
Note there could be many.
EDIT: I added the sample query and error. Note that I understand why it's happening, I'm just wondering if there is some known way to split those loops up into separate polygon's (some algorithm or within Mongo).
Query:
db.items.find({
"address.location": {
"$geoWithin": {
"$geometry": {
"type": "Polygon",
"coordinates": [[
[-97.209091, 49.905691],
[-97.206345, 49.918072],
[-97.178879, 49.919399],
[-97.165146, 49.907903],
[-97.164459, 49.892865],
[-97.180939, 49.889326],
[-97.197418, 49.895077],
[-97.200165, 49.902596],
[-97.203598, 49.919399],
[-97.216644, 49.928682],
[-97.244797, 49.927356],
[-97.255096, 49.913209],
[-97.209091, 49.905691]
]]
}
}
}
});
Error:
Error: error: {
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg" : "Loop is not valid: [
[ -97.209091, 49.905691 ]
[ -97.206345, 49.918072 ],
[ -97.17887899999999, 49.919399 ],
[ -97.16514599999999, 49.907903 ],
[ -97.16445899999999, 49.892865 ],
[ -97.180939, 49.889326 ],
[ -97.197418, 49.895077 ],
[ -97.200165, 49.902596 ],
[ -97.203598, 49.919399 ],
[ -97.216644, 49.928682 ],
[ -97.24479700000001, 49.927356 ],
[ -97.25509599999999, 49.913209 ],
[ -97.209091, 49.905691 ]
]
Edges 1 and 7 cross.
Edge locations in degrees: [-97.2063450, 49.9180720]-[-97.1788790, 49.9193990]
and [-97.2001650, 49.9025960]-[-97.2035980, 49.9193990]
",
"code" : 2
}
UPDATE
I've added an image of a brute force approach.
Polygon Slicing
Basically it's doing a look ahead on intersects.
If it finds one, it swaps out the points so that it stays within a loop.
It would add the cut off as a "starting point" in some queue.
When a look ahead goes around and finds it's own starting point we have a loop.
Then keep going through the "starting point" queue until it's empty.
The new set of polygons should contain all separate loops (in theory).
There are some issues with this though, it can get quite expensive going through all these loops. Say a max of 50 points would be about 1275 operations.
Also dealing with wrap around on 0/180 degreee coordinates could be a challenge.
Anyway, I didn't want to spend a whole day on this, I could even deal with a solution that does NOT deal with the wrap around condition.
Hoping there is a nice algorithm for this somewhere already I can just pop in (probably has some fancy technical term).
Also would be great if there was a more efficient approach then the brute force look-ahead.
It is because your coordinates are identical that creates an anomaly in the polygon shape: [-97.1788790, 49.9193990] and [-97.2035980, 49.9193990]. In your code remove or change any of the duplicate coordinate,
"coordinates": [[
[-97.209091, 49.905691],
[-97.206345, 49.918072],
[-97.178879, 49.919399], // this line
[-97.165146, 49.907903],
[-97.164459, 49.892865],
[-97.180939, 49.889326],
[-97.197418, 49.895077],
[-97.200165, 49.902596],
[-97.203598, 49.919399], // and this one
[-97.216644, 49.928682],
[-97.244797, 49.927356],
[-97.255096, 49.913209],
[-97.209091, 49.905691]
]]
As I mentioned in comments the better tool to query spatial data would be to use PostGIS.
For example PostGIS have the ST_validReason() to find the problem with polygon and a st_makevalid to fix but if this is not an option then I would create a service available to your PHP script with shapely python library https://github.com/Toblerity/Shapely
http://toblerity.org/shapely/manual.html
The first premise of Shapely is that Python programmers should be able
to perform PostGIS type geometry operations outside of an RDBMS
Certainly shapely is a popular tool, and you should get future help with in on StackOverflow or gis.stackexchange.com from more experienced users
I think the problem you are trying to solve is not as trivial as it sounds.
So I would perform following steps:
1 Find how to do it with shapely
similar questions:
Splitting self-intersecting polygon only returned one polygon in shapely in Python
2 create a simple php service which will pass query details to python script
3 Shapely will not prevent the creation of invalid polygon, but exceptions will be raised when they are operated on. So basically on such exception, I would call the script from step 1.
If this is just for one of query, I would just use QGIS software ( import points as CSV, create a new shapefile layer ( of type polygon ), and use Numerical vertex edit plugin )
update
I think the polygon should be fixed by the tool that created it so in this case, it should be a user. Maybe you fix that problem by looking from a different angle and if the shape is coming from the user and is drawn in GoogleMap in your app maybe enforce no intersection during drawing.
Found also this Drawing a Polygon ( sorting points and creating polygon with no intersection )

phpunit - Help needed about risky tests

i'm implementing some tests for a site.
In a particular test, this result occurred:
{
"event": "test",
"suite": "Example_V_test",
"test": "Example_V_test::test_3",
"status": "error",
"time": 13.469105958939,
"trace": [
{
"file": "\/opt\/lampp\/htdocs\/buy\/application\/tests\/phpunit.phar",
"line": 569,
"function": "main",
"class": "PHPUnit_TextUI_Command",
"type": "::"
}
],
"message": "Risky Test: Test code or tested code did not (only) close its own output buffers",
"output": ""
}R 3 / 3 (100%)
Time: 25.76 seconds, Memory: 59.25MB
There was 1 risky test:
1) Example_V_test::test_3
Test code or tested code did not (only) close its own output buffers
/opt/lampp/htdocs/buy/application/tests/phpunit.phar:569
OK, but incomplete, skipped, or risky tests!
My question is this: how to find the line of code that cause this 'issue'?
The message is reported if PHPUnit detects that the output buffering level at the end of a test method is different from the level at the beginning. This is due to the fact that it uses its own output buffer and checks that the test produces no output.
Because there is no option in PHPUnit to ignore this, you need to find the reason why the output buffering in your code was started but not ended (or ended too much) and fix that.
That might be really difficult to achieve because there is no hint, where output buffering is started, but you could use a full-text search on your source code (and libs) to identify candidates. Then look for Exceptions, which might break the normal program flow and so prevent ob_end_flush() (or similar) to be called.
Specifically to Test code or tested code did not (only) close its own output buffers
Recently ran into this.
Originally this SO question, and the answer was a solution for me. However, I didnt like the solution. There was a deeper reason as to why this was happening, and only on one test out of hundreds I have written...
After doing a little bit more digging, I was able to locate my issue stemmed from a #section() and #endsection call within my blade template.
Some how, through mistake the keyboard was struck and a random key was left there. This didn't cause issues visually, or any red flags to the end user when page rendered so it went unnoticed.
In my case -- My error was
#section('sudo')
...
#endsection2
removing the 2 fixed the original issue and allowed me to remove the hacky solution for
// // start hacky
// ob_start();
// ob_end_flush();
// ob_get_clean();
// // end hacky
ps. this may only fix a percentage of peoples issues that trip across this SO question. Sorry if it doesnt solve your issue :)
Mine is fixed by using:
ob_end_flush();
ob_get_clean();
At end of tests.
I ran into this error also:
Test code or tested code did not (only) close its own output buffers
It's useful to use ob_get_level() to determine the output buffer level at the start and end of the affected test. I put breakpoints in the test then used the console in PHPStorm to check this.
In my case, I was testing a method that opened a Symfony\Component\HttpFoundation\StreamedResponse. The level was 1 at the start of the test, but 0 at the end - so for me the solution was to add ob_start() at the end of the test.

Categories