Hi I'm new with PHP usually I work with Java, how actually the right way to write an array of many arguments/parameters in this insertSQL function as I have a lot SQL objects have to be inserted. Thank you
//Store User into MySQL DB
$res = $db->insertSQL(
$data[$i]->id,
$data[$i]->location_id,
$data[$i]->section_id,
$data[$i]->inspection_date,
$data[$i]->photo_entire_path,
$data[$i]->photo_isolator_path,
$data[$i]->pole_no,
$data[$i]->pole_iron,
$data[$i]->pole_iron,
$data[$i]->pole_iron);
//Based on inserttion, create JSON response
if($res){
$b["id"] = $data[$i]->id;
$b["status"] = 'yes';
array_push($a,$b);
}else{
$b["id"] = $data[$i]->id;
$b["status"] = 'no';
array_push($a,$b);
}
Right now it looks like this
$res = $db->insertSQL(
$data[$i]->id,
$data[$i]->location_id,
$data[$i]->section_id,
$data[$i]->inspection_date,
$data[$i]->photo_entire_path,
$data[$i]->photo_isolator_path,
$data[$i]->pole_no,
$data[$i]->pole_iron,
$data[$i]->pole_concrete,
$data[$i]->pole_wood,
$data[$i]->pole_condition_broken,
$data[$i]->pole_condition_tilt,
$data[$i]->pole_condition_shift,
$data[$i]->cros_arm_twist,
$data[$i]->cross_arm_rust,
$data[$i]->cross_arm_tilt,
$data[$i]->arm_tie_repair,
$data[$i]->arm_tie_rust,
$data[$i]->arm_tie_brace,
$data[$i]->isolator_fulcrum_r_leak,
$data[$i]->isolator_fulcrum_r_broken,
$data[$i]->isolator_fulcrum_s_leak,
$data[$i]->isolator_fulcrum_s_broken,
$data[$i]->isolator_fulcrum_t_leak,
$data[$i]->isolator_fulcrum_t_broken,
$data[$i]->isolator_pull_r_leak,
$data[$i]->isolator_pull_r_broken,
$data[$i]->isolator_pull_s_leak,
$data[$i]->isolator_pull_s_broken,
$data[$i]->isolator_pull_t_leak,
$data[$i]->isolator_pull_t_broken,
$data[$i]->arrester_r_broken,
$data[$i]->arrester_s_broken,
$data[$i]->arrester_t_broken,
$data[$i]->conductor_r_buyer,
$data[$i]->conductor_r_loose,
$data[$i]->conductor_s_buyer,
$data[$i]->conductor_s_loose,
$data[$i]->conductor_t_buyer,
$data[$i]->conductor_t_loose,
$data[$i]->connector_pg_r_35mm,
$data[$i]->connector_pg_r_70mm,
$data[$i]->connector_pg_r_150mm,
$data[$i]->connector_pg_s_35mm,
$data[$i]->connector_pg_s_70mm,
$data[$i]->connector_pg_s_150mm,
$data[$i]->connector_pg_t_35mm,
$data[$i]->connector_pg_t_70mm,
$data[$i]->connector_pg_t_150mm,
$data[$i]->bending_wire_r,
$data[$i]->bending_wire_s,
$data[$i]->bending_wire_t,
$data[$i]->ultrasonic_r,
$data[$i]->ultrasonic_s,
$data[$i]->ultrasonic_t,
$data[$i]->gws_exist,
$data[$i]->gws_not_exist,
$data[$i]->tree_exist,
$data[$i]->tree_not_exist,
$data[$i]->longitude,
$data[$i]->latitude,
$data[$i]->suggestion,
$data[$i]->descr
);
If I understand you correctly, this is what your function should look like:
function insertSQL(Array $sqlData)
{
Extract the values from the $sqlData variable here.
}
Try the following solution using call_user_func_array, func_get_args, json_encode and json_decode functions (to deal with an array of arguments):
...
$encoded = json_encode($data[$i]);
$fields_arr = json_decode($encoded, true); // to get an array of object properties with values.
// Also you should, probably, check the order of fields
$fields_arr['id'] = false;
$fields_arr = array_filter($fields_arr); // for removing the 'id' field
$args = ['id' => $data[$i]->id]; // first argument
call_user_func_array(array($db, 'insetSQL'), array_merge($args, $fields_arr)); // it also may require your current Namespace in the first arg
...
// Then, the 'insetSQL' method should process the arguments in the following manner:
function insetSQL($fields = []) {
$fields = func_get_args();
$id = $fields[0];
// adjusting the rest of fields ($fields[1], $fields[2] ...)
...
}
Although, I also would suggest to pass the initial object $data[$i] right into the insetSQL method as parameter and get the needed fields for sql INSERT statement
Create a JSON object as
$named_array = array(
"longitude" => "12.2"
"latitude" => "12.2"
);
$named_array = json_encode($named_array);
This $named_array can be array of fields.
You can pass $named_array object to other function as an argument.
Then use
$named_array = json_decode($named_array)
echo $named_array->longitude
You can access any Key Value pair as $named_array->latitude
Related
I'm struggling to find a way to convert my object to the correct format.
I want to replace a function that we currently use on generating detailed array, as you can see below everything is static.
private function departmentArray($content=[])
{
return [ static::$A_DEPT_ID => $content
, static::$O_DEPT_ID => $content
];
}
A sample result when that runs is this
{"3":{"complete":0,"incomplete":0},"5":{"complete":0,"incomplete":0}}
I converted the method
private function departmentArray($content=[])
{
$depts = d::getAllMainDepartment();
$dept_array = [];
foreach ($depts as $dept) {
$dept_array[] = array($dept->id => $content);
}
return $dept_array;
}
The resulting format looks like this
[{"3":{"complete":0,"incomplete":0}},{"5":{"complete":0,"incomplete":0}}]
How can I maintain the same format on the first version of code?
You don't push into an associative array, you use the new key as an index.
$dept_array[$dept->id] = $content;
SOLVED
I have route that does a POST route towards store() in the controller.
I'm trying to test if the action is working properly.
Controller:
public function store() {
$d= Input::json()->all();
//May need to check here if authorized
$foo= new Foo;
$d = array();
$d['name'] = $d['name'];
$d['address'] = $d['address'];
$d['nickname'] = $d['nickname'];
if($foo->validate($d))
{
$new_foo= $foo->create($d);
return Response::json(Foo::where('id','=',$new_foo->id)->first(),200);
}
else
{
return Response::json($foo->errors(),400);
}
}
Now I'm trying to test this using a new class called FooTest.php
Here is the function i'm currently trying to do to make the check work:
public function testFooCreation()
{
$jsonString = '{"address": "82828282", "email": "test#gmail.com", "name":"Tester"}';
$json = json_decode($jsonString);
$this->client->request('POST', 'foo');
$this->assertEquals($json, $this->client->getResponse());
}
when I run phpunit in my cmd, I get an error stating that "name" is undefined. I know i'm not actually passing anything to the request so I'm positive that nothing is actually being checked, but my question is how do I actually pass my json strings to check?
Everytime I put the $json inside the client request, it asks for an array, but when I convert my json string to an array, json_decode wants a string.
UPDATE
I was messing around with the passing of input data and I came across this:
$input = [
'name' => 'TESTNAME',
'address' => '299 TESTville',
'nickname' => 't'
];
Input::replace($input);
Auth::shouldReceive('attempt')
->with(array('name' => Input::get('name'),
'address' => Input::get('address'),
'nickname' => Input::get('nickname')))
->once()
->andReturn(true);
$response = $this->call('POST', 'foo', $input);
$content = $response->getContent();
$data = json_decode($response->getContent());
But whenever I run the test, i still get "name:undefined" It's still not passing the input i've created.
$d= Input::json()->all();
The above statement gets Input in $d.
$d = array();
Now the last statement again initialises $d as an empty new array.
So there is no: $['name'] . Hence, Undefined.
I think, that's the problem with the above code.
Hope it helps :)
I was able to pass the input into a POST route from the test.
public function testFooCreation(){
$json = '{"name":"Bar", "address":"FooLand", "nickname":"foobar"}';
$post = $this->action('POST', 'FooController#store', null, array(), array(), array(), $json);
if($this->assertTrue($this->client->getResponse()->isOk()) == true && $this->assertResponseStatus(201)){
echo "Test passed";
}
}
Turns out that in order for me to actually pass input into the controller through test POST, I have to pass it through the 7th parameter.
I hope this helps others.
of course you get an error , just look at your code
$aInputs = Input::json()->all();
//May need to check here if authorized
$foo= new Foo;
$d = array();
$d['name'] = $d['name'];
$d['address'] = $d['address'];
$d['nickname'] = $d['nickname'];
your assigning the array to it self, which is empty
Is it possible to do in one line calling a method that returns an array() and directly get a value of this array ?
For example, instead of :
$response = $var->getResponse()->getResponseInfo();
$http_code = $response['http_code'];
echo $http_code;
Do something like this :
echo $var->getResponse()->getResponseInfo()['http_code'];
This example does not work, I get a syntax error.
If you're using >= PHP 5.4, you can.
Otherwise, you'll need to use a new variable.
What you can do is to pass the directly to your function. Your function should be such that if a variable name is passed to it, it should the value of that variable, else an array with all variables values.
You can do it as:
<?php
// pass your variable to the function getResponseInfo, for which you want the value.
echo $var->getResponse()->getResponseInfo('http_code');
?>
Your function:
<?php
// by default, it returns an array of all variables. If a variable name is passed, it returns just that value.
function getResponseInfo( $var=null ) {
// create your array as usual. let's assume it's $result
/*
$result = array( 'http_code'=>200,'http_status'=>'ok','content_length'=>1589 );
*/
if( isset( $var ) && array_key_exists( $var, $result ) ) {
return $result[ $var ];
} else {
return $result;
}
}
?>
Hope it helps.
Language itself does not support that for an array.
In case you can change what getResponseInfo() return:
You can create simple class, which will have array as an constructor parameter. Then define magical getter which will be just pulling the keys from the instance array
function __get($key)
{
return #content[$key]
}
Then you'll be able to do
echo $var->getResponse()->getResponseInfo()->http_code;
// or
echo $var->getResponse()->getResponseInfo()->$keyWhichIWant;
What i wrote is just proposal. The real __get method should have some check if the exists and so
I know that it is possible to call a function with a variable number of parameters with call_user_func_array() found here -> http://php.net/manual/en/function.call-user-func-array.php . What I want to do is nearly identical, but instead of a function, I want to call a PHP class with a variable number of parameters in it's constructor.
It would work something like the below, but I won't know the number of parameters, so I won't know how to instantiate the class.
<?php
//The class name will be pulled dynamically from another source
$myClass = '\Some\Dynamically\Generated\Class';
//The parameters will also be pulled from another source, for simplicity I
//have used two parameters. There could be 0, 1, 2, N, ... parameters
$myParameters = array ('dynamicparam1', 'dynamicparam2');
//The instantiated class needs to be called with 0, 1, 2, N, ... parameters
//not just two parameters.
$myClassInstance = new $myClass($myParameters[0], $myParameters[1]);
You can do the following using ReflectionClass
$myClass = '\Some\Dynamically\Generated\a';
$myParameters = array ('dynamicparam1', 'dynamicparam2');
$reflection = new \ReflectionClass($myClass);
$myClassInstance = $reflection->newInstanceArgs($myParameters);
PHP manual: http://www.php.net/manual/en/reflectionclass.newinstanceargs.php
Edit:
In php 5.6 you can achieve this with Argument unpacking.
$myClass = '\Some\Dynamically\Generated\a';
$myParameters = ['dynamicparam1', 'dynamicparam2'];
$myClassInstance = new $myClass(...$myParameters);
I implement this approach a lot when function args are > 2, rather then end up with an Christmas list of arguments which must be in a specific order, I simply pass in an associative array. By passing in an associative array, I can check for necessary and optional args and handle missing values as needed. Something like:
class MyClass
{
protected $requiredArg1;
protected $optionalArg1;
public function __construct(array $options = array())
{
// Check for a necessary arg
if (!isset($options['requiredArg1'])) {
throw new Exception('Missing requiredArg1');
}
// Now I can just localize
$requiredArg1 = $options['requiredArg1'];
$optionalArg1 = (isset($options['optionalArg1'])) ? $options['optionalArg1'] : null;
// Now that you have localized args, do what you want
$this->requiredArg1 = $requiredArg1;
$this->optionalArg1 = $optionalArg1;
}
}
// Example call
$class = 'MyClass';
$array = array('requiredArg1' => 'Foo!', 'optionalArg1' => 'Bar!');
$instance = new $class($array);
var_dump($instance->getRequiredArg1());
var_dump($instance->getOptionalArg1());
I highly recommend using an associative array, however it is possible to use a 0-index array. You will have to be extremely careful when constructing the array and account for indices that have meaning, otherwise you will pass in an array with offset args and wreck havoc with your function.
You can do that using func_get_args().
class my_class {
function __construct( $first = NULL ) {
$params = func_get_args();
if( is_array( $first ) )
$params = $first;
// the $params array will contain the
// arguments passed to the child function
foreach( $params as $p )
echo "Param: $p\n";
}
}
function my_function() {
$instance = new my_class( func_get_args() );
}
echo "you can still create my_class instances like normal:";
$instance = new my_class( "one", "two", "three" );
echo "\n\n\n";
echo "but also through my_function:";
my_function( "one", "two", "three" );
Basically, you simply pass the result of func_get_args to the constructor of your class, and let it decide whether it is being called with an array of arguments from that function, or whether it is being called normally.
This code outputs
you can still create my_class instances like normal:
Param: one
Param: two
Param: three
but also through my_function:
Param: one
Param: two
Param: three
Hope that helps.
I've found here
Is there a call_user_func() equivalent to create a new class instance?
the example:
function createInstance($className, array $arguments = array())
{
if(class_exists($className)) {
return call_user_func_array(array(
new ReflectionClass($className), 'newInstance'),
$arguments);
}
return false;
}
But can somebody tell me if there is an example for classes with protected constructors?
A comment on the PHP manual states:
If you are using this method, remember
that the array of arguments need to be
passed in with the ordering being the
same order that the SOAP endpoint
expects.
e.g
//server expects: Foo(string name, int age)
//won't work
$args = array(32, 'john');
$out = $client->__soapCall('Foo', $args);
//will work
$args = array('john', 32);
$out = $client->__soapCall('Foo', $args);
I'm building a SOAP client that dynamically assigns the argument values, which means that it happens that the arguments aren't always in the correct order. This then breaks the actual SOAP call.
Is there an easy solution to this, short of checking the order of the parameters for each call?
I had the same problem where I dynamically added the SOAP parameters and I had to get them in the correct order for my SOAP call to work.
So I had to write something that will get all the SOAP methods from the WSDL and then determine in which order to arrange the method arguments.
Luckily PHP makes it easy to get the SOAP functions using the '$client->__getFunctions()' method, so all you need to do is search for the service method you want to call which will contain the method arguments in the correct order and then do some array matching to get your request parameter array in the same order.
Here is the code...
<?php
// Instantiate the soap client
$client = new SoapClient("http://localhost/magento/api/v2_soap?wsdl", array('trace'=>1));
$wsdlFunctions = $client->__getFunctions();
$wsdlFunction = '';
$requestParams = NULL;
$serviceMethod = 'catalogProductInfo';
$params = array('product'=>'ch124-555U', 'sessionId'=>'eeb7e00da7c413ceae069485e319daf5', 'somethingElse'=>'xxx');
// Search for the service method in the wsdl functions
foreach ($wsdlFunctions as $func) {
if (stripos($func, "{$serviceMethod}(") !== FALSE) {
$wsdlFunction = $func;
break;
}
}
// Now we need to get the order in which the params should be called
foreach ($params as $k=>$v) {
$match = strpos($wsdlFunction, "\${$k}");
if ($match !== FALSE) {
$requestParams[$k] = $match;
}
}
// Sort the array so that our requestParams are in the correct order
if (is_array($requestParams)) {
asort($requestParams);
} else {
// Throw an error, the service method or param names was not found.
die('The requested service method or parameter names was not found on the web-service. Please check the method name and parameters.');
}
// The $requestParams array now contains the parameter names in the correct order, we just need to add the values now.
foreach ($requestParams as $k=>$paramName) {
$requestParams[$k] = $params[$k];
}
try {
$test = $client->__soapCall($serviceMethod, $requestParams);
print_r($test);
} catch (SoapFault $e) {
print_r('Error: ' . $e->getMessage());
}
An easy solution exists for named parameters:
function checkParams($call, $parameters) {
$param_template = array(
'Foo' => array('name', 'age'),
'Bar' => array('email', 'opt_out'),
);
//If there's no template, just return the parameters as is
if (!array_key_exists($call, $param_template)) {
return $parameters;
}
//Get the Template
$template = $param_template[$call];
//Use the parameter names as keys
$template = array_combine($template, range(1, count($template)));
//Use array_intersect_key to filter the elements
return array_intersect_key($parameters, $template);
}
$parameters = checkParams('Foo', array(
'age' => 32,
'name' => 'john',
'something' => 'else'
));
//$parameters is now array('name' => 'john', 'age' => 32)
$out = $client->__soapCall('Foo', $parameters);
Not only does it correctly order the parameters, it also filters the parameters in the array.
Another solution is to verify the xsd files from your wsdl.
PHP SOAP construct the request based on parameters order in xsd files.