Exemplo n.º 1
0
 public function testToISO8601UTC()
 {
     $newTZ = new DateTimeZone("America/New_York");
     $d = new DateTime('2012-01-01T12:12:12', $newTZ);
     $s = DateTimeHelper::toISO8601UTC($d);
     $this->assertEquals("2012-01-01T12:12:12", $s);
 }
 /**
  * This method is invoked after saving a record successfully.
  * The default implementation does nothing.
  * You may override this method to do postprocessing after record saving.
  *
  * @param mixed $data The model or action's data
  * @param mixed $configType the class name or instance of class that is configured in 'config/activityNotice.php'
  * @param string $action the action, the possible values: "add", "update", "batch"
  * @param mixed $attributeNames the attributes
  * @param array $modelList1 if $action is "batchSave", this will be the inserted models, if action is "batchDelete" and
  * there's multiple deletion executed, this will be the deleted models
  * @param array $modelList2 if $action is "batchSave", this will be the updated models, if action is "batchDelete", this parameter is ignored.
  * @return ActivityNotice The activity notice data that sent to AMQP Server. If the notification action is failed, FALSE will be returned.
  */
 public function noticeAfterModelAction($data, $configType, $action, $attributeNames = null, $modelList1 = null, $modelList2 = null)
 {
     if (is_object($configType)) {
         $classId = get_class($configType);
     } else {
         $classId = $configType;
     }
     $noticeAction = $action === 'delete' && is_array($modelList1) ? 'batchDelete' : $action;
     $serializer = $this->getSerializer();
     $config = $serializer->getActivityNoticeConfig($classId, $noticeAction, $attributeNames);
     if (!isset($config)) {
         return false;
     }
     $a = explode('\\', $classId);
     $shortClassId = array_pop($a);
     $notice = new ActivityNotice(['kind' => lcfirst($shortClassId) . 'AUD', 'dispatchTime' => DateTimeHelper::currentDateTime(), 'dispatcher' => $this->getDispatcher(), 'contentUpdatedFields' => $attributeNames]);
     if ($action === 'batchSave') {
         if (count($modelList1) > 0) {
             $notice->action = 'add';
             $notice->content = $serializer->getSerializeListData($modelList1, $config);
             $this->sendActivityNotice($notice);
         }
         if (count($modelList2) > 0) {
             $notice->action = 'update';
             $notice->content = $serializer->getSerializeListData($modelList2, $config);
             $this->sendActivityNotice($notice);
         }
     } else {
         $notice->action = $noticeAction;
         if ($noticeAction === 'batchDelete') {
             if (isset($modelList1)) {
                 $notice->content = $serializer->getSerializeListData($modelList1, $config);
             } else {
                 $notice->content = $data;
             }
         } elseif ($noticeAction === 'delete' && !empty($modelList1) && !is_array($modelList1)) {
             $notice->content = $serializer->getSerializeData($modelList1, $config);
         } else {
             $notice->content = $serializer->getSerializeData($data, $config);
         }
         $this->sendActivityNotice($notice);
     }
     return $notice;
 }
Exemplo n.º 3
0
 /**
  *
  * Pre-processes the data before sending it to `json_encode()`.
  * @param mixed $data the data to be processed
  * @return mixed the processed data
  *
  */
 protected static function processData($data)
 {
     if (is_object($data)) {
         if ($data instanceof DateTime) {
             $data = DateTimeHelper::toISO8601UTC($data);
         } elseif ($data instanceof JsonSerializable) {
             $data = $data->jsonSerialize();
         } else {
             $result = [];
             foreach ($data as $name => $value) {
                 $result[$name] = $value;
             }
             $data = $result;
         }
         if ($data === []) {
             return new stdClass();
         }
     }
     if (is_array($data)) {
         foreach ($data as $key => $value) {
             if (is_array($value) || is_object($value)) {
                 $data[$key] = static::processData($value);
             }
         }
     }
     return $data;
 }
 /**
  * Get configured serialize data of the original data.
  * @param $data
  * @param $config
  * @return array
  */
 public function getSerializeData($data, $config)
 {
     if (is_null($config) || !is_array($config)) {
         return null;
     } elseif (isset($config['serializeDelegateFunction'])) {
         $delegate = $config['serializeDelegateFunction'];
         if (is_string($delegate)) {
             if (is_object($data) && method_exists($data, $delegate)) {
                 return (array) call_user_func([$data, $delegate], $config);
             }
             return null;
         }
         return (array) call_user_func($delegate, $data, $config);
     } elseif (is_string($data)) {
         return $data;
     }
     if (isset($config['serializeAttributes'])) {
         $serializeAttributes = $config['serializeAttributes'];
     }
     if (isset($config['notSerializeAttributes'])) {
         $notSerializeAttributes = $config['notSerializeAttributes'];
     }
     if (!isset($serializeAttributes) && !isset($notSerializeAttributes)) {
         $serializeData = $data;
     } else {
         $serializeData = [];
         if (isset($serializeAttributes)) {
             foreach ($serializeAttributes as $attChain) {
                 $attChain = explode('.', $attChain);
                 if (!isset($notSerializeAttributes) || !in_array($attChain[0], $notSerializeAttributes)) {
                     $cnt = count($attChain);
                     $dt = $data;
                     $sd =& $serializeData;
                     $i = 0;
                     while ($i < $cnt - 1) {
                         $att = $attChain[$i];
                         if (is_object($dt) && property_exists($dt, $att)) {
                             if (!isset($sd[$att])) {
                                 if (empty($dt->{$att})) {
                                     $sd[$att] = null;
                                     break;
                                 }
                                 $sd[$att] = [];
                             }
                             $dt = $dt->{$att};
                         } elseif (is_array($dt) && array_key_exists($att, $dt)) {
                             if (!isset($sd[$att])) {
                                 $sd[$att] = [];
                             }
                             $dt = $dt[$att];
                         } else {
                             break;
                         }
                         $sd =& $sd[$att];
                         $i++;
                     }
                     if ($i == $cnt - 1) {
                         $att = $attChain[$i];
                         if (is_object($dt) && property_exists($dt, $att)) {
                             $sd[$att] = $dt->{$att};
                         } elseif (is_array($dt) && array_key_exists($att, $dt)) {
                             $sd[$att] = $dt[$att];
                         }
                         if ($sd[$att] instanceof \DateTime) {
                             /** @var \DateTime $date */
                             $date = $sd[$att];
                             $sd[$att] = DateTimeHelper::toISO8601UTC($date);
                         }
                     }
                 } else {
                     $serializeData[$attChain[0]] = null;
                 }
             }
         } else {
             if (is_object($data)) {
                 if (isset($this->getSerializableAttributesFunction)) {
                     $properties = call_user_func($this->getSerializableAttributesFunction, $this, $data);
                 } else {
                     $properties = $this->getSerializableAttributes($data);
                 }
                 foreach ($properties as $key => $prop) {
                     if ($prop instanceof ReflectionProperty) {
                         $att = $prop->name;
                     } else {
                         $att = $key;
                     }
                     if (!in_array($att, $notSerializeAttributes)) {
                         $serializeData[$att] = $data->{$att};
                     } else {
                         $serializeData[$att] = null;
                     }
                 }
             } elseif (is_array($data)) {
                 foreach ($data as $att => $value) {
                     if (!in_array($att, $notSerializeAttributes)) {
                         $serializeData[$att] = $value;
                     } else {
                         $serializeData[$att] = null;
                     }
                 }
             }
         }
     }
     return $serializeData;
 }