<commit_before><?php

/**
 * Copyright (C) 2016-18 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @author <NAME> <https://benjamin.heisig.name/>
 * @copyright Copyright (C) 2016-18 <NAME>
 * @license http://www.gnu.org/licenses/agpl-3.0 GNU Affero General Public License (AGPL)
 * @link https://github.com/bheisig/i-doit-api-client-php
 */

namespace bheisig\idoitapi;

/**
 * Requests for API namespace 'cmdb.object'
 */
class CMDBObject extends Request {

    /**
     * Create new object
     *
     * @param int|string $type Object type identifier or its constant
     * @param string $title Object title
     * @param array $attributes (Optional) additional common attributes
     * ('category', 'purpose', 'cmdb_status', 'description')
     *
     * @return int Object identifier
     *
     * @throws \Exception on error
     */
    public function create($type, $title, array $attributes = []) {
        $attributes['type'] = $type;
        $attributes['title'] = $title;

        $result = $this->api->request(
            'cmdb.object.create',
            $attributes
        );

        if (array_key_exists('id', $result)) {
            return (int) $result['id'];
        } else {
            throw new \RuntimeException('Unable to create object');
        }
    }

    /**
     * Create new object with category entries
     *
     * @param int|string $type Object type identifier or its constant
     * @param string $title Object title
     * @param array $categories Also create category entries;
     * set category constant (string) as key and
     * one (array of attributes) entry or even several entries (array of arrays) as value
     * @param array $attributes (Optional) additional common attributes
     * ('category', 'purpose', 'cmdb_status', 'description')
     *
     * @return array Result with object identifier ('id') and
     * key-value pairs of category constants and array of category entry identifiers as integers
     *
     * @throws \Exception on error
     */
    public function createWithCategories($type, $title, array $categories = [], array $attributes = []) {
        $attributes['type'] = $type;
        $attributes['title'] = $title;

        if (count($categories) > 0) {
            $attributes['categories'] = $categories;
        }

        return $this->api->request(
            'cmdb.object.create',
            $attributes
        );
    }

    /**
     * Read common information about object
     *
     * @param int $objectID Object identifier
     *
     * @return array Associative array
     *
     * @throws \Exception on error
     */
    public function read($objectID) {
        return $this->api->request('cmdb.object.read', [
            'id' => $objectID
        ]);
    }

    /**
     * Update existing object
     *
     * @param int $objectID Object identifier
     * @param array $attributes (Optional) common attributes (only 'title' is supported at the moment)
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    public function update($objectID, array $attributes = []) {
        $params = [
            'id' => $objectID
        ];

        $supportedAttributes = [
            'title'
        ];

        foreach ($supportedAttributes as $supportedAttribute) {
            if (array_key_exists($supportedAttribute, $attributes)) {
                $params[$supportedAttribute] = $attributes[$supportedAttribute];
            }
        }

        $result = $this->api->request(
            'cmdb.object.update',
            $params
        );

        if (!is_array($result) ||
            !array_key_exists('success', $result) ||
            $result['success'] === false) {
            throw new \RuntimeException(sprintf(
                'Unable to archive object %s',
                $objectID
            ));
        }

        return $this;
    }

    /**
     * Archive object
     *
     * @param int $objectID Object identifier
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    public function archive($objectID) {
        $this->api->request(
            'cmdb.object.archive',
            [
                'object' => $objectID
            ]
        );

        return $this;
    }

    /**
     * Mark object as deleted (it's still available)
     *
     * @param int $objectID Object identifier
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    public function delete($objectID) {
        $this->api->request(
            'cmdb.object.delete',
            [
                'object' => $objectID
            ]
        );

        return $this;
    }

    /**
     * Purge object (delete it irrevocable)
     *
     * @param int $objectID Object identifier
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    public function purge($objectID) {
        $this->api->request(
            'cmdb.object.delete',
            [
                'object' => $objectID
            ]
        );

        return $this;
    }

    /**
     * Convert object to template<commit_msg>Fix typo in method "purge"<commit_after><?php

/**
 * Copyright (C) 2016-18 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @author <NAME> <https://benjamin.heisig.name/>
 * @copyright Copyright (C) 2016-18 <NAME>
 * @license http://www.gnu.org/licenses/agpl-3.0 GNU Affero General Public License (AGPL)
 * @link https://github.com/bheisig/i-doit-api-client-php
 */

namespace bheisig\idoitapi;

/**
 * Requests for API namespace 'cmdb.object'
 */
class CMDBObject extends Request {

