Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "php-activerecord/php-activerecord",
"name": "foxycart/php-activerecord",
"type": "library",
"description": "php-activerecord is an open source ORM library based on the ActiveRecord pattern.",
"keywords": ["activerecord", "orm"],
Expand All @@ -14,6 +14,7 @@
"pear/log": "~1.12"
},
"autoload": {
"files": [ "ActiveRecord.php" ]
"classmap": ["lib/"],
"files": ["lib/Utils.php"]
}
}
4 changes: 2 additions & 2 deletions lib/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static function castIntegerSafely($value)

// If adding 0 to a string causes a float conversion,
// we have a number over PHP_INT_MAX
elseif (is_string($value) && is_float($value + 0))
elseif (is_string($value) && is_numeric($value) && is_float($value + 0))
return (string) $value;

// If a float was passed and its greater than PHP_INT_MAX
Expand All @@ -153,7 +153,7 @@ public static function castIntegerSafely($value)
public function cast($value, $connection)
{
if ($value === null)
return null;
return $this->type === self::STRING ? '' : null;

switch ($this->type)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public function string_to_datetime($string)
$date = date_create($string);
$errors = \DateTime::getLastErrors();

if ($errors['warning_count'] > 0 || $errors['error_count'] > 0)
if ($errors && ($errors['warning_count'] > 0 || $errors['error_count'] > 0))
return null;

$date_class = Config::instance()->get_date_class();
Expand Down
44 changes: 24 additions & 20 deletions lib/DateTime.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

/**
* @package ActiveRecord
*/

namespace ActiveRecord;

use DateTimeZone;

/**
* An extension of PHP's DateTime class to provide dirty flagging and easier formatting options.
*
Expand Down Expand Up @@ -43,7 +47,7 @@ class DateTime extends \DateTime implements DateTimeInterface
/**
* Pre-defined format strings.
*/
public static $FORMATS = array(
public static $FORMATS = [
'db' => 'Y-m-d H:i:s',
'number' => 'YmdHis',
'time' => 'H:i',
Expand All @@ -59,7 +63,8 @@ class DateTime extends \DateTime implements DateTimeInterface
'rfc2822' => \DateTime::RFC2822,
'rfc3339' => \DateTime::RFC3339,
'rss' => \DateTime::RSS,
'w3c' => \DateTime::W3C);
'w3c' => \DateTime::W3C
];

private $model;
private $attribute_name;
Expand All @@ -84,7 +89,7 @@ public function attribute_of($model, $attribute_name)
* @param string $format A format string accepted by get_format()
* @return string formatted date and time string
*/
public function format($format=null)
public function format(?string $format = null): string
{
return parent::format(self::get_format($format));
}
Expand All @@ -99,15 +104,15 @@ public function format($format=null)
* @param string $format A pre-defined string format or a raw format string
* @return string a format string
*/
public static function get_format($format=null)
public static function get_format($format = null)
{
// use default format if no format specified
if (!$format)
$format = self::$DEFAULT_FORMAT;

// format is a friendly
if (array_key_exists($format, self::$FORMATS))
return self::$FORMATS[$format];
return self::$FORMATS[$format];

// raw format
return $format;
Expand All @@ -117,13 +122,13 @@ public static function get_format($format=null)
* This needs to be overriden so it returns an instance of this class instead of PHP's \DateTime.
* See http://php.net/manual/en/datetime.createfromformat.php
*/
public static function createFromFormat($format, $time, $tz = null)
public static function createFromFormat(string $format, string $time, ?DateTimeZone $tz = null): static|false
{
$phpDate = $tz ? parent::createFromFormat($format, $time, $tz) : parent::createFromFormat($format, $time);
if (!$phpDate)
return false;
// convert to this class using the timestamp
$ourDate = new static(null, $phpDate->getTimezone());
$ourDate = new static('now', $phpDate->getTimezone());
$ourDate->setTimestamp($phpDate->getTimestamp());
return $ourDate;
}
Expand Down Expand Up @@ -153,52 +158,51 @@ private function flag_dirty()
$this->model->flag_dirty($this->attribute_name);
}

public function setDate($year, $month, $day)
public function setDate(int $year, int $month, int $day): static
{
$this->flag_dirty();
return parent::setDate($year, $month, $day);
}

public function setISODate($year, $week , $day = 1)
public function setISODate(int $year, int $week, int $day = 1): static
{
$this->flag_dirty();
return parent::setISODate($year, $week, $day);
}

public function setTime($hour, $minute, $second = 0, $microseconds = 0)
public function setTime(int $hour, int $minute, int $second = 0, int $microseconds = 0): static
{
$this->flag_dirty();
return parent::setTime($hour, $minute, $second);
return parent::setTime($hour, $minute, $second, $microseconds);
}

public function setTimestamp($unixtimestamp)
public function setTimestamp(int $unixtimestamp): static
{
$this->flag_dirty();
return parent::setTimestamp($unixtimestamp);
}

public function setTimezone($timezone)
public function setTimezone(\DateTimeZone $timezone): static
{
$this->flag_dirty();
return parent::setTimezone($timezone);
}
public function modify($modify)

public function modify(string $modifier): static
{
$this->flag_dirty();
return parent::modify($modify);
return parent::modify($modifier);
}
public function add($interval)

public function add(\DateInterval $interval): static
{
$this->flag_dirty();
return parent::add($interval);
}

public function sub($interval)
public function sub(\DateInterval $interval): static
{
$this->flag_dirty();
return parent::sub($interval);
}

}
10 changes: 7 additions & 3 deletions lib/DateTimeInterface.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

/**
* @package ActiveRecord
*/

namespace ActiveRecord;

use DateTimeZone;

/**
* Interface for the ActiveRecord\DateTime class so that ActiveRecord\Model->assign_attribute() will
* know to call attribute_of() on passed values. This is so the DateTime object can flag the model
Expand All @@ -18,18 +22,18 @@ interface DateTimeInterface
* Indicates this object is an attribute of the specified model, with the given attribute name.
*
* @param Model $model The model this object is an attribute of
* @param string $attribute_name The attribute name
* @param string $attribute_name The attribute name
* @return void
*/
public function attribute_of($model, $attribute_name);

/**
* Formats the DateTime to the specified format.
*/
public function format($format=null);
public function format(?string $format = null): string;

/**
* See http://php.net/manual/en/datetime.createfromformat.php
*/
public static function createFromFormat($format, $time, $tz = null);
public static function createFromFormat(string $format, string $time, ?DateTimeZone $tz = null): static|false;
}
Loading