1: <?php
2: /**
3: * Pinoco: makes existing static web site dynamic transparently.
4: * Copyright 2010-2012, Hisateru Tanaka <tanakahisateru@gmail.com>
5: *
6: * Licensed under The MIT License
7: * Redistributions of files must retain the above copyright notice.
8: *
9: * PHP Version 5
10: *
11: * @author Hisateru Tanaka <tanakahisateru@gmail.com>
12: * @copyright Copyright 2010-2012, Hisateru Tanaka <tanakahisateru@gmail.com>
13: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
14: * @package Pinoco
15: */
16:
17: /**
18: * Dynamic vars model base
19: * @package Pinoco
20: * @abstract
21: */
22: class Pinoco_DynamicVars extends Pinoco_Vars
23: {
24: /**
25: * Returns a value or default by name.
26: *
27: * @param string $name
28: * @param mixed $default
29: * @return mixed
30: * @see src/Pinoco/Pinoco_Vars#get($name)
31: */
32: public function get($name, $default=Pinoco_OptionalParam::UNSPECIFIED)
33: {
34: if (method_exists($this, 'get_' . $name)) {
35: return call_user_func(array($this, 'get_' . $name));
36: }
37: else {
38: if (Pinoco_OptionalParam::isSpecifiedBy($default)) {
39: return parent::get($name, $default);
40: }
41: else {
42: return parent::get($name);
43: }
44: }
45: }
46:
47: /**
48: * Checks if this object has certain property or not.
49: * If setLoose is set true then it returns true always.
50: *
51: * @param string $name
52: * @return bool
53: * @see src/Pinoco/Pinoco_Vars#has($name)
54: */
55: public function has($name)
56: {
57: return method_exists($this, 'get_' . $name) || parent::has($name);
58: }
59:
60: /**
61: * Returns all property names in this object.
62: *
63: * @return Pinoco_List
64: * @see src/Pinoco/Pinoco_Vars#keys()
65: */
66: public function keys()
67: {
68: $meths = get_class_methods($this);
69: $ks = array();
70: $m = array();
71: foreach ($meths as $meth) {
72: if (preg_match("/^get_(.*)$/", $meth, $m)) {
73: array_push($ks, $m[1]);
74: }
75: }
76: $ks = Pinoco_List::fromArray($ks);
77: $ks->concat(parent::keys());
78: return $ks;
79: }
80:
81: /**
82: * Property setter.
83: *
84: * @param string $name
85: * @param mixed $value
86: * @throws InvalidArgumentException
87: * @return void
88: * @see src/Pinoco/Pinoco_Vars#set($name, $value)
89: */
90: public function set($name, $value)
91: {
92: if (method_exists($this, 'set_' . $name)) {
93: call_user_func(array($this, 'set_' . $name), $value);
94: }
95: elseif (method_exists($this, 'get_' . $name)) {
96: throw new InvalidArgumentException("Cannot reassign to ". $name . ".");
97: }
98: else {
99: parent::set($name, $value);
100: }
101: }
102:
103: public function getIterator()
104: {
105: // to include reserved special vars
106: $arr = $this->toArray();
107: return new Pinoco_ArrayConvertiblesIterator($arr);
108: }
109: }
110:
111: