Skip to content

Commit e27a022

Browse files
committed
Added OrderHydrator and OrderValidation and creation
1 parent 3b43184 commit e27a022

File tree

20 files changed

+900
-216
lines changed

20 files changed

+900
-216
lines changed

.idea/workspace.xml

+309-188
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"peridot-php/peridot-prophecy-plugin": "^1.1"
1414
},
1515
"require": {
16-
"zendframework/zendframework": "^2.4"
16+
"zendframework/zendframework": "^2.4",
17+
"vnn/keyper": "^1.3"
1718
}
1819
}

composer.lock

+48-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/autoload/global.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use CleanPhp\Invoicer\Persistence\Zend\DataTable\OrderTable;
2020
use CleanPhp\Invoicer\Persistence\Zend\TableGateway\TableGatewayFactory;
2121
use Zend\Stdlib\Hydrator\ClassMethods;
22-
22+
use CleanPhp\Invoicer\Persistence\Hydrator\OrderHydrator;
2323
return array('service_manager' => array(
2424
'factories' => array(
2525
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
@@ -47,9 +47,15 @@
4747
$hydrator
4848
);
4949
},
50+
'OrderHydrator' => function ($sm) {
51+
return new OrderHydrator(
52+
new ClassMethods(),
53+
$sm->get('CustomerTable')
54+
);
55+
},
5056
'OrderTable' => function ($sm) {
5157
$factory = new TableGatewayFactory();
52-
$hydrator = new ClassMethods();
58+
$hydrator = $sm->get('OrderHydrator');
5359
return new OrderTable($factory->createGateway(
5460
$sm->get('Zend\Db\Adapter\Adapter'), $hydrator,
5561
new Order(),

module/Application/config/module.config.php

+14-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @license http://framework.zend.com/license/new-bsd New BSD License
88
*/
99
use CleanPhp\Invoicer\Service\InputFilter\CustomerInputFilter;
10+
use CleanPhp\Invoicer\Service\InputFilter\OrderInputFilter;
1011
use Zend\Stdlib\Hydrator\ClassMethods;
1112

1213
return array(
@@ -42,7 +43,8 @@
4243
),
4344
'defaults' => array(
4445
'action' => 'new-or-edit',
45-
),)
46+
),
47+
),
4648
),
4749
'edit' => array(
4850
'type' => 'Segment',
@@ -53,23 +55,16 @@
5355
),
5456
'defaults' => array(
5557
'action' => 'new-or-edit',
56-
),)
57-
),
58-
'create' => array(
59-
'type' => 'Segment',
60-
'options' => array(
61-
'route' => '/new',
62-
'defaults' => array(
63-
'action' => 'new',
6458
),
65-
)
59+
),
6660
),
67-
)
61+
62+
),
6863
),
6964
'orders' => array(
7065
'type' => 'Segment',
7166
'options' => array(
72-
'route' => '/orders',
67+
'route' => '/orders[/:action[/:id]]',
7368
'defaults' => array(
7469
'controller' => 'Application\Controller\Orders',
7570
'action' => 'index',
@@ -147,6 +142,13 @@
147142
new ClassMethods()
148143
);
149144
},
145+
'Application\Controller\Orders' => function ($sm) {
146+
return new \Application\Controller\OrdersController(
147+
$sm->getServiceLocator()->get('OrderTable'),
148+
$sm->getServiceLocator()->get('CustomerTable'),
149+
new OrderInputFilter(), $sm->getServiceLocator()->get('OrderHydrator')
150+
);
151+
},
150152
),
151153
),
152154
'view_manager' => array(

module/Application/src/Application/Controller/CustomersController.php

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public function newOrEditAction()
4343
$customer = $id ? $this->customerRepository->getById($id) : new Customer();
4444

4545
$viewModel = new ViewModel();
46-
// $customer = new Customer();
4746

4847

4948
if ($this->getRequest()->isPost()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: francisc
5+
* Date: 23/07/15
6+
* Time: 21:54
7+
*/
8+
9+
namespace Application\Controller;
10+
11+
use CleanPhp\Invoicer\Domain\Repository\OrderRepositoryInterface;
12+
use CleanPhp\Invoicer\Domain\Repository\CustomerRepositoryInterface;
13+
use CleanPhp\Invoicer\Domain\Entity\Order;
14+
use Zend\Mvc\Controller\AbstractActionController;
15+
use Zend\View\Model\ViewModel;
16+
use CleanPhp\Invoicer\Persistence\Hydrator\OrderHydrator;
17+
use CleanPhp\Invoicer\Service\InputFilter\OrderInputFilter;
18+
19+
class OrdersController extends AbstractActionController
20+
{
21+
protected $orderRepository;
22+
protected $customerRepository;
23+
protected $inputFilter;
24+
protected $hydrator;
25+
26+
public function __construct(OrderRepositoryInterface $orderRepository, CustomerRepositoryInterface $customerRepository, OrderInputFilter $inputFilter,
27+
OrderHydrator $hydrator
28+
)
29+
{
30+
$this->orderRepository = $orderRepository;
31+
$this->customerRepository = $customerRepository;
32+
$this->inputFilter = $inputFilter;
33+
$this->hydrator = $hydrator;
34+
}
35+
36+
public
37+
function indexAction()
38+
{
39+
return [
40+
'orders' => $this->orderRepository->getAll()
41+
];
42+
}
43+
44+
public
45+
function viewAction()
46+
{
47+
$id = $this->params()->fromRoute('id');
48+
$order = $this->orderRepository->getById($id);
49+
50+
if (!$order) {
51+
$this->getResponse()->setStatusCode(404);
52+
return null;
53+
}
54+
55+
return [
56+
'order' => $order
57+
];
58+
}
59+
60+
public function newAction()
61+
{
62+
$viewModel = new ViewModel();
63+
$order = new Order();
64+
if ($this->getRequest()->isPost()) {
65+
$this->inputFilter
66+
->setData($this->params()->fromPost());
67+
if ($this->inputFilter->isValid()) {
68+
$order = $this->hydrator->hydrate($this->inputFilter->getValues(),
69+
$order
70+
);
71+
$this->orderRepository->begin()
72+
->persist($order)
73+
->commit();
74+
$this->flashMessenger()->addSuccessMessage('Order Created');
75+
$this->redirect()->toUrl('/orders/view/' . $order->getId());
76+
} else {
77+
$this->hydrator->hydrate(
78+
$this->params()->fromPost(),
79+
$order
80+
);
81+
$viewModel->setVariable(
82+
'errors',
83+
$this->inputFilter->getMessages()
84+
);
85+
}
86+
}
87+
$viewModel->setVariable(
88+
'customers',
89+
$this->customerRepository->getAll()
90+
);
91+
$viewModel->setVariable('order', $order);
92+
return $viewModel;
93+
}
94+
}

module/Application/src/Application/View/Helper/ValidationErrors.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
* Time: 07:30
88
*/
99
namespace Application\View\Helper;
10+
1011
use Zend\View\Helper\AbstractHelper;
12+
use Vnn\Keyper\Keyper;
1113

1214
class ValidationErrors extends AbstractHelper
1315
{
@@ -26,10 +28,7 @@ protected function getErrors($element)
2628
if (!isset($this->getView()->errors)) {
2729
return false;
2830
}
29-
$errors = $this->getView()->errors;
30-
if (isset($errors[$element])) {
31-
return $errors[$element];
32-
}
33-
return false;
31+
$errors = Keyper::create($this->getView()->errors);
32+
return $errors->get($element) ?: false;
3433
}
3534
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
<div class="page-header clearfix">
3+
<h2 class="pull-left"> Orders</h2>
4+
<a href="/orders/new" class="btn btn-success pull-right">
5+
Create Order </a>
6+
</div>
7+
<table class="table table-striped clearfix">
8+
<thead>
9+
<tr>
10+
<th>#</th>
11+
<th> Order Number</th>
12+
<th> Customer</th>
13+
<th> Description</th>
14+
<th class="text-right"> Total</th>
15+
</tr>
16+
</thead>
17+
<?php foreach ($this->orders as $order): ?>
18+
<tr>
19+
<td>
20+
<a href="/orders/view/<?= $this->escapeHtmlAttr($order->getId()) ?>">
21+
<?= $this->escapeHtml($order->getId()) ?></a></td>
22+
<td><?= $this->escapeHtml($order->getOrderNumber()) ?></td>
23+
<td>
24+
<a href="/customers/edit/<?= $this->escapeHtmlAttr($order->getCustomer()->getId()) ?>"> <?= $this->escapeHtml($order->getCustomer()->getName()) ?></a>
25+
</td>
26+
<td><?= $this->escapeHtml($order->getDescription()) ?></td>
27+
<td class="text-right">
28+
$ <?= number_format($order->getTotal(), 2) ?> </td>
29+
</tr>
30+
<?php endforeach; ?>
31+
</table>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<div class="page-header clearfix">
2+
<h2>Create Order</h2>
3+
</div>
4+
<form role="form" action="" method="post">
5+
<div class="form-group">
6+
<label for="customer_id">Customer:</label>
7+
<select class="form-control" name="customer[id]" id="customer_id">
8+
<option value=""></option>
9+
<?php foreach ($this->customers as $customer): ?>
10+
<option value="<?= $this->escapeHtmlAttr($customer->getId()) ?>"<?=
11+
!is_null($this->order->getCustomer()) &&
12+
$this->order->getCustomer()->getId() == $customer->getId() ?
13+
' selected="selected"' : '' ?>>
14+
<?= $this->escapeHtml($customer->getName()) ?>
15+
</option>
16+
<?php endforeach; ?>
17+
</select>
18+
<?= $this->validationErrors('customer.id') ?>
19+
</div>
20+
<div class="form-group">
21+
<label for="orderNumber">Order Number:</label>
22+
<input type="text" class="form-control" name="orderNumber"
23+
id="order_number" placeholder="Enter Order Number"
24+
value="<?= $this->escapeHtmlAttr($this->order->getOrderNumber()) ?>"> <?= $this->validationErrors('orderNumber') ?>
25+
</div>
26+
<div class="form-group">
27+
<label for="description">Description:</label>
28+
<input type="text" class="form-control" name="description"
29+
id="description" placeholder="Enter Description"
30+
value="<?= $this->escapeHtmlAttr($this->order->getDescription()) ?>"> <?= $this->validationErrors('description') ?>
31+
</div>
32+
<div class="form-group">
33+
<label for="total">Total:</label>
34+
<input type="text" class="form-control" name="total"
35+
id="total" placeholder="Enter Total"
36+
value="<?= $this->escapeHtmlAttr($this->order->getTotal()) ?>"> <?= $this->validationErrors('total') ?>
37+
</div>
38+
<button type="submit" class="btn btn-primary">Save</button>
39+
</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div class="page-header clearfix">
2+
<h2>Order #<?= $this->escapeHtml($this->order->getOrderNumber()) ?></h2></div>
3+
<table class="table table-striped">
4+
<thead>
5+
<tr>
6+
<th colspan="2">Order Details</th>
7+
</tr>
8+
</thead>
9+
<tr>
10+
<th>Customer:</th>
11+
<td>
12+
<a href="/customers/edit/<?= $this->escapeHtmlAttr($this->order->getCustomer()->getId()) ?>"> <?= $this->escapeHtml($this->order->getCustomer()->getName()) ?></a>
13+
</td>
14+
</tr>
15+
<tr>
16+
<th>Description:</th>
17+
<td><?= $this->escapeHtml($this->order->getDescription()) ?></td>
18+
</tr>
19+
<tr>
20+
<th>Total:</th>
21+
<td>$ <?= number_format($this->order->getTotal(), 2) ?></td>
22+
</tr>
23+
</table>

public/.htaccess

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ RewriteCond %{REQUEST_FILENAME} -s [OR]
55
RewriteCond %{REQUEST_FILENAME} -l [OR]
66
RewriteCond %{REQUEST_FILENAME} -d
77
RewriteRule ^.*$ - [NC,L]
8-
# The following rewrites all other queries to index.php. The
8+
# The following rewrites all other queries to index.phtml. The
99
# condition ensures that if you are using Apache aliases to do
1010
# mass virtual hosting, the base path will be prepended to
11-
# allow proper resolution of the index.php file; it will work
11+
# allow proper resolution of the index.phtml file; it will work
1212
# in non-aliased environments as well, providing a safe, one-size
1313
# fits all solution.
1414
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$

0 commit comments

Comments
 (0)