<commit_before><?php
/*
 * Doctrine Admin
 * Copyright (C) 2013 <NAME>, <EMAIL>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

namespace Bast1aan\DoctrineAdmin\View {
	use Bast1aan\DoctrineAdmin\Entity;
	use Bast1aan\DoctrineAdmin\Exception;
	use Bast1aan\DoctrineAdmin\Property;
	use Bast1aan\DoctrineAdmin\ScalarProperty;
	use Bast1aan\DoctrineAdmin\AssociationProperty;
	use Bast1aan\DoctrineAdmin\CollectionAssociationProperty;
	use Bast1aan\DoctrineAdmin\DoctrineAdmin;
	use DateTime;
	use Doctrine\DBAL\Types\Type;
	
	class View {
		
		const FORMAT_DATE = 'Y-m-d';
		const FORMAT_DATETIME = 'Y-m-d H:i:s';
		const FORMAT_TIME = 'H:i:s';

		/**
		 * @var DoctrineAdmin;
		 */
		private $doctrineAdmin;

		/**
		 * @param DoctrineAdmin $doctrineAdmin
		 */
		public function setDoctrineAdmin($doctrineAdmin)
		{
			$this->doctrineAdmin = $doctrineAdmin;
		}

		/**
		 * @return DoctrineAdmin
		 */
		public function getDoctrineAdmin()
		{
			return $this->doctrineAdmin;
		}

		public function __construct(DoctrineAdmin $doctrineAdmin = null) {
			$this->doctrineAdmin = $doctrineAdmin;
		}

		/**
		 * @return Form
		 */
		public function getForm() {
			return new Form($this);
		}
		
		/**
		 * 
		 * @param Property $property
		 * @param Form $form
		 * @return FormElement
		 */
		public function getFormElement(Property $property, Form $form) {
			if ($property instanceof ScalarProperty) {
				return $this->getScalarFormElement($property, $form);
			} elseif ($property instanceof AssociationProperty) {
				return $this->getAssociationFormElement($property, $form);
			}
		}
		
		/**
		 * 
		 * @param ScalarProperty $property
		 * @param Form $form
		 * @return FormElementScalar
		 */
		protected function getScalarFormElement(ScalarProperty $property, Form $form) {
			switch($property->getType()) {
				case Type::BOOLEAN:
					return new FormElementCheckbox($property, $form);
				case Type::STRING:
					$length = $property->getLength();
					// for strings without a known length or with a length larger than 255
					// create a textarea
					if ($length == 0 || $length > 255) {
						return new FormElementTextarea($property, $form);
					}
				default:
					return new FormElementScalar($property, $form);
			}
		}

		/**
		 * 
		 * @param AssociationProperty $property
		 * @param Form $form
		 * @return FormElementAssociation
		 */
		protected function getAssociationFormElement(AssociationProperty $property, Form $form) {
			if ($property instanceof CollectionAssociationProperty) {
				return new FormElementCollectionAssociation($property, $form);
			} elseif($property instanceof AssociationProperty) {
				return new FormElementAssociation($property, $form);
			}
		}
		
		/**
		 * @param ScalarProperty $property
		 * @return string
		 */
		public function formatScalarPropertyToString(ScalarProperty $property) {
			if ($property->isNull()) {
				return null;
			}
			switch($property->getType()) {
				case Type::BOOLEAN:
					return $property->getValue() ? 'true' : 'false';
				case Type::DATE:
					$dateTime = $this->getDateTimeFromProperty($property);
					return $dateTime->format(self::FORMAT_DATE);
				case Type::DATETIME:
				case Type::DATETIMETZ:
					$dateTime = $this->getDateTimeFromProperty($property);
					return $dateTime->format(self::FORMAT_DATETIME);
				case Type::TIME:
					$dateTime = $this->getDateTimeFromProperty($property);
					return $dateTime->format(self::FORMAT_TIME);
				default:
					return (string) $property->getValue();
			}
		}
		
		/**
		 * @param string $string
		 * @param ScalarProperty $property
		 */
		public function formatStringToScalarProperty($string, ScalarProperty& $property) {
			switch($property->getType()) {
				case Type::BOOLEAN:
					$property->setValue($string == 'true');
					return;
				case Type::DATE:
					$property->setValue(DateTime::createFromFormat(self::FORMAT_DATE, $string));
					return;
				case Type::DATETIME:
				case Type::DATETIMETZ:
					$property->setValue(DateTime::createFromFormat(self::FORMAT_DATETIME, $string));
					return;
				case Type::TIME:
					$property->setValue(DateTime::createFromFormat(self::FORMAT_TIME, $string));
					return;
				default:
					$property->setValue($string);
			}
		}
		
		/**
		 * @param ScalarProperty $property
		 * @return DateTime
		 * @throws Exception
		 */
		private function getDateTimeFromProperty(ScalarProperty $property) {
			$dateTime = $property->getValue();
			if (!$dateTime instanceof DateTime) {<commit_msg>Explicitly convert integer and float values as well Else it will fail when creating new entities<commit_after><?php
/*
 * Doctrine Admin
 * Copyright (C) 2013 <NAME>, <EMAIL>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

namespace Bast1aan\DoctrineAdmin\View {
	use Bast1aan\DoctrineAdmin\Entity;
	use Bast1aan\DoctrineAdmin\Exception;
	use Bast1aan\DoctrineAdmin\Property;
	use Bast1aan\DoctrineAdmin\ScalarProperty;
	use Bast1aan\DoctrineAdmin\AssociationProperty;
	use Bast1aan\DoctrineAdmin\CollectionAssociationProperty;
	use Bast1aan\DoctrineAdmin\DoctrineAdmin;
	use DateTime;
	use Doctrine\DBAL\Types\Type;
	
	class View {
		
		const FORMAT_DATE = 'Y-m-d';
		const FORMAT_DATETIME = 'Y-m-d H:i:s';
		const FORMAT_TIME = 'H:i:s';

		/**
		 * @var DoctrineAdmin;
		 */
		private $doctrineAdmin;

		/**
		 * @param DoctrineAdmin $doctrineAdmin
		 */
		public function setDoctrineAdmin($doctrineAdmin)
		{
			$this->doctrineAdmin = $doctrineAdmin;
		}

		/**
		 * @return DoctrineAdmin
		 */
		public function getDoctrineAdmin()
		{
			return $this->doctrineAdmin;
		}

		public function __construct(DoctrineAdmin $doctrineAdmin = null) {
			$this->doctrineAdmin = $doctrineAdmin;
		}

		/**
		 * @return Form
		 */
		public function getForm() {
			return new Form($this);
		}
		
		/**
		 * 
		 * @param Property $property
		 * @param Form $form
		 * @return FormElement
		 */
		public function getFormElement(Property $property, Form $form) {
			if ($property instanceof ScalarProperty) {
				return $this->getScalarFormElement($property, $form);
			} elseif ($property instanceof AssociationProperty) {
				return $this->getAssociationFormElement($property, $form);
			}
		}
		
		/**
		 * 
		 * @param ScalarProperty $property
		 * @param Form $form
		 * @return FormElementScalar
		 */
		protected function getScalarFormElement(ScalarProperty $property, Form $form) {
			switch($property->getType()) {
				case Type::BOOLEAN:
					return new FormElementCheckbox($property, $form);
				case Type::STRING:
					$length = $property->getLength();
					// for strings without a known length or with a length larger than 255
					// create a textarea
					if ($length == 0 || $length > 255) {
						return new FormElementTextarea($property, $form);
					}
				default:
					return new FormElementScalar($property, $form);
			}
		}

		/**
		 * 
		 * @param AssociationProperty $property
		 * @param Form $form
		 * @return FormElementAssociation
		 */
		protected function getAssociationFormElement(AssociationProperty $property, Form $form) {
			if ($property instanceof CollectionAssociationProperty) {
				return new FormElementCollectionAssociation($property, $form);
			} elseif($property instanceof AssociationProperty) {
				return new FormElementAssociation($property, $form);
			}
		}
		
		/**
		 * @param ScalarProperty $property
		 * @return string
		 */
		public function formatScalarPropertyToString(ScalarProperty $property) {
			if ($property->isNull()) {
				return null;
			}
			switch($property->getType()) {
				case Type::BOOLEAN:
					return $property->getValue() ? 'true' : 'false';
				case Type::DATE:
					$dateTime = $this->getDateTimeFromProperty($property);
					return $dateTime->format(self::FORMAT_DATE);
				case Type::DATETIME:
				case Type::DATETIMETZ:
					$dateTime = $this->getDateTimeFromProperty($property);
					return $dateTime->format(self::FORMAT_DATETIME);
				case Type::TIME:
					$dateTime = $this->getDateTimeFromProperty($property);
					return $dateTime->format(self::FORMAT_TIME);
				default:
					return (string) $property->getValue();
			}
		}
		
		/**
		 * @param string $string
		 * @param ScalarProperty $property
		 */
		public function formatStringToScalarProperty($string, ScalarProperty& $property) {
			switch($property->getType()) {
				case Type::BOOLEAN:
					$property->setValue($string == 'true');
					return;
				case Type::DATE:
					$property->setValue(DateTime::createFromFormat(self::FORMAT_DATE, $string));
					return;
				case Type::DATETIME:
				case Type::DATETIMETZ:
					$property->setValue(DateTime::createFromFormat(self::FORMAT_DATETIME, $string));
					return;
				case Type::TIME:
					$property->setValue(DateTime::createFromFormat(self::FORMAT_TIME, $string));
					return;
				case Type::BIGINT:
				case Type::SMALLINT:
				case Type::INTEGER:
					$property->setValue((int) $string);
					return;
				case Type::FLOAT:
				case Type::DECIMAL:
					$property->setValue((float) $string);
					return;
				default:
					$property->setValue($string);
			}
		}
		
		/**
		 * @param ScalarProperty $property
		 * @return DateTime
		 * @throws Exception
		 */
		private function getDateTimeFromProperty(ScalarProperty $property) {
			$dateTime = $property->getValue();
			if (!$dateTime instanceof DateTime) {
{'fraction_non_alphanumeric': 0.11593524843267522, 'fraction_numerical': 0.005988584261251989, 'mean_word_length': 3.7166813768755516, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 59, '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': '2126319', 'n_tokens_mistral': 3758, 'n_tokens_neox': 3021, 'n_words': 829}