Skip to content
This repository

Small corerctions in the String property #29

Merged
merged 3 commits into from over 2 years ago

2 participants

Kanstantsin Kamkou Jonathan Moss
Kanstantsin Kamkou

Just small correction for the PHP 5.4

p.s. there is another issue with the JSON encoding of an object. See: 71f9046 (I forgot to cleanup my working copy, so, please review it too)

p.p.s 8fa0bd6

Jonathan Moss a-musing-moose merged commit abd1e05 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
1  .gitignore
@@ -7,3 +7,4 @@ doc/api
7 7 doc/test_report
8 8 doc/integration_report
9 9 doc/manual/build/*
  10 +/.idea/
67 src/morph/Object.php
@@ -37,8 +37,7 @@ class Object
37 37 protected $validators;
38 38
39 39 /**
40   - * @param string $id If supplied this will be the id used to reference this object
41   - * @return Morph_Object
  40 + * @param string $id If supplied this will be the id used to reference this object
42 41 */
43 42 public function __construct($id = null)
44 43 {
@@ -92,8 +91,8 @@ public function state()
92 91 /**
93 92 * Adds a new property to this object
94 93 *
95   - * @param Morph_Property_Generic $property
96   - * @param string $alias
  94 + * @param \morph\property\Generic $property
  95 + * @param string $alias (Default: null)
97 96 * @return \morph\Object
98 97 */
99 98 protected function addProperty(\morph\property\Generic $property, $alias = null)
@@ -108,9 +107,9 @@ protected function addProperty(\morph\property\Generic $property, $alias = null)
108 107 /**
109 108 * Sets the property data for this object
110 109 *
111   - * @param $data
112   - * @param $state
113   - * @return Morph_Object
  110 + * @param array $data
  111 + * @param string $state (Default: Enum::STATE_DIRTY)
  112 + * @return \morph\Object
114 113 */
115 114 public function __setData(array $data, $state = Enum::STATE_DIRTY)
116 115 {
@@ -145,7 +144,7 @@ public function __getData()
145 144 }
146 145 return $data;
147 146 }
148   -
  147 +
149 148 /**
150 149 * @return \morph\PropertSet
151 150 */
@@ -159,7 +158,6 @@ public function __getPropertySet()
159 158 // ********************** //
160 159
161 160 /**
162   - *
163 161 * @param $propertyName
164 162 * @return mixed
165 163 */
@@ -175,10 +173,9 @@ public function __get($propertyName)
175 173 }
176 174
177 175 /**
178   - *
179   - * @param $propertyName
180   - * @param $propertyValue
181   - * @return Morph_Object
  176 + * @param string $propertyName
  177 + * @param string $propertyValue
  178 + * @return \morph\Object
182 179 */
183 180 public function __set($propertyName, $propertyValue)
184 181 {
@@ -191,6 +188,14 @@ public function __set($propertyName, $propertyValue)
191 188 return $this;
192 189 }
193 190
  191 + /**
  192 + * Clones the current object
  193 + */
  194 + public function __clone()
  195 + {
  196 + $this->id = null;
  197 + }
  198 +
