Skip to content

Commit 71a4a19

Browse files
committed
Update source
1 parent 07ef36a commit 71a4a19

File tree

6 files changed

+63
-62
lines changed

6 files changed

+63
-62
lines changed

src/Base.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
<?php
2-
namespace PhpOrm;
2+
declare(strict_types=1);
3+
4+
namespace Riverside\Orm;
35

46
/**
57
* Class Base
68
*
7-
* @package PhpOrm
9+
* @package Riverside\Orm
810
*/
911
class Base
1012
{
1113
/**
1214
* Throws an exception
1315
*
1416
* @param string $message
15-
* @param int|null $code
16-
* @param \Throwable|null $previous
17+
* @param int $code (optional)
18+
* @param \Throwable|null $previous (optional)
1719
* @throws Exception
1820
*/
19-
public function throwException(string $message, int $code=null, \Throwable $previous=null)
21+
public function throwException(string $message, int $code=0, \Throwable $previous=null)
2022
{
2123
throw new Exception($message, $code, $previous);
2224
}
23-
}
25+
}

src/Configuration.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
2-
namespace PhpOrm;
2+
declare(strict_types=1);
3+
4+
namespace Riverside\Orm;
35

46
/**
57
* Class Configuration
68
*
7-
* @package PhpOrm
9+
* @package Riverside\Orm
810
*/
911
class Configuration
1012
{

src/Connection.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
2-
namespace PhpOrm;
2+
declare(strict_types=1);
3+
4+
namespace Riverside\Orm;
35

46
/**
57
* Class Connection
68
*
7-
* @package PhpOrm
9+
* @package Riverside\Orm
810
*/
911
class Connection extends Base
1012
{
@@ -155,4 +157,4 @@ public function reconnect(): Connection
155157

156158
return $this;
157159
}
158-
}
160+
}

src/DB.php

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?php
2-
namespace PhpOrm;
2+
declare(strict_types=1);
3+
4+
namespace Riverside\Orm;
35

