Check & convert Fields for Insert, Update

@param $obj
@return object
@throws \GapOrm\Exceptions\TypeNotExistException
483665
private function convertToDB($obj)
    {
        $o = $this->getEmptyObject();

        foreach ($this->fields as $field) {
            if (!property_exists($obj, $field->identifier()))
                continue;

            if (is_null($obj->{$field->identifier()})) {
                $o->{$field->identifier()} = 'NULL';
                continue;
            }

            $val = $obj->{$field->identifier()};

            switch ($field->type()) {
                case self::FIELD_TYPE_BOOL :
                    $o->{$field->identifier()} = $val ? 1 : 0;
                    break;
                case self::FIELD_TYPE_INT :
                    $o->{$field->identifier()} = (int) $val;
                    break;
                case self::FIELD_TYPE_FLOAT :
                    $o->{$field->identifier()} = (float) $val;
                    break;
                case self::FIELD_TYPE_STR :
                    $o->{$field->identifier()} = $this->escape($val);
                    break;
                case self::FIELD_TYPE_DATETIME :
                    if ($val && !is_null($val)) {
                        $o->{$field->identifier()} = $val->getTimestamp();
                    } else {
                        $o->{$field->identifier()} = 0;
                    }

                    break;
                case self::FIELD_TYPE_STR_ARRAY :
                    $delimiter = '(^!)';
                    $o->{$field->identifier()} = '"' . implode($delimiter, array_map(function($v) use ($delimiter) {
                            str_replace($delimiter, '', $v);
                        }, $val)) . '"';
                    break;
                case self::FIELD_TYPE_INT_ARRAY :
                    $val = array_map(function($v){ return "'" . (int)$v . "'"; }, $val);
                    $o->{$field->identifier()} = '"' . implode(',', $val) . '"';
                    break;
                case self::FIELD_TYPE_OBJ :
                    $o->{$field->identifier()} = '"' . addslashes(json_encode($val)) . '"';
                    break;
                default :
                    throw new TypeNotExistException();
            }
        }

        return $o;
    }
935731
protected function validate($obj)
    {
        $errors = $this->container->get('validator')->validate($obj);

        if (count($errors) > 0) {
            throw new ValidationException($errors);
        }
    }
855761
public function Equals( $obj )
	{
		if ( ! $obj instanceof NameValue )
		{
			return false;
		}

		/**
		 * @var TokenValue $other
		 */
		$other = $obj;
		return strcmp( $this->Value, $other->Value ) == 0;
	}
1109280
public function Equals( $obj )
	{
		if ( ! $obj instanceof GYearValue )
		{
			return false;
		}

		/**
		 * @var DateTimeValueBase $other
		 */
		$other = $obj;
		return $this->ToString( null ) == $other->ToString( null );
	}
1141064
public static function object_to_array($obj)
	{
		//$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
		if(is_object($obj) || is_array($obj)){
			if(is_object($obj)){
				$_arr = get_object_vars($obj);
			}else{
				$_arr = $obj;
			}
			foreach ($_arr as $key => $val)
			{
				$val = (is_array($val) || is_object($val)) ? self::object_to_array($val) : $val;
				$arr[$key] = $val;
			}
			return $arr;
		}else{
			return $obj;
		}
		
	}
299591
public function equals($obj)
    {
        return (gettype($obj) === 'object' &&
            $obj instanceof self &&
            $obj->getX() === $this->getX() &&
            $obj->getY() === $this->getY());
    }
583855
public function equals($obj)
    {
        if ($obj instanceof QtiScalar) {
            return $obj->getValue() === $this->getValue();
        } else {
            return $this->getValue() === $obj;
        }
    }
