| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Surfaces; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Helpers\Schema; |
| 6 |
use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Schema_Helpers_Surface. |
| 10 |
* |
| 11 |
* Surface for the indexables. |
| 12 |
* |
| 13 |
* @property Schema\Article_Helper $article |
| 14 |
* @property Schema\HTML_Helper $html |
| 15 |
* @property Schema\ID_Helper $id |
| 16 |
* @property Schema\Image_Helper $image |
| 17 |
* @property Schema\Language_Helper $language |
| 18 |
*/ |
| 19 |
class Schema_Helpers_Surface { |
| 20 |
|
| 21 |
/** |
| 22 |
* The DI container. |
| 23 |
* |
| 24 |
* @var ContainerInterface |
| 25 |
*/ |
| 26 |
private $container; |
| 27 |
|
| 28 |
/** |
| 29 |
* Helpers that should be fully capitalized. |
| 30 |
* |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
private $capitalized_helpers = [ 'html', 'id' ]; |
| 34 |
|
| 35 |
/** |
| 36 |
* Loader constructor. |
| 37 |
* |
| 38 |
* @param ContainerInterface $container The dependency injection container. |
| 39 |
*/ |
| 40 |
public function __construct( ContainerInterface $container ) { |
| 41 |
$this->container = $container; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Magic getter for getting helper classes. |
| 46 |
* |
| 47 |
* @param string $helper The helper to get. |
| 48 |
* |
| 49 |
* @return mixed The helper class. |
| 50 |
*/ |
| 51 |
public function __get( $helper ) { |
| 52 |
return $this->container->get( $this->get_helper_class( $helper ) ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Magic isset for ensuring helper exists. |
| 57 |
* |
| 58 |
* @param string $helper The helper to get. |
| 59 |
* |
| 60 |
* @return bool The helper class. |
| 61 |
*/ |
| 62 |
public function __isset( $helper ) { |
| 63 |
return $this->container->has( $this->get_helper_class( $helper ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get the class name from a helper slug |
| 68 |
* |
| 69 |
* @param string $helper The name of the helper. |
| 70 |
* |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
protected function get_helper_class( $helper ) { |
| 74 |
if ( \in_array( $helper, $this->capitalized_helpers, true ) ) { |
| 75 |
$helper = \strtoupper( $helper ); |
| 76 |
} |
| 77 |
$helper = \implode( '_', \array_map( 'ucfirst', \explode( '_', $helper ) ) ); |
| 78 |
return "Yoast\WP\SEO\Helpers\Schema\\{$helper}_Helper"; |
| 79 |
} |
| 80 |
} |
| 81 |
|