194 199 // ********************* //
195 200 // PERSISTANCE FUNCTIONS //
196 201 // ********************* //
@@ -198,8 +203,8 @@ public function __set($propertyName, $propertyValue)
198 203 /**
199 204 * Saves this object
200 205 *
201   - * @param array $options Support the same options as MongoCollection::save()
202   - * @return Morph_Object
  206 + * @param array $options Support the same options as MongoCollection::save()
  207 + * @return \morph\Object
203 208 */
204 209 public function save(array $options = array())
205 210 {
@@ -222,7 +227,7 @@ public function loadById($id)
222 227
223 228 /**
224 229 * Fetch multiple objects by their ids
225   - *
  230 + *
226 231 * By default Morph sets the id to be an instance of MongoId(). When searching you need
227 232 * to ensure you do the same by wrapping your id string in a MongoId object
228 233 *
@@ -233,12 +238,12 @@ public function findByIds(array $ids)
233 238 {
234 239 return Storage::instance()->fetchByIds($this, $ids);
235 240 }
236   -
  241 +
237 242 /**
238 243 * Find objects by query
239 244 *
240   - * @param Morph_IQuery $query
241   - * @return Morph_Iterator
  245 + * @param IQuery $query
  246 + * @return \morph\Iterator
242 247 */
243 248 public function findByQuery(IQuery $query)
244 249 {
@@ -248,8 +253,8 @@ public function findByQuery(IQuery $query)
248 253 /**
249 254 * Finds one object by query
250 255 *
251   - * @param Morph_Query $query
252   - * @return Morph_Object
  256 + * @param IQuery $query
  257 + * @return Object
253 258 */
254 259 public function findOneByQuery(IQuery $query)
255 260 {
@@ -257,8 +262,18 @@ public function findOneByQuery(IQuery $query)
257 262 }
258 263
259 264 /**
  265 + * Returns all entries for the current document
  266 + *
  267 + * @return \morph\Iterator
  268 + */
  269 + public function fetchAll()
  270 + {
  271 + return Storage::instance()->findByQuery($this);
  272 + }
  273 +
  274 + /**
260 275 * Deletes this object from the database
261   - *
  276 + *
262 277 * @return boolean
263 278 */
264 279 public function delete()
@@ -279,17 +294,17 @@ public function delete()
279 294 */
280 295 public function __toString()
281 296 {
282   - // create the array that we will be encoding and returning
283   - // also put inside the array the mongodb ID and the 'state'
  297 + // create the array that we will be encoding and returning
  298 + // also put inside the array the mongodb ID and the 'state'
284 299 $data = array(
285 300 'Id' => $this->id(),
286 301 'State' => $this->state()
287 302 );
288 303
289   - // iterate through all the properties this object has and print them out
  304 + // iterate through all the properties this object has and print them out
290 305 foreach ($this->propertySet as $name => $property) {
291 306 $data[$name] = (string)$property;
292   - }
  307 + }
293 308
294 309 return \json_encode($data);
295 310 }
43 src/morph/Storage.php
@@ -23,7 +23,7 @@ class Storage
23 23 * @var MongoDB
24 24 */
25 25 private $db;
26   -
  26 +
27 27 private $useSafe = false;
28 28
29 29 /**
@@ -69,10 +69,10 @@ private function __construct(\MongoDB $db)
69 69 {
70 70 $this->db = $db;
71 71 }
72   -
  72 +
73 73 /**
74 74 * If set to true then the 'safe' option for saves is used
75   - *
  75 + *
76 76 * @param boolean $useSafe
77 77 */
78 78 public function useSafe($useSafe)
@@ -94,7 +94,7 @@ public function getDatabase()
94 94 /**
95 95 * Retrieves the contents of the specified $id
96 96 * and assigns them into $object
97   - *
  97 + *
98 98 * By default Morph sets the id to be an instance of MongoId(). When searching you need
99 99 * to ensure you do the same by wrapping your id string in a MongoId object
100 100 *
@@ -111,7 +111,7 @@ public function fetchById(Object $object, $id)
111 111
112 112 /**
113 113 * Returns all objects with an _id in $ids
114   - *
  114 + *
115 115 * By default Morph sets the id to be an instance of MongoId(). When searching you need
116 116 * to ensure you do the same by wrapping your id string in a MongoId object
117 117 *
@@ -129,7 +129,7 @@ public function fetchByIds(Object $object, array $ids)
129 129 /**
130 130 * Retrieves the contents of the specified $dbRef
131 131 * and assigns them into $object
132   - *
  132 + *
133 133 * @param Morph_Object $object
134 134 * @param array $dbRef
135 135 * @return Morph_Object
@@ -161,7 +161,7 @@ public function save(Object $object)
161 161 * Inserts a new object into the database
162 162 *
163 163 * @param \morph\Object $object
164   - * @param array $options
  164 + * @param array $options
165 165 * @return \morph\Object
166 166 */
167 167 private function insert(Object $object, array $options = array())
@@ -175,9 +175,9 @@ private function insert(Object $object, array $options = array())
175 175 );
176 176 $data = \array_merge($id, $data);
177 177 }
178   -
  178 +
179 179 $options = array_merge(array('safe'=>$this->useSafe), $options);
180   -
  180 +
