diff --git a/plugins/interactivity/lib/api/KalturaBaseInteractivity.php b/plugins/interactivity/lib/api/KalturaBaseInteractivity.php index 0d914a7c922..41055e280e2 100644 --- a/plugins/interactivity/lib/api/KalturaBaseInteractivity.php +++ b/plugins/interactivity/lib/api/KalturaBaseInteractivity.php @@ -4,6 +4,7 @@ * @package plugins.interactivity * @subpackage api.objects */ + abstract class KalturaBaseInteractivity extends KalturaObject { /** diff --git a/plugins/interactivity/lib/api/KalturaInteractivity.php b/plugins/interactivity/lib/api/KalturaInteractivity.php index 2f84c5fe3c6..909e39052f4 100644 --- a/plugins/interactivity/lib/api/KalturaInteractivity.php +++ b/plugins/interactivity/lib/api/KalturaInteractivity.php @@ -16,7 +16,7 @@ public function toObject($object_to_fill = null, $propsToSkip = array()) { if (!$object_to_fill) { - $object_to_fill = new kInteractivity(); + $object_to_fill = new kInteractivity(); } return parent::toObject($object_to_fill, $propsToSkip); diff --git a/plugins/interactivity/lib/api/KalturaInteractivityDataFieldsFilter.php b/plugins/interactivity/lib/api/KalturaInteractivityDataFieldsFilter.php new file mode 100644 index 00000000000..b589980add5 --- /dev/null +++ b/plugins/interactivity/lib/api/KalturaInteractivityDataFieldsFilter.php @@ -0,0 +1,25 @@ +fields); + } + + /** + * @param string $fields + */ + public function setFields($fields) + { + $this->fields = array_map('trim', explode(self::FIELDS_DELIMITER, $fields)); + } + + /** + * @return array + */ + public function getFieldsAsArray() + { + return $this->fields; + } +} \ No newline at end of file diff --git a/plugins/interactivity/lib/model/kInteractivityDataFieldsName.php b/plugins/interactivity/lib/model/kInteractivityDataFieldsName.php new file mode 100644 index 00000000000..36b998e487d --- /dev/null +++ b/plugins/interactivity/lib/model/kInteractivityDataFieldsName.php @@ -0,0 +1,19 @@ +rootFilter; + } + + /** + * @param kInteractivityRootFilter $rootFilter + */ + public function setRootFilter($rootFilter) + { + $this->rootFilter = $rootFilter; + } + + /** + * @return kInteractivityInteractionFilter + */ + public function getInteractionFilter() + { + return $this->interactionFilter; + } + + /** + * @param kInteractivityInteractionFilter $interactionFilter + */ + public function setInteractionFilter($interactionFilter) + { + $this->interactionFilter = $interactionFilter; + } + + /** + * @return kInteractivityNodeFilter + */ + public function getNodeFilter() + { + return $this->nodeFilter; + } + + /** + * @param kInteractivityNodeFilter $nodeFilter + */ + public function setNodeFilter($nodeFilter) + { + $this->nodeFilter = $nodeFilter; + } + + /** + * @param string $data + * @return string + */ + public function filterData($data) + { + $dataJson = json_decode($data, true); + if($this->rootFilter) + { + $dataJson = $this->filterObject($dataJson, $this->rootFilter->getFieldsAsArray()); + } + + if(isset($dataJson[kInteractivityDataFieldsName::NODES])) + { + $dataJson[kInteractivityDataFieldsName::NODES] = $this->filterNodes($dataJson[kInteractivityDataFieldsName::NODES]); + } + + return json_encode($dataJson); + } + + /** + * @param array $nodes + * @return array + */ + protected function filterNodes($nodes) + { + $result = array(); + foreach ($nodes as $node) + { + if($this->nodeFilter) + { + $filteredNode = $this->filterObject($node, $this->nodeFilter->getFieldsAsArray()); + } + else + { + $filteredNode = $node; + } + + if(isset($filteredNode[kInteractivityDataFieldsName::INTERACTIONS])) + { + $filteredNode[kInteractivityDataFieldsName::INTERACTIONS] = $this->filterInteractions($filteredNode[kInteractivityDataFieldsName::INTERACTIONS]); + } + + $result[] = $filteredNode; + } + + return $result; + } + + /** + * @param array $interactions + * @return array + */ + protected function filterInteractions($interactions) + { + $result = array(); + if($this->interactionFilter) + { + foreach ($interactions as $interaction) + { + $result[] = $this->filterObject($interaction, $this->interactionFilter->getFieldsAsArray()); + } + } + else + { + $result = $interactions; + } + + return $result; + } + + /** + * @param array $object + * @param array $fieldsToKeep + * @return array + */ + protected function filterObject($object, $fieldsToKeep) + { + $result = array(); + foreach($object as $key => $value) + { + if(in_array($key, $fieldsToKeep)) + { + $result[$key] = $value; + } + } + + return $result; + } +} diff --git a/plugins/interactivity/lib/model/kInteractivityInteractionFilter.php b/plugins/interactivity/lib/model/kInteractivityInteractionFilter.php new file mode 100644 index 00000000000..60ecb07ae57 --- /dev/null +++ b/plugins/interactivity/lib/model/kInteractivityInteractionFilter.php @@ -0,0 +1,9 @@ +validateMandatoryField($data, self::OBJECT_NAME, self::NODES); - $this->validateArrayField($data, self::OBJECT_NAME, self::NODES); - if(count($data[self::NODES]) != 1) + $this->validateMandatoryField($data, self::OBJECT_NAME, kInteractivityDataFieldsName::NODES); + $this->validateArrayField($data, self::OBJECT_NAME, kInteractivityDataFieldsName::NODES); + if(count($data[kInteractivityDataFieldsName::NODES]) != 1) { throw new kInteractivityException(kInteractivityException::ENTRY_ILLEGAL_NODE_NUMBER, kInteractivityException::ENTRY_ILLEGAL_NODE_NUMBER); } - $this->nodeValidator->validate($data[self::NODES][0]); + $this->nodeValidator->validate($data[kInteractivityDataFieldsName::NODES][0]); if($this->isThereDuplicateValues($this->nodeValidator->getInteractionIds())) { throw new kInteractivityException(kInteractivityException::DUPLICATE_INTERACTIONS_IDS, kInteractivityException::DUPLICATE_INTERACTIONS_IDS); diff --git a/plugins/interactivity/lib/model/validators/kInteractivityDataValidator.php b/plugins/interactivity/lib/model/validators/kInteractivityDataValidator.php index 95dc3def483..55160112682 100644 --- a/plugins/interactivity/lib/model/validators/kInteractivityDataValidator.php +++ b/plugins/interactivity/lib/model/validators/kInteractivityDataValidator.php @@ -6,9 +6,7 @@ class kInteractivityDataValidator extends kInteractivityBaseValidator { - const NODES = 'nodes'; const OBJECT_NAME = 'InteractivityData'; - const NODE_ID = 'id'; /** @var kInteractivityNodeValidator*/ protected $nodeValidator; @@ -49,15 +47,15 @@ public function validate($data) */ protected function validateNodes($data) { - if(isset($data[self::NODES])) + if(isset($data[kInteractivityDataFieldsName::NODES])) { $nodesIds = array(); $interactionsIds = array(); - $this->validateArrayField($data, self::OBJECT_NAME, self::NODES); - foreach ($data[self::NODES] as $node) + $this->validateArrayField($data, self::OBJECT_NAME, kInteractivityDataFieldsName::NODES); + foreach ($data[kInteractivityDataFieldsName::NODES] as $node) { $this->nodeValidator->validate($node); - $nodesIds[] = $node[self::NODE_ID]; + $nodesIds[] = $node[kInteractivityDataFieldsName::NODE_ID]; $interactionsIds = array_merge($interactionsIds, $this->nodeValidator->getInteractionIds()); } diff --git a/plugins/interactivity/lib/model/validators/kInteractivityEntryNodeValidator.php b/plugins/interactivity/lib/model/validators/kInteractivityEntryNodeValidator.php index 6553dc02fef..7540555ce15 100644 --- a/plugins/interactivity/lib/model/validators/kInteractivityEntryNodeValidator.php +++ b/plugins/interactivity/lib/model/validators/kInteractivityEntryNodeValidator.php @@ -8,7 +8,7 @@ class kInteractivityEntryNodeValidator extends kInteractivityNodeValidator { public function validate($data) { - if(isset($data[self::ENTRY_ID])) + if(isset($data[kInteractivityDataFieldsName::ENTRY_ID])) { throw new kInteractivityException(kInteractivityException::ILLEGAL_ENTRY_NODE_ENTRY_ID, kInteractivityException::ILLEGAL_ENTRY_NODE_ENTRY_ID); } diff --git a/plugins/interactivity/lib/model/validators/kInteractivityInteractionValidator.php b/plugins/interactivity/lib/model/validators/kInteractivityInteractionValidator.php index 5912deccf28..b4488f60360 100644 --- a/plugins/interactivity/lib/model/validators/kInteractivityInteractionValidator.php +++ b/plugins/interactivity/lib/model/validators/kInteractivityInteractionValidator.php @@ -6,11 +6,6 @@ class kInteractivityInteractionValidator extends kInteractivityBaseValidator { - const ID = 'id'; - const TYPE = 'type'; - const TAGS = 'tags'; - const START_TIME = 'startTime'; - const END_TIME = 'endTime'; const OBJECT_NAME = 'interaction'; /** @@ -19,9 +14,9 @@ class kInteractivityInteractionValidator extends kInteractivityBaseValidator */ public function validate($data) { - $this->validateMandatoryStringField($data, self::OBJECT_NAME, self::ID); - $this->validateMandatoryStringField($data, self::OBJECT_NAME, self::TYPE); - $this->validateOptionalStringField($data, self::OBJECT_NAME, self::TAGS); + $this->validateMandatoryStringField($data, self::OBJECT_NAME, kInteractivityDataFieldsName::INTERACTION_ID); + $this->validateMandatoryStringField($data, self::OBJECT_NAME, kInteractivityDataFieldsName::TYPE); + $this->validateOptionalStringField($data, self::OBJECT_NAME, kInteractivityDataFieldsName::TAGS); $this->validateTimeFields($data); } @@ -31,13 +26,13 @@ public function validate($data) */ protected function validateTimeFields($data) { - if(isset($data[self::START_TIME]) && !is_int($data[self::START_TIME])) + if(isset($data[kInteractivityDataFieldsName::START_TIME]) && !is_int($data[kInteractivityDataFieldsName::START_TIME])) { $data = array(kInteractivityErrorMessages::ERR_MSG => 'start time ' . kInteractivityErrorMessages::LEGAL_TIME_FORMAT); throw new kInteractivityException(kInteractivityException::ILLEGAL_FIELD_VALUE, kInteractivityException::ILLEGAL_FIELD_VALUE, $data); } - if(isset($data[self::END_TIME]) && !is_int($data[self::END_TIME])) + if(isset($data[kInteractivityDataFieldsName::END_TIME]) && !is_int($data[kInteractivityDataFieldsName::END_TIME])) { $data = array(kInteractivityErrorMessages::ERR_MSG => 'end time ' . kInteractivityErrorMessages::LEGAL_TIME_FORMAT); throw new kInteractivityException(kInteractivityException::ILLEGAL_FIELD_VALUE, kInteractivityException::ILLEGAL_FIELD_VALUE, $data); diff --git a/plugins/interactivity/lib/model/validators/kInteractivityNodeValidator.php b/plugins/interactivity/lib/model/validators/kInteractivityNodeValidator.php index aaea78b5ccb..b27431aaef0 100644 --- a/plugins/interactivity/lib/model/validators/kInteractivityNodeValidator.php +++ b/plugins/interactivity/lib/model/validators/kInteractivityNodeValidator.php @@ -6,13 +6,7 @@ class kInteractivityNodeValidator extends kInteractivityBaseValidator { - const ID = 'id'; - const NAME = 'name'; - const ENTRY_ID = 'entryId'; const OBJECT_NAME = 'node'; - const INTERACTIONS = 'interactions'; - const TAGS = 'tags'; - protected $interactionValidator; protected $interactionsIds; @@ -33,18 +27,18 @@ public function __construct($entry) public function validate($data) { $this->interactionsIds = array(); - $this->validateMandatoryStringField($data, self::OBJECT_NAME, self::ID); - $this->validateMandatoryStringField($data, self::OBJECT_NAME, self::NAME); - $this->validateOptionalStringField($data, self::OBJECT_NAME, self::ENTRY_ID); - $this->validateOptionalStringField($data, self::OBJECT_NAME, self::TAGS); + $this->validateMandatoryStringField($data, self::OBJECT_NAME, kInteractivityDataFieldsName::INTERACTION_ID); + $this->validateMandatoryStringField($data, self::OBJECT_NAME, kInteractivityDataFieldsName::NAME); + $this->validateOptionalStringField($data, self::OBJECT_NAME, kInteractivityDataFieldsName::ENTRY_ID); + $this->validateOptionalStringField($data, self::OBJECT_NAME, kInteractivityDataFieldsName::TAGS); - if(isset($data[self::INTERACTIONS])) + if(isset($data[kInteractivityDataFieldsName::INTERACTIONS])) { - $this->validateArrayField($data, self::OBJECT_NAME, self::INTERACTIONS); - foreach ($data[self::INTERACTIONS] as $interaction) + $this->validateArrayField($data, self::OBJECT_NAME, kInteractivityDataFieldsName::INTERACTIONS); + foreach ($data[kInteractivityDataFieldsName::INTERACTIONS] as $interaction) { $this->interactionValidator->validate($interaction); - $this->interactionsIds[] = $interaction[kInteractivityInteractionValidator::ID]; + $this->interactionsIds[] = $interaction[kInteractivityDataFieldsName::INTERACTION_ID]; } } } diff --git a/plugins/interactivity/services/InteractivityService.php b/plugins/interactivity/services/InteractivityService.php index 8663bae68c9..39439479711 100644 --- a/plugins/interactivity/services/InteractivityService.php +++ b/plugins/interactivity/services/InteractivityService.php @@ -12,15 +12,24 @@ class InteractivityService extends KalturaBaseService * * @action get * @param string $entryId + * @param KalturaInteractivityDataFilter $dataFilter * @return KalturaInteractivity - * @throws Exception + * @throws kCoreException + * @throws KalturaAPIException */ - public function get($entryId) + public function get($entryId, $dataFilter = null) { $kInteractivity = new kInteractivity(); $kInteractivity->setEntry($entryId); $kalturaInteractivity = new KalturaInteractivity(); $kalturaInteractivity->fromObject($kInteractivity, $this->getResponseProfile()); + if($dataFilter) + { + $kDataFilter = $dataFilter->toObject(); + /* @var $kDataFilter kInteractivityDataFilter */ + $kalturaInteractivity->data = $kDataFilter->filterData($kalturaInteractivity->data); + } + return $kalturaInteractivity; }