Class function as array value - php

i want to place an class function as array value, but it shows parsing error:
Linter error message - Expecting `']"
Php error message -
ErrorException [ Parsing Error ]: syntax error, unexpected '['
Example
'name' => [
'data_type' => 'varchar',
'label' => Lang::get('site.general.name'),
...
As you see, i want the label to be the value returned from the "lang" class "get" function.

Array short syntax [i.e. $a = ['a', 'b']] is available from version 5.4 on.
Be sure to have the right PHP version to use it, otherwise you should stick to old array('a', 'b') syntax.

You have a syntax error on key name line.
Try syntax like this:
$array = array(
'name' => 'val',
'data_type' => 'varchar',
'label' => Lang::get('site.general.name'),
'array' => array(
...
)
);
And be sure that Lang::get('site.general.name') return a value.

Change [ to array( like this:
'name' => array(
'data_type' => 'varchar',
'label' => Lang::get('site.general.name'),
[] for defining arrays isn't supported in older versions.

Related

phpseclib asn1 encoding structure

I'm trying to use phpseclib ASN1.php and i have a map like below;
$IdentityObjectMap = array('type' =>FILE_ASN1_TYPE_SEQUENCE,
'children'=> array(
'identityIdentificationData' => array('type'=>FILE_ASN1_TYPE_SEQUENCE,
'children'=> array(
'version' => array('type' => FILE_ASN1_TYPE_IA5_STRING),
'staticData' =>array('type' => FILE_ASN1_TYPE_SEQUENCE,
'children'=> array(
'acceptedPolicyVersion' => array('type' =>FILE_ASN1_TYPE_IA5_STRING),
'cardHolderID' => array('type' =>FILE_ASN1_TYPE_INTEGER),
'deviceSerialNumber' => array('type' => FILE_ASN1_TYPE_SEQUENCE,
'children'=> array(
'deviceType' => array('type' =>FILE_ASN1_TYPE_INTEGER),
'deviceUniqueID' => array('type' =>FILE_ASN1_TYPE_OCTET_STRING)
),
),
'appLabel' => array('type' =>FILE_ASN1_TYPE_UTF8_STRING),
'requestorRole' => array('type' => FILE_ASN1_TYPE_ENUMERATED,
'roleClient'=> array('mapping' =>0),
'roleParticipant' =>array('mapping' =>1)
),
'creationTime' => array('type' =>FILE_ASN1_TYPE_UTC_TIME)
)
)
)
)
)
);
And i have a json and using json_decode(IdentityObject,true) for this map like below;
json:
{
\"identityIdentificationData\":{
\"version\":\"2.0\",
\"staticData\":{
\"acceptedPolicyVersion\":\"2\",
\"cardHolderID\":11111111111,
\"deviceSerialNumber\":{
\"deviceType\":3,
\"deviceUniqueID\":\"11111111\"
},
\"appLabel\":\"examination\",
\"requestorRole\": \"roleClient\",
\"creationTime\": \"180319141236Z\"
}
}
}";
And this jsons output array :
array
'identityIdentificationData' =>
array
'version' => '2.0'
'staticData' =>
array
'acceptedPolicyVersion' => '2'
'cardHolderID' => 11111111111
'deviceSerialNumber' =>
array
'deviceType' => 3
'deviceUniqueID' => '11111111'
'appLabel' => 'examination'
'requestorRole' => 'roleClient'
'creationTime' => '180319141236Z'
What structure this array should be to i can successfully compile.
Final code which gives this error
Undefined index: children .../ASN1.php on line 950.
Final code:
$asn1->encodeDER($IdentityObject,$IdentityObjectMap);
In File/X509.php there's only one enumerated type and it' defined thusly:
$this->CRLReason = array('type' => FILE_ASN1_TYPE_ENUMERATED,
'mapping' => array(
'unspecified',
'keyCompromise',
'cACompromise',
'affiliationChanged',
'superseded',
'cessationOfOperation',
'certificateHold',
// Value 7 is not used.
8 => 'removeFromCRL',
'privilegeWithdrawn',
'aACompromise'
)
);
Your enumerated type definition doesn't include a mapping key. That's probably what you need. eg.
'requestorRole' => array('type' => FILE_ASN1_TYPE_ENUMERATED,
'mapping' => array(
'roleClient',
'roleParticipant'
)
),
That said, what version of phpseclib are you using? I tried your code with 1.0.10 (I think) and got a different error than the one you reported:
Fatal error: Uncaught Error: Call to a member function toBytes() on string
When I used my alternatively defined requestorRole definition I got this error message:
Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (180319141236Z) at position 11 (6)
I was able to fix that last error by replacing 'creationTime' => '180319141236Z' with 'creationTime': 'January 1, 2018'. 180319141236Z is closer to the format that X.509 certs use but phpseclib generates that value itself after running it through DateTime or strtotime (per https://github.com/phpseclib/phpseclib/blob/1.0.10/phpseclib/File/X509.php#L3420) and then formatting it appropriately. If you wanted to set it directly, yourself, check this out:
https://github.com/phpseclib/phpseclib/blob/1.0.10/phpseclib/File/X509.php#L3952
Here's my code:
https://pastebin.com/cmmSf6S6

How to create and pass an array of object to Blade's #include?

I'm new to Blade, and couldn't find how to do that.
Unfortunately, Blade's documentation doesn't help much, but I've already tried the following:
#include('module.my-component', [
'param' => [{
'x' => 'A',
'y' => false
}, {
'x' => 'B',
'y' => true
}]
])
As expected, I've got the following syntax error:
Parse error: syntax error, unexpected '{', expecting ']'
Does anybody know the correct syntax?
You have to use valid PHP syntax. This is not valid PHP:
[{'b' => 'a'}]
This is valid PHP though:
[
(object) ['x' => 'B', 'y' => true],
(object) [ ... ],
]
That would be an array of stdClass objects. When you cast an array to object you get an stdClass object with those properties.
Basically everything inside a Blade directive is a single 'string'/'expression' ... when blade compiles it replaces itself with PHP and that expression usually just gets dropped in directly, so it is all PHP in the end, so it has to be valid. You can open up any compiled view from the storage folder to see this in action.

IDE Displaying Error when [( Is Used in An Array Variable

I am using Aptana Studio 3 and it's highlighting the square brackets as a potential error. It inteferes with my coding because I keep thinking that am messing somewhere. The code is running well and no error is displayed on the console. How do I fix it?
$series_data[] = array(
'name' => 'Brands',
'colorByPoint' => true,
'data'=> array(
['name' => 'Nairobi, KE', 'y' => 56.33],
['name'=> 'Kisumu, KE', 'y'=>64.03 ],
['name'=> 'New Yorm, USA', 'y'=>44.03 ]
The error is displayed between ( and [ as below:
data=array([
The [...] array syntax is only valid in PHP 5.4 and higher, so possibly the problem is that your IDE is configured to expect PHP 5.3 syntax? Check the IDE's config.
Also, can I recommend that you be a bit more consistent in which array syntax you use? There's nothing inherently wrong with mixing between array(...) and [...] notations, but it does make the code harder to read.
Check this
you forgot to use )
$series_data[] = array(
'name' => 'Brands',
'colorByPoint' => true,
'data'=> array(
['name' => 'Nairobi, KE', 'y' => 56.33],
['name'=> 'Kisumu, KE', 'y'=>64.03 ],
['name'=> 'New Yorm, USA', 'y'=>44.03 ]
)
);

Expected argument of type "array or (\Traversable and \ArrayAccess) in Symfony collection field type

I get the message :
Expected argument of type "array or (\Traversable and \ArrayAccess)", "string" given
during the execution of this code:
$form = $this->createFormBuilder([], ['csrf_protection' => false])
->add('collectionField', 'collection', [
'type' => 'text',
'constraints' => [new Type('array')]]
)
->add('integerField', 'integer', [
'constraints' => [new Type('integer')]]
)
->getForm();
$form->submit(['collectionField' => 'error', 'integerField' => 'error']);
This is because I pass string data to "collectionField" field.
But when I pass array to "integerField":
$form->submit(['collectionField' => [], 'integerField' => []]);
I get a normal message "This value is not valid.".
I think the behaviour should be the same. Of course, I could make some work around and find some solution but I'd be interested to know if it's a bug or if I missed something.
Ok. Answer on my question by myself. It's a bug and will be fixed in the nearest Symfony release. https://github.com/symfony/symfony/pull/13851

PHP - Use range() as value for Array

I've seen a few example here on stack but they don't seem to cover this scenario.
I'm attempting this;
$flight_range = array(
array('range' => range(1,50), 'service' => 'Long Haul'),
array('range' => range(51,54), 'service' => 'Short Haul'),
....
);
but PHP won't let me. It returns;
Parse error: syntax error, unexpected '(', expecting ')' on line 02
This does not work either;
array(range(1,50), range(51,54) ...
The problem is with trying to assign a value of range().
I have 20+ sets of range values to assign.
Can anyone suggest an easy method for achieving these sorts of array values?
EDIT;
haike00, Jack and Sean are right.
Maybe my question should be how do i make $flight_range a member variable of a class;
private $flight_range = array(array('range' => range(1,50), 'service' => 'Long Haul'));
What is the problem with doing this in your constructor?
class MyClass {
private $flight_range;
public function __construct() {
$this->flight_range = array(
array(
'range' => range( 1, 50 ),
'service' => 'Long Haul'
)
);
}
}
Its working fine on my end.
$flight_range = array(
array('range' => range(1,50) , 'service' => 'Long Haul'), array('range' => range(51,54),'service' => 'Short Haul'));
print_r($flight_range);
Just copy and paste above code and run it.

Categories