1: <?php
2:
3: /**
4: * Copyright 2015, Eduardo Trujillo
5: *
6: * For the full copyright and license information, please view the LICENSE
7: * file that was distributed with this source code.
8: *
9: * This file is part of the Nucleus package
10: */
11:
12: namespace Chromabits\Nucleus\Foundation;
13:
14: use Chromabits\Nucleus\Exceptions\LackOfCoffeeException;
15: use DomainException;
16: use Iterator;
17:
18: /**
19: * Class BaseObject.
20: *
21: * * A base object class based of Libphutil's Phobject class.
22: *
23: * - (NEW) Prevent the constructor of an object being called with additional
24: * parameters by accident.
25: * - Protect against the use of undefined properties.
26: * - Prevent accidental object iteration on objects.
27: *
28: * See: <libphutil>/src/object/Phobject.php
29: *
30: * @author Eduardo Trujillo <ed@chromabits.com>
31: * @package Chromabits\Nucleus\Foundation
32: */
33: class BaseObject implements Iterator
34: {
35: /**
36: * Construct an instance of a BaseObject.
37: *
38: * @throws LackOfCoffeeException
39: */
40: public function __construct()
41: {
42: $argCount = func_num_args();
43:
44: if ($argCount > 0) {
45: throw new LackOfCoffeeException(vsprintf(
46: 'The constructor of the class %s only accepts 0 arguments,'
47: . ' however, it was called with %d arguments.',
48: [static::class, $argCount]
49: ));
50: }
51: }
52:
53: /**
54: * @param string $name
55: */
56: public function __get($name)
57: {
58: throw new DomainException(
59: sprintf(
60: 'Attempt to read from undeclared property %s.',
61: get_class($this) . '::' . $name
62: )
63: );
64: }
65:
66: /**
67: * @param string $name
68: * @param mixed $value
69: */
70: public function __set($name, $value)
71: {
72: throw new DomainException(
73: sprintf(
74: 'Attempt to write to undeclared property %s.',
75: get_class($this) . '::' . $name
76: )
77: );
78: }
79:
80: /**
81: * Get the current element.
82: */
83: public function current()
84: {
85: $this->throwOnAttemptedIteration();
86: }
87:
88: /**
89: * Get the key of the current of element.
90: */
91: public function key()
92: {
93: $this->throwOnAttemptedIteration();
94: }
95:
96: /**
97: * Get the next.
98: */
99: public function next()
100: {
101: $this->throwOnAttemptedIteration();
102: }
103:
104: /**
105: * Rewind the iterator to the first element.
106: */
107: public function rewind()
108: {
109: $this->throwOnAttemptedIteration();
110: }
111:
112: /**
113: * Return whether or not the iterator is in a valid position.
114: */
115: public function valid()
116: {
117: $this->throwOnAttemptedIteration();
118: }
119:
120: /**
121: * Throw a new attempted iteration exception.
122: */
123: private function throwOnAttemptedIteration()
124: {
125: throw new DomainException(
126: sprintf(
127: 'Attempting to iterate an object (of class %s) which is not'
128: . ' iterable.',
129: get_class($this)
130: )
131: );
132: }
133: }
134: