PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.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 28.5, at src/presentations/abstract-presentation.php

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