Skip to content

Commit 867e299

Browse files
author
Januar Siregar
committed
fixing field using schema and add monggodbconnection for php 7 and new schema for php7
1 parent 3010bfd commit 867e299

14 files changed

+537
-5
lines changed

docs/TODO.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
- Cursor WILL HAVE (bulk) remove method.
1+
- Cursor WILL HAVE (bulk) remove method.
2+
- Field will have custom attributes and class.

src/Norm/Connection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function marshall($object)
199199
*
200200
* @return Norm\Cursor
201201
*/
202-
abstract public function query($collection, array $criteria = null);
202+
abstract public function query($collection, array $criteria = array());
203203

204204
/**
205205
* Persist specified document with current connection
+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php namespace Norm\Connection;
2+
3+
4+
use DateTime;
5+
use Exception;
6+
use Norm\Model;
7+
use MongoDB\Driver\Manager as MongoClient;
8+
use MongoDB\BSON\UTCDateTime as MongoDate;
9+
use MongoDB\BSON\ObjectId as MongoId;
10+
use Norm\Connection;
11+
use Norm\Collection;
12+
use Norm\Type\NObject;
13+
use Norm\Type\NDateTime as NormDateTime;
14+
use Norm\Type\NormArray;
15+
use Norm\Cursor\MongoCursor;
16+
17+
/**
18+
* Mongo Connection.
19+
*
20+
* @author Ganesha <reekoheek@gmail.com>
21+
* @copyright 2013 PT Sagara Xinix Solusitama
22+
* @link http://xinix.co.id/products/norm Norm
23+
* @license https://raw.github.com/xinix-technology/norm/master/LICENSE
24+
*/
25+
class MongoDBConnection extends Connection
26+
{
27+
/**
28+
* MongoDB client object
29+
*
30+
* @var \MongoClient
31+
*/
32+
protected $client;
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function __construct($options)
38+
{
39+
parent::__construct($options);
40+
41+
$defaultOptions = array(
42+
'hostname' => '127.0.0.1',
43+
'port' => '27017',
44+
);
45+
46+
$this->options = $options + $defaultOptions;
47+
48+
if (isset($this->options['connectionString'])) {
49+
$connectionString = $this->options['connectionString'];
50+
} else {
51+
$hostname = $this->options['hostname'];
52+
$port = $this->options['port'];
53+
54+
if (isset($this->options['database'])) {
55+
$database = $this->options['database'];
56+
} else {
57+
throw new Exception('[Norm/MongoConnection] Missing database name, check your configuration!');
58+
}
59+
60+
$prefix = '';
61+
62+
if (isset($this->options['username'])) {
63+
$prefix = $this->options['username'].':'.$this->options['password'].'@';
64+
}
65+
66+
$connectionString = "mongodb://$prefix$hostname:$port/$database";
67+
}
68+
69+
$this->client = new MongoClient($connectionString);
70+
$this->raw = $this->client->$database;
71+
}
72+
73+
/**
74+
* {@inheritDoc}
75+
*/
76+
public function query($collection, array $criteria = array())
77+
{
78+
return new MongoCursor($this->factory($collection), $criteria);
79+
}
80+
81+
/**
82+
* {@inheritDoc}
83+
*/
84+
public function persist($collection, array $document)
85+
{
86+
if ($collection instanceof Collection) {
87+
$collection = $collection->getName();
88+
}
89+
90+
$marshalledDocument = $this->marshall($document);
91+
92+
$result = false;
93+
94+
if (isset($document['$id'])) {
95+
$criteria = array(
96+
'_id' => new MongoId($document['$id']),
97+
);
98+
99+
$marshalledDocument = $this->raw->$collection->findAndModify(
100+
$criteria,
101+
array('$set' => $marshalledDocument),
102+
null,
103+
array('new' => true)
104+
);
105+
} else {
106+
$retval = $this->raw->$collection->insert($marshalledDocument);
107+
108+
if (!$retval['ok']) {
109+
throw new Exception($retval['errmsg']);
110+
}
111+
}
112+
113+
return $this->unmarshall($marshalledDocument);
114+
}
115+
116+
/**
117+
* {@inheritDoc}
118+
*/
119+
public function remove($collection, $criteria = null)
120+
{
121+
if ($collection instanceof Collection) {
122+
$collection = $collection->getName();
123+
}
124+
125+
if (func_num_args() === 1) {
126+
$result = $this->raw->$collection->remove();
127+
} else {
128+
if ($criteria instanceof Model) {
129+
$criteria = $criteria->getId();
130+
}
131+
132+
if (is_string($criteria)) {
133+
$criteria = array(
134+
'_id' => new MongoId($criteria),
135+
);
136+
} elseif (! is_array($criteria)) {
137+
throw new Exception('[Norm/Connection] Cannot remove with specified criteria. Criteria must be array, string, or model');
138+
}
139+
140+
$result = $this->raw->$collection->remove($criteria);
141+
}
142+
143+
if ((int) $result['ok'] !== 1) {
144+
throw new Exception($result['errmsg']);
145+
}
146+
}
147+
148+
/**
149+
* Get MongoDB client
150+
*
151+
* @return MongoClient MongoDB client
152+
*/
153+
public function getClient()
154+
{
155+
return $this->client;
156+
}
157+
158+
/**
159+
* {@inheritDoc}
160+
*/
161+
public function unmarshall($object)
162+
{
163+
if (isset($object['_id'])) {
164+
$object['$id'] = (string) $object['_id'];
165+
unset($object['_id']);
166+
}
167+
168+
foreach ($object as $key => &$value) {
169+
if ($value instanceof MongoDate) {
170+
$value = new DateTime('@'.$value->sec);
171+
} elseif ($value instanceof MongoId) {
172+
$value = (string) $value;
173+
}
174+
175+
if ($key[0] === '_') {
176+
unset($object[$key]);
177+
$key[0] = '$';
178+
$object[$key] = $value;
179+
}
180+
}
181+
182+
return $object;
183+
}
184+
185+
/**
186+
* {@inheritDoc}
187+
*/
188+
public function marshall($object)
189+
{
190+
if ($object instanceof NormDateTime) {
191+
return new MongoDate($object->getTimestamp());
192+
} elseif ($object instanceof NormArray) {
193+
return $object->toArray();
194+
} elseif ($object instanceof NObject) {
195+
return $object->toObject();
196+
} else {
197+
return parent::marshall($object);
198+
}
199+
}
200+
}

