PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / presentations / abstract-presentation.php

abstract-presentation.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.5, at src/presentations/abstract-presentation.php

94 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Presentations;
4
5 use Exception;
6
7 /**
8 * The abstract presentation class.
9 */
10 class Abstract_Presentation {
11
12 /**
13 * The model.
14 *
15 * @var mixed
16 */
17 public $model;
18
19 /**
20 * Whether or not there is a presentation prototype.
21 *
22 * @var bool
23 */
24 private $is_prototype = true;
25
26 /**
27 * Creates a model presentation.
28 *
29 * @param array $data The data that this is a presentation of.
30 *
31 * @return static A model presentation.
32 *
33 * @throws Exception If attempting to create a model presentation from another model presentation.
34 */
35 public function of( $data ) {
36 if ( ! $this->is_prototype() ) {
37 throw new Exception( 'Attempting to create a model presentation from another model presentation. Use the prototype presentation gained from DI instead.' );
38 }
39
40 // Clone self to allow stateful services that do benefit from DI.
41 $presentation = clone $this;
42 foreach ( $data as $key => $value ) {
43 $presentation->{$key} = $value;
44 }
45 $presentation->is_prototype = false;
46 return $presentation;
47 }
48
49 /**
50 * Magic getter for lazy loading of generate functions.
51 *
52 * @param string $name The property to get.
53 *
54 * @return mixed The value if it could be generated.
55 *
56 * @throws Exception If there is no generator for the property.
57 */
58 public function __get( $name ) {
59 if ( $this->is_prototype() ) {
60 throw new Exception( 'Attempting property access on prototype presentation. Use Presentation::of( $data ) to get a model presentation.' );
61 }
62 $generator = "generate_$name";
63 if ( \method_exists( $this, $generator ) ) {
64 $this->{$name} = $this->$generator();
65 return $this->{$name};
66 }
67 throw new Exception( "Property $name has no generator. Expected function $generator." );
68 }
69
70 /**
71 * Magic isset for ensuring methods that have a generator are recognised.
72 *
73 * @codeCoverageIgnore Wrapper method.
74 *
75 * @param string $name The property to get.
76 *
77 * @return bool Whether or not there is a generator for the requested property.
78 */
79 public function __isset( $name ) {
80 return \method_exists( $this, "generate_$name" );
81 }
82
83 /**
84 * Returns `true` if this class is a prototype.
85 *
86 * @codeCoverageIgnore Wrapper method.
87 *
88 * @return bool If this class is a prototype or not.
89 */
90 protected function is_prototype() {
91 return $this->is_prototype;
92 }
93 }
94