PHP: Get the timezone from where the application was accessed - php

Laravel 7.x
I need to access the timezone from where the application was accessed. I am only getting UTC in return from echo date_default_timezone_get();.
But I need to get something like Asia/Kolkata in return. For the access logs.

As said in the comments, you can only make a guess, but not have 100% sure.
I made some modifications to Chandra Nakka's function.
It will validate the guess with the timezone_identifiers_list() function, if it's not a valid timezone, the default will be returned.
function set_timezone_by_client_location($ip = NULL, $deep_detect = TRUE) {
$output = NULL;
if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
$ip = $_SERVER["REMOTE_ADDR"];
if ($deep_detect) {
if (filter_var(#$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
if (filter_var(#$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
}
$continents = array(
"AF" => "Africa",
"AN" => "Antarctica",
"AS" => "Asia",
"EU" => "Europe",
"OC" => "Australia",
"NA" => "America",
"SA" => "America"
);
$timezone = null;
if (filter_var($ip, FILTER_VALIDATE_IP)) {
$ipdat = #json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
if (#strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
$timezone = #$continents[strtoupper($ipdat->geoplugin_continentCode)] . '/' . #$ipdat->geoplugin_regionName;
$timezone = str_replace(' ', '_', $timezone);
}
}
if(!in_array($timezone, timezone_identifiers_list())){
$timezone = date_default_timezone_get();
}
return $timezone;
}
$timezone = set_timezone_by_client_location();
echo $timezone;

Framework: Laravel 7.x
namespace App\Services;
class TimeZoneService
{
/**
* Protected Variables
*/
protected $host = null;
protected $filter = null;
protected $ip = null;
protected $json = null;
public function __construct($ip, $filter = false)
{
$this->ip = $ip;
$this->filter = $filter;
$this->host = "http://www.geoplugin.net/json.gp?ip={$ip}";
}
/**
* fetch the location data via given IP address
*
* #return array
*/
public function getData()
{
if(function_exists('curl_init'))
{
# use cURL to fetch data
$request = curl_init();
# setting options
curl_setopt($request, CURLOPT_URL, $this->host);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.1');
# response
$this->json = curl_exec($request);
# closing cURL
curl_close($request);
}
else if (ini_get('allow_url_fopen'))
{
# fall back to fopen()
$this->json = file_get_contents($this->host, 'r');
}
else
{
trigger_error('geoPlugin Error: Cannot retrieve data.', E_USER_ERROR);
return;
}
# returning
return $this;
}
/**
* convert the json string to php array
* based on the filter property
*
* #return array
*/
public function toArray()
{
# condition(s)
if($this->filter != true)
{
return json_decode($this->json, true);
}
# filtering & returning
return $this->__filter();
}
/**
* convert the json string to php object
* based on the filter property
*
* #return object
*/
public function toObject()
{
# condition(s)
if($this->filter != true)
{
return json_decode($this->json);
}
# filtering & returning
return (object)$this->__filter();
}
/**
* return collected location data in the form of json
* based on the filter property
*
* #return string
*/
public function toJson()
{
# condition(s)
if($this->filter != true)
{
return $this->json;
}
# filtering & returning
return json_encode($this->__filter());
}
/**
* filter the object keys
*
* #return array
*/
private function __filter()
{
# applying filter
foreach(json_decode($this->json, true) as $key => $item)
{
$return[str_replace('geoplugin_', '', $key)] = $item;
}
# returning
return $return;
}
}
SomeController.php
/**
* Services
*/
use App\Services\TimeZoneService;
class SomeController extends Controller
{
public function someMethod()
{
# services
$TimeZone = new TimeZoneService(<string IP_ADDRESS>, <boolean FILTER>);
$locationData = $TimeZone->getData()->toJson();
# OR
$locationData = $TimeZone->getData()->toArray();
# OR
$locationData = $TimeZone->getData()->toObject();
...
}
}
This code is working fine for me. However, If you see any room for improvement then please feel free to update the code to help others.
Thank you.

Related

Symfony - DateTime as param

I have trouble defining my datetime param with Symfony.
I am trying to return null if last_scan_date is null
and if not to return results!
Error says:
Argument 2 passed to checkLastScan() must be an instance of DateTime, string given
My function:
public function checkLastScan($hash, \DateTime $lastScanDate)
{
$findLastScan = $this->getMyRepository()->findOneBy([
'hash' => $hash,
'lastScanDate' => $lastScanDate
]);
if (!$findLastScan) {
throw new \Exception('Not found!');
}
if ($lastScanDate === null) {
return null;
} else {
return $findLastScan;
}
}
and my call:
$this->requirePostParams(['hash', 'last_scan_date']);
$this->container->get('app')->checkLastScan(
$this->data['hash'],
$this->data['last_scan_date']
);
return $this->success();
And enitity:
/**
* #ORM\Column(name="last_scan_date", type="date", nullable=true)
*/
private $lastScanDate;
Maybe problem is when requiring post params like:
/**
* Require post params
*
* #param $params
*/
protected function requirePostParams($params)
{
$currentRequest = $this->get('request_stack')->getCurrentRequest();
$postData = $currentRequest->request->all();
$postContent = json_decode($currentRequest->getContent(), true);
if (!empty($postContent)) {
$postData = $postContent;
}
$this->data = $postData;
$missingParams = [];
foreach ($params as $param) {
if (!array_key_exists($param, $postData)) {
$missingParams[] = $param;
}
}
If $this->data (I asked at first but you did not answer to me properly) is just the POST array, of course all members of the array are treated as string.
You have to parse last_scan_date string and transform it to DateTime type.
Here is the code of the function (change the value of YOUR_POST_FORMAT to the format you use in your HTML form):
/**
* Require post params
*
* #param $params
*/
protected function requirePostParams($params)
{
$currentRequest = $this->get('request_stack')->getCurrentRequest();
$postData = $currentRequest->request->all();
$postContent = json_decode($currentRequest->getContent(), true);
if (!empty($postContent)) {
$postData = $postContent;
}
// HERE YOU PARSE STRING TO DATETIME TYPE
if (isset($postData['last_scan_date']) && !empty($postData['last_scan_date'])) {
$postData['last_scan_date'] = DateTime::createFromFormat('YOUR POST FORMAT', $postData['last_scan_date'])
} else {
$postData['last_scan_date'] = null;
}
$this->data = $postData;
$missingParams = [];
foreach ($params as $param) {
if (!array_key_exists($param, $postData)) {
$missingParams[] = $param;
}
}
}

How to convert an Object to an array in PHP7? [duplicate]

I'm integrating an API to my website which works with data stored in objects while my code is written using arrays.
I'd like a quick-and-dirty function to convert an object to an array.
Just typecast it
$array = (array) $yourObject;
From Arrays:
If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.
Example: Simple Object
$object = new StdClass;
$object->foo = 1;
$object->bar = 2;
var_dump( (array) $object );
Output:
array(2) {
'foo' => int(1)
'bar' => int(2)
}
Example: Complex Object
class Foo
{
private $foo;
protected $bar;
public $baz;
public function __construct()
{
$this->foo = 1;
$this->bar = 2;
$this->baz = new StdClass;
}
}
var_dump( (array) new Foo );
Output (with \0s edited in for clarity):
array(3) {
'\0Foo\0foo' => int(1)
'\0*\0bar' => int(2)
'baz' => class stdClass#2 (0) {}
}
Output with var_export instead of var_dump:
array (
'' . "\0" . 'Foo' . "\0" . 'foo' => 1,
'' . "\0" . '*' . "\0" . 'bar' => 2,
'baz' =>
stdClass::__set_state(array(
)),
)
Typecasting this way will not do deep casting of the object graph and you need to apply the null bytes (as explained in the manual quote) to access any non-public attributes. So this works best when casting StdClass objects or objects with only public properties. For quick and dirty (what you asked for) it's fine.
Also see this in-depth blog post:
Fast PHP Object to Array conversion
You can quickly convert deeply nested objects to associative arrays by relying on the behavior of the JSON encode/decode functions:
$array = json_decode(json_encode($nested_object), true);
From the first Google hit for "PHP object to assoc array" we have this:
function object_to_array($data)
{
if (is_array($data) || is_object($data))
{
$result = [];
foreach ($data as $key => $value)
{
$result[$key] = (is_array($value) || is_object($value)) ? object_to_array($value) : $value;
}
return $result;
}
return $data;
}
The source is at codesnippets.joyent.com.
To compare it to the solution of json_decode & json_encode, this one seems faster. Here is a random benchmark (using the simple time measuring):
$obj = (object) [
'name' =>'Mike',
'surname' =>'Jovanson',
'age' =>'45',
'time' =>1234567890,
'country' =>'Germany',
];
##### 100 000 cycles ######
* json_decode(json_encode($var)) : 4.15 sec
* object_to_array($var) : 0.93 sec
If your object properties are public you can do:
$array = (array) $object;
If they are private or protected, they will have weird key names on the array. So, in this case you will need the following function:
function dismount($object) {
$reflectionClass = new ReflectionClass(get_class($object));
$array = array();
foreach ($reflectionClass->getProperties() as $property) {
$property->setAccessible(true);
$array[$property->getName()] = $property->getValue($object);
$property->setAccessible(false);
}
return $array;
}
What about get_object_vars($obj)? It seems useful if you only want to access the public properties of an object.
See get_object_vars.
class Test{
const A = 1;
public $b = 'two';
private $c = test::A;
public function __toArray(){
return call_user_func('get_object_vars', $this);
}
}
$my_test = new Test();
var_dump((array)$my_test);
var_dump($my_test->__toArray());
Output
array(2) {
["b"]=>
string(3) "two"
["Testc"]=>
int(1)
}
array(1) {
["b"]=>
string(3) "two"
}
Type cast your object to an array.
$arr = (array) $Obj;
It will solve your problem.
Here is some code:
function object_to_array($data) {
if ((! is_array($data)) and (! is_object($data)))
return 'xxx'; // $data;
$result = array();
$data = (array) $data;
foreach ($data as $key => $value) {
if (is_object($value))
$value = (array) $value;
if (is_array($value))
$result[$key] = object_to_array($value);
else
$result[$key] = $value;
}
return $result;
}
All other answers posted here are only working with public attributes. Here is one solution that works with JavaBeans-like objects using reflection and getters:
function entity2array($entity, $recursionDepth = 2) {
$result = array();
$class = new ReflectionClass(get_class($entity));
foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
$methodName = $method->name;
if (strpos($methodName, "get") === 0 && strlen($methodName) > 3) {
$propertyName = lcfirst(substr($methodName, 3));
$value = $method->invoke($entity);
if (is_object($value)) {
if ($recursionDepth > 0) {
$result[$propertyName] = $this->entity2array($value, $recursionDepth - 1);
}
else {
$result[$propertyName] = "***"; // Stop recursion
}
}
else {
$result[$propertyName] = $value;
}
}
}
return $result;
}
To convert an object into array just cast it explicitly:
$name_of_array = (array) $name_of_object;
You can also create a function in PHP to convert an object array:
function object_to_array($object) {
return (array) $object;
}
Use:
function readObject($object) {
$name = get_class ($object);
$name = str_replace('\\', "\\\\", $name); // Outcomment this line, if you don't use
// class namespaces approach in your project
$raw = (array)$object;
$attributes = array();
foreach ($raw as $attr => $val) {
$attributes[preg_replace('('.$name.'|\*|)', '', $attr)] = $val;
}
return $attributes;
}
It returns an array without special characters and class names.
You can easily use this function to get the result:
function objetToArray($adminBar){
$reflector = new ReflectionObject($adminBar);
$nodes = $reflector->getProperties();
$out = [];
foreach ($nodes as $node) {
$nod = $reflector->getProperty($node->getName());
$nod->setAccessible(true);
$out[$node->getName()] = $nod->getValue($adminBar);
}
return $out;
}
Use PHP 5 or later.
Short solution of #SpYk3HH
function objectToArray($o)
{
$a = array();
foreach ($o as $k => $v)
$a[$k] = (is_array($v) || is_object($v)) ? objectToArray($v): $v;
return $a;
}
Here is my recursive PHP function to convert PHP objects to an associative array:
// ---------------------------------------------------------
// ----- object_to_array_recursive --- function (PHP) ------
// ---------------------------------------------------------
// --- arg1: -- $object = PHP Object - required --
// --- arg2: -- $assoc = TRUE or FALSE - optional --
// --- arg3: -- $empty = '' (Empty String) - optional --
// ---------------------------------------------------------
// ----- Return: Array from Object --- (associative) -------
// ---------------------------------------------------------
function object_to_array_recursive($object, $assoc=TRUE, $empty='')
{
$res_arr = array();
if (!empty($object)) {
$arrObj = is_object($object) ? get_object_vars($object) : $object;
$i=0;
foreach ($arrObj as $key => $val) {
$akey = ($assoc !== FALSE) ? $key : $i;
if (is_array($val) || is_object($val)) {
$res_arr[$akey] = (empty($val)) ? $empty : object_to_array_recursive($val);
}
else {
$res_arr[$akey] = (empty($val)) ? $empty : (string)$val;
}
$i++;
}
}
return $res_arr;
}
// ---------------------------------------------------------
// ---------------------------------------------------------
Usage example:
// ---- Return associative array from object, ... use:
$new_arr1 = object_to_array_recursive($my_object);
// -- or --
// $new_arr1 = object_to_array_recursive($my_object, TRUE);
// -- or --
// $new_arr1 = object_to_array_recursive($my_object, 1);
// ---- Return numeric array from object, ... use:
$new_arr2 = object_to_array_recursive($my_object, FALSE);
Custom function to convert stdClass to an array:
function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
} else {
// Return array
return $d;
}
}
Another custom function to convert Array to stdClass:
function arrayToObject($d) {
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return (object) array_map(__FUNCTION__, $d);
} else {
// Return object
return $d;
}
}
Usage Example:
// Create new stdClass Object
$init = new stdClass;
// Add some test data
$init->foo = "Test data";
$init->bar = new stdClass;
$init->bar->baaz = "Testing";
$init->bar->fooz = new stdClass;
$init->bar->fooz->baz = "Testing again";
$init->foox = "Just test";
// Convert array to object and then object back to array
$array = objectToArray($init);
$object = arrayToObject($array);
// Print objects and array
print_r($init);
echo "\n";
print_r($array);
echo "\n";
print_r($object);
First of all, if you need an array from an object you probably should constitute the data as an array first. Think about it.
Don't use a foreach statement or JSON transformations. If you're planning this, again you're working with a data structure, not with an object.
If you really need it use an object-oriented approach to have a clean and maintainable code. For example:
Object as array
class PersonArray implements \ArrayAccess, \IteratorAggregate
{
public function __construct(Person $person) {
$this->person = $person;
}
// ...
}
If you need all properties, use a transfer object:
class PersonTransferObject
{
private $person;
public function __construct(Person $person) {
$this->person = $person;
}
public function toArray() {
return [
// 'name' => $this->person->getName();
];
}
}
Also you can use The Symfony Serializer Component
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
$array = json_decode($serializer->serialize($object, 'json'), true);
You might want to do this when you obtain data as objects from databases:
// Suppose 'result' is the end product from some query $query
$result = $mysqli->query($query);
$result = db_result_to_array($result);
function db_result_to_array($result)
{
$res_array = array();
for ($count=0; $row = $result->fetch_assoc(); $count++)
$res_array[$count] = $row;
return $res_array;
}
This answer is only the union of the different answers of this post, but it's the solution to convert a PHP object with public or private properties with simple values or arrays to an associative array...
function object_to_array($obj)
{
if (is_object($obj))
$obj = (array)$this->dismount($obj);
if (is_array($obj)) {
$new = array();
foreach ($obj as $key => $val) {
$new[$key] = $this->object_to_array($val);
}
}
else
$new = $obj;
return $new;
}
function dismount($object)
{
$reflectionClass = new \ReflectionClass(get_class($object));
$array = array();
foreach ($reflectionClass->getProperties() as $property) {
$property->setAccessible(true);
$array[$property->getName()] = $property->getValue($object);
$property->setAccessible(false);
}
return $array;
}
Some impovements to the "well-knwon" code
/*** mixed Obj2Array(mixed Obj)***************************************/
static public function Obj2Array($_Obj) {
if (is_object($_Obj))
$_Obj = get_object_vars($_Obj);
return(is_array($_Obj) ? array_map(__METHOD__, $_Obj) : $_Obj);
} // BW_Conv::Obj2Array
Notice that if the function is member of a class (like above) you must change __FUNCTION__ to __METHOD__
For your case it was right/beautiful if you would use the "decorator" or "date model transformation" patterns. For example:
Your model
class Car {
/** #var int */
private $color;
/** #var string */
private $model;
/** #var string */
private $type;
/**
* #return int
*/
public function getColor(): int
{
return $this->color;
}
/**
* #param int $color
* #return Car
*/
public function setColor(int $color): Car
{
$this->color = $color;
return $this;
}
/**
* #return string
*/
public function getModel(): string
{
return $this->model;
}
/**
* #param string $model
* #return Car
*/
public function setModel(string $model): Car
{
$this->model = $model;
return $this;
}
/**
* #return string
*/
public function getType(): string
{
return $this->type;
}
/**
* #param string $type
* #return Car
*/
public function setType(string $type): Car
{
$this->type = $type;
return $this;
}
}
Decorator
class CarArrayDecorator
{
/** #var Car */
private $car;
/**
* CarArrayDecorator constructor.
* #param Car $car
*/
public function __construct(Car $car)
{
$this->car = $car;
}
/**
* #return array
*/
public function getArray(): array
{
return [
'color' => $this->car->getColor(),
'type' => $this->car->getType(),
'model' => $this->car->getModel(),
];
}
}
Usage
$car = new Car();
$car->setType('type#');
$car->setModel('model#1');
$car->setColor(255);
$carDecorator = new CarArrayDecorator($car);
$carResponseData = $carDecorator->getArray();
So it will be more beautiful and more correct code.
Converting and removing annoying stars:
$array = (array) $object;
foreach($array as $key => $val)
{
$new_array[str_replace('*_', '', $key)] = $val;
}
Probably, it will be cheaper than using reflections.
I use this (needed recursive solution with proper keys):
/**
* This method returns the array corresponding to an object, including non public members.
*
* If the deep flag is true, is will operate recursively, otherwise (if false) just at the first level.
*
* #param object $obj
* #param bool $deep = true
* #return array
* #throws \Exception
*/
public static function objectToArray(object $obj, bool $deep = true)
{
$reflectionClass = new \ReflectionClass(get_class($obj));
$array = [];
foreach ($reflectionClass->getProperties() as $property) {
$property->setAccessible(true);
$val = $property->getValue($obj);
if (true === $deep && is_object($val)) {
$val = self::objectToArray($val);
}
$array[$property->getName()] = $val;
$property->setAccessible(false);
}
return $array;
}
Example of usage, the following code:
class AA{
public $bb = null;
protected $one = 11;
}
class BB{
protected $two = 22;
}
$a = new AA();
$b = new BB();
$a->bb = $b;
var_dump($a)
Will print this:
array(2) {
["bb"] => array(1) {
["two"] => int(22)
}
["one"] => int(11)
}
There's my proposal, if you have objects in objects with even private members:
public function dismount($object) {
$reflectionClass = new \ReflectionClass(get_class($object));
$array = array();
foreach ($reflectionClass->getProperties() as $property) {
$property->setAccessible(true);
if (is_object($property->getValue($object))) {
$array[$property->getName()] = $this->dismount($property->getValue($object));
} else {
$array[$property->getName()] = $property->getValue($object);
}
$property->setAccessible(false);
}
return $array;
}
Since a lot of people find this question because of having trouble with dynamically access attributes of an object, I will just point out that you can do this in PHP: $valueRow->{"valueName"}
In context (removed HTML output for readability):
$valueRows = json_decode("{...}"); // Rows of unordered values decoded from a JSON object
foreach ($valueRows as $valueRow) {
foreach ($references as $reference) {
if (isset($valueRow->{$reference->valueName})) {
$tableHtml .= $valueRow->{$reference->valueName};
}
else {
$tableHtml .= " ";
}
}
}
I think it is a nice idea to use traits to store object-to-array converting logic. A simple example:
trait ArrayAwareTrait
{
/**
* Return list of Entity's parameters
* #return array
*/
public function toArray()
{
$props = array_flip($this->getPropertiesList());
return array_map(
function ($item) {
if ($item instanceof \DateTime) {
return $item->format(DATE_ATOM);
}
return $item;
},
array_filter(get_object_vars($this), function ($key) use ($props) {
return array_key_exists($key, $props);
}, ARRAY_FILTER_USE_KEY)
);
}
/**
* #return array
*/
protected function getPropertiesList()
{
if (method_exists($this, '__sleep')) {
return $this->__sleep();
}
if (defined('static::PROPERTIES')) {
return static::PROPERTIES;
}
return [];
}
}
class OrderResponse
{
use ArrayAwareTrait;
const PROP_ORDER_ID = 'orderId';
const PROP_TITLE = 'title';
const PROP_QUANTITY = 'quantity';
const PROP_BUYER_USERNAME = 'buyerUsername';
const PROP_COST_VALUE = 'costValue';
const PROP_ADDRESS = 'address';
private $orderId;
private $title;
private $quantity;
private $buyerUsername;
private $costValue;
private $address;
/**
* #param $orderId
* #param $title
* #param $quantity
* #param $buyerUsername
* #param $costValue
* #param $address
*/
public function __construct(
$orderId,
$title,
$quantity,
$buyerUsername,
$costValue,
$address
) {
$this->orderId = $orderId;
$this->title = $title;
$this->quantity = $quantity;
$this->buyerUsername = $buyerUsername;
$this->costValue = $costValue;
$this->address = $address;
}
/**
* #inheritDoc
*/
public function __sleep()
{
return [
static::PROP_ORDER_ID,
static::PROP_TITLE,
static::PROP_QUANTITY,
static::PROP_BUYER_USERNAME,
static::PROP_COST_VALUE,
static::PROP_ADDRESS,
];
}
/**
* #return mixed
*/
public function getOrderId()
{
return $this->orderId;
}
/**
* #return mixed
*/
public function getTitle()
{
return $this->title;
}
/**
* #return mixed
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* #return mixed
*/
public function getBuyerUsername()
{
return $this->buyerUsername;
}
/**
* #return mixed
*/
public function getCostValue()
{
return $this->costValue;
}
/**
* #return string
*/
public function getAddress()
{
return $this->address;
}
}
$orderResponse = new OrderResponse(...);
var_dump($orderResponse->toArray());
$Menu = new Admin_Model_DbTable_Menu();
$row = $Menu->fetchRow($Menu->select()->where('id = ?', $id));
$Addmenu = new Admin_Form_Addmenu();
$Addmenu->populate($row->toArray());
Here I've made an objectToArray() method, which also works with recursive objects, like when $objectA contains $objectB which points again to $objectA.
Additionally I've restricted the output to public properties using ReflectionClass. Get rid of it, if you don't need it.
/**
* Converts given object to array, recursively.
* Just outputs public properties.
*
* #param object|array $object
* #return array|string
*/
protected function objectToArray($object) {
if (in_array($object, $this->usedObjects, TRUE)) {
return '**recursive**';
}
if (is_array($object) || is_object($object)) {
if (is_object($object)) {
$this->usedObjects[] = $object;
}
$result = array();
$reflectorClass = new \ReflectionClass(get_class($this));
foreach ($object as $key => $value) {
if ($reflectorClass->hasProperty($key) && $reflectorClass->getProperty($key)->isPublic()) {
$result[$key] = $this->objectToArray($value);
}
}
return $result;
}
return $object;
}
To identify already used objects, I am using a protected property in this (abstract) class, named $this->usedObjects. If a recursive nested object is found, it will be replaced by the string **recursive**. Otherwise it would fail in because of infinite loop.
By using typecasting you can resolve your problem.
Just add the following lines to your return object:
$arrObj = array(yourReturnedObject);
You can also add a new key and value pair to it by using:
$arrObj['key'] = value;

Detect if there is an exception ( 404, 500, ....) before sending response in symfony php

I'm developing an application with symfony3, in which I'm tracking routes and saving them into my db with a service. This service is saving also a null route when there is an exception ( 500 , 404 ... ) so I want to add something to avoid saving this.
This is my service:
<?php
namespace Alpha\VisitorTrackingBundle\EventListener;
use Alpha\VisitorTrackingBundle\Entity\Lifetime;
use Alpha\VisitorTrackingBundle\Entity\PageView;
use Alpha\VisitorTrackingBundle\Entity\Session;
use Doctrine\ORM\EntityManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Class VisitorTrackingSubscriber
* #package Alpha\VisitorTrackingBundle\EventListener
*
* Tracks the source of a session and each pageview in that session. Primary use of this is to be able to see the path through our site each
*/
class VisitorTrackingSubscriber implements EventSubscriberInterface
{
/**
* #var \Doctrine\ORM\EntityManager
*/
protected $em;
/**
* #var Session
*/
protected $session;
/**
* #var Lifetime
*/
protected $lifetime;
protected $routesExclues;
protected $utmCodes = [
"utm_source",
"utm_medium",
"utm_campaign",
"utm_term",
"utm_content"
];
const COOKIE_LIFETIME = "lifetime";
const COOKIE_SESSION = "session";
public function __construct(EntityManager $em, $routesExclues)
{
$this->em = $em;
$this->routesExclues = $routesExclues;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (substr($request->get("_route"), 0, 1) == "_" ) {
//these are requests for assets/symfony toolbar etc. Not relevant for our tracking
return;
}
if ($request->cookies->has(self::COOKIE_SESSION) and !$this->requestHasUTMParameters($request)) {
$this->session = $this->em->getRepository("AlphaVisitorTrackingBundle:Session")->find($request->cookies->get(self::COOKIE_SESSION));
if ($this->session instanceof Session) {
$this->lifetime = $this->session->getLifetime();
} else {
$this->generateSessionAndLifetime($request);
}
} else {
$this->generateSessionAndLifetime($request);
}
}
public function onKernelResponse(FilterResponseEvent $event)
{
if (false === $this->session instanceof Session) {
return;
}
$request = $event->getRequest();
$response = $event->getResponse();
$exception = $event->getKernel()->handle($request)->getStatusCode();
if (substr($request->get("_route"), 0, 1) == "_" or ($response instanceof RedirectResponse) ) {
//these are requests for assets/symfony toolbar etc. Not relevant for our tracking
return;
}
$pageView = new PageView();
$route = $request->get('_route');
$pageView->setUrl($route);
if (strpos($request->getRequestUri(), $this->routesExclues) === false // && $response->getStatusCode() === '200'
)
$this->session->addPageView($pageView);
$this->em->flush($this->session);
if ($this->requestHasUTMParameters($request)) {
$this->setUTMSessionCookies($request, $response);
}
if (!$request->cookies->has(self::COOKIE_LIFETIME)) {
$response->headers->setCookie(new Cookie(self::COOKIE_LIFETIME, $this->lifetime->getId(), 0, "/", null, false, false));
}
//no session cookie set OR session cookie value != current session ID
if (!$request->cookies->has(self::COOKIE_SESSION) or ($request->cookies->get(self::COOKIE_SESSION) != $this->session->getId())) {
$response->headers->setCookie(new Cookie(self::COOKIE_SESSION, $this->session->getId(), 0, "/", null, false, false));
}
}
protected function requestHasUTMParameters(Request $request)
{
foreach ($this->utmCodes as $code) {
if ($request->query->has($code)) {
return true;
}
}
return false;
}
protected function setUTMSessionCookies(Request $request, Response $response)
{
foreach ($this->utmCodes as $code) {
$response->headers->clearCookie($code);
if ($request->query->has($code)) {
$response->headers->setCookie(new Cookie($code, $request->query->get($code), 0, "/", null, false, false));
}
}
}
/**
* #return Session
*/
public function getSession()
{
return $this->session;
}
/**
* #return Lifetime
*/
public function getLifetime()
{
return $this->lifetime;
}
public static function getSubscribedEvents()
{
return array(
KernelEvents::RESPONSE => ['onKernelResponse', 1024],
KernelEvents::REQUEST => ["onKernelRequest", 16]
);
}
private function generateSessionAndLifetime(Request $request)
{
if (strpos($request->headers->get("User-Agent"), "bot") === false && $request->getClientIp() !== "::1"
) {
$lifetime = false;
if ($request->cookies->has(self::COOKIE_LIFETIME)) {
$lifetime = $this->em->getRepository("AlphaVisitorTrackingBundle:Lifetime")->find($request->cookies->get(self::COOKIE_LIFETIME));
}
if (!$lifetime) {
$lifetime = new Lifetime();
$this->em->persist($lifetime);
}
$session = new Session();
$session->setIp($request->getClientIp() ?: "");
$session->setReferrer($request->headers->get("Referer") ?: "");
$session->setUserAgent($request->headers->get("User-Agent") ?: "");
$session->setQueryString($request->getQueryString() ?: "");
$session->setLoanTerm($request->query->get("y") ?: "");
$session->setRepApr($request->query->has("r") ? hexdec($request->query->get("r")) / 100 : "");
$details = json_decode(file_get_contents("http://ipinfo.io/{$request->getClientIp()}/json"));
$names = json_decode(file_get_contents("http://country.io/names.json"), true);
$session->setPays($names[$details->country]);
foreach ($this->utmCodes as $code) {
$method = "set" . \Doctrine\Common\Inflector\Inflector::classify($code);
$session->$method($request->query->get($code) ?: "");
}
$lifetime->addSession($session);
$this->em->flush();
$this->session = $session;
$this->lifetime = $lifetime;
}
}
}
I have added something to get statusCode from response but it always
returns 200. ( if statusCode !== '200' it must not save route).
Anyone to help me to get an idea or a solution to my problem? Thanks

Failed to load function

i tried to generate URL with load another class
the error messages is :
Notice: Undefined variable: user in
/var/www/html/lab/system/core/Router.php on line 260
Warning: call_user_func_array() expects parameter 1 to be a valid
callback, first array member is not a valid class name or object in
/var/www/html/lab/system/core/Router.php on line 260
Router.php :
<?php
require_once 'class.php';
class AltoRouter {
/**
* #var array Array of all routes (incl. named routes).
*/
protected $routes = array();
/**
* #var array Array of all named routes.
*/
protected $namedRoutes = array();
/**
* #var string Can be used to ignore leading part of the Request URL (if main file lives in subdirectory of host)
*/
protected $basePath = '';
/**
* #var array Array of default match types (regex helpers)
*/
protected $matchTypes = array(
'i' => '[0-9]++',
'a' => '[0-9A-Za-z]++',
'h' => '[0-9A-Fa-f]++',
'*' => '.+?',
'**' => '.++',
'' => '[^/\.]++'
);
/**
* Create router in one call from config.
*
* #param array $routes
* #param string $basePath
* #param array $matchTypes
*/
public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) {
$this->addRoutes($routes);
$this->setBasePath($basePath);
$this->addMatchTypes($matchTypes);
}
/**
* Retrieves all routes.
* Useful if you want to process or display routes.
* #return array All routes.
*/
public function getRoutes() {
return $this->routes;
}
/**
* Add multiple routes at once from array in the following format:
*
* $routes = array(
* array($method, $route, $target, $name)
* );
*
* #param array $routes
* #return void
* #author Koen Punt
* #throws Exception
*/
public function addRoutes($routes){
if(!is_array($routes) && !$routes instanceof Traversable) {
throw new \Exception('Routes should be an array or an instance of Traversable');
}
foreach($routes as $route) {
call_user_func_array(array($this, 'map'), $route);
}
}
/**
* Set the base path.
* Useful if you are running your application from a subdirectory.
*/
public function setBasePath($basePath) {
$this->basePath = $basePath;
}
/**
* Add named match types. It uses array_merge so keys can be overwritten.
*
* #param array $matchTypes The key is the name and the value is the regex.
*/
public function addMatchTypes($matchTypes) {
$this->matchTypes = array_merge($this->matchTypes, $matchTypes);
}
/**
* Map a route to a target
*
* #param string $method One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE)
* #param string $route The route regex, custom regex must start with an #. You can use multiple pre-set regex filters, like [i:id]
* #param mixed $target The target where this route should point to. Can be anything.
* #param string $name Optional name of this route. Supply if you want to reverse route this url in your application.
* #throws Exception
*/
public function map($method, $route, $target, $name = null) {
$this->routes[] = array($method, $route, $target, $name);
if($name) {
if(isset($this->namedRoutes[$name])) {
throw new \Exception("Can not redeclare route '{$name}'");
} else {
$this->namedRoutes[$name] = $route;
}
}
return;
}
/**
* Reversed routing
*
* Generate the URL for a named route. Replace regexes with supplied parameters
*
* #param string $routeName The name of the route.
* #param array #params Associative array of parameters to replace placeholders with.
* #return string The URL of the route with named parameters in place.
* #throws Exception
*/
public function generate($routeName, array $params = array()) {
// Check if named route exists
if(!isset($this->namedRoutes[$routeName])) {
throw new \Exception("Route '{$routeName}' does not exist.");
}
// Replace named parameters
$route = $this->namedRoutes[$routeName];
// prepend base path to route url again
$url = $this->basePath . $route;
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
list($block, $pre, $type, $param, $optional) = $match;
if ($pre) {
$block = substr($block, 1);
}
if(isset($params[$param])) {
$url = str_replace($block, $params[$param], $url);
} elseif ($optional) {
$url = str_replace($pre . $block, '', $url);
}
}
}
return $url;
}
/**
* Match a given Request Url against stored routes
* #param string $requestUrl
* #param string $requestMethod
* #return array|boolean Array with route information on success, false on failure (no match).
*/
public function match($requestUrl = null, $requestMethod = null) {
$params = array();
$match = false;
// set Request Url if it isn't passed as parameter
if($requestUrl === null) {
$requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
}
// strip base path from request url
$requestUrl = substr($requestUrl, strlen($this->basePath));
// Strip query string (?a=b) from Request Url
if (($strpos = strpos($requestUrl, '?')) !== false) {
$requestUrl = substr($requestUrl, 0, $strpos);
}
// set Request Method if it isn't passed as a parameter
if($requestMethod === null) {
$requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
}
foreach($this->routes as $handler) {
list($method, $_route, $target, $name) = $handler;
$methods = explode('|', $method);
$method_match = false;
// Check if request method matches. If not, abandon early. (CHEAP)
foreach($methods as $method) {
if (strcasecmp($requestMethod, $method) === 0) {
$method_match = true;
break;
}
}
// Method did not match, continue to next route.
if(!$method_match) continue;
// Check for a wildcard (matches all)
if ($_route === '*') {
$match = true;
} elseif (isset($_route[0]) && $_route[0] === '#') {
$pattern = '`' . substr($_route, 1) . '`u';
$match = preg_match($pattern, $requestUrl, $params);
} else {
$route = null;
$regex = false;
$j = 0;
$n = isset($_route[0]) ? $_route[0] : null;
$i = 0;
// Find the longest non-regex substring and match it against the URI
while (true) {
if (!isset($_route[$i])) {
break;
} elseif (false === $regex) {
$c = $n;
$regex = $c === '[' || $c === '(' || $c === '.';
if (false === $regex && false !== isset($_route[$i+1])) {
$n = $_route[$i + 1];
$regex = $n === '?' || $n === '+' || $n === '*' || $n === '{';
}
if (false === $regex && $c !== '/' && (!isset($requestUrl[$j]) || $c !== $requestUrl[$j])) {
continue 2;
}
$j++;
}
$route .= $_route[$i++];
}
$regex = $this->compileRoute($route);
$match = preg_match($regex, $requestUrl, $params);
}
if(($match == true || $match > 0)) {
if($params) {
foreach($params as $key => $value) {
if(is_numeric($key)) unset($params[$key]);
}
}
return array(
'target' => $target,
'params' => $params,
'name' => $name
);
}
}
return false;
}
/**
* Compile the regex for a given route (EXPENSIVE)
*/
private function compileRoute($route) {
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
$matchTypes = $this->matchTypes;
foreach($matches as $match) {
list($block, $pre, $type, $param, $optional) = $match;
if (isset($matchTypes[$type])) {
$type = $matchTypes[$type];
}
if ($pre === '.') {
$pre = '\.';
}
//Older versions of PCRE require the 'P' in (?P<named>)
$pattern = '(?:'
. ($pre !== '' ? $pre : null)
. '('
. ($param !== '' ? "?P<$param>" : null)
. $type
. '))'
. ($optional !== '' ? '?' : null);
$route = str_replace($block, $pattern, $route);
}
}
return "`^$route$`u";
}
}
$router = new AltoRouter();
$user = new Users();
$router->setBasePath('/lab/system/core/Router.php');
############################################################
$router->map('GET', '/home', function()
{
echo 'Home page';
}, 'home');
$router->map('GET', '/user', function()
{
call_user_func_array(array($user, 'user'), array());
}, 'user');
############################################################
$match = $router->match();
if($match && is_callable($match['target']))
{
call_user_func_array($match['target'], $match['params']);
}
else
{
echo "404 Not Found";
}
?>
class.php
<?php
class Users
{
function user()
{
echo "User page";
}
}
?>
anyone can explain it? thanks
did you try this ?
$router->map('GET', '/user', function($user)
{
call_user_func_array(array($user, 'user'), array());
}, 'user');

Empty response from Zend_Amf_Server

When I call endpoint from flash all the action is done well but the response is empty. The code is:
class AmfController extends Zend_Controller_Action {
public function indexAction()
{
$server = new Zend_Amf_Server();
$server->setProduction(false);
$server->setClass('Application_Model_Amf');
$response = $server->handle();
echo $response;
}
}
and
class Application_Model_Amf {
/**
*
* #param bytearray $data
* #param string $dateString
* #return int
*/
public function save($data, $dateString)
{
$dateString = str_replace(array('|', ':'), array('_', ''), $dateString);
//file_put_contents("$dateString.jpg", $data);
$r = new stdClass();
$r->error = 0;
return $r;
}
}
I also tried
public function save($data, $dateString)
{
$dateString = str_replace(array('|', ':'), array('_', ''), $dateString);
//file_put_contents("$dateString.jpg", $data);
return true;
}
but it worked neither - still empty response. How can I return response like this stdClass() ? Or only integer value 1 or 0?
The solution is to add die()
public function indexAction()
{
$server = new Zend_Amf_Server();
$server->setProduction(false);
$server->setClass('Application_Model_Amf');
$response = $server->handle();
echo $response;
die();
}

Categories