2014-02-25 17:43:06 +01:00
|
|
|
<?php
|
2014-05-12 18:26:17 +02:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor;
|
2014-02-25 17:43:06 +01:00
|
|
|
|
2018-03-17 03:24:30 +01:00
|
|
|
use PHPCensor\Exception\InvalidArgumentException;
|
2018-03-03 18:27:48 +01:00
|
|
|
|
|
|
|
class Model
|
2014-02-25 17:43:06 +01:00
|
|
|
{
|
2018-03-03 18:27:48 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $data = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $modified = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $tableName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $initialData
|
|
|
|
*/
|
|
|
|
public function __construct($initialData = [])
|
|
|
|
{
|
|
|
|
if (is_array($initialData)) {
|
|
|
|
foreach ($initialData as $index => $item) {
|
|
|
|
if (!array_key_exists($index, $this->data)) {
|
2018-03-17 03:24:30 +01:00
|
|
|
throw new InvalidArgumentException(sprintf(
|
2018-03-03 18:27:48 +01:00
|
|
|
'Model "%s" doesn\'t have field "%s"',
|
|
|
|
get_called_class(),
|
|
|
|
$index
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data[$index] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTableName()
|
|
|
|
{
|
|
|
|
return $this->tableName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getDataArray()
|
|
|
|
{
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getModified()
|
|
|
|
{
|
|
|
|
return $this->modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $column
|
2018-03-11 06:13:28 +01:00
|
|
|
*
|
|
|
|
* @return boolean
|
2018-03-03 18:27:48 +01:00
|
|
|
*/
|
|
|
|
protected function setModified($column)
|
|
|
|
{
|
|
|
|
$this->modified[$column] = $column;
|
2018-03-11 06:13:28 +01:00
|
|
|
|
|
|
|
return true;
|
2018-03-03 18:27:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
2018-03-17 03:24:30 +01:00
|
|
|
* @throws InvalidArgumentException
|
2018-03-03 18:27:48 +01:00
|
|
|
*/
|
|
|
|
protected function validateString($name, $value)
|
|
|
|
{
|
|
|
|
if (!is_string($value) && !is_null($value)) {
|
2018-03-17 03:24:30 +01:00
|
|
|
throw new InvalidArgumentException('Column "' . $name . '" must be a string.');
|
2018-03-03 18:27:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
2018-03-17 03:24:30 +01:00
|
|
|
* @throws InvalidArgumentException
|
2018-03-03 18:27:48 +01:00
|
|
|
*/
|
|
|
|
protected function validateInt($name, $value)
|
|
|
|
{
|
|
|
|
if (!is_integer($value) && !is_null($value)) {
|
2018-03-17 03:24:30 +01:00
|
|
|
throw new InvalidArgumentException('Column "' . $name . '" must be an integer.');
|
2018-03-03 18:27:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 19:00:53 +01:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
2018-03-17 03:24:30 +01:00
|
|
|
* @throws InvalidArgumentException
|
2018-03-09 19:00:53 +01:00
|
|
|
*/
|
|
|
|
protected function validateBoolean($name, $value)
|
|
|
|
{
|
|
|
|
if (!is_bool($value) && !is_null($value)) {
|
2018-03-17 03:24:30 +01:00
|
|
|
throw new InvalidArgumentException('Column "' . $name . '" must be a boolean.');
|
2018-03-09 19:00:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 18:27:48 +01:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
2018-03-17 03:24:30 +01:00
|
|
|
* @throws InvalidArgumentException
|
2018-03-03 18:27:48 +01:00
|
|
|
*/
|
|
|
|
protected function validateNotNull($name, $value)
|
|
|
|
{
|
|
|
|
if (is_null($value)) {
|
2018-03-17 03:24:30 +01:00
|
|
|
throw new InvalidArgumentException('Column "' . $name . '" must not be null.');
|
2018-03-03 18:27:48 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 15:35:14 +01:00
|
|
|
}
|