1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: /**
15: * Default object class.
16: *
17: * @category Jyxo
18: * @package Jyxo_Spl
19: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
20: * @license https://github.com/jyxo/php/blob/master/license.txt
21: * @author Jaroslav HanslĂk
22: */
23: class Jyxo_Spl_Object implements Jyxo_Spl_ArrayCopy
24: {
25: /**
26: * Returns instance class name.
27: *
28: * @return string
29: */
30: public final function getClass()
31: {
32: return get_class($this);
33: }
34:
35: /**
36: * Returns a property value if it has a getter defined.
37: *
38: * @param string $name Property name
39: * @return mixed
40: */
41: public function &__get($name)
42: {
43: $class = get_class($this);
44: $name = ucfirst($name);
45:
46: // Return null if no getter is found
47: $value = null;
48:
49: // Tests for possible getters
50: static $types = array('get', 'is');
51: foreach ($types as $type) {
52: $getter = $type . $name;
53: if (self::hasMethod($class, $getter)) {
54: // It's necessary to save the value to a variable first because of using &
55: $value = $this->$getter();
56: break;
57: }
58: }
59:
60: return $value;
61: }
62:
63: /**
64: * Sets property value if it has a setter defined.
65: *
66: * @param string $name Propety name
67: * @param mixed $value Property value
68: */
69: public function __set($name, $value)
70: {
71: $setter = 'set' . ucfirst($name);
72: if (self::hasMethod(get_class($this), $setter)) {
73: $this->$setter($value);
74: }
75: }
76:
77: /**
78: * Returns if property exists. Property exists if it has defined getter.
79: *
80: * @param string $name
81: * @return boolean
82: */
83: public function __isset($name)
84: {
85: $class = get_class($this);
86: $name = ucfirst($name);
87:
88: // Tests for possible getters
89: static $types = array('get', 'is');
90: foreach ($types as $type) {
91: $getter = $type . $name;
92: if (self::hasMethod($class, $getter)) {
93: return true;
94: }
95: }
96:
97: return false;
98: }
99:
100: /**
101: * Returns if a class has the given method defined.
102: *
103: * @param string $class Class name
104: * @param string $method Method name
105: * @return boolean
106: */
107: private static function hasMethod($class, $method)
108: {
109: static $cache;
110: if (!isset($cache[$class])) {
111: $cache[$class] = array_flip(get_class_methods($class));
112: }
113: return isset($cache[$class][$method]);
114: }
115:
116: /**
117: * Converts an object to an array
118: *
119: * @return array
120: */
121: public function toArray()
122: {
123: $values = array();
124: foreach ((array) $this as $key => $value) {
125: // Private and protected properties have ugly array key prefixes which we remove
126: $key = preg_replace('~^.+\0~', '', $key);
127: $values[$key] = $value;
128: }
129: return $values;
130: }
131:
132: }
133: