|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\Castor\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\Castor\Gallery; |
|
6
|
|
|
use GeminiLabs\Castor\Image; |
|
7
|
|
|
use GeminiLabs\Castor\Video; |
|
8
|
|
|
use BadMethodCallException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @method string gallery( array $args ) |
|
12
|
|
|
* @method \WP_Query getGallery( array $args ) |
|
13
|
|
|
* @method object|void getImage( int|string $attachment ) |
|
14
|
|
|
* @method object|void getVideo( string|array $args ) |
|
15
|
|
|
* @method string|void image( int|string $attachment, string|array $size ) |
|
16
|
|
|
* @method string|void video( string|array $args ) |
|
17
|
|
|
*/ |
|
18
|
|
|
class Media |
|
19
|
|
|
{ |
|
20
|
|
|
protected $gallery; |
|
21
|
|
|
protected $image; |
|
22
|
|
|
protected $video; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct( Gallery $gallery, Image $image, Video $video ) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->gallery = $gallery; |
|
27
|
|
|
$this->image = $image; |
|
28
|
|
|
$this->video = $video; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $name |
|
33
|
|
|
* |
|
34
|
|
|
* @return string|void |
|
35
|
|
|
* @throws BadMethodCallException |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __call( $name, array $args ) |
|
38
|
|
|
{ |
|
39
|
|
|
$mediaType = $this->validateMethod( $name ); |
|
40
|
|
|
if( !$mediaType ) { |
|
|
|
|
|
|
41
|
|
|
throw new BadMethodCallException( sprintf( 'Not a valid method: %s', $name )); |
|
42
|
|
|
} |
|
43
|
|
|
if( !count( $args )) { |
|
44
|
|
|
throw new BadMethodCallException( sprintf( 'Missing arguments for: %s', $name )); |
|
45
|
|
|
} |
|
46
|
|
|
if( str_replace( $mediaType, '', strtolower( $name ))) { |
|
47
|
|
|
return $this->$mediaType->get( $args[0] )->$mediaType; |
|
48
|
|
|
} |
|
49
|
|
|
return !empty( $args[1] ) |
|
50
|
|
|
? $this->$mediaType->get( $args[0] )->render( $args[1] ) |
|
51
|
|
|
: $this->$mediaType->get( $args[0] )->render(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $name |
|
56
|
|
|
* @param mixed $args |
|
57
|
|
|
* |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public function get( $name, $args = [] ) |
|
61
|
|
|
{ |
|
62
|
|
|
if( $mediaType = $this->validateMethod( $name )) { |
|
63
|
|
|
return $this->$mediaType->get( $args )->$mediaType; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $name |
|
69
|
|
|
* |
|
70
|
|
|
* @return string|false |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function validateMethod( $name ) |
|
73
|
|
|
{ |
|
74
|
|
|
foreach( [$name, strtolower( substr( $name, 3 ))] as $method ) { |
|
75
|
|
|
if( in_array( $method, ['gallery', 'image', 'video'] ) |
|
76
|
|
|
&& property_exists( $this, $method ) |
|
77
|
|
|
&& is_object( $this->$method )) { |
|
78
|
|
|
return $method; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: