private function buildObject(KalturaTypeReflector $typeReflector, array &$params, $objectName) { // if objectType was specified, we will use it only if the anotation type is it's base type if (array_key_exists("objectType", $params)) { $possibleType = $params["objectType"]; if (strtolower($possibleType) !== strtolower($typeReflector->getType())) { if ($typeReflector->isParentOf($possibleType)) { $newTypeReflector = KalturaTypeReflectorCacher::get($possibleType); if ($newTypeReflector) { $typeReflector = $newTypeReflector; } } } } if ($typeReflector->isAbstract()) { throw new KalturaAPIException(KalturaErrors::OBJECT_TYPE_ABSTRACT, $typeReflector->getType()); } $class = $typeReflector->getType(); $obj = new $class(); $properties = $typeReflector->getProperties(); foreach ($params as $name => $value) { $isNull = false; if (kString::endsWith($name, '__null')) { $name = str_replace('__null', '', $name); $isNull = true; } if (!array_key_exists($name, $properties)) { continue; } $property = $properties[$name]; /* @var $property KalturaPropertyInfo */ $type = $property->getType(); if ($isNull && !$property->isArray()) { $obj->{$name} = new KalturaNullField(); continue; } if ($property->isSimpleType()) { if ($property->isTime()) { $type = "time"; } $value = $this->castSimpleType($type, $value); if (!kXml::isXMLValidContent($value)) { throw new KalturaAPIException(KalturaErrors::INVALID_PARAMETER_CHAR, $name); } $this->validateParameter($name, $value, $property); $obj->{$name} = $value; continue; } if ($property->isEnum()) { if (strtolower($value) == 'true') { $value = 1; } if (strtolower($value) == 'false') { $value = 0; } if (!$property->getTypeReflector()->checkEnumValue($value)) { throw new KalturaAPIException(KalturaErrors::INVALID_ENUM_VALUE, $value, $name, $property->getType()); } if ($type == 'KalturaNullableBoolean') { $obj->{$name} = KalturaNullableBoolean::toBoolean($value); continue; } $obj->{$name} = $this->castSimpleType("int", $value); continue; } if ($property->isStringEnum()) { if (!$property->getTypeReflector()->checkStringEnumValue($value)) { throw new KalturaAPIException(KalturaErrors::INVALID_ENUM_VALUE, $value, $name, $property->getType()); } $value = $this->castSimpleType("string", $value); if (!kXml::isXMLValidContent($value)) { throw new KalturaAPIException(KalturaErrors::INVALID_PARAMETER_CHAR, $name); } $obj->{$name} = $value; continue; } if ($property->isArray() && is_array($value)) { $arrayObj = new $type(); if ($property->isAssociativeArray()) { foreach ($value as $arrayItemKey => $arrayItemParams) { if ($arrayItemKey === '-') { break; } $arrayObj[$arrayItemKey] = $this->buildObject($property->getArrayTypeReflector(), $arrayItemParams, "{$objectName}:{$name}"); } } else { ksort($value); foreach ($value as $arrayItemKey => $arrayItemParams) { if ($arrayItemKey === '-') { break; } $arrayObj[] = $this->buildObject($property->getArrayTypeReflector(), $arrayItemParams, "{$objectName}:{$name}"); } } $obj->{$name} = $arrayObj; continue; } if ($property->isComplexType() && is_array($value)) { $obj->{$name} = $this->buildObject($property->getTypeReflector(), $value, "{$objectName}:{$name}"); continue; } if ($property->isFile()) { $obj->{$name} = $value; continue; } } return $obj; }
private function buildObject(KalturaTypeReflector $typeReflector, array &$params, $objectName) { // if objectType was specified, we will use it only if the anotation type is it's base type if (array_key_exists("objectType", $params)) { $possibleType = $params["objectType"]; if (strtolower($possibleType) !== strtolower($typeReflector->getType())) { if ($typeReflector->isParentOf($possibleType)) { $newTypeReflector = KalturaTypeReflectorCacher::get($possibleType); if ($newTypeReflector) { $typeReflector = $newTypeReflector; } } } } if ($typeReflector->isAbstract()) { throw new KalturaAPIException(KalturaErrors::OBJECT_TYPE_ABSTRACT, $typeReflector->getType()); } $class = $typeReflector->getType(); $obj = new $class(); $properties = $typeReflector->getProperties(); foreach ($properties as $property) { /* @var $property KalturaPropertyInfo */ $name = $property->getName(); $type = $property->getType(); if ($property->isSimpleType() || $property->isEnum() || $property->isStringEnum()) { if (array_key_exists($name . '__null', $params)) { $obj->{$name} = new KalturaNullField(); continue; } if (!array_key_exists($name, $params)) { // missing parameters should be null or default propery value continue; } $value = $params[$name]; if ($property->isSimpleType()) { $value = $this->castSimpleType($type, $value); if (!kXml::isXMLValidContent($value)) { throw new KalturaAPIException(KalturaErrors::INVALID_PARAMETER_CHAR, $name); } $obj->{$name} = $value; continue; } if ($property->isEnum()) { if (!$property->getTypeReflector()->checkEnumValue($value)) { throw new KalturaAPIException(KalturaErrors::INVALID_ENUM_VALUE, $value, $name, $property->getType()); } if ($type == 'KalturaNullableBoolean') { $obj->{$name} = KalturaNullableBoolean::toBoolean($value); continue; } $obj->{$name} = $this->castSimpleType("int", $value); continue; } if ($property->isStringEnum()) { if (!$property->getTypeReflector()->checkStringEnumValue($value)) { throw new KalturaAPIException(KalturaErrors::INVALID_ENUM_VALUE, $value, $name, $property->getType()); } $value = $this->castSimpleType("string", $value); if (!kXml::isXMLValidContent($value)) { throw new KalturaAPIException(KalturaErrors::INVALID_PARAMETER_CHAR, $name); } $obj->{$name} = $value; continue; } } if ($property->isArray()) { if (isset($params[$name]) && is_array($params[$name])) { ksort($params[$name]); $arrayObj = new $type(); foreach ($params[$name] as $arrayItemParams) { $arrayObj[] = $this->buildObject($property->getArrayTypeReflector(), $arrayItemParams, "{$objectName}:{$name}"); } $obj->{$name} = $arrayObj; } continue; } if ($property->isComplexType()) { if (isset($params[$name]) && is_array($params[$name])) { $obj->{$name} = $this->buildObject($property->getTypeReflector(), $params[$name], "{$objectName}:{$name}"); } continue; } if ($property->isFile()) { if (isset($params[$name])) { $obj->{$name} = $params[$name]; } continue; } } return $obj; }
private function buildObject(KalturaTypeReflector $typeReflector, array &$params) { // if objectType was specified, we will use it only if the anotation type is it's base type if (array_key_exists("objectType", $params)) { $possibleType = $params["objectType"]; if (strtolower($possibleType) !== strtolower($typeReflector->getType())) { if ($typeReflector->isParentOf($possibleType)) { // we know that the objectType that came from the user is right, and we can use it to initiate the object $typeReflector = KalturaTypeReflectorCacher::get($possibleType); } } } $class = $typeReflector->getType(); $obj = new $class(); $properties = $typeReflector->getProperties(); foreach ($properties as $property) { $name = $property->getName(); $type = $property->getType(); if ($property->isSimpleType() || $property->isEnum() || $property->isStringEnum()) { if (!array_key_exists($name, $params)) { // missing parameters should be null or default propery value continue; } $value = $params[$name]; if ($property->isSimpleType()) { $obj->{$name} = $this->castSimpleType($type, $value); } else { if ($property->isEnum()) { if (!$property->getTypeReflector()->checkEnumValue($value)) { throw new KalturaAPIException(KalturaErrors::INVALID_ENUM_VALUE, $value, $name, $property->getType()); } $obj->{$name} = $this->castSimpleType("int", $value); } else { if ($property->isStringEnum()) { if (!$property->getTypeReflector()->checkStringEnumValue($value)) { throw new KalturaAPIException(KalturaErrors::INVALID_ENUM_VALUE, $value, $name, $property->getType()); } $obj->{$name} = $this->castSimpleType("string", $value); } } } } else { if ($property->isArray()) { if (isset($params[$name]) && is_array($params[$name])) { $arrayObj = new $type(); foreach ($params[$name] as $arrayItemParams) { $arrayObj[] = $this->buildObject($property->getArrayTypeReflector(), $arrayItemParams); } $obj->{$name} = $arrayObj; } } else { if ($property->isComplexType()) { if (isset($params[$name]) && is_array($params[$name])) { $obj->{$name} = $this->buildObject($property->getTypeReflector(), $params[$name]); } } } } } return $obj; }