<commit_before><?php
App::uses('JSONConverterTool', 'Tools');

/**
 * WorkflowFormatConverterTool convert passed data into the MISP core format with these additional properties:
 * - Attributes are encapsulated in the Event they belong to as well as their object (if applicable)
 * - Events have an additional key `_AttributeFlattened` which combines both Attribute and ObjectAttribute in the same array
 * - Attributes have an additional key `_allTags` which group both AttributeTag and EventTag.
 *      - Tags in this `_allTags` key have an additional flag `inherited` indicating if the tag has been propagated from the Event to the Attribute
 */
class WorkflowFormatConverterTool
{
    private static $fakeSiteAdminUser = ['Role' => ['perm_site_admin' => true]];

    public static function convert(array $data, $scope=''): array
    {
        if (empty($scope)) {
            $scope = self::__guessScopeFromData($data);
        }
        $converted = [];
        switch ($scope) {
            case 'event':
                $converted = self::__convertEvent($data);
                break;
            case 'attribute':
                $converted = self::__convertAttribute($data);
                break;
            case 'object':
                $converted = self::__convertObject($data);
                break;
            default:
                break;
        }
        $converted = self::__includeFlattenedAttributes($converted);
        return $converted;
    }

    private static function __convertEvent(array $event): array
    {
        $converted = [];
        $converted = JSONConverterTool::convert($event, false, true);
        return $converted;
    }

    private static function __convertObject(array $object): array
    {
        $converted = [];
        $convertedObject = JSONConverterTool::convertObject($object, false, true);
        $convertedObject = ['Object' => $convertedObject['Object']];
        $converted = self::__encapsulateEntityWithEvent($convertedObject);
        return $converted;
    }

    /**
     * __convertAttribute Convert and clean an attribute. May also transform the attribute into an Object if applicable.
     * However, the object will not be full and will only contain the attribute
     *
     * @param array $attribute
     * @return array
     */
    private static function __convertAttribute(array $attribute): array
    {
        $allTags = [];
        if (!empty($attribute['EventTag'])) {
            foreach ($attribute['AttributeTag'] as $attributeTag) {
                $attributeTag['Tag']['inherited'] = false;
                $allTags[] = $attributeTag['Tag'];
            }
            foreach ($attribute['EventTag'] as $eventTag) {
                $eventTag['Tag']['inherited'] = true;
                $allTags[] = $eventTag['Tag'];
            }
        }
        $convertedAttribute = JSONConverterTool::convertAttribute($attribute, true);
        $convertedAttribute['Attribute']['_allTags'] = $allTags;
        if ($convertedAttribute['Attribute']['object_id'] != 0) {
            $objectModel = ClassRegistry::init('MispObject');
            $object = $objectModel->fetchObjectSimple(self::$fakeSiteAdminUser, [
                'conditions' => [
                    'Object.id' => $convertedAttribute['Attribute']['object_id'],
                ],
            ]);
            if (!empty($object)) {
                $object = $object[0]['Object'];
                $object['Attribute'][] = $convertedAttribute['Attribute'];<commit_msg>fix: [workflow:formatConverter] Typo in condition leading to ignore attribute tags if event tags were missing<commit_after><?php
App::uses('JSONConverterTool', 'Tools');

/**
 * WorkflowFormatConverterTool convert passed data into the MISP core format with these additional properties:
 * - Attributes are encapsulated in the Event they belong to as well as their object (if applicable)
 * - Events have an additional key `_AttributeFlattened` which combines both Attribute and ObjectAttribute in the same array
 * - Attributes have an additional key `_allTags` which group both AttributeTag and EventTag.
 *      - Tags in this `_allTags` key have an additional flag `inherited` indicating if the tag has been propagated from the Event to the Attribute
 */
class WorkflowFormatConverterTool
{
    private static $fakeSiteAdminUser = ['Role' => ['perm_site_admin' => true]];

    public static function convert(array $data, $scope=''): array
    {
        if (empty($scope)) {
            $scope = self::__guessScopeFromData($data);
        }
        $converted = [];
        switch ($scope) {
            case 'event':
                $converted = self::__convertEvent($data);
                break;
            case 'attribute':
                $converted = self::__convertAttribute($data);
                break;
            case 'object':
                $converted = self::__convertObject($data);
                break;
            default:
                break;
        }
        $converted = self::__includeFlattenedAttributes($converted);
        return $converted;
    }

    private static function __convertEvent(array $event): array
    {
        $converted = [];
        $converted = JSONConverterTool::convert($event, false, true);
        return $converted;
    }

    private static function __convertObject(array $object): array
    {
        $converted = [];
        $convertedObject = JSONConverterTool::convertObject($object, false, true);
        $convertedObject = ['Object' => $convertedObject['Object']];
        $converted = self::__encapsulateEntityWithEvent($convertedObject);
        return $converted;
    }

    /**
     * __convertAttribute Convert and clean an attribute. May also transform the attribute into an Object if applicable.
     * However, the object will not be full and will only contain the attribute
     *
     * @param array $attribute
     * @return array
     */
    private static function __convertAttribute(array $attribute): array
    {
        $allTags = [];
        if (!empty($attribute['AttributeTag'])) {
            foreach ($attribute['AttributeTag'] as $attributeTag) {
                $attributeTag['Tag']['inherited'] = false;
                $allTags[] = $attributeTag['Tag'];
            }
        }
        if (!empty($attribute['EventTag'])) {
            foreach ($attribute['EventTag'] as $eventTag) {
                $eventTag['Tag']['inherited'] = true;
                $allTags[] = $eventTag['Tag'];
            }
        }
        $convertedAttribute = JSONConverterTool::convertAttribute($attribute, true);
        $convertedAttribute['Attribute']['_allTags'] = $allTags;
        if ($convertedAttribute['Attribute']['object_id'] != 0) {
            $objectModel = ClassRegistry::init('MispObject');
            $object = $objectModel->fetchObjectSimple(self::$fakeSiteAdminUser, [
                'conditions' => [
                    'Object.id' => $convertedAttribute['Attribute']['object_id'],
                ],
            ]);
            if (!empty($object)) {
                $object = $object[0]['Object'];
                $object['Attribute'][] = $convertedAttribute['Attribute'];
{'fraction_non_alphanumeric': 0.11925327389244915, 'fraction_numerical': 0.0005572582892170521, 'mean_word_length': 2.5036603221083453, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2549347', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1749, 'n_words': 489}