3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class ReminderPushEvents { /** * @var array */ private $properties; /** * @var string */ private $event = 'reminder_push_event'; /** * @var \DateTimeInterface */ private $timestamp; /** * Constructor. */ public function __construct() { $this->properties = ['customers' => []]; } /** * [ * 'customers' => [ * ['customerId' => 1213], * ['customerId' => 1212] * ] * ] * * @param array $properties * * @return $this */ public function addProperties(array $properties) { $this->properties['customers'] = $properties; return $this; } /** * @return array */ public function getProperties() { if (!$this->properties) { throw new \RuntimeException(); } return $this->properties; } /** * @return string */ public function getEvent() { if (!$this->event) { throw new \RuntimeException(); } return $this->event; } /** * @return \DateTimeInterface */ public function getTimestamp() { if (!$this->timestamp) { $this->timestamp = new \DateTimeImmutable(); } return $this->timestamp->format('c'); } } class EventRequest { /** * @var string */ private $providerId; /** * @var array */ private $events; /** * Constructor. */ public function __construct($providerId) { $this->providerId = $providerId; $this->events = []; } /** * @return string */ public function getProviderId() { return $this->providerId; } /** * @return array */ public function getEvents() { return $this->events; } /** * @param EventsInterface $events * * @return $this */ public function addEvents( $events) { $this->events[] = $events; return $this; } public function serialize($entity = null, $recursionDepth = 2) { $entity = $entity?: $this; $result = []; $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_array($value)) { foreach ($value as $name => $subValue) { if (is_object($subValue)) { if ($recursionDepth > 0) { $result[$propertyName][$name] = $this->serialize($subValue, $recursionDepth - 1); continue; } $result[$propertyName][$name] = "***"; continue; } if (is_array($subValue)) { $result[$propertyName][$name] = $subValue; continue; } $result[$propertyName][$name] = $subValue; } continue; } if (is_object($value)) { if ($recursionDepth > 0) { $result[$propertyName] = $this->serialize($value, $recursionDepth - 1); continue; } $result[$propertyName] = "***"; //stop recursion continue; } $result[$propertyName] = $value; } } return $result; } } $foo = new EventRequest('crm_newsletter'); $event = new ReminderPushEvents(); $event->addProperties( [['customerId' => 1213], ['customerId' => 1212]]); $foo->addEvents($event); $foo->addEvents($event); var_dump( $foo->serialize());
based on aMUn6
Output for 5.6.0 - 5.6.30, 7.0.0 - 7.3.0rc3
array(2) { ["providerId"]=> string(14) "crm_newsletter" ["events"]=> array(2) { [0]=> array(3) { ["properties"]=> array(1) { ["customers"]=> array(2) { [0]=> array(1) { ["customerId"]=> int(1213) } [1]=> array(1) { ["customerId"]=> int(1212) } } } ["event"]=> string(19) "reminder_push_event" ["timestamp"]=> string(25) "2017-07-04T11:12:10+02:00" } [1]=> array(3) { ["properties"]=> array(1) { ["customers"]=> array(2) { [0]=> array(1) { ["customerId"]=> int(1213) } [1]=> array(1) { ["customerId"]=> int(1212) } } } ["event"]=> string(19) "reminder_push_event" ["timestamp"]=> string(25) "2017-07-04T11:12:10+02:00" } } }
Output for hhvm-3.15.4
array(2) { ["providerId"]=> string(14) "crm_newsletter" ["events"]=> array(2) { [0]=> array(3) { ["properties"]=> array(1) { ["customers"]=> array(2) { [0]=> array(1) { ["customerId"]=> int(1213) } [1]=> array(1) { ["customerId"]=> int(1212) } } } ["event"]=> string(19) "reminder_push_event" ["timestamp"]=> string(25) "2017-07-04T11:12:11+02:00" } [1]=> array(3) { ["properties"]=> array(1) { ["customers"]=> array(2) { [0]=> array(1) { ["customerId"]=> int(1213) } [1]=> array(1) { ["customerId"]=> int(1212) } } } ["event"]=> string(19) "reminder_push_event" ["timestamp"]=> string(25) "2017-07-04T11:12:11+02:00" } } }