PluginProbe ʕ •ᴥ•ʔ
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF / trunk
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF vtrunk
2.0.5 trunk 1.3.7 1.4.0 1.4.1 1.4.2 1.4.6 1.5.0 1.5.3 1.5.6 1.5.8 1.6.5 1.6.6 1.6.9 1.7.0 1.7.4 1.8.1 1.8.2 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4
robin-image-optimizer / includes / classes / models / class-rio-base-object.php
robin-image-optimizer / includes / classes / models Last commit date
class-rio-attachment-extra-data.php 6 months ago class-rio-base-active-record.php 6 months ago class-rio-base-extra-data.php 6 months ago class-rio-base-helper.php 6 months ago class-rio-base-object.php 6 months ago class-rio-process-queue-table.php 4 months ago class-rio-server-smushit-extra-data.php 6 months ago class.webp-extra-data.php 6 months ago
class-rio-base-object.php
102 lines
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 /**
9 * Class RIO_Base_Object is a base class that implements property feature.
10 *
11 * It takes advantage of __get() and __set(), see further implementation below in the class.
12 *
13 * When property of the class is being used and it has a getter, it will be used instead of directly accessing it.
14 *
15 * The same logic applies for setter.
16 *
17 * Example:
18 *
19 * ```php
20 * // equivalent to $label = $object->getLabel();
21 * $label = $object->label;
22 * // equivalent to $object->setLabel('abc');
23 * $object->label = 'abc';
24 * ```
25 */
26 class RIO_Base_Object {
27
28 /**
29 * RIO_Base_Object constructor.
30 *
31 * @param array $config name-value pairs that will be used to initialize the object properties.
32 */
33 public function __construct( $config = [] ) {
34
35 if ( ! empty( $config ) ) {
36 $this->configure( $config );
37 }
38
39 $this->init();
40 }
41
42 /**
43 * Initiate model.
44 */
45 public function init() {
46 }
47
48 /**
49 * Configure object.
50 *
51 * @param array $config name-value pairs that will be used to initialize the object properties.
52 */
53 public function configure( $config ) {
54 RIO_Base_Helper::configure( $this, $config );
55 }
56
57 /**
58 * Returns the value of an object property.
59 *
60 * Do not call this method directly as it is a PHP magic method that
61 * will be implicitly called when executing `$value = $object->property;`.
62 *
63 * @param string $name the property name
64 *
65 * @return mixed the property value
66 * @throws Exception if the property is not defined
67 * @see __set()
68 */
69 public function __get( $name ) {
70 $getter = 'get_' . $name;
71 if ( method_exists( $this, $getter ) ) {
72 return $this->$getter();
73 } elseif ( method_exists( $this, 'set' . $name ) ) {
74 throw new \Exception( 'Getting write-only property: ' . get_class( $this ) . '::' . $name );
75 }
76 throw new Exception( 'Getting unknown property: ' . get_class( $this ) . '::' . $name );
77 }
78
79 /**
80 * Sets value of an object property.
81 *
82 * Do not call this method directly as it is a PHP magic method that
83 * will be implicitly called when executing `$object->property = $value;`.
84 *
85 * @param string $name the property name or the event name
86 * @param mixed $value the property value
87 *
88 * @throws Exception if the property is not defined
89 * @see __get()
90 */
91 public function __set( $name, $value ) {
92 $setter = 'set_' . $name;
93 if ( method_exists( $this, $setter ) ) {
94 $this->$setter( $value );
95 } elseif ( method_exists( $this, 'get' . $name ) ) {
96 throw new \Exception( 'Setting read-only property: ' . get_class( $this ) . '::' . $name );
97 } else {
98 throw new \Exception( 'Setting unknown property: ' . get_class( $this ) . '::' . $name );
99 }
100 }
101 }
102