Skip to content

Commit d211e83

Browse files
committed
Code updates
1 parent 5f99d54 commit d211e83

31 files changed

+902
-222
lines changed

notes.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* @todo Change bootstrap to routing.
4+
* Alter config to including routing config
5+
*
6+
* @todo Question, does a Exception with a code of 404 have a header of 404?
7+
* if so remove try catch
8+
*
9+
* @todo Check in View::Render that template exists
10+
*
11+
* @todo Put Connection creds into config.php
12+
*
13+
* @todo typo in creds
14+
* @todo put DB::Connection into a try catch and catch PDOException
15+
*
16+
* @todo Refactor query builder so that query statements are not exposed
17+
* EG QueryBuilder::where() from repository
18+
* Possibly have a interface that just exposes the find*, insert, insertOrUpdate and
19+
* update methods
20+
*
21+
* Haven't checked Helper or Manager yet!
22+
*
23+
* @todo Fix the failing 404 acceptance tests
24+
* vendor/bin/codecept run acceptance -g page-not-found
25+
* it is failing due to not running /public/404.html
26+
*
27+
*
28+
* @todo Add abstraction layer for repositories to allow controllers to pull out
29+
* repositories from a factory
30+
* EG:
31+
* $this->getDoctrine()->getRepository(\App\Entity\Status::class)->findAll()
32+
* $this->repostioryFactory()->make(\App\Entity\Status::class)->findAll()
33+
*
34+
* $this->getDB()->getRepository(\App\Entity\Invoice::class)->findAll();
35+
*
36+
* $this->repositoryLoader->getStatus()->findAll();
37+
*
38+
* @todo Add abstraction layer for managers to allow controller to pull out
39+
* managers from a factory
40+
* $this->getDB()->getManager(\App\Entity\Status::class)->save($status);
41+
*
42+
*
43+
*
44+
*/

