Subversion Repositories PHPX

Rev

Rev 60 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
34 PointedEar 1
<?php
2
 
3
namespace de\pointedears;
4
 
5
/**
6
 * Base class providing a generic getter and setter
7
 *
8
 * @author Thomas 'PointedEars' Lahn
9
 */
10
abstract class Base
11
{
12
  /**
13
   * Retrieves a property value.
14
   *
15
   * @param string $name
16
   *   Property name
17
   * @throws InvalidArgumentException
18
   *   if the property does not exist or has no getter
19
   * @return mixed
20
   *   Property value
21
   */
22
  public function __get ($name)
23
  {
24
    $getter = 'get' . ucfirst($name);
25
    if (method_exists($this, $getter))
26
    {
27
      return $this->$getter();
28
    }
29
 
30
    if (property_exists($this, "_$name"))
31
    {
32
      throw new \InvalidArgumentException("Property '_$name' has no getter");
33
    }
34
 
35
    throw new \InvalidArgumentException("No such property: '_$name'");
36
  }
37
 
38
  /**
39
   * Sets a property value.
40
   *
41
   * @param string $name
42
   *   Property name
43
   * @param mixed $value
44
   *   Property value
45
   * @throws InvalidArgumentException
46
   *   if the property does not exist or is read-only
47
   * @return mixed
48
   *   Return value of the property-specific setter
49
   */
50
  public function __set ($name, $value)
51
  {
52
    $setter = 'set' . ucfirst($name);
53
    if (method_exists($this, $setter))
54
    {
55
      return $this->$setter($value);
56
    }
57
 
58
    if (property_exists($this, "_$name"))
59
    {
60
      throw new \InvalidArgumentException("Property '_$name' has no setter");
61
    }
62
 
63
    throw new \InvalidArgumentException("No such property: '_$name'");
64
  }
65
}