    /**
     * Create new object
     *
     * @param int|string $type Object type identifier or its constant
     * @param string $title Object title
     * @param array $attributes (Optional) additional common attributes
     * ('category', 'purpose', 'cmdb_status', 'description')
     *
     * @return int Object identifier
     *
     * @throws \Exception on error
     */
    public function create($type, $title, array $attributes = []) {
        $attributes['type'] = $type;
        $attributes['title'] = $title;

        $result = $this->api->request(
            'cmdb.object.create',
            $attributes
        );

        if (array_key_exists('id', $result)) {
            return (int) $result['id'];
        } else {
            throw new \RuntimeException('Unable to create object');
        }
    }

    /**
     * Create new object with category entries
     *
     * @param int|string $type Object type identifier or its constant
     * @param string $title Object title
     * @param array $categories Also create category entries;
     * set category constant (string) as key and
     * one (array of attributes) entry or even several entries (array of arrays) as value
     * @param array $attributes (Optional) additional common attributes
     * ('category', 'purpose', 'cmdb_status', 'description')
     *
     * @return array Result with object identifier ('id') and
     * key-value pairs of category constants and array of category entry identifiers as integers
     *
     * @throws \Exception on error
     */
    public function createWithCategories($type, $title, array $categories = [], array $attributes = []) {
        $attributes['type'] = $type;
        $attributes['title'] = $title;

        if (count($categories) > 0) {
            $attributes['categories'] = $categories;
        }

        return $this->api->request(
            'cmdb.object.create',
            $attributes
        );
    }

    /**
     * Read common information about object
     *
     * @param int $objectID Object identifier
     *
     * @return array Associative array
     *
     * @throws \Exception on error
     */
    public function read($objectID) {
        return $this->api->request('cmdb.object.read', [
            'id' => $objectID
        ]);
    }

    /**
     * Update existing object
     *
     * @param int $objectID Object identifier
     * @param array $attributes (Optional) common attributes (only 'title' is supported at the moment)
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    public function update($objectID, array $attributes = []) {
        $params = [
            'id' => $objectID
        ];

        $supportedAttributes = [
            'title'
        ];

        foreach ($supportedAttributes as $supportedAttribute) {
            if (array_key_exists($supportedAttribute, $attributes)) {
                $params[$supportedAttribute] = $attributes[$supportedAttribute];
            }
        }

        $result = $this->api->request(
            'cmdb.object.update',
            $params
        );

        if (!is_array($result) ||
            !array_key_exists('success', $result) ||
            $result['success'] === false) {
            throw new \RuntimeException(sprintf(
                'Unable to archive object %s',
                $objectID
            ));
        }

        return $this;
    }

    /**
     * Archive object
     *
     * @param int $objectID Object identifier
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    public function archive($objectID) {
        $this->api->request(
            'cmdb.object.archive',
            [
                'object' => $objectID
            ]
        );

        return $this;
    }

    /**
     * Mark object as deleted (it's still available)
     *
     * @param int $objectID Object identifier
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    public function delete($objectID) {
        $this->api->request(
            'cmdb.object.delete',
            [
                'object' => $objectID
            ]
        );

        return $this;
    }

    /**
     * Purge object (delete it irrevocable)
     *
     * @param int $objectID Object identifier
     *
     * @return self Returns itself
     *
     * @throws \Exception on error
     */
    public function purge($objectID) {
        $this->api->request(
            'cmdb.object.purge',
            [
                'object' => $objectID
            ]
        );

        return $this;
    }

    /**
     * Convert object to template
{'fraction_non_alphanumeric': 0.11389994806993249, 'fraction_numerical': 0.0027696036004846805, 'mean_word_length': 2.002078461938166, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 53, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, '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': '3665154', 'n_tokens_mistral': 3124, 'n_tokens_neox': 3031, 'n_words': 967}