<commit_before><?php

/**
 * Comments
 * 
 * The `Comments` class provides static methods to be accessed by its own
 * instances and other parts of the plugin. `Comments` instances manage the
 * comments of a specific Kirby page. This involves processing HTTP POST data
 * for creating comment previews, submitting comments, storing comments as
 * Kirby pages, reading those pages and reporting status.
 * 
 * `Comments` instances provide additional convenience methods for creating
 * comments forms.
 * 
 * The preferred way of creating and accessing `Comments` instances is by
 * calling `$page->comments()` where `$page` is a Kirby `Page` object.
 *
 * @package   Kirby Comments
 * @author    <NAME> <<EMAIL>>
 * @link      https://addpixel.net/kirby-comments/
 * @copyright <NAME>
 * @license   https://addpixel.net/kirby-comments/LICENSE
 */
class Comments implements Iterator, Countable
{
	/**
	 * The default option values referenced by their name.
	 *
	 * @var mixed[string]
	 */
	private static $defaults = null;
	
	/**
	 * Deprecated option names referenced by their new counterparts.
	 *
	 * @var string[string]
	 */
	private static $deprecated_keys = array(
		'pages.comments.title'      => 'comments-page.title',
		'pages.comments.dirname'    => 'comments-page.dirname',
		'pages.comments.template'   => 'comments-page.template',
		'pages.comment.dirname'     => 'comment-page.dirname',
		'pages.comment.template'    => 'comment-page.template',
		'form.email.required'       => 'require.email',
		'honeypot.enabled'          => 'use.honeypot',
		'email.enabled'             => 'use.email',
		'form.message.allowed_tags' => 'allowed_tags',
		'form.message.smartypants'  => 'smartypants',
		'form.message.max-length'   => 'max-character-count',
		'form.name.max-length'      => 'max-field-length',
		'form.email.max-length'     => 'max-field-length',
		'form.website.max-length'   => 'max-field-length',
		'honeypot.human-value'      => 'human-honeypot-value',
		'setup.content-page.title'  => 'setup.page.title_key'
	);
	
	/**
	 * All instances created using `Comments::for_page($page)` (which is also used
	 * by `$page->comments()`) referenced by the URI of the page.
	 *
	 * @var Comments[string]
	 */
	private static $instances = array();
	
	/**
	 * Kirby page on which the comments are posted.
	 *
	 * @var Page
	 */
	private $page;
	
	/**
	 * Status of the comments. Is modified during construction of a `Comments`
	 * instance and which processing the comments using `$this->process()`.
	 *
	 * @var CommentsStatus
	 */
	private $status;
	
	/**
	 * Whether `$this->process()` has been invoked.
	 *
	 * @var bool
	 */
	private $has_been_processed;
	
	/**
	 * The index of the iterator. Used for the implementation of the `Iterator`
	 * interface.
	 *
	 * @var integer
	 */
	private $iterator_index;
	
	/**
	 * An array of comments on $this->page (published and unpublished).
	 * All comments managed by a `Comments` instance. Is modified during
	 * construction of a `Comments` instance and which processing the comments
	 * using `$this->process()`.
	 *
	 * @var Comment[]
	 */
	private $comments;
	
	/**
	 * Whether the current comment preview is valid. `false` iff no preview is
	 * requested.
	 *
	 * @var bool
	 */
	private $is_valid_preview;
	
	/**
	 * Configures the plugin by setting default values and reading custom field
	 * types from Kirby’s configuration. This static method will early exit if
	 * called multiple times during a single HTTP request.
	 * 
	 * @param mixed[string] $defaults
	 */
	static public function init($defaults)
	{
		// Early exit if this method has already been called during this request
		if (Comments::$defaults !== null) { return; }
		
		// Store default options
		Comments::$defaults = $defaults;
		
		// Register custom fields
		CommentsFieldType::$instances = array_map(function ($instruction) {
			return CommentsFieldType::from_array($instruction);
		}, Comments::option('custom-fields'));
	}
	