857479
public function before_insert(Model $obj)
	{
		// slug should be overwritten if it is enabled to be or there is no manually assigned value
		$overwrite = $this->_overwrite === true || empty($obj->{$this->_property});
		$slug = $obj->{$this->_property};

		// query to check for existence of this slug
		$query = $obj->query();

		// only determine the slug if it should be overwritten
		// fill the query with appropriate where condition
		if ($overwrite === true)
		{
			$properties = (array) $this->_source;
			$source = '';
			foreach ($properties as $property)
			{
				$source .= $this->_separator.$obj->{$property};
			}
			$slug = \Inflector::friendly_title(substr($source, 1), $this->_separator, true);

			$query->where($this->_property, 'like', $slug.'%');
		}
		else
		{
			$query->where($this->_property, $slug);
		}


		if($this->_unique === true)
		{
			// query to check for existence of this slug
			$query = $obj->query()->where($this->_property, 'like', $slug.'%');

			// is this a temporal model?
			if ($obj instanceOf Model_Temporal)
			{
				// add a filter to only check current revisions excluding the current object
				$class = get_class($obj);
				$query->where($class::temporal_property('end_column'), '=', $class::temporal_property('max_timestamp'));
				foreach($class::getNonTimestampPks() as $key)
				{
					$query->where($key, '!=', $obj->{$key});
				}
			}

			// do we have records with this slug?
			$same = $query->get();

			// make sure our slug is unique
			if ( ! empty($same))
			{
				if ($overwrite === false)
				{
					throw new \FuelException('Slug ' . $slug . ' already exists.');
				}

				$max = -1;

				foreach ($same as $record)
				{
					if (preg_match('/^'.$slug.'(?:-([0-9]+))?$/', $record->{$this->_property}, $matches))
					{
						$index = isset($matches[1]) ? (int) $matches[1] : 0;
						$max < $index and $max = $index;
					}
				}

				$max < 0 or $slug .= $this->_separator.($max + 1);
			}
		}

		$obj->{$this->_property} = $slug;
	}
1227248
public function before_update(Orm\Model $obj)
	{
		// If there are any relations loop through and check if any of them have been changed
		$relation_changed = false;

		foreach ($this->_relations as $relation)
		{
			if ($this->relation_changed($obj, $relation))
			{
				$relation_changed = true;
				break;
			}
		}

		if (($obj->is_changed() or $relation_changed) and $user_id = \Auth::get_user_id())
		{
			$obj->{$this->_property} = $user_id[1];
		}
	}
1100390
public static function create($obj)
    {
        $nomeTabela        = FuncoesReflections::pegaNomeClasseObjeto($obj);
        $atributosTabela   = FuncoesReflections::pegaAtributosDoObjeto($obj);
        $annotationsTabela = Annotations::getAnnotation($obj);
        $arrFormatado      = [];
        $stringSql = "C" . "REATE TABLE IF NOT EXISTS `" . strtolower($nomeTabela) . "` (";

        for ($i = 0; $i < count($atributosTabela); $i++) {

            $stringSql .= $atributosTabela[$i] . " ";
            for ($j = 0; $j < count($annotationsTabela[$atributosTabela[$i]]); $j++) {

                $arrAtual = explode("=", $annotationsTabela[$atributosTabela[$i]][$j]);
                for ($k = 0; $k < count($arrAtual) - 1; $k++) {
                    $arrFormatado[FuncoesString::substituiOcorrenciasDeUmaString($arrAtual[$k], "@_", "")] = $arrAtual[$k + 1];
                }
            }
            if (array_key_exists('type', $arrFormatado)) {
                $stringSql .= " " . $arrFormatado['type'] . "";

            } else {
                $stringSql .= "";
            }

            if (array_key_exists('size', $arrFormatado)) {
                if ($arrFormatado['size'] != 'none') {
                    $stringSql .= "(" . $arrFormatado['size'] . ") ";
                }

            } else {
                $stringSql .= "";
            }

            /**
             * @todo NOT NULL AQUI
             */

            if (array_key_exists('notNull', $arrFormatado)) {
                if ($arrFormatado['notNull'] === "true") {
                    $stringSql .= " NOT NULL ";
                } else {
                    $stringSql .= "";
                };
            } else {
                $stringSql .= "";
            }

            if (array_key_exists('primaryKey', $arrFormatado)) {
                if ($arrFormatado['primaryKey'] === "true") {
                    $stringSql .= " PRIMARY KEY ";
                } else {
                    $stringSql .= "";
                };
            } else {
                $stringSql .= "";
            }

            if (array_key_exists('autoIncrement', $arrFormatado)) {
                if ($arrFormatado['autoIncrement'] === "true") {
                    $stringSql .= " AUTO_INCREMENT ";
                } else {
                    $stringSql .= "";
                };
            } else {
                $stringSql .= "";
            }

            if ($i != count($atributosTabela) - 1) {

                $stringSql .= " , ";
            }
        }

        $stringSql .= ") ENGINE = InnoDB;";
        
        echo $stringSql;
        if (JsonReader::read(BASE_DIR . "/phiber_config.json")->phiber->code_sync == 1 ? true : false) {
            $pdo = self::getConnection()->prepare($stringSql);
            if ($pdo->execute()) {
                return true;
            };
        } else {
            return $stringSql;
        }
        
        return false;
    }
