| 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 |
|