181 181 $savedOk = $this->db->selectCollection($object->collection())->save($data, $options);
182 182 if($savedOk){
183 183 $object->__setData($data, Enum::STATE_CLEAN);
@@ -211,18 +211,18 @@ public function delete(Object $object)
211 211 /**
212 212 * Runs query against the database
213 213 *
214   - * The results come packages up in a Morph_Iterator object
  214 + * The results come packages up in a \morph\Iterator object
215 215 *
216   - * @param \morph\Object $object Required to determine the correct collection query against
217   - * @param Morph_IQuery $query
218   - * @return Morph_Iterator
  216 + * @param Object $object Required to determine the correct collection query against
  217 + * @param IQuery $query
  218 + * @return \morph\Iterator
219 219 */
220 220 public function findByQuery(Object $object, IQuery $query = null)
221 221 {
222 222 $class = get_class($object);
223 223
224 224 $query = (is_null($query)) ? new Query() : $query;
225   -
  225 +
226 226 $rawQuery = $this->getRawQuery($object, $query);
227 227 $cursor = $this->db->selectCollection($object->collection())->find($rawQuery);
228 228
@@ -263,10 +263,10 @@ public function findOneByQuery(Object $object, IQuery $query = null)
263 263 $data = $this->db->selectCollection($object->collection())->findOne($rawQuery);
264 264 return $this->setData($object, $data);
265 265 }
266   -
  266 +
267 267 /**
268 268 * Ensures that aliased properties are correctly converted in query
269   - *
  269 + *
270 270 * @param Object $object
271 271 * @param IQuery $query
272 272 */
@@ -314,9 +314,12 @@ public function saveFile($filePath, $oldReference = null)
314 314 }
315 315
316 316 /**
317   - * @param Morph_Object $object
318   - * @param array $data
319   - * @return Morph_Object
  317 + * Sets data to the morph object
  318 + *
  319 + * @param Object $object
  320 + * @param mixed $data
  321 + * @return Object
  322 + * @throw ObjectNotFound if data is empty
320 323 */
321 324 private function setData(Object $object, $data)
322 325 {
@@ -326,4 +329,4 @@ private function setData(Object $object, $data)
326 329 $object->__setData($data, Enum::STATE_CLEAN);
327 330 return $object;
328 331 }
329   -}
  332 +}
28 src/morph/property/String.php
@@ -22,11 +22,12 @@ class String extends Generic
22 22 protected $maximumLength;
23 23
24 24 /**
25   - * @param string $Name
26   - * @param string $Default
27   - * @param integer $MaximumLength
  25 + * @param string $name
  26 + * @param string $default
  27 + * @param integer $maximumLength
28 28 */
29   - public function __construct($name, $default = null, $maximumLength = null){
  29 + public function __construct($name, $default = null, $maximumLength = null)
  30 + {
30 31 parent::__construct($name, $default);
31 32 $this->maximumLength = (is_null($maximumLength)) ? null : (int)$maximumLength;
32 33 }
@@ -34,11 +35,13 @@ public function __construct($name, $default = null, $maximumLength = null){
34 35 /**
35 36 * Sets the value of this attribute
36 37 *
37   - * @param integer $Value
  38 + * @param mixed $value
  39 + * @return void
38 40 */
39   - public function setValue($value){
  41 + public function setValue($value)
  42 + {
40 43 if (null === $value) {
41   - return parent::setValue($value);
  44 + return parent::setValue(null);
42 45 }
43 46
44 47 $cleanValue = (string)$value;
@@ -48,6 +51,7 @@ public function setValue($value){
48 51 'substr', array($cleanValue, 0, $this->maximumLength)
49 52 );
50 53 }
  54 +
51 55 parent::setValue($cleanValue);
52 56 }
53 57
@@ -56,7 +60,8 @@ public function setValue($value){
56 60 *
57 61 * @return string
58 62 */
59   - public function getValue(){
  63 + public function getValue()
  64 + {
60 65 return (null === parent::getValue()) ? null : (string)parent::getValue();
61 66 }
62 67
@@ -69,10 +74,17 @@ public function getValue(){
69 74 */
70 75 protected function _getUnicodeValue($fncName, $value)
71 76 {
  77 + // PHP 5.4 has native support of unicode chars
  78 + if (version_compare(PHP_VERSION, '5.4', '>=')) {
  79 + return call_user_func_array($fncName, (array)$value);
  80 + }
  81 +
  82 + // iconv or mbstring
72 83 $prefix = 'iconv';
73 84 if (extension_loaded('mbstring')) {
74 85 $prefix = 'mb';
75 86 }
  87 +
76 88 return call_user_func_array($prefix . '_' . $fncName, (array)$value);
77 89 }
78 90 }

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.