src/Norm/Schema/Field.php

+39-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,45 @@ public function formatInput($value, $entry = null)
190190
if (!empty($value)) {
191191
$value = htmlentities($value);
192192
}
193-
return '<input type="text" name="'.$this['name'].'" value="'.$value.'" placeholder="'.l($this['label']).
194-
'" autocomplete="off" />';
193+
194+
return $this->render('_schema/field/input', array(
195+
'self' => $this,
196+
'value' => $value,
197+
'entry' => $entry
198+
));
199+
200+
}
201+
202+
public function inputAttributes(){
203+
$attributes = array();
204+
205+
if($this['input_attributes']){
206+
$attributes = $this['input_attributes'];
207+
}
208+
209+
return implode(" ",$attributes);
210+
211+
212+
}
213+
214+
public function inputClass(){
215+
$class = array();
216+
217+
$app = \Bono\App::getInstance();
218+
219+
if(!empty($app->config('bono.theme')['htmlclass'])){
220+
$class = $app->config('bono.theme')['htmlclass'];
221+
}
222+
223+
224+
225+
if(!empty($this['class'])){
226+
$class = array_merge($class,$this['class']);
227+
}
228+
229+
230+
231+
return implode(" ",$class);
195232
}
196233

197234
public function render($template, array $context = array())

src/Norm/Schema/NormDate.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Norm\Schema;
4+
5+
class NormDate extends Field
6+
{
7+
8+
9+
public function prepare($value)
10+
{
11+
if (empty($value)) {
12+
return null;
13+
} elseif ($value instanceof \Norm\Type\NDate) {
14+
return $value;
15+
} elseif ($value instanceof \DateTime) {
16+
$t = $value->format('c');
17+
} elseif (is_string($value)) {
18+
$t = date('c', strtotime($value));
19+
} else {
20+
$t = date('c', (int) $value);
21+
}
22+
return new \Norm\Type\NDate($t);
23+
}
24+
25+
public function formatInput($value, $entry = null)
26+
{
27+
$value = $this->prepare($value);
28+
if ($value) {
29+
$value->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
30+
}
31+
32+
return '<input type="date" name="'.$this['name'].'" value="'.($value ? $value->format('Y-m-d') : '').
33+
'" placeholder="'.$this['label'].
34+
'" autocomplete="off" />';
35+
}
36+
37+
public function formatPlain($value, $entry = null)
38+
{
39+
$value = $this->prepare($value);
40+
if ($value) {
41+
$value->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
42+
}
43+
44+
return ($value ? $value->format('Y-m-d') : null);
45+
}
46+
47+
public function formatReadonly($value, $entry = null)
48+
{
49+
$value = $this->prepare($value);
50+
if ($value) {
51+
$value->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
52+
}
53+
54+
return '<span class="field">'.($value ? $value->format('Y-m-d') : '&nbsp;').'</span>';
55+
}
56+
57+
// DEPRECATED replaced by Field::render
58+
// public function cell($value, $entry = null)
59+
// {
60+
// if ($this->has('cellFormat') && $format = $this['cellFormat']) {
61+
// return $format($value, $entry);
62+
// }
63+
// return $value->format('Y-m-d');
64+
// }
65+
}

src/Norm/Schema/NormDateTime.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Norm\Schema;
4+
5+
class NormDateTime extends Field
6+
{
7+
public function prepare($value)
8+
{
9+
if (empty($value)) {
10+
return null;
11+
} elseif ($value instanceof \Norm\Type\NDateTime) {
12+
return $value;
13+
} elseif ($value instanceof \DateTime) {
14+
$t = $value->format('c');
15+
} elseif (is_string($value)) {
16+
$t = date('c', strtotime($value));
17+
} else {
18+
$t = date('c', (int) $value);
19+
}
20+
return new \Norm\Type\DateTime($t);
21+
}
22+
23+
public function formatInput($value, $entry = null)
24+
{
25+
$value = $this->prepare($value);
26+
if ($value) {
27+
$value->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
28+
}
29+
30+
return '<input type="datetime-local" name="'.$this['name'].'" value="'.
31+
($value ? $value->format("Y-m-d\TH:i") : '').'" placeholder="'.
32+
$this['label'].'" autocomplete="off" />';
33+
}
34+
35+
public function formatPlain($value, $entry = null){
36+
37+
}
38+
39+
40+
public function formatReadonly($value, $entry = null)
41+
{
42+
$value = $this->prepare($value);
43+
if ($value) {
44+
$value->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
45+
}
46+
47+
return '<span class="field">'.($value ? $value->format('c') : '&nbsp;').'</span>';
48+
}
49+
50+
51+
}

src/Norm/Schema/NormFloat.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Norm\Schema;
4+
5+
class NormFloat extends Field
6+
{
7+
public function prepare($value)
8+
{
9+
return (double) $value;
10+
}
11+
}

0 commit comments

Comments
 (0)