<commit_before><?php

namespace Parvula\Model;

use DateTime;
use Parvula\Model\Mapper\Pages;
use Parvula\Exception\PageException;

/**
 * This class represents a Page
 *
 * @package Parvula
 * @version 0.7.0
 * @since 0.1.0
 * @author <NAME>
 * @license MIT License
 */
class Page {

	/**
	 * @var string Page's title
	 */
	public $title;

	/**
	 * @var string Page's slug ([a-z0-9-_+/]+)
	 */
	public $slug;

	/**
	 * @var string Page's content
	 */
	public $content;

	/**
	 * @var array Page's sections (optional)
	 */
	public $sections;

	/**
	 * @var string Page's parent slug (optional)
	 */
	public $parent;

	/**
	 * @var array Page's children (optional)
	 */
	public $children;

	/**
	 * @var array Array of Closure
	 */
	protected $_lazyFunctions;

	/**
	 * Page factory, create a new page from an array
	 * The parameter $infos must contain at least `title` and `slug` fields.
	 * The `slug` need to be normalized (a-z0-9-_+/).
	 *
	 * @param array $infos Array with page informations (must contain `title` and `slug` fields)
	 * @throws PageException if `$pageInfo` does not have field `title` and `slug`
	 * @throws PageException if `$pageInfo[slug]` value is not normalized
	 * @return Page The created Page
	 */
	public static function pageFactory(array $infos) {
		$content = isset($infos['content']) ? $infos['content'] : '';
		$sections = isset($infos['sections']) ? $infos['sections'] : [];
		unset($infos['content']);
		unset($infos['sections']);

		return new static($infos, $content, $sections);
	}

	/**
	 * Constructor
	 *
	 * @param array $meta Metadata
	 * @param string $content (optional) Content
	 * @param array $sections (optional) array of Section
	 */
	public function __construct(array $meta, $content = '', array $sections = []) {
		// Check if required meta informations are available
		if (empty($meta['title']) || empty($meta['slug'])) {
			throw new PageException('Page cannot be created, $meta MUST contain `title` and `slug` keys');
		}

		if (!preg_match('/^[a-z0-9\-_\+\/]+$/', $meta['slug'])) {
			throw new PageException('Page cannot be created, $meta[slug] (' .
				htmlspecialchars($meta['slug']) . ') value is not normalized');
		}

		foreach ($meta as $key => $value) {
			// object with private fields casted to array will have keys prepended with \0
			// https://php.net/manual/en/language.types.array.php#language.types.array.casting
			if (!is_null($value) && $key[0] !== "\0") {
				$this->{$key} = $value;
			}
		}
<commit_msg>Add sections in page factory<commit_after><?php

namespace Parvula\Model;

use DateTime;
use Parvula\Model\Mapper\Pages;
use Parvula\Model\Section;
use Parvula\Exception\PageException;

/**
 * This class represents a Page
 *
 * @package Parvula
 * @version 0.7.0
 * @since 0.1.0
 * @author <NAME>
 * @license MIT License
 */
class Page {

	/**
	 * @var string Page's title
	 */
	public $title;

	/**
	 * @var string Page's slug ([a-z0-9-_+/]+)
	 */
	public $slug;

	/**
	 * @var string Page's content
	 */
	public $content;

	/**
	 * @var array Page's sections (optional)
	 */
	public $sections;

	/**
	 * @var string Page's parent slug (optional)
	 */
	public $parent;

	/**
	 * @var array Page's children (optional)
	 */
	public $children;

	/**
	 * @var array Array of Closure
	 */
	protected $_lazyFunctions;

	/**
	 * Page factory, create a new page from an array
	 * The parameter $infos must contain at least `title` and `slug` fields.
	 * The `slug` need to be normalized (a-z0-9-_+/).
	 *
	 * @param array $infos Array with page informations (must contain `title` and `slug` fields)
	 * @throws PageException if `$pageInfo` does not have field `title` and `slug`
	 * @throws PageException if `$pageInfo[slug]` value is not normalized
	 * @return Page The created Page
	 */
	public static function pageFactory(array $infos) {
		$content = isset($infos['content']) ? $infos['content'] : '';

		if (isset($infos['sections'])) {
			$sections = array_map(function($section) {
				return Section::sectionFactory($section);
			}, $infos['sections']);

			unset($infos['sections']);
		} else {
			$sections = [];
		}

		unset($infos['content']);

		return new static($infos, $content, $sections);
	}

	/**
	 * Constructor
	 *
	 * @param array $meta Metadata
	 * @param string $content (optional) Content
	 * @param array $sections (optional) array of Section
	 */
	public function __construct(array $meta, $content = '', array $sections = []) {
		// Check if required meta informations are available
		if (empty($meta['title']) || empty($meta['slug'])) {
			throw new PageException('Page cannot be created, $meta MUST contain `title` and `slug` keys');
		}

		if (!preg_match('/^[a-z0-9\-_\+\/]+$/', $meta['slug'])) {
			throw new PageException('Page cannot be created, $meta[slug] (' .
				htmlspecialchars($meta['slug']) . ') value is not normalized');
		}

		foreach ($meta as $key => $value) {
			// object with private fields casted to array will have keys prepended with \0
			// https://php.net/manual/en/language.types.array.php#language.types.array.casting
			if (!is_null($value) && $key[0] !== "\0") {
				$this->{$key} = $value;
			}
		}

{'fraction_non_alphanumeric': 0.16786757546251216, 'fraction_numerical': 0.005842259006815969, 'mean_word_length': 3.5092186128182616, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '1521566', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1639, 'n_words': 504}