<commit_before><?php

/**
 * Copyright 2015-2016 Unicity International
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

declare(strict_types = 1);

namespace Unicity\Core {

	use \Unicity\Common;
	use \Unicity\Core;
	use \Unicity\Throwable;

	/**
	 * This class converts a base data type to another base data type.
	 *
	 * @access public
	 * @class
	 * @package Core
	 */
	class Convert extends Core\Object {

		/**
		 * This method converts the specified value to the specified type.
		 *
		 * @access public
		 * @static
		 * @param mixed $value                                      the value to be converted
		 * @param string $type                                      the data type to be applied
		 * @return mixed                                            the new value
		 * @throws \Unicity\Throwable\Parse\Exception               indicates that the value could not
		 *                                                          be converted
		 */
		public static function changeType($value, $type) {
			$type2 = strtolower($type);
			switch ($type2) {
				case 'array':
				case 'list':
					return static::toArray($value);
				case 'assoc':
				case 'dictionary':
				case 'map':
					return static::toDictionary($value);
				case 'bool':
				case 'boolean':
					return static::toBoolean($value);
				case 'char':
					return static::toChar($value);
				case 'date':
				case 'datetime':
				case 'time':
				case 'timestamp':
					return static::toDateTime($value);
				case 'decimal':
				case 'double':
				case 'float':
				case 'money':
				case 'number':
				case 'real':
				case 'single':
					return static::toDouble($value);
				case 'bit':
				case 'byte':
				case 'int':
				case 'int8':
				case 'int16':
				case 'int32':
				case 'int64':
				case 'long':
				case 'short':
				case 'uint':
				case 'uint8':
				case 'uint16':
				case 'uint32':
				case 'uint64':
				case 'integer':
				case 'word':
					return static::toInteger($value);
				case 'object':
					return static::toObject($value);
				case 'ord':
				case 'ordinal':
					return static::toOrdinal($value);
				case 'nil':
				case 'null':
					return null;
				case 'nvarchar':
				case 'string':
				case 'varchar':
					return static::toString($value);
				case 'undefined':
					return Core\Data\Undefined::instance();
				default:
					throw new Throwable\Parse\Exception('Invalid cast. Could not convert value of type ":type1" to a(n) :type2.', array(':type1' => gettype($value), ':type2' => $type));
			}
		}

		/**
		 * This method converts the specified value to an array.
		 *
		 * @access public
		 * @static
		 * @param mixed $value                                      the value to be converted
		 * @return array                                            an array
		 * @throws \Unicity\Throwable\Parse\Exception               indicates that the value could not
		 *                                                          be converted
		 */
		public static function toArray($value) : array {
			$type = gettype($value);
			switch ($type) {
				case 'array':
					$keys = array_keys($value);
					if (array_keys($keys) !== $keys) { // is_dictionary
						return array_values($value);
					}
					return $value;
				case 'object':
					if (method_exists($value, 'toArray')) {
						return $value->toArray();
					}
					if (method_exists($value, 'toList')) {
						return Core\Convert::toArray($value->toList());
					}
					return array_values(get_object_vars($value));
				default:
					throw new Throwable\Parse\Exception('Invalid cast. Could not convert value of type ":type" to an array.', array(':type' => $type));
			}
		}

		/**
		 * This method converts the specified value to a boolean.
		 *
		 * @access public
		 * @static
		 * @param mixed $value                                      the value to be converted
		 * @return boolean                                          the equivalent boolean representation
		 * @throws \Unicity\Throwable\Parse\Exception               indicates that the value could not
		 *                                                          be converted
		 */
		public static function toBoolean($value) : bool {
			$type = gettype($value);
			switch ($type) {
				case 'boolean':
					return $value;
				case 'double':
					return boolval($value);
				case 'integer':
					return boolval($value);
				case 'NULL':
					return false;
				case 'object':
					if (method_exists($value, 'toBoolean')) {
						return $value->toBoolean();
					}
					else if ($value instanceof Common\StringRef) {
						return static::toBoolean($value->__toString());
					}
					else {
						throw new Throwable\Parse\Exception('Invalid cast. Could not convert value of type ":type" to a boolean.', array(':type' => get_class($value)));
					}
				case 'string':
					return (in_array(strtolower($value), array('false', 'f', 'no', 'n', '0', 'null', 'nil'))) ? false : (bool) $value;
				default:
					throw new Throwable\Parse\Exception('Invalid cast. Could not convert value of type ":type" to a boolean.', array(':type' => $type));
			}
		}

		/**
		 * This method converts the specified value to a char.
		 *
		 * @access public
		 * @static
		 * @param mixed $value                                      the value to be converted
		 * @return string                                           the equivalent char representation
		 * @throws \Unicity\Throwable\Parse\Exception               indicates that the value could not
		 *                                                          be converted<commit_msg>Converting undefined field to false.<commit_after><?php

/**
 * Copyright 2015-2016 Unicity International
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

declare(strict_types = 1);

namespace Unicity\Core {

	use \Unicity\Common;
	use \Unicity\Core;
	use \Unicity\Throwable;

	/**
	 * This class converts a base data type to another base data type.
	 *
	 * @access public
	 * @class
	 * @package Core
	 */
	class Convert extends Core\Object {

		/**
		 * This method converts the specified value to the specified type.
		 *
		 * @access public
		 * @static
		 * @param mixed $value                                      the value to be converted
		 * @param string $type                                      the data type to be applied
		 * @return mixed                                            the new value
		 * @throws \Unicity\Throwable\Parse\Exception               indicates that the value could not
		 *                                                          be converted
		 */
		public static function changeType($value, $type) {
			$type2 = strtolower($type);
			switch ($type2) {
				case 'array':
				case 'list':
					return static::toArray($value);
				case 'assoc':
				case 'dictionary':
				case 'map':
					return static::toDictionary($value);
				case 'bool':
				case 'boolean':
					return static::toBoolean($value);
				case 'char':
					return static::toChar($value);
				case 'date':
				case 'datetime':
				case 'time':
				case 'timestamp':
					return static::toDateTime($value);
				case 'decimal':
				case 'double':
				case 'float':
				case 'money':
				case 'number':
				case 'real':
				case 'single':
					return static::toDouble($value);
				case 'bit':
				case 'byte':
				case 'int':
				case 'int8':
				case 'int16':
				case 'int32':
				case 'int64':
				case 'long':
				case 'short':
				case 'uint':
				case 'uint8':
				case 'uint16':
				case 'uint32':
				case 'uint64':
				case 'integer':
				case 'word':
					return static::toInteger($value);
				case 'object':
					return static::toObject($value);
				case 'ord':
				case 'ordinal':
					return static::toOrdinal($value);
				case 'nil':
				case 'null':
					return null;
				case 'nvarchar':
				case 'string':
				case 'varchar':
					return static::toString($value);
				case 'undefined':
					return Core\Data\Undefined::instance();
				default:
					throw new Throwable\Parse\Exception('Invalid cast. Could not convert value of type ":type1" to a(n) :type2.', array(':type1' => gettype($value), ':type2' => $type));
			}
		}

		/**
		 * This method converts the specified value to an array.
		 *
		 * @access public
		 * @static
		 * @param mixed $value                                      the value to be converted
		 * @return array                                            an array
		 * @throws \Unicity\Throwable\Parse\Exception               indicates that the value could not
		 *                                                          be converted
		 */
		public static function toArray($value) : array {
			$type = gettype($value);
			switch ($type) {
				case 'array':
					$keys = array_keys($value);
					if (array_keys($keys) !== $keys) { // is_dictionary
						return array_values($value);
					}
					return $value;
				case 'object':
					if (method_exists($value, 'toArray')) {
						return $value->toArray();
					}
					if (method_exists($value, 'toList')) {
						return Core\Convert::toArray($value->toList());
					}
					return array_values(get_object_vars($value));
				default:
					throw new Throwable\Parse\Exception('Invalid cast. Could not convert value of type ":type" to an array.', array(':type' => $type));
			}
		}

		/**
		 * This method converts the specified value to a boolean.
		 *
		 * @access public
		 * @static
		 * @param mixed $value                                      the value to be converted
		 * @return boolean                                          the equivalent boolean representation
		 * @throws \Unicity\Throwable\Parse\Exception               indicates that the value could not
		 *                                                          be converted
		 */
		public static function toBoolean($value) : bool {
			$type = gettype($value);
			switch ($type) {
				case 'boolean':
					return $value;
				case 'double':
					return boolval($value);
				case 'integer':
					return boolval($value);
				case 'NULL':
					return false;
				case 'object':
					if (method_exists($value, 'toBoolean')) {
						return $value->toBoolean();
					}
					else if ($value instanceof Common\StringRef) {
						return static::toBoolean($value->__toString());
					}
					else if ($value instanceof Core\Data\Undefined) {
						return false;
					}
					else {
						throw new Throwable\Parse\Exception('Invalid cast. Could not convert value of type ":type" to a boolean.', array(':type' => get_class($value)));
					}
				case 'string':
					return (in_array(strtolower($value), array('false', 'f', 'no', 'n', '0', 'null', 'nil'))) ? false : (bool) $value;
				default:
					throw new Throwable\Parse\Exception('Invalid cast. Could not convert value of type ":type" to a boolean.', array(':type' => $type));
			}
		}

		/**
		 * This method converts the specified value to a char.
		 *
		 * @access public
		 * @static
		 * @param mixed $value                                      the value to be converted
		 * @return string                                           the equivalent char representation
		 * @throws \Unicity\Throwable\Parse\Exception               indicates that the value could not
		 *                                                          be converted
{'fraction_non_alphanumeric': 0.1156963890935888, 'fraction_numerical': 0.005567837550151478, 'mean_word_length': 2.037552847550361, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1860500', 'n_tokens_mistral': 4097, 'n_tokens_neox': 3140, 'n_words': 1098}