Skip to content
This repository
tree: 6c743dd006
Fetching contributors…

Cannot retrieve contributors at this time

file 348 lines (320 sloc) 9.102 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
<?php
/**
* @package Morph
* @author Jonathan Moss <xirisr@gmail.com>
* @copyright Jonathan Moss 2009
*/
namespace morph;
/**
* A base class for creating model objects for storing in MongoDB.
*
* @package Morph
*/
class Object
{
    /**
* The name of the collection this object is stored within
* @var string
*/
    protected $collection;

    /**
* The unique id for this object
* @var string
*/
    protected $id;

    /**
* The current state of this object
*
* one of Morph_Enum::STATE_*
* @var string
*/
    protected $state;

    /**
* The data associated with this object
* @var \morph\PropertySet
*/
    protected $propertySet;

    /**
* The collections of validators for propertys
* @var array
*/
    protected $validators;

    /**
* @param string $id If supplied this will be the id used to reference this object
* @return Morph_Object
*/
    public function __construct($id = null)
    {
        $this->state = Enum::STATE_NEW;
        $this->id = $id;
        $this->propertySet = new PropertySet();
        $this->validators = array();
    }

    // *********************** //
    // GETTER/SETTER FUNCTIONS //
    // *********************** //

    /**
* Returns the name of the collection this object should be stored in
*
* @param string $collection
* @return string
*/
    public function collection($collection = null)
    {
        if (!\is_null($collection)) {
            $this->collection = $collection;
        }elseif (\is_null($this->collection)) {
            //defaults collection name to class name
            $this->collection = Utils::collectionName($this);
        }
        return $this->collection;
    }

    /**
* Returns the Id of this object
* @return string
*/
    public function id()
    {
        return $this->id;
    }

    /**
* Returns the state of this object
*
* Will be one of Morph_Enum::STATE_*
*
* @return string
*/
    public function state()
    {
        return $this->state;
    }

    /**
* Adds a new property to this object
*
* @param Morph_Property_Generic $property
* @param string $alias
* @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);
        }
        return $this;
    }

    /**
* Sets the property data for this object
*
* @param $data
* @param $state
* @return Morph_Object
*/
    public function __setData(array $data, $state = Enum::STATE_DIRTY)
    {
        if (\array_key_exists('_id', $data)) {
            $this->id = $data['_id'];
            unset($data['_id']);
        }
        if (\array_key_exists('_ns', $data)) {
            unset($data['_ns']);
        }
        foreach ($data as $propertyName => $value) {
            $this->propertySet->__setRawPropertyValue($propertyName, $value);
        }
        $this->state = $state;
        return $this;
    }

    /**
* Gets the property data for this object
*
* @return array
*/
    public function __getData()
    {
        $data = array();
        if (!is_null($this->id)) {
            $data['_id'] = $this->id;
        }
        $data['_ns'] = \get_class($this);
        foreach($this->propertySet as $property) {
         $storageName = $this->propertySet->getStorageName($property->getName());
            $data[$storageName] = $property->__getRawValue();
        }
        return $data;
    }
    
    /**
* @return \morph\PropertSet
*/
    public function __getPropertySet()
    {
     return $this->propertySet;
    }

    // ********************** //
    // MAGIC ACCESS FUNCTIONS //
    // ********************** //

    /**
*
* @param $propertyName
* @return mixed
*/
    public function __get($propertyName)
    {
        $value = null;
        if (\array_key_exists($propertyName, $this->propertySet)) {
            $value = $this->propertySet[$propertyName]->getValue();
        }else{
            \trigger_error("The property $propertyName was not found in object of class " . \get_class($this), E_USER_WARNING);
        }
        return $value;
    }

    /**
*
* @param $propertyName
* @param $propertyValue
* @return Morph_Object
*/
    public function __set($propertyName, $propertyValue)
    {
        if (\array_key_exists($propertyName, $this->propertySet)) {
            $this->propertySet[$propertyName]->setValue($propertyValue);
            if ($this->state == Enum::STATE_CLEAN) {
                $this->state = Enum::STATE_DIRTY;
            }
        }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;
    }

    // ********************* //
    // PERSISTANCE FUNCTIONS //
    // ********************* //

    /**
* Saves this object
*
* @param array $options Support the same options as MongoCollection::save()
* @return Morph_Object
*/
    public function save(array $options = array())
    {
        return Storage::instance()->save($this, $options);
    }

    /**
* Attempts to load the current object with data from the document id specified
*
* @param mixed $id
* @return Morph_Object
*/
    public function loadById($id)
    {
        return Storage::instance()->fetchById($this, $id);
    }

    /**
* Fetch multiple objects by their ids
*
* @param array $ids
* @return Morph_Iterator
*/
    public function findByIds(array $ids)
    {
        return Storage::instance()->fetchByIds($this, $ids);
    }

    /**
* Find objects by query
*
* @param Morph_IQuery $query
* @return Morph_Iterator
*/
    public function findByQuery(IQuery $query)
    {
        return Storage::instance()->findByQuery($this, $query);
    }

    /**
* Finds one object by query
*
* @param Morph_Query $query
* @return Morph_Object
*/
    public function findOneByQuery(IQuery $query)
    {
        return Storage::instance()->findOneByQuery($this, $query);
    }

    /**
* Deletes this object from the database
*
* @return boolean
*/
    public function delete()
    {
     return Storage::instance()->delete($this);
    }

    // ***************** //
    // UTILITY FUNCTIONS //
    // ***************** //

    /**
* Converts this object to a string
*
* Useful for debugging output but not much else
*
* @return string
*/
    public function __toString()
    {
        $data = array(
            'Id' => $this->id(),
            'State' => $this->state()
        );
        foreach ($this->propertySet as $name => $property) {
            $data[$name] = (string)$property;
        }
        return \json_encode($data);
    }

    /**
* Returns the content of this object as an array suitable for merging with a template
*
* @param $depth The maximum depth to traverse the tree
* @return array
*/
    public function __toArray($depth = 1)
    {
        $content = array();
        if (!empty($this->id)) {
            $content['_id'] = $this->id;
        }
        --$depth;
        foreach ($this->propertySet as $property) {
            $value = $property->getValue();

            switch (true) {
                case ($property instanceof \morph\property\ComposeOne):
                    $content[$value->getName()] = $value->__toArray($depth);
                    break;
                case ($property instanceof morph\Property\ComposeMany):
                    $valueContents = array();
                    foreach ($value as $object) {
                        $valueContents[] = $object->__toArray($depth);
                    }
                    $content[$property->getName()] = $valueContents;
                    break;
                case ($value instanceof Object):
                    if ($depth > 0) {
                        $content[$value->getName()] = $value->__toArray($depth);
                    }
                    break;
                case ($value instanceof Collection):
                    if ($depth > 0) {
                        $valueContents = array();
                        foreach ($value as $object) {
                            $valueContents[] = $object->__toArray($depth);
                        }
                        $content[$property->getName()] = $valueContents;
                    }
                    break;
                default:
                    $content[$property->getName()] = $value;
                    break;
            }

        }
        return $content;
    }
}
Something went wrong with that request. Please try again.