	/**
	 * Returns a `Comments` instance for `$page`. Constructs a new instance
	 * if no instance exists for the given page, returns a reference to an
	 * existing instance otherwise. Page equality is determined based on their
	 * URI.
	 * 
	 * @param Page $page
	 * @return Comments
	 */
	static public function for_page($page)
	{
		$uri = $page->uri();
		
		// Check for existing instance
		if (array_key_exists($uri, Comments::$instances)) {
			// Return existing instance
			return Comments::$instances[$uri];
		}
		
		// Construct new instance
		$new_instance = new Comments($page);
		Comments::$instances[$uri] = $new_instance;<commit_msg>Add missing throws info to Doc-comment<commit_after><?php

/**
 * Comments
 * 
 * The `Comments` class provides static methods to be accessed by its own
 * instances and other parts of the plugin. `Comments` instances manage the
 * comments of a specific Kirby page. This involves processing HTTP POST data
 * for creating comment previews, submitting comments, storing comments as
 * Kirby pages, reading those pages and reporting status.
 * 
 * `Comments` instances provide additional convenience methods for creating
 * comments forms.
 * 
 * The preferred way of creating and accessing `Comments` instances is by
 * calling `$page->comments()` where `$page` is a Kirby `Page` object.
 *
 * @package   Kirby Comments
 * @author    <NAME> <<EMAIL>>
 * @link      https://addpixel.net/kirby-comments/
 * @copyright <NAME>
 * @license   https://addpixel.net/kirby-comments/LICENSE
 */
class Comments implements Iterator, Countable
{
	/**
	 * The default option values referenced by their name.
	 *
	 * @var mixed[string]
	 */
	private static $defaults = null;
	
	/**
	 * Deprecated option names referenced by their new counterparts.
	 *
	 * @var string[string]
	 */
	private static $deprecated_keys = array(
		'pages.comments.title'      => 'comments-page.title',
		'pages.comments.dirname'    => 'comments-page.dirname',
		'pages.comments.template'   => 'comments-page.template',
		'pages.comment.dirname'     => 'comment-page.dirname',
		'pages.comment.template'    => 'comment-page.template',
		'form.email.required'       => 'require.email',
		'honeypot.enabled'          => 'use.honeypot',
		'email.enabled'             => 'use.email',
		'form.message.allowed_tags' => 'allowed_tags',
		'form.message.smartypants'  => 'smartypants',
		'form.message.max-length'   => 'max-character-count',
		'form.name.max-length'      => 'max-field-length',
		'form.email.max-length'     => 'max-field-length',
		'form.website.max-length'   => 'max-field-length',
		'honeypot.human-value'      => 'human-honeypot-value',
		'setup.content-page.title'  => 'setup.page.title_key'
	);
	
	/**
	 * All instances created using `Comments::for_page($page)` (which is also used
	 * by `$page->comments()`) referenced by the URI of the page.
	 *
	 * @var Comments[string]
	 */
	private static $instances = array();
	
	/**
	 * Kirby page on which the comments are posted.
	 *
	 * @var Page
	 */
	private $page;
	
	/**
	 * Status of the comments. Is modified during construction of a `Comments`
	 * instance and which processing the comments using `$this->process()`.
	 *
	 * @var CommentsStatus
	 */
	private $status;
	
	/**
	 * Whether `$this->process()` has been invoked.
	 *
	 * @var bool
	 */
	private $has_been_processed;
	
	/**
	 * The index of the iterator. Used for the implementation of the `Iterator`
	 * interface.
	 *
	 * @var integer
	 */
	private $iterator_index;
	
	/**
	 * An array of comments on $this->page (published and unpublished).
	 * All comments managed by a `Comments` instance. Is modified during
	 * construction of a `Comments` instance and which processing the comments
	 * using `$this->process()`.
	 *
	 * @var Comment[]
	 */
	private $comments;
	
	/**
	 * Whether the current comment preview is valid. `false` iff no preview is
	 * requested.
	 *
	 * @var bool
	 */
	private $is_valid_preview;
	
	/**
	 * Configures the plugin by setting default values and reading custom field
	 * types from Kirby’s configuration. This static method will early exit if
	 * called multiple times during a single HTTP request.
	 * 
	 * @param mixed[string] $defaults
	 */
	static public function init($defaults)
	{
		// Early exit if this method has already been called during this request
		if (Comments::$defaults !== null) { return; }
		
		// Store default options
		Comments::$defaults = $defaults;
		
		// Register custom fields
		CommentsFieldType::$instances = array_map(function ($instruction) {
			return CommentsFieldType::from_array($instruction);
		}, Comments::option('custom-fields'));
	}
	
	/**
	 * Returns a `Comments` instance for `$page`. Constructs a new instance
	 * if no instance exists for the given page, returns a reference to an
	 * existing instance otherwise. Page equality is determined based on their
	 * URI.
	 *
	 * @param Page $page
	 * @return Comments
	 * @throws Exception Throws if a comment could not be constructed based on a
	 * existing comment page.
	 */
	static public function for_page($page)
	{
		$uri = $page->uri();
		
		// Check for existing instance
		if (array_key_exists($uri, Comments::$instances)) {
			// Return existing instance
			return Comments::$instances[$uri];
		}
		
		// Construct new instance
		$new_instance = new Comments($page);
		Comments::$instances[$uri] = $new_instance;
{'fraction_non_alphanumeric': 0.12688724702858978, 'fraction_numerical': 0.0, 'mean_word_length': 3.4160756501182035, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 57, 'https://': 4, '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': '3994713', 'n_tokens_mistral': 2912, 'n_tokens_neox': 2595, 'n_words': 880}