46
/**
57
* Class DB
68
*
7-
* @package PhpOrm
9+
* @package Riverside\Orm
810
*/
911
class DB extends Base
1012
{
1113
protected $attributes = array();
1214

13-
private static $config = 'database.php';
14-
1515
protected $connection = 'default';
1616

1717
private $data = null;
@@ -53,9 +53,13 @@ class DB extends Base
5353
/**
5454
* DB constructor.
5555
*
56+
* @param string|null $connection
5657
* @throws Exception
5758
*/
58-
public function __construct() {
59+
public function __construct(string $connection=null) {
60+
if ($connection) {
61+
$this->connection = $connection;
62+
}
5963
$this->mount();
6064
}
6165

@@ -238,17 +242,10 @@ public function commit(): DB
238242
}
239243

240244
/**
241-
* @param string $filename
242-
*/
243-
public static function config(string $filename) {
244-
self::$config = $filename;
245-
}
246-
247-
/**
248-
* @return int|null
245+
* @return int
249246
* @throws Exception
250247
*/
251-
public function count(): ?int
248+
public function count(): int
252249
{
253250
$statement = "SELECT COUNT(*) AS cnt";
254251

@@ -280,7 +277,9 @@ public function count(): ?int
280277

281278
$this->dump();
282279

283-
return $this->sth->fetchColumn();
280+
$result = $this->sth->fetchColumn();
281+
282+
return $result ? (int) $result : 0;
284283
}
285284

286285
/**
@@ -296,10 +295,10 @@ public function debug(bool $value): DB
296295

297296
/**
298297
* @param null $modifiers
299-
* @return int|null
298+
* @return int
300299
* @throws Exception
301300
*/
302-
public function delete($modifiers = null): ?int
301+
public function delete($modifiers = null): int
303302
{
304303
if ($modifiers) {
305304
$modifiers = is_array($modifiers) ? $modifiers : array($modifiers);
@@ -427,18 +426,18 @@ public function having(string $value = null): DB
427426
/**
428427
* @param array $data
429428
* @param null $modifiers
430-
* @return int|null
429+
* @return int
431430
* @throws Exception
432431
*/
433-
public function insert(array $data, $modifiers = null): ?int
432+
public function insert(array $data, $modifiers = null): int
434433
{
435434
$this->fire($this->buildInsert($data, $modifiers));
436435

437436
$this->dump();
438437

439438
return $this->sth->rowCount()
440-
? $this->dbh->lastInsertId()
441-
: false;
439+
? (int) $this->dbh->lastInsertId()
440+
: 0;
442441
}
443442

444443
/**
@@ -500,21 +499,13 @@ protected function mount(): DB
500499
return $this;
501500
}
502501

503-
if (!is_file(self::$config))
504-
{
505-
$this->throwException("File '".self::$config."' not found.");
506-
}
507-
$database = include self::$config;
508-
if (!isset($database[$this->connection]))
509-
{
510-
$this->throwException("Connection not found.");
511-
}
512-
$opts = $database[$this->connection];
502+
$opts = [];
513503
foreach (array('username', 'password', 'database', 'host', 'port', 'driver', 'charset', 'collation') as $key)
514504
{
515-
if (!array_key_exists($key, $opts))
505+
$opts[$key] = getenv(strtoupper($this->connection . '_' . $key));
506+
if ($opts[$key] === false)
516507
{
517-
$this->throwException("The '$key' index was not found in config.");
508+
$this->throwException("The '$key' index was not found as environment variable.");
518509
}
519510
}
520511

@@ -523,7 +514,7 @@ protected function mount(): DB
523514
$opts['password'],
524515
$opts['database'],
525516
$opts['host'],
526-
$opts['port'],
517+
intval($opts['port']),
527518
$opts['driver'],
528519
$opts['charset'],
529520
$opts['collation']);
@@ -587,18 +578,18 @@ public function releaseSavepoint(string $identifier): DB
587578
/**
588579
* @param array $data
589580
* @param null $modifiers
590-
* @return int|null
581+
* @return int
591582
* @throws Exception
592583
*/
593-
public function replace(array $data, $modifiers = null): ?int
584+
public function replace(array $data, $modifiers = null): int
594585
{
595586
$this->fire($this->buildInsert($data, $modifiers, 'replace'));
596587

597588
$this->dump();
598589

599590
return $this->sth->rowCount()
600-
? $this->dbh->lastInsertId()
601-
: false;
591+
? (int) $this->dbh->lastInsertId()
592+
: 0;
602593
}
603594

604595
/**
@@ -785,31 +776,31 @@ public function update(array $data, $modifiers = null): int
785776
/**
786777
* @param array $data
787778
* @param null $modifiers
788-
* @return int|null
779+
* @return int
789780
* @throws Exception
790781
*/
791-
public function upsert(array $data, $modifiers = null): ?int
782+
public function upsert(array $data, $modifiers = null): int
792783
{
793784
$this->fire($this->buildInsert($data, $modifiers, 'upsert'));
794785

795786
return $this->sth->rowCount()
796-
? $this->dbh->lastInsertId()
797-
: false;
787+
? (int) $this->dbh->lastInsertId()
788+
: 0;
798789
}
799790

800791
/**
801792
* @param string $column
802-
* @return string
793+
* @return null|string
803794
* @throws Exception
804795
*/
805-
public function value(string $column): string
796+
public function value(string $column): ?string
806797
{
807798
$row = $this->first();
808799
if (!$row) {
809-
return false;
800+
return null;
810801
}
811802

812-
return array_key_exists($column, $row) ? $row[$column] : NULL;
803+
return array_key_exists($column, $row) ? $row[$column] : null;
813804
}
814805

815806
/**

src/Exception.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
2-
namespace PhpOrm;
2+
declare(strict_types=1);
3+
4+
namespace Riverside\Orm;
35

46
/**
57
* Class Exception
68
*
7-
* @package PhpOrm
9+
* @package Riverside\Orm
810
*/
911
class Exception extends \Exception
1012
{
1113

12-
}
14+
}

src/Expression.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
2-
namespace PhpOrm;
2+
declare(strict_types=1);
3+
4+
namespace Riverside\Orm;
35

46
/**
57
* Class Expression
68
*
7-
* @package PhpOrm
9+
* @package Riverside\Orm
810
*/
911
class Expression
1012
{

0 commit comments

Comments
 (0)