<?php
/**
*
*/
/**
* Midori_Object is the core base class for all of the objects
* in the midori library and hold the basic methods for creating
* objects with type information in php.
*
* <p>Midori_Object is design to help create plain old php objects that
* utilize {@link http://us.php.net/oop5.magic magic methods} for properties
* which means pretty much all properties within the library are public.</p>
*
* <pre class="brush: php">
*
* /**
* *
* *
* * @property string $type
* *\/
* class Car extends Midori_Object
* {
*
* /**
* * @ignore
* *\/
* protected function getType()
* {
* return $this->get("type");
* }
*
* /**
* * @ignore
* *\/
* protected function setType($value)
* {
* $this->set("type", $value);
* }
* }
* </pre>
*
*
* @author Michael Herndon
* @package Midori
*
*/
class Midori_Object
{
private $fields = array();
/**
* static constructor for chaining purposes.
*
* <code>
* $obj = Midori_String::ctr("i am a woman")
* ->replace("woman", "man");
* echo $obj; //i am a man
* </code>
*
* @returns Midori_Object
*/
public static function ctr()
{
return new Midori_Object();
}
/**
* generic get method that pulls a property from the private
* fields array which is used to hold values for cloning purposes
* and help with api changes by having properties with gets/sets
*
* @param string $property
* @return mixed
*/
protected function get($property)
{
return isset($this->fields[$property]) ? $this->fields[$property] : null;
}
/**
* generic set method that sets a value in the private fields array
* which is used to hold properties for cloning purposes and help
* with api changes of having properties
*
* @param $property
* @param $value
* @return unknown_type
*/
protected function set($property, $value)
{
$this->fields[$property] = $value;
}
/**
* gets the name the of the current class.
*
*
* @see get_class()
* @param boolean $returnPhpString
* @return Midori_String|string
*/
public function getClassName($returnPhpString = false)
{
$name = get_class($this);
return $returnPhpString ? $name : new Midori_String($name);
}
/**
* gets the reflected type information about the class.
*
* @see Reflection
* @return ReflectionObject
*/
public function getReflectedType()
{
return new ReflectionObject($this);
}
/**
* determines if the current object is equal to the
* object being passed into this method.
*
* @param mixed $obj
* @return boolean
*/
public function equals($obj)
{
return ($obj == ($this));
}
/**
* coverts the object into a string value, by the object
* will generally return the name of the object.
*
* @param boolean $returnPhpString
* @return Midori_String|string
*/
public function toString($returnPhpString = false)
{
return $this->getClassName($returnPhpString);
}
/**
* magic method __get, looks for getters inside the class.
*
* @param string $property
* @return mixed
*/
public function __get($property)
{
$get = "get".$property;
return $this->$get();
}
/**
* magic method __set, looks for the setter insder the class
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value)
{
$set = "set".$property;
$this->$set($value);
}
/**
* magic method __clone clones the current object and returns
* the cloned object.
*
* @return self
*/
public function __clone()
{
$class = $this->getClassName(true);
$obj->fields = $this->fields;
return $obj;
}
/**
* magic method __toString returns the string value of the object
* and primary used for echo/print/string concatination statments.
*
* @return string
*/
public function __toString()
{
return $this->getClassName(true);
}
}