<commit_before><?php

namespace PeskyCMF\CMS\ApiDocs;

use PeskyCMF\HttpCode;
use Ramsey\Uuid\Uuid;

abstract class CmsApiDocs {

    // override next properties and methods

    public $title = '';
    public $description = <<<HTML

HTML;

    /**
     * You can use '{url_parameter}' or ':url_parameter' to insert parameters into url and be able to
     * export it to postman properly (postman uses ':url_parameter' format but it is not expressive
     * enough unlike '{url_parameter}' variant)
     * @var string
     */
    public $url = '/api/example/{url_parameter}/list';
    public $httpMethod = 'GET';

    public $headers = [
        'Accept' => 'application/json',
        'Authorization' => 'Bearer {{token}}'
    ];
    /**
     * List of parameters used inside URL
     * For url: '/api/items/{id}/list' 'id' is url parameter (brackets needed only to highlight url parameter)
     * @var array
     */
    public $urlParameters = [
//        'url_parameter' => 'int'
    ];
    public $urlQueryParameters = [
//        '_method' => 'PUT',
//        'token' => 'string',
    ];
    public $postParameters = [
//        'id' => 'int',
    ];
    public $validationErrors = [
//        'token' => ['required', 'string'],
//        'id' => ['required', 'integer', 'min:1']
    ];

    public $onSuccess = [
//        'name' => 'string',
    ];

    /**
     * @return array
     */
    public function getPossibleErrors() {
        /* Example:
            [
                'code' => HttpCode::NOT_FOUND,
                'title' => 'Not found',
                'response' => [
                    'error' => 'item_not_found'
                ]
            ],
        */
        return [];
    }

    // service properties and methods

    /**
     * @return array
     */
    public function getCommonErrors() {
        $errors = [
            static::$authFailError,
            static::$accessDeniedError,
            static::$serverError,
        ];
        if (count($this->validationErrors)) {
            $errors[] = array_merge(static::$dataValidationError, [
                'response' => $this->validationErrors
            ]);
        }
        return $errors;
    }

    protected $uuid;

    static protected $authFailError = [
        'code' => HttpCode::UNAUTHORISED,
        'title' => 'Не удалось авторизовать пользователя',
        'response' => [
            'error' => 'Unauthenticated.'
        ]
    ];

    static protected $accessDeniedError = [
        'code' => HttpCode::FORBIDDEN,
        'title' => 'Доступ запрещен',
        'response' => []
    ];

    static protected $dataValidationError = [
        'code' => HttpCode::CANNOT_PROCESS,
        'title' => 'Ошибки валидации данных',
        'response' => []
    ];

    static protected $serverError = [
        'code' => HttpCode::SERVER_ERROR,
        'title' => 'Критическая ошибка на стороне сервера',
        'response' => []
    ];

    static public function create() {
        return new static();
    }

    public function __construct() {
        $this->uuid = Uuid::uuid4()->toString();
    }

    public function getUuid() {
        return $this->uuid;
    }<commit_msg>CmsApiDocs - updated to allow usage of class methods instead of class fields for most fields<commit_after><?php

namespace PeskyCMF\CMS\ApiDocs;

use PeskyCMF\HttpCode;
use Ramsey\Uuid\Uuid;

abstract class CmsApiDocs {

    // override next properties and methods

    public $title = '';
    public $description = <<<HTML

HTML;

    /**
     * You can use '{url_parameter}' or ':url_parameter' to insert parameters into url and be able to
     * export it to postman properly (postman uses ':url_parameter' format but it is not expressive
     * enough unlike '{url_parameter}' variant)
     * @var string
     */
    public $url = '/api/example/{url_parameter}/list';
    public $httpMethod = 'GET';

    public $headers = [
        'Accept' => 'application/json',
        'Authorization' => 'Bearer {{token}}'
    ];
    /**
     * List of parameters used inside URL
     * For url: '/api/items/{id}/list' 'id' is url parameter (brackets needed only to highlight url parameter)
     * @var array
     */
    public $urlParameters = [
//        'url_parameter' => 'int'
    ];
    public $urlQueryParameters = [
//        '_method' => 'PUT',
//        'token' => 'string',
    ];
    public $postParameters = [
//        'id' => 'int',
    ];
    public $validationErrors = [
//        'token' => ['required', 'string'],
//        'id' => ['required', 'integer', 'min:1']
    ];

    public $onSuccess = [
//        'name' => 'string',
    ];

    /**
     * @return array
     */
    public function getPossibleErrors() {
        /* Example:
            [
                'code' => HttpCode::NOT_FOUND,
                'title' => 'Not found',
                'response' => [
                    'error' => 'item_not_found'
                ]
            ],
        */
        return [];
    }

    // service properties and methods

    /**
     * @return array
     */
    public function getCommonErrors() {
        $errors = [
            static::$authFailError,
            static::$accessDeniedError,
            static::$serverError,
        ];
        if (count($this->validationErrors)) {
            $errors[] = array_merge(static::$dataValidationError, [
                'response' => $this->validationErrors
            ]);
        }
        return $errors;
    }

    protected $uuid;

    static protected $authFailError = [
        'code' => HttpCode::UNAUTHORISED,
        'title' => 'Не удалось авторизовать пользователя',
        'response' => [
            'error' => 'Unauthenticated.'
        ]
    ];

    static protected $accessDeniedError = [
        'code' => HttpCode::FORBIDDEN,
        'title' => 'Доступ запрещен',
        'response' => []
    ];

    static protected $dataValidationError = [
        'code' => HttpCode::CANNOT_PROCESS,
        'title' => 'Ошибки валидации данных',
        'response' => []
    ];

    static protected $serverError = [
        'code' => HttpCode::SERVER_ERROR,
        'title' => 'Критическая ошибка на стороне сервера',
        'response' => []
    ];

    static public function create() {
        return new static();
    }

    public function __construct() {
        $this->uuid = Uuid::uuid4()->toString();
        // load data from class methods
        foreach (['headers', 'onSuccess', 'validationErrors', 'postParameters', 'urlQueryParameters', 'urlParameters'] as $field) {
            if ($this->$field === null) {
                if (method_exists($this, $field)) {
                    $this->$field = $this->$field();
                    if (!is_array($this->$field)) {
                        throw new \UnexpectedValueException(get_class($this) . '->' . $field . '() method must return an array');
                    }
                } else {
                    throw new \UnexpectedValueException(get_class($this) . '->' . $field . ' can be null only when ' . $field . '() method is defined');
                }
            }
        }
    }

    public function getUuid() {
        return $this->uuid;
    }
{'fraction_non_alphanumeric': 0.1454266592303402, 'fraction_numerical': 0.0005577244841048522, 'mean_word_length': 1.891172914147521, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 73, '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': '2543581', 'n_tokens_mistral': 2097, 'n_tokens_neox': 2067, 'n_words': 495}