Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Object::__isset (test for existence of property in object's pro…

…pertySet)
  • Loading branch information...
commit eecbcc9648a29ddcbda94038763ef5b6886799ed 1 parent 18cf4e2
Christopher Martinez authored
Showing with 41 additions and 24 deletions.
  1. +41 −24 src/morph/Object.php
View
65 src/morph/Object.php
@@ -37,7 +37,7 @@ class Object
protected $validators;
/**
- * @param string $id If supplied this will be the id used to reference this object
+ * @param string $id If supplied this will be the id used to reference this object
*/
public function __construct($id = null)
{
@@ -53,17 +53,18 @@ public function __construct($id = null)
/**
* Returns the name of the collection this object should be stored in
*
- * @param string $collection
+ * @param string $collection
* @return string
*/
public function collection($collection = null)
{
if (!\is_null($collection)) {
$this->collection = $collection;
- }elseif (\is_null($this->collection)) {
+ } elseif (\is_null($this->collection)) {
//defaults collection name to class name
$this->collection = Utils::collectionName($this);
}
+
return $this->collection;
}
@@ -92,23 +93,24 @@ public function state()
* Adds a new property to this object
*
* @param \morph\property\Generic $property
- * @param string $alias (Default: null)
+ * @param string $alias (Default: null)
* @return \morph\Object
*/
protected function addProperty(\morph\property\Generic $property, $alias = null)
{
$this->propertySet[$property->getName()] = $property;
if (null !== $alias) {
- $this->propertySet->setStorageName($property->getName(), $alias);
+ $this->propertySet->setStorageName($property->getName(), $alias);
}
+
return $this;
}
/**
* Sets the property data for this object
*
- * @param array $data
- * @param string $state (Default: Enum::STATE_DIRTY)
+ * @param array $data
+ * @param string $state (Default: Enum::STATE_DIRTY)
* @return \morph\Object
*/
public function __setData(array $data, $state = Enum::STATE_DIRTY)
@@ -123,13 +125,14 @@ public function __setData(array $data, $state = Enum::STATE_DIRTY)
foreach ($data as $propertyName => $value) {
$this->propertySet->__setRawPropertyValue($propertyName, $value, $state);
}
+
return $this;
}
/**
* Gets the property data for this object
*
- * @param boolean $whereNew Whether or not to only get dirty/new properties.
+ * @param boolean $whereNew Whether or not to only get dirty/new properties.
* @return array
*/
public function __getData($whereNew = false)
@@ -158,12 +161,12 @@ public function __getData($whereNew = false)
/**
* Returns the PropertySet for this object
- *
+ *
* @return \morph\PropertySet
*/
public function __getPropertySet()
{
- return $this->propertySet;
+ return $this->propertySet;
}
// ********************** //
@@ -172,7 +175,7 @@ public function __getPropertySet()
/**
* Gets a property by name if it exists. Issues an E_USER_WARNING if it does not exist.
- *
+ *
* @param $propertyName
* @return mixed
*/
@@ -181,27 +184,29 @@ public function __get($propertyName)
$value = null;
if (\array_key_exists($propertyName, $this->propertySet)) {
$value = $this->propertySet[$propertyName]->getValue();
- }else{
+ } else {
\trigger_error("The property $propertyName was not found in object of class " . \get_class($this), E_USER_WARNING);
}
+
return $value;
}
/**
* Sets a property by name and value
- *
- * @param string $propertyName
- * @param string $propertyValue
+ *
+ * @param string $propertyName
+ * @param string $propertyValue
* @return \morph\Object
*/
public function __set($propertyName, $propertyValue)
{
if (\array_key_exists($propertyName, $this->propertySet)) {
$this->propertySet[$propertyName]->setValue($propertyValue);
- }else{
+ } else {
$this->addProperty(new \morph\property\Generic($propertyName, $propertyValue));
\trigger_error("The property $propertyName was not found in object of class " . \get_class($this) . ' but I have added it as a generic property type', E_USER_WARNING);
}
+
return $this;
}
@@ -213,6 +218,17 @@ public function __clone()
$this->id = null;
}
+ /**
+ * Checks for existence of property
+ *
+ * @param string $propertyName
+ * @return boolean
+ */
+ public function __isset($propertyName)
+ {
+ return \array_key_exists($propertyName, $this->propertySet);
+ }
+
// ********************* //
// PERSISTANCE FUNCTIONS //
// ********************* //
@@ -220,7 +236,7 @@ public function __clone()
/**
* Saves this object
*
- * @param array $options Support the same options as MongoCollection::save()
+ * @param array $options Support the same options as MongoCollection::save()
* @return \morph\Object
*/
public function save(array $options = array())
@@ -234,7 +250,7 @@ public function save(array $options = array())
* By default Morph sets the id to be an instance of MongoId(). When searching you need
* to ensure you do the same by wrapping your id string in a MongoId object
*
- * @param mixed $id
+ * @param mixed $id
* @return Morph_Object
*/
public function loadById($id)
@@ -248,7 +264,7 @@ public function loadById($id)
* By default Morph sets the id to be an instance of MongoId(). When searching you need
* to ensure you do the same by wrapping your id string in a MongoId object
*
- * @param array $ids
+ * @param array $ids
* @return Morph_Iterator
*/
public function findByIds(array $ids)
@@ -259,18 +275,18 @@ public function findByIds(array $ids)
/**
* Find objects by query
*
- * @param IQuery $query
+ * @param IQuery $query
* @return \morph\Iterator
*/
public function findByQuery(IQuery $query)
{
return Storage::instance()->findByQuery($this, $query);
}
-
+
/**
* Deletes objects by query
*
- * @param IQuery $query
+ * @param IQuery $query
* @param boolean $safe
* @return boolean
*/
@@ -307,7 +323,7 @@ public function fetchAll()
*/
public function delete()
{
- return Storage::instance()->delete($this);
+ return Storage::instance()->delete($this);
}
// ***************** //
@@ -332,7 +348,7 @@ public function __toString()
// iterate through all the properties this object has and print them out
foreach ($this->propertySet as $name => $property) {
- $data[$name] = (string)$property;
+ $data[$name] = (string) $property;
}
return \json_encode($data);
@@ -385,6 +401,7 @@ public function __toArray($depth = 1)
}
}
+
return $content;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.