919952
public function delete($obj)
    {
        $o      = $this->convertToDB($obj);
        $pk     = $this->getPK();
        $sql    = 'DELETE FROM ' . $this->table() . ' WHERE ' . $pk->identifier() . ' = ?';
        $params = [$o->{$pk->identifier()}];

        GapOrm::getDriver()->query($sql, $params);
        $query = GapOrm::getDriver()->delete();

        if ($query) {
            return true;
        }

        return false;
    }
1236624
static function exists($obj)
    {
        $tabela = strtolower(FuncoesReflections::pegaNomeClasseObjeto($obj));
        $schema = JsonReader::read(BASE_DIR . '/phiber_config.json')->phiber->link->database_name;
        $sql    = "SELECT * FROM information_schema.tables WHERE table_schema = ? AND table_name = ?";
        
        if (JsonReader::read(BASE_DIR . "/phiber_config.json")->phiber->execute_querys == 1 ? true : false) {
            $pdo = self::getConnection()->prepare($sql);
            $pdo->bindValue(1, $schema);
            $pdo->bindValue(2, $tabela);
            $pdo->execute();
            
            if ($pdo->rowCount() > 0) {
                return true;
            }
                
            return false;

        } else {
            return $sql;
        }
    }
61262
static function alter($obj)
    {
        $tabela            = strtolower(FuncoesReflections::pegaNomeClasseObjeto($obj));
        $atributosTabela   = FuncoesReflections::pegaAtributosDoObjeto($obj);
        $annotationsTabela = Annotations::getAnnotation($obj);
        $arrFormatado      = [];
        $arrFinal          = [];
        $stringAlterTable  = "";
        for ($i = 0; $i < count($atributosTabela); $i++) {
            for ($j = 0; $j < count($annotationsTabela[$atributosTabela[$i]]); $j++) {
                $arrAtual = explode("=", $annotationsTabela[$atributosTabela[$i]][$j]);
                for ($k = 0; $k < count($arrAtual) - 1; $k++) {
                    $arrFormatado[FuncoesString::substituiOcorrenciasDeUmaString($arrAtual[$k], "@_", "")] = $arrAtual[$k + 1];
                }
            }

            $arrFinal[$i] = $arrFormatado;
        }

        $stringAlterTable .= "ALTER TABLE $tabela \n";
        $primKey = false;
        $columnPrimaryKey = "";

        $columnsTabela = self::columns($tabela);

        $arrayCamposTabela = [];
        for ($i = 0; $i < count($columnsTabela); $i++) {
            array_push($arrayCamposTabela, $columnsTabela[$i]['Field']);
        }

        $arrayDiff = array_diff($atributosTabela, $arrayCamposTabela);
        $arrayDiff = array_values($arrayDiff);

        $stringSql = $stringAlterTable;
        for ($j = 0; $j < count($arrayDiff); $j++) {
            if ($j != count($arrayDiff) - 1) {
                $stringSql .= "ADD " . $arrayDiff[$j];
            } else {
                $stringSql .= "ADD " . $arrayDiff[$j];
            }
            if (array_key_exists('type', $arrFormatado)) {

                $stringSql .= " " . $arrFormatado['type'] . "";

            } else {
                $stringSql .= "";
            }

            if (array_key_exists('size', $arrFormatado)) {

                $stringSql .= "(" . $arrFormatado['size'] . ") ";

            } else {
                $stringSql .= "";
            }

            /**
             * @todo NOT NULL AQUI
            */

            if (array_key_exists('notNull', $arrFormatado)) {
                if ($arrFormatado['notNull'] === "true") {
                    $stringSql .= " NOT NULL ";
                } else {
                    $stringSql .= "";
                };
            } else {
                $stringSql .= "";
            }

            if (array_key_exists('primaryKey', $arrFormatado)) {
                if ($arrFormatado['primaryKey'] === "true") {
                    $stringSql .= " PRIMARY KEY ";
                } else {
                    $stringSql .= "";
                };
            } else {
                $stringSql .= "";
            }

            if (array_key_exists('autoIncrement', $arrFormatado)) {
                if ($arrFormatado['autoIncrement'] === "true") {
                    $stringSql .= " AUTO_INCREMENT ";
                } else {
                    $stringSql .= "";
                };
            } else {
                $stringSql .= "";
            }

            $fileContent = JsonReader::read(BASE_DIR."/phiber_config.json")->phiber->code_sync == 1 ? true : false;
            if ($fileContent) {
                $pdo = self::getConnection()->prepare($stringSql);
                $pdo->execute();
            }
        }

        $columnsTabela = self::columns($tabela);
        for ($i = 0; $i < count($arrFinal); $i++) {
            if ($columnsTabela[$i]['Field'] != $atributosTabela[$i]) {
                $stringAlterTable .= "CHANGE `" . $columnsTabela[$i]['Field'] . "` `$atributosTabela[$i]` ";
                $stringAlterTable .= strtoupper($arrFinal[$i]['type']) . "(" . $arrFinal[$i]['size'] . ")\n";

                if ($arrFinal[$i])

                    if ($i != count($arrFinal) - 1) {
                        $stringAlterTable .= ", \n";
                    }
            } else {
                $stringAlterTable .= "CHANGE  `$atributosTabela[$i]` `$atributosTabela[$i]` ";

                $stringAlterTable .= strtoupper($arrFinal[$i]['type']) . "(" . $arrFinal[$i]['size'] . ")";

                $respIfNotNull = $columnsTabela[$i]['Null'] == 'NO' ? 'false' : 'true';

                if ($arrFinal[$i]['notNull'] == $respIfNotNull) {
                    $stringAlterTable .= " NOT NULL ";
                } else {
                    $stringAlterTable .= " NULL ";
                }

                if (array_key_exists('default', $arrFinal[$i]) && $arrFinal[$i]['default'] != "none") {
                    $stringAlterTable .= "DEFAULT '" . $arrFinal[$i]['default'] . "'";
                }

                if (array_key_exists('autoIncrement', $arrFinal[$i]) && $arrFinal[$i]['autoIncrement'] != "false") {
                    $stringAlterTable .= " AUTO_INCREMENT ";
                }

                $stringAlterTable .= ", \n";

                if ($arrFinal[$i]['primaryKey'] == "true" and $primKey != true) {
                    $primKey = true;
                    $columnPrimaryKey = $atributosTabela[$i];
                }
                if ($i == count($arrFinal) - 1) {
                    if ($primKey) {
                        $stringAlterTable .= " DROP PRIMARY KEY, ADD PRIMARY KEY(`$columnPrimaryKey`);";
                    } else {
                        $stringAlterTable .= " DROP PRIMARY KEY";
                    }
                }
            }
        }

        $jsonFile = JsonReader::read(BASE_DIR."/phiber_config.json")->phiber->code_sync == 1 ? true : false;
        if ($jsonFile) {
            $pdo = self::getConnection()->prepare($stringAlterTable);
            if ($pdo->execute()) {
                return true;
            };
        } else {
            return $stringAlterTable;
        }
        return false;
    }
1060998
public function Equals( $obj )
	{
		if ( ! $obj instanceof GMonthValue )
		{
			return false;
		}

		/**
		 * @var DateTimeValueBase $other
		 */
		$other = $obj;
		return $this->ToString( null ) == $other->ToString( null );
	}
699144
public function equals($obj) {
        return (is_object($obj) && $obj instanceof xPDOCriteria && $this->sql === $obj->sql && !array_diff_assoc($this->bindings, $obj->bindings));
    }
338479
public static function emptyValues($obj)
    {
        if (is_array($obj)) {
            $values = $obj;
        } elseif ($obj instanceof StripeObject) {
            $values = $obj->_values;
        } else {
            throw new \InvalidArgumentException(
                "empty_values got got unexpected object type: " . get_class($obj)
            );
        }
        $update = array_fill_keys(array_keys($values), "");
        return $update;
    }
1108391
public function Equals($obj)
	{
		if ( ! $obj instanceof IDREFValue )
		{
			return false;
		}

		/**
		 * @var TokenValue $other
		 */
		$other = $obj;
		return strcmp( $this->Value, $other->Value ) == 0;
	}