src/DB/Builder/Builder.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace App\DB\Builder;
3+
4+
class Builder
5+
{
6+
private $collections = [];
7+
8+
/**
9+
* @param string $className
10+
* @return CollectionInterface
11+
*/
12+
public function make(string $className):CollectionInterface
13+
{
14+
$found = null;
15+
foreach($this->collections as $collection) {
16+
if($collection instanceof $className) {
17+
$found = $collection;
18+
break;
19+
}
20+
}
21+
22+
if(FALSE === $found instanceof CollectionInterface) {
23+
$found = new $className();
24+
$this->collections[] = $found;
25+
}
26+
27+
return $found;
28+
}
29+
30+
/**
31+
* @return WhereCollection
32+
*/
33+
public function where(): WhereCollection
34+
{
35+
36+
return $this->make(WhereCollection::class);
37+
}
38+
39+
/**
40+
* @return FieldCollection
41+
*/
42+
public function fields(): FieldCollection
43+
{
44+
return $this->make(FieldCollection::class);
45+
}
46+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace App\DB\Builder;
4+
5+
interface CollectionInterface
6+
{
7+
public function set(array $data = []): CollectionInterface;
8+
public function getSQL(): string;
9+
}

src/DB/Builder/FieldCollection.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace App\DB\Builder;
4+
5+
class FieldCollection implements CollectionInterface
6+
{
7+
/**
8+
* @var array
9+
*/
10+
private $fields = [];
11+
12+
/**
13+
* @param string $name
14+
* @return FieldCollection
15+
*/
16+
public function add(string $name): FieldCollection
17+
{
18+
$this->fields[] = $name;
19+
return $this;
20+
}
21+
22+
/**
23+
* @param array $fields
24+
* @return FieldCollection
25+
*/
26+
public function set(array $fields = []):CollectionInterface
27+
{
28+
$this->fields = [];
29+
for ($i = 0; $i < count($fields); $i++) {
30+
$this->add($fields[$i]);
31+
}
32+
return $this;
33+
}
34+
35+
/**
36+
* @return string
37+
*/
38+
public function getSQL(): string
39+
{
40+
$sql = '*';
41+
if(count($this->fields) > 0) {
42+
$sql = '';
43+
for ($i = 0; $i < count($this->fields); $i++) {
44+
if ($i > 0) {
45+
$sql .= ", ";
46+
}
47+
$sql .= "`" . $this->fields[$i] . "`";
48+
}
49+
}
50+
return $sql;
51+
}
52+
53+
}

src/DB/Builder/WhereCollection.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\DB\Builder;
4+
5+
class WhereCollection implements CollectionInterface
6+
{
7+
/**
8+
* @var array
9+
*/
10+
private $whereClause = [];
11+
12+
/**
13+
* @param string $field
14+
* @param $value
15+
* @return WhereCollection
16+
*/
17+
public function add(string $field, $value): WhereCollection
18+
{
19+
$this->whereClause[$field] = $value;
20+
return $this;
21+
}
22+
23+
/**
24+
* @param array $where
25+
* @return WhereCollection
26+
*/
27+
public function set(array $where = []): CollectionInterface
28+
{
29+
$this->whereClause = $where;
30+
31+
return $this;
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public function getSQL(): string
38+
{
39+
$sql = '';
40+
$counter = 0;
41+
$count = count($this->whereClause);
42+
43+
if($count > 0) {
44+
$sql.='WHERE ';
45+
}
46+
foreach ($this->whereClause as $field => $value) {
47+
$counter++;
48+
$sql .= '`' . $field . '` =:' . $field;
49+
50+
if ($counter < $count) {
51+
$sql .= ' AND ';
52+
}
53+
54+
}
55+
return $sql;
56+
}
57+
58+
}

src/DB/QueryBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace App\DB;
44

5-
class QueryBuilder
5+
class QueryBuilder extends QueryCreate implements QueryBuilderInterface
66
{
7+
78
/**
89
* @param array $data
910
* @param string $table

src/DB/QueryBuilderInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace App\DB;
3+
4+
interface QueryBuilderInterface
5+
{
6+
public function select(string $tableName, array $fields = []): QueryBuilderInterface;
7+
public function andWhere(string $fieldName, $value): QueryBuilderInterface;
8+
public function getSQL():string;
9+
10+
}

src/DB/QueryCreate.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace App\DB;
4+
5+
use App\DB\Builder\Builder;
6+
7+
class QueryCreate implements QueryBuilderInterface
8+
{
9+
private $builder;
10+
11+
public function __construct(Builder $builder)
12+
{
13+
$this->builder = $builder;
14+
}
15+
16+
private $sql = '';
17+
18+
/**
19+
* @param string $tableName
20+
* @param array $fields
21+
* @return QueryBuilderInterface
22+
*/
23+
public function select(string $tableName, array $fields = []): QueryBuilderInterface
24+
{
25+
$fieldCollection = $this->builder->fields()->set($fields);
26+
27+
$this->sql.= "SELECT " . $fieldCollection->getSQL() . " FROM `" . $tableName . "` ";
28+
29+
return $this;
30+
}
31+
32+
33+
34+
/**
35+
* @param string $field
36+
* @param $value
37+
* @return QueryBuilderInterface
38+
*/
39+
public function andWhere(string $field, $value): QueryBuilderInterface
40+
{
41+
$this->builder->where()->add($field, $value);
42+
43+
$this->sql.=$this->builder->where()->getSQL();
44+
45+
return $this;
46+
}
47+
48+
/**
49+
* @return string
50+
*/
51+
public function getSQL(): string
52+
{
53+
return trim($this->sql);
54+
}
55+
56+
57+
}

src/Manager/CustomerManager.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Manager;
44

55
use App\Entity\Customer;
6+
use App\Hydration\CustomerHydrator;
67
use App\Repository\CustomerRepository;
78

89
class CustomerManager extends AbstractManager
@@ -37,7 +38,16 @@ public function findOne(int $id): ?Customer
3738
*/
3839
public function findAll(): array
3940
{
40-
return $this->repository->findAll();
41+
$results = [];
42+
$rows = $this->repository->findAll('customer');
43+
44+
if (is_array($rows)) {
45+
foreach ($rows as $row) {
46+
$results[] = CustomerHydrator::hydrate($row);
47+
}
48+
}
49+
50+
return $results;
4151
}
4252

4353
/**

src/Manager/InvoiceItemManager.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Manager;
44

55
use App\Entity\InvoiceItem;
6+
use App\Hydration\InvoiceItemHydrator;
67
use App\Repository\InvoiceItemRepository;
78

89
class InvoiceItemManager extends AbstractManager
@@ -37,14 +38,23 @@ public function findOne(int $id): ?InvoiceItem
3738
*/
3839
public function findAll(): array
3940
{
40-
return $this->repository->findAll();
41+
$results = [];
42+
$rows = $this->repository->findAll('invoice_item');
43+
44+
if (is_array($rows)) {
45+
foreach ($rows as $row) {
46+
$results[] = InvoiceItemHydrator::hydrate($row);
47+
}
48+
}
49+
50+
return $results;
4151
}
4252

4353
/**
4454
* @param int $id
4555
* @return array
4656
*/
47-
public function findAllByInvoiceID(int $id):array
57+
public function findAllByInvoiceID(int $id): array
4858
{
4959
return $this->repository->findAllByInvoiceID($id);
5060
}

0 commit comments

Comments
 (0)