Skip to content

Commit 48fb9cf

Browse files
authored
Merge pull request #4 from SoftwarePunt/php81
PHP 8.1
2 parents 70d9b1b + 58c6c1e commit 48fb9cf

23 files changed

Lines changed: 198 additions & 276 deletions

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
**`php-activerecord` is no longer actively being maintained, the original site is dead, and the project is mostly abandoned.**
88

9-
I would not recommend this ORM for any new projects or developments.
9+
I would ___not___ recommend this ORM for any new projects or developments.
1010

11-
This fork contains some quality-of-life enhancements and fixes that are relevant to us and some of our older projects.
11+
This fork contains some quality-of-life enhancements, support for the latest PHP version, and fixes, which are relevant to us and some of our older projects that we maintain.
1212

1313
## Installation
1414

@@ -27,9 +27,11 @@ Because we do not actively maintain tags or versions for this project, you shoul
2727

2828
## Fork changes
2929

30+
**This fork is currently only compatible with PHP 8.1!**
31+
3032
### Fixes
3133

32-
- Compatibility with PHP 7.2+ (DateTime) and PHP 8.0+ (fix deprecated behavior).
34+
- Compatibility fixes over time for PHP 7.2, 8.0, 8.1
3335
- `activerecord_autoload` no longer causes conflicts when the library is loaded more than once.
3436
- Fix `count(): Parameter must be an array or an object that implements Countable` error in `find_by_pk()`.
3537
- Fix `castIntegerSafely()` throwing an error if the input is empty (e.g. empty string), now just returns zero.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
"php-activerecord/php-activerecord": "*"
1616
},
1717
"require-dev": {
18-
"phpunit/phpunit": "4.*",
1918
"pear/pear_exception": "1.0-beta1",
20-
"pear/log": "~1.12"
19+
"pear/log": "~1.12",
20+
"phpunit/phpunit": "^9.5"
2121
},
2222
"autoload": {
2323
"files": [ "ActiveRecord.php" ]

lib/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ public function string_to_datetime($string)
528528
$date = date_create($string);
529529
$errors = \DateTime::getLastErrors();
530530

531-
if ($errors['warning_count'] > 0 || $errors['error_count'] > 0)
531+
if ($errors !== false && ($errors['warning_count'] > 0 || $errors['error_count'] > 0))
532532
return null;
533533

534534
$date_class = Config::instance()->get_date_class();

lib/DateTime.php

Lines changed: 90 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/**
33
* @package ActiveRecord
44
*/
5+
56
namespace ActiveRecord;
67

78
/**
@@ -44,169 +45,156 @@ class DateTime extends \DateTime implements DateTimeInterface, \JsonSerializable
4445
* Pre-defined format strings.
4546
*/
4647
public static $FORMATS = array(
47-
'db' => 'Y-m-d H:i:s',
48-
'number' => 'YmdHis',
49-
'time' => 'H:i',
50-
'short' => 'd M H:i',
51-
'long' => 'F d, Y H:i',
52-
'atom' => \DateTime::ATOM,
53-
'cookie' => \DateTime::COOKIE,
48+
'db' => 'Y-m-d H:i:s',
49+
'number' => 'YmdHis',
50+
'time' => 'H:i',
51+
'short' => 'd M H:i',
52+
'long' => 'F d, Y H:i',
53+
'atom' => \DateTime::ATOM,
54+
'cookie' => \DateTime::COOKIE,
5455
'iso8601' => \DateTime::ISO8601,
55-
'rfc822' => \DateTime::RFC822,
56-
'rfc850' => \DateTime::RFC850,
56+
'rfc822' => \DateTime::RFC822,
57+
'rfc850' => \DateTime::RFC850,
5758
'rfc1036' => \DateTime::RFC1036,
5859
'rfc1123' => \DateTime::RFC1123,
5960
'rfc2822' => \DateTime::RFC2822,
6061
'rfc3339' => \DateTime::RFC3339,
61-
'rss' => \DateTime::RSS,
62-
'w3c' => \DateTime::W3C);
62+
'rss' => \DateTime::RSS,
63+
'w3c' => \DateTime::W3C);
6364

6465
private $model;
6566
private $attribute_name;
6667

67-
public function attribute_of($model, $attribute_name)
68-
{
69-
$this->model = $model;
70-
$this->attribute_name = $attribute_name;
71-
}
68+
// -----------------------------------------------------------------------------------------------------------------
69+
// Implementation overrides
7270

73-
/**
74-
* Formats the DateTime to the specified format.
75-
*
76-
* <code>
77-
* $datetime->format(); # uses the format defined in DateTime::$DEFAULT_FORMAT
78-
* $datetime->format('short'); # d M H:i
79-
* $datetime->format('Y-m-d'); # Y-m-d
80-
* </code>
81-
*
82-
* @see FORMATS
83-
* @see get_format
84-
* @param string $format A format string accepted by get_format()
85-
* @return string formatted date and time string
86-
*/
87-
public function format($format=null)
71+
public function add($interval): \DateTime
8872
{
89-
return parent::format(self::get_format($format));
73+
$this->flag_dirty();
74+
return parent::add($interval);
9075
}
9176

92-
/**
93-
* Returns the format string.
94-
*
95-
* If $format is a pre-defined format in $FORMATS it will return that otherwise
96-
* it will assume $format is a format string itself.
97-
*
98-
* @see FORMATS
99-
* @param string $format A pre-defined string format or a raw format string
100-
* @return string a format string
101-
*/
102-
public static function get_format($format=null)
103-
{
104-
// use default format if no format specified
105-
if (!$format)
106-
$format = self::$DEFAULT_FORMAT;
107-
108-
// format is a friendly
109-
if (array_key_exists($format, self::$FORMATS))
110-
return self::$FORMATS[$format];
111-
112-
// raw format
113-
return $format;
114-
}
115-
116-
/**
117-
* This needs to be overriden so it returns an instance of this class instead of PHP's \DateTime.
118-
* See http://php.net/manual/en/datetime.createfromformat.php
119-
*/
120-
public static function createFromFormat($format, $time, $tz = null)
77+
public static function createFromFormat($format, $time, $tz = null): \DateTime|false
12178
{
12279
$phpDate = $tz ? parent::createFromFormat($format, $time, $tz) : parent::createFromFormat($format, $time);
12380
if (!$phpDate)
12481
return false;
12582
// convert to this class using the timestamp
126-
$ourDate = new static(null, $phpDate->getTimezone());
83+
$ourDate = new static('', $phpDate->getTimezone());
12784
$ourDate->setTimestamp($phpDate->getTimestamp());
12885
return $ourDate;
12986
}
13087

131-
/**
132-
* @inheritDoc
133-
*/
134-
public function jsonSerialize()
135-
{
136-
return $this->format("c");
137-
}
88+
// createFromImmutable
13889

139-
public function __toString()
140-
{
141-
return $this->format();
142-
}
90+
// createFromInterface
14391

144-
/**
145-
* Handle PHP object `clone`.
146-
*
147-
* This makes sure that the object doesn't still flag an attached model as
148-
* dirty after cloning the DateTime object and making modifications to it.
149-
*
150-
* @return void
151-
*/
152-
public function __clone()
153-
{
154-
$this->model = null;
155-
$this->attribute_name = null;
156-
}
92+
// getLastErrors
15793

158-
private function flag_dirty()
94+
public function modify($modify): \DateTime
15995
{
160-
if ($this->model)
161-
$this->model->flag_dirty($this->attribute_name);
96+
$this->flag_dirty();
97+
return parent::modify($modify);
16298
}
16399

164-
public function setDate($year, $month, $day)
100+
// __set_state
101+
102+
public function setDate($year, $month, $day): \DateTime
165103
{
166104
$this->flag_dirty();
167105
return parent::setDate($year, $month, $day);
168106
}
169107

170-
public function setISODate($year, $week , $day = 1)
108+
public function setISODate($year, $week, $day = 1): \DateTime
171109
{
172110
$this->flag_dirty();
173111
return parent::setISODate($year, $week, $day);
174112
}
175113

176-
public function setTime($hour, $minute, $second = NULL, $microseconds = NULL)
114+
public function setTime($hour, $minute, $second = NULL, $microseconds = NULL): \DateTime
177115
{
178116
$this->flag_dirty();
179117
return parent::setTime($hour, $minute, $second, $microseconds);
180118
}
181119

182-
public function setTimestamp($unixtimestamp)
120+
public function setTimestamp($unixtimestamp): \DateTime
183121
{
184122
$this->flag_dirty();
185123
return parent::setTimestamp($unixtimestamp);
186124
}
187125

188-
public function setTimezone($timezone)
126+
public function setTimezone($timezone): \DateTime
189127
{
190128
$this->flag_dirty();
191129
return parent::setTimezone($timezone);
192130
}
193131

194-
public function modify($modify)
132+
public function sub($interval): \DateTime
195133
{
196134
$this->flag_dirty();
197-
return parent::modify($modify);
135+
return parent::sub($interval);
198136
}
199137

200-
public function add($interval)
138+
// diff
139+
140+
public function format($format = null): string
201141
{
202-
$this->flag_dirty();
203-
return parent::add($interval);
142+
return parent::format(self::get_format($format));
204143
}
205144

206-
public function sub($interval)
145+
// getOffset
146+
147+
// getTimestamp
148+
149+
// getTimezone
150+
151+
// __wakeUp
152+
153+
// -----------------------------------------------------------------------------------------------------------------
154+
// Activerecord extensions
155+
156+
public static function get_format($format = null)
207157
{
208-
$this->flag_dirty();
209-
return parent::sub($interval);
158+
// use default format if no format specified
159+
if (!$format)
160+
$format = self::$DEFAULT_FORMAT;
161+
162+
// format is a friendly
163+
if (array_key_exists($format, self::$FORMATS))
164+
return self::$FORMATS[$format];
165+
166+
// raw format
167+
return $format;
210168
}
211169

170+
public function attribute_of($model, $attribute_name)
171+
{
172+
$this->model = $model;
173+
$this->attribute_name = $attribute_name;
174+
}
175+
176+
private function flag_dirty()
177+
{
178+
if ($this->model)
179+
$this->model->flag_dirty($this->attribute_name);
180+
}
181+
182+
public function __toString()
183+
{
184+
return $this->format();
185+
}
186+
187+
public function __clone()
188+
{
189+
$this->model = null;
190+
$this->attribute_name = null;
191+
}
192+
193+
// -----------------------------------------------------------------------------------------------------------------
194+
// JsonSerializable
195+
196+
public function jsonSerialize(): mixed
197+
{
198+
return $this->format("c");
199+
}
212200
}

lib/SQLBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class SQLBuilder
1919
private $order;
2020
private $limit;
2121
private $offset;
22-
private $group;
23-
private $having;
24-
private $update;
22+
private $group = '';
23+
private $having = '';
24+
private $update = '';
2525

2626
// for where
2727
private $where;
@@ -419,4 +419,4 @@ private function quoted_key_names()
419419

420420
return $keys;
421421
}
422-
}
422+
}

lib/Validations.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Validations
4545
private $options = array();
4646
private $validators = array();
4747
private $record;
48+
private $klass;
4849

4950
private static $VALIDATION_FUNCTIONS = array(
5051
'validates_presence_of',
@@ -517,7 +518,7 @@ public function validates_length_of($attrs)
517518
$message = $options['message'];
518519
else
519520
$message = $options[$messageOptions[$range_option]];
520-
521+
521522

522523
$message = str_replace('%d', $option, $message);
523524
$attribute_value = $this->model->$attribute;
@@ -904,7 +905,7 @@ public function size()
904905
*
905906
* @return ArrayIterator
906907
*/
907-
public function getIterator()
908+
public function getIterator(): ArrayIterator
908909
{
909910
return new ArrayIterator($this->full_messages());
910911
}

0 commit comments

Comments
 (0)