Skip to content

Commit

Permalink
Improve ActivityNoticeSerializer: support 'getSerializableAttributesF…
Browse files Browse the repository at this point in the history
…unction' callback
  • Loading branch information
Bui Sy Nguyen committed Aug 21, 2015
1 parent 7c508cc commit 8e38713
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
22 changes: 16 additions & 6 deletions fproject/amqp/ActivityNoticeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public function sendActivityNotice($notice)
return true;
}

/**
* Get the data serializer instance
*
* @return ActivityNoticeSerializer
* */
public function getSerializer()
{
return new ActivityNoticeSerializer($this->params);
}

/**
* This method is invoked after saving a record successfully.
* The default implementation does nothing.
Expand All @@ -73,9 +83,9 @@ public function noticeAfterModelAction($model, $configType, $action, $attributeN

$noticeAction = ($action === 'delete' && isset($modelList1)) ? 'batchDelete' : $action;

$helper = new ActivityNoticeSerializer($this->params);
$serializer = $this->getSerializer();

$config = $helper->getActivityNoticeConfig($classId, $noticeAction, $attributeNames);
$config = $serializer->getActivityNoticeConfig($classId, $noticeAction, $attributeNames);

if(!isset($config))
return;
Expand All @@ -94,23 +104,23 @@ public function noticeAfterModelAction($model, $configType, $action, $attributeN
if(count($modelList1) > 0)
{
$notice->action = 'add';
$notice->content = $helper->getSerializeListData($modelList1, $config);
$notice->content = $serializer->getSerializeListData($modelList1, $config);
$this->sendActivityNotice($notice);
}
if(count($modelList2) > 0)
{
$notice->action = 'update';
$notice->content = $helper->getSerializeListData($modelList2, $config);
$notice->content = $serializer->getSerializeListData($modelList2, $config);
$this->sendActivityNotice($notice);
}
}
else
{
$notice->action = $action;
if($action==='delete' && isset($modelList1))
$notice->content = $helper->getSerializeListData($modelList1, $config);
$notice->content = $serializer->getSerializeListData($modelList1, $config);
else
$notice->content = $helper->getSerializeData($model, $config);
$notice->content = $serializer->getSerializeData($model, $config);
$this->sendActivityNotice($notice);
}
}
Expand Down
39 changes: 21 additions & 18 deletions fproject/amqp/ActivityNoticeSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,16 @@ public function getSerializeData($data, $config)
{
if(is_object($data))
{
$reflection = new ReflectionClass($data);
$public = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
$static = $reflection->getProperties(ReflectionProperty::IS_STATIC);
$properties = array_diff($public, $static);
/** @var ReflectionProperty $prop */
foreach($properties as $prop)
if(isset($this->getSerializableAttributesFunction))
$properties = call_user_func($this->getSerializableAttributesFunction, $this, $data);
else
$properties = $this->getSerializableAttributes($data);
foreach($properties as $key=>$prop)
{
$att = $prop->name;
if($prop instanceof ReflectionProperty)
$att = $prop->name;
else
$att = $key;
if(!in_array($att, $notSerializeAttributes))
$serializeData[$att] = $data->{$att};
else
Expand All @@ -367,23 +369,24 @@ public function getSerializeData($data, $config)
}

/**
* Recursively get an associative array of class properties by property name => ReflectionProperty() object
* including inherited ones from extended classes
* @var callable a PHP callable that will be called to get an associative array of ReflectionProperty objects
* for serializable attributes of the data object
* @param string $classOrInstance Class name or an instance of the class
* @return ReflectionProperty[]
*/
public $getSerializableAttributesFunction;

/**
* Get an array of ReflectionProperty objects represents the serializable attributes of given class or instance
* @param string $classOrInstance Class name or an instance of the class
* @return array
*/
private function getClassPublicProperties($classOrInstance){
public function getSerializableAttributes($classOrInstance)
{
$reflection = new ReflectionClass($classOrInstance);
$public = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
$static = $reflection->getProperties(ReflectionProperty::IS_STATIC);
$properties = array_diff($public, $static);

if($parentClass = $reflection->getParentClass()){
$parentProps = $this->getClassPublicProperties($parentClass->getName());//RECURSION
if(count($parentProps) > 0)
$properties = array_merge($parentProps, $properties);
}
return $properties;
return array_diff($public, $static);
}

/**
Expand Down

0 comments on commit 8e38713

Please sign in to comment.