@@ -20,160 +20,160 @@ |
||
| 20 | 20 | class ClassInterfaceCache |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * array of interfaces indexed by FQCNs where values are arrays of interface FQNs |
|
| 25 | - * |
|
| 26 | - * @var string[][] $interfaces |
|
| 27 | - */ |
|
| 28 | - private $interfaces = array(); |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @type string[][] $aliases |
|
| 32 | - */ |
|
| 33 | - protected $aliases = array(); |
|
| 34 | - |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @param string $fqn |
|
| 38 | - * @return string |
|
| 39 | - */ |
|
| 40 | - public function getFqn($fqn) |
|
| 41 | - { |
|
| 42 | - return $fqn instanceof FullyQualifiedName ? $fqn->string() : $fqn; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @param string $fqn |
|
| 48 | - * @return array |
|
| 49 | - */ |
|
| 50 | - public function getInterfaces($fqn) |
|
| 51 | - { |
|
| 52 | - $fqn = $this->getFqn($fqn); |
|
| 53 | - // have we already seen this FQCN ? |
|
| 54 | - if (! array_key_exists($fqn, $this->interfaces)) { |
|
| 55 | - $this->interfaces[ $fqn ] = array(); |
|
| 56 | - if (class_exists($fqn)) { |
|
| 57 | - $this->interfaces[ $fqn ] = class_implements($fqn, false); |
|
| 58 | - $this->interfaces[ $fqn ] = $this->interfaces[ $fqn ] !== false |
|
| 59 | - ? $this->interfaces[ $fqn ] |
|
| 60 | - : array(); |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - return $this->interfaces[ $fqn ]; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @param string $fqn |
|
| 69 | - * @param string $interface |
|
| 70 | - * @return bool |
|
| 71 | - */ |
|
| 72 | - public function hasInterface($fqn, $interface) |
|
| 73 | - { |
|
| 74 | - $fqn = $this->getFqn($fqn); |
|
| 75 | - $interfaces = $this->getInterfaces($fqn); |
|
| 76 | - return in_array($interface, $interfaces, true); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * adds an alias for a classname |
|
| 82 | - * |
|
| 83 | - * @param string $fqn the class name that should be used (concrete class to replace interface) |
|
| 84 | - * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
| 85 | - * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
| 86 | - */ |
|
| 87 | - public function addAlias($fqn, $alias, $for_class = '') |
|
| 88 | - { |
|
| 89 | - $fqn = $this->getFqn($fqn); |
|
| 90 | - $alias = $this->getFqn($alias); |
|
| 91 | - // are we adding an alias for a specific class? |
|
| 92 | - if ($for_class !== '') { |
|
| 93 | - // make sure it's set up as an array |
|
| 94 | - if (! isset($this->aliases[ $for_class ])) { |
|
| 95 | - $this->aliases[ $for_class ] = array(); |
|
| 96 | - } |
|
| 97 | - $this->aliases[ $for_class ][ $alias ] = $fqn; |
|
| 98 | - return; |
|
| 99 | - } |
|
| 100 | - $this->aliases[ $alias ] = $fqn; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * returns TRUE if the provided FQN is an alias |
|
| 106 | - * |
|
| 107 | - * @param string $fqn |
|
| 108 | - * @param string $for_class |
|
| 109 | - * @return bool |
|
| 110 | - */ |
|
| 111 | - public function isAlias($fqn = '', $for_class = '') |
|
| 112 | - { |
|
| 113 | - $fqn = $this->getFqn($fqn); |
|
| 114 | - if ($this->isAliasForClass($fqn, $for_class)) { |
|
| 115 | - return true; |
|
| 116 | - } |
|
| 117 | - if ($for_class === '' && $this->isDirectAlias($fqn)) { |
|
| 118 | - return true; |
|
| 119 | - } |
|
| 120 | - return false; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * returns TRUE if the provided FQN is an alias |
|
| 126 | - * |
|
| 127 | - * @param string $fqn |
|
| 128 | - * @return bool |
|
| 129 | - */ |
|
| 130 | - protected function isDirectAlias($fqn = '') |
|
| 131 | - { |
|
| 132 | - return isset($this->aliases[ (string) $fqn ]) && ! is_array($this->aliases[ (string) $fqn ]); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * returns TRUE if the provided FQN is an alias for the specified class |
|
| 138 | - * |
|
| 139 | - * @param string $fqn |
|
| 140 | - * @param string $for_class |
|
| 141 | - * @return bool |
|
| 142 | - */ |
|
| 143 | - protected function isAliasForClass($fqn = '', $for_class = '') |
|
| 144 | - { |
|
| 145 | - return ( |
|
| 146 | - $for_class !== '' |
|
| 147 | - && isset($this->aliases[ (string) $for_class ][ (string) $fqn ]) |
|
| 148 | - ); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * returns FQN for provided alias if one exists, otherwise returns the original FQN |
|
| 154 | - * functions recursively, so that multiple aliases can be used to drill down to a FQN |
|
| 155 | - * for example: |
|
| 156 | - * if the following two entries were added to the aliases array: |
|
| 157 | - * array( |
|
| 158 | - * 'interface_alias' => 'some\namespace\interface' |
|
| 159 | - * 'some\namespace\interface' => 'some\namespace\classname' |
|
| 160 | - * ) |
|
| 161 | - * then one could use Loader::getNew( 'interface_alias' ) |
|
| 162 | - * to load an instance of 'some\namespace\classname' |
|
| 163 | - * |
|
| 164 | - * @param string $alias |
|
| 165 | - * @param string $for_class |
|
| 166 | - * @return string |
|
| 167 | - */ |
|
| 168 | - public function getFqnForAlias($alias = '', $for_class = '') |
|
| 169 | - { |
|
| 170 | - $alias = $this->getFqn($alias); |
|
| 171 | - if ($this->isAliasForClass($alias, $for_class)) { |
|
| 172 | - return $this->getFqnForAlias($this->aliases[ (string) $for_class ][ (string) $alias ], $for_class); |
|
| 173 | - } |
|
| 174 | - if ($this->isDirectAlias($alias)) { |
|
| 175 | - return $this->getFqnForAlias($this->aliases[ (string) $alias ], ''); |
|
| 176 | - } |
|
| 177 | - return $alias; |
|
| 178 | - } |
|
| 23 | + /** |
|
| 24 | + * array of interfaces indexed by FQCNs where values are arrays of interface FQNs |
|
| 25 | + * |
|
| 26 | + * @var string[][] $interfaces |
|
| 27 | + */ |
|
| 28 | + private $interfaces = array(); |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @type string[][] $aliases |
|
| 32 | + */ |
|
| 33 | + protected $aliases = array(); |
|
| 34 | + |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @param string $fqn |
|
| 38 | + * @return string |
|
| 39 | + */ |
|
| 40 | + public function getFqn($fqn) |
|
| 41 | + { |
|
| 42 | + return $fqn instanceof FullyQualifiedName ? $fqn->string() : $fqn; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @param string $fqn |
|
| 48 | + * @return array |
|
| 49 | + */ |
|
| 50 | + public function getInterfaces($fqn) |
|
| 51 | + { |
|
| 52 | + $fqn = $this->getFqn($fqn); |
|
| 53 | + // have we already seen this FQCN ? |
|
| 54 | + if (! array_key_exists($fqn, $this->interfaces)) { |
|
| 55 | + $this->interfaces[ $fqn ] = array(); |
|
| 56 | + if (class_exists($fqn)) { |
|
| 57 | + $this->interfaces[ $fqn ] = class_implements($fqn, false); |
|
| 58 | + $this->interfaces[ $fqn ] = $this->interfaces[ $fqn ] !== false |
|
| 59 | + ? $this->interfaces[ $fqn ] |
|
| 60 | + : array(); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + return $this->interfaces[ $fqn ]; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @param string $fqn |
|
| 69 | + * @param string $interface |
|
| 70 | + * @return bool |
|
| 71 | + */ |
|
| 72 | + public function hasInterface($fqn, $interface) |
|
| 73 | + { |
|
| 74 | + $fqn = $this->getFqn($fqn); |
|
| 75 | + $interfaces = $this->getInterfaces($fqn); |
|
| 76 | + return in_array($interface, $interfaces, true); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * adds an alias for a classname |
|
| 82 | + * |
|
| 83 | + * @param string $fqn the class name that should be used (concrete class to replace interface) |
|
| 84 | + * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
| 85 | + * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
| 86 | + */ |
|
| 87 | + public function addAlias($fqn, $alias, $for_class = '') |
|
| 88 | + { |
|
| 89 | + $fqn = $this->getFqn($fqn); |
|
| 90 | + $alias = $this->getFqn($alias); |
|
| 91 | + // are we adding an alias for a specific class? |
|
| 92 | + if ($for_class !== '') { |
|
| 93 | + // make sure it's set up as an array |
|
| 94 | + if (! isset($this->aliases[ $for_class ])) { |
|
| 95 | + $this->aliases[ $for_class ] = array(); |
|
| 96 | + } |
|
| 97 | + $this->aliases[ $for_class ][ $alias ] = $fqn; |
|
| 98 | + return; |
|
| 99 | + } |
|
| 100 | + $this->aliases[ $alias ] = $fqn; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * returns TRUE if the provided FQN is an alias |
|
| 106 | + * |
|
| 107 | + * @param string $fqn |
|
| 108 | + * @param string $for_class |
|
| 109 | + * @return bool |
|
| 110 | + */ |
|
| 111 | + public function isAlias($fqn = '', $for_class = '') |
|
| 112 | + { |
|
| 113 | + $fqn = $this->getFqn($fqn); |
|
| 114 | + if ($this->isAliasForClass($fqn, $for_class)) { |
|
| 115 | + return true; |
|
| 116 | + } |
|
| 117 | + if ($for_class === '' && $this->isDirectAlias($fqn)) { |
|
| 118 | + return true; |
|
| 119 | + } |
|
| 120 | + return false; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * returns TRUE if the provided FQN is an alias |
|
| 126 | + * |
|
| 127 | + * @param string $fqn |
|
| 128 | + * @return bool |
|
| 129 | + */ |
|
| 130 | + protected function isDirectAlias($fqn = '') |
|
| 131 | + { |
|
| 132 | + return isset($this->aliases[ (string) $fqn ]) && ! is_array($this->aliases[ (string) $fqn ]); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * returns TRUE if the provided FQN is an alias for the specified class |
|
| 138 | + * |
|
| 139 | + * @param string $fqn |
|
| 140 | + * @param string $for_class |
|
| 141 | + * @return bool |
|
| 142 | + */ |
|
| 143 | + protected function isAliasForClass($fqn = '', $for_class = '') |
|
| 144 | + { |
|
| 145 | + return ( |
|
| 146 | + $for_class !== '' |
|
| 147 | + && isset($this->aliases[ (string) $for_class ][ (string) $fqn ]) |
|
| 148 | + ); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * returns FQN for provided alias if one exists, otherwise returns the original FQN |
|
| 154 | + * functions recursively, so that multiple aliases can be used to drill down to a FQN |
|
| 155 | + * for example: |
|
| 156 | + * if the following two entries were added to the aliases array: |
|
| 157 | + * array( |
|
| 158 | + * 'interface_alias' => 'some\namespace\interface' |
|
| 159 | + * 'some\namespace\interface' => 'some\namespace\classname' |
|
| 160 | + * ) |
|
| 161 | + * then one could use Loader::getNew( 'interface_alias' ) |
|
| 162 | + * to load an instance of 'some\namespace\classname' |
|
| 163 | + * |
|
| 164 | + * @param string $alias |
|
| 165 | + * @param string $for_class |
|
| 166 | + * @return string |
|
| 167 | + */ |
|
| 168 | + public function getFqnForAlias($alias = '', $for_class = '') |
|
| 169 | + { |
|
| 170 | + $alias = $this->getFqn($alias); |
|
| 171 | + if ($this->isAliasForClass($alias, $for_class)) { |
|
| 172 | + return $this->getFqnForAlias($this->aliases[ (string) $for_class ][ (string) $alias ], $for_class); |
|
| 173 | + } |
|
| 174 | + if ($this->isDirectAlias($alias)) { |
|
| 175 | + return $this->getFqnForAlias($this->aliases[ (string) $alias ], ''); |
|
| 176 | + } |
|
| 177 | + return $alias; |
|
| 178 | + } |
|
| 179 | 179 | } |
@@ -51,16 +51,16 @@ discard block |
||
| 51 | 51 | { |
| 52 | 52 | $fqn = $this->getFqn($fqn); |
| 53 | 53 | // have we already seen this FQCN ? |
| 54 | - if (! array_key_exists($fqn, $this->interfaces)) { |
|
| 55 | - $this->interfaces[ $fqn ] = array(); |
|
| 54 | + if ( ! array_key_exists($fqn, $this->interfaces)) { |
|
| 55 | + $this->interfaces[$fqn] = array(); |
|
| 56 | 56 | if (class_exists($fqn)) { |
| 57 | - $this->interfaces[ $fqn ] = class_implements($fqn, false); |
|
| 58 | - $this->interfaces[ $fqn ] = $this->interfaces[ $fqn ] !== false |
|
| 59 | - ? $this->interfaces[ $fqn ] |
|
| 57 | + $this->interfaces[$fqn] = class_implements($fqn, false); |
|
| 58 | + $this->interfaces[$fqn] = $this->interfaces[$fqn] !== false |
|
| 59 | + ? $this->interfaces[$fqn] |
|
| 60 | 60 | : array(); |
| 61 | 61 | } |
| 62 | 62 | } |
| 63 | - return $this->interfaces[ $fqn ]; |
|
| 63 | + return $this->interfaces[$fqn]; |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | |
@@ -91,13 +91,13 @@ discard block |
||
| 91 | 91 | // are we adding an alias for a specific class? |
| 92 | 92 | if ($for_class !== '') { |
| 93 | 93 | // make sure it's set up as an array |
| 94 | - if (! isset($this->aliases[ $for_class ])) { |
|
| 95 | - $this->aliases[ $for_class ] = array(); |
|
| 94 | + if ( ! isset($this->aliases[$for_class])) { |
|
| 95 | + $this->aliases[$for_class] = array(); |
|
| 96 | 96 | } |
| 97 | - $this->aliases[ $for_class ][ $alias ] = $fqn; |
|
| 97 | + $this->aliases[$for_class][$alias] = $fqn; |
|
| 98 | 98 | return; |
| 99 | 99 | } |
| 100 | - $this->aliases[ $alias ] = $fqn; |
|
| 100 | + $this->aliases[$alias] = $fqn; |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | */ |
| 130 | 130 | protected function isDirectAlias($fqn = '') |
| 131 | 131 | { |
| 132 | - return isset($this->aliases[ (string) $fqn ]) && ! is_array($this->aliases[ (string) $fqn ]); |
|
| 132 | + return isset($this->aliases[(string) $fqn]) && ! is_array($this->aliases[(string) $fqn]); |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | { |
| 145 | 145 | return ( |
| 146 | 146 | $for_class !== '' |
| 147 | - && isset($this->aliases[ (string) $for_class ][ (string) $fqn ]) |
|
| 147 | + && isset($this->aliases[(string) $for_class][(string) $fqn]) |
|
| 148 | 148 | ); |
| 149 | 149 | } |
| 150 | 150 | |
@@ -169,10 +169,10 @@ discard block |
||
| 169 | 169 | { |
| 170 | 170 | $alias = $this->getFqn($alias); |
| 171 | 171 | if ($this->isAliasForClass($alias, $for_class)) { |
| 172 | - return $this->getFqnForAlias($this->aliases[ (string) $for_class ][ (string) $alias ], $for_class); |
|
| 172 | + return $this->getFqnForAlias($this->aliases[(string) $for_class][(string) $alias], $for_class); |
|
| 173 | 173 | } |
| 174 | 174 | if ($this->isDirectAlias($alias)) { |
| 175 | - return $this->getFqnForAlias($this->aliases[ (string) $alias ], ''); |
|
| 175 | + return $this->getFqnForAlias($this->aliases[(string) $alias], ''); |
|
| 176 | 176 | } |
| 177 | 177 | return $alias; |
| 178 | 178 | } |
@@ -61,7 +61,7 @@ |
||
| 61 | 61 | { |
| 62 | 62 | $custom_post_types = $this->custom_post_types->getDefinitions(); |
| 63 | 63 | foreach ($custom_post_types as $custom_post_type => $CPT) { |
| 64 | - $this->wp_post_types[ $custom_post_type ] = $this->registerCustomPostType( |
|
| 64 | + $this->wp_post_types[$custom_post_type] = $this->registerCustomPostType( |
|
| 65 | 65 | $custom_post_type, |
| 66 | 66 | $CPT['singular_name'], |
| 67 | 67 | $CPT['plural_name'], |
@@ -18,240 +18,240 @@ |
||
| 18 | 18 | class RegisterCustomPostTypes |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var CustomPostTypeDefinitions $custom_post_types |
|
| 23 | - */ |
|
| 24 | - public $custom_post_types; |
|
| 21 | + /** |
|
| 22 | + * @var CustomPostTypeDefinitions $custom_post_types |
|
| 23 | + */ |
|
| 24 | + public $custom_post_types; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var WP_Post_Type[] $wp_post_types |
|
| 28 | - */ |
|
| 29 | - public $wp_post_types = array(); |
|
| 26 | + /** |
|
| 27 | + * @var WP_Post_Type[] $wp_post_types |
|
| 28 | + */ |
|
| 29 | + public $wp_post_types = array(); |
|
| 30 | 30 | |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * RegisterCustomPostTypes constructor. |
|
| 34 | - * |
|
| 35 | - * @param CustomPostTypeDefinitions $custom_post_types |
|
| 36 | - */ |
|
| 37 | - public function __construct(CustomPostTypeDefinitions $custom_post_types) |
|
| 38 | - { |
|
| 39 | - $this->custom_post_types = $custom_post_types; |
|
| 40 | - } |
|
| 32 | + /** |
|
| 33 | + * RegisterCustomPostTypes constructor. |
|
| 34 | + * |
|
| 35 | + * @param CustomPostTypeDefinitions $custom_post_types |
|
| 36 | + */ |
|
| 37 | + public function __construct(CustomPostTypeDefinitions $custom_post_types) |
|
| 38 | + { |
|
| 39 | + $this->custom_post_types = $custom_post_types; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @return WP_Post_Type[] |
|
| 45 | - */ |
|
| 46 | - public function getRegisteredCustomPostTypes() |
|
| 47 | - { |
|
| 48 | - return $this->wp_post_types; |
|
| 49 | - } |
|
| 43 | + /** |
|
| 44 | + * @return WP_Post_Type[] |
|
| 45 | + */ |
|
| 46 | + public function getRegisteredCustomPostTypes() |
|
| 47 | + { |
|
| 48 | + return $this->wp_post_types; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * @return void |
|
| 54 | - * @throws DomainException |
|
| 55 | - */ |
|
| 56 | - public function registerCustomPostTypes() |
|
| 57 | - { |
|
| 58 | - $custom_post_types = $this->custom_post_types->getDefinitions(); |
|
| 59 | - foreach ($custom_post_types as $custom_post_type => $CPT) { |
|
| 60 | - $this->wp_post_types[ $custom_post_type ] = $this->registerCustomPostType( |
|
| 61 | - $custom_post_type, |
|
| 62 | - $CPT['singular_name'], |
|
| 63 | - $CPT['plural_name'], |
|
| 64 | - $CPT['singular_slug'], |
|
| 65 | - $CPT['plural_slug'], |
|
| 66 | - $CPT['args'] |
|
| 67 | - ); |
|
| 68 | - } |
|
| 69 | - } |
|
| 52 | + /** |
|
| 53 | + * @return void |
|
| 54 | + * @throws DomainException |
|
| 55 | + */ |
|
| 56 | + public function registerCustomPostTypes() |
|
| 57 | + { |
|
| 58 | + $custom_post_types = $this->custom_post_types->getDefinitions(); |
|
| 59 | + foreach ($custom_post_types as $custom_post_type => $CPT) { |
|
| 60 | + $this->wp_post_types[ $custom_post_type ] = $this->registerCustomPostType( |
|
| 61 | + $custom_post_type, |
|
| 62 | + $CPT['singular_name'], |
|
| 63 | + $CPT['plural_name'], |
|
| 64 | + $CPT['singular_slug'], |
|
| 65 | + $CPT['plural_slug'], |
|
| 66 | + $CPT['args'] |
|
| 67 | + ); |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Registers a new custom post type. Sets default settings given only the following params. |
|
| 74 | - * Returns the registered post type object, or an error object. |
|
| 75 | - * |
|
| 76 | - * @param string $post_type the actual post type name |
|
| 77 | - * IMPORTANT: |
|
| 78 | - * this must match what the slug is for admin pages related to this CPT |
|
| 79 | - * Also any models must use this slug as well |
|
| 80 | - * @param string $singular_name a pre-internationalized string for the singular name of the objects |
|
| 81 | - * @param string $plural_name a pre-internationalized string for the plural name of the objects |
|
| 82 | - * @param string $singular_slug |
|
| 83 | - * @param string $plural_slug |
|
| 84 | - * @param array $override_arguments exactly like $args as described in |
|
| 85 | - * http://codex.wordpress.org/Function_Reference/register_post_type |
|
| 86 | - * @return WP_Post_Type|WP_Error |
|
| 87 | - * @throws DomainException |
|
| 88 | - */ |
|
| 89 | - public function registerCustomPostType( |
|
| 90 | - $post_type, |
|
| 91 | - $singular_name, |
|
| 92 | - $plural_name, |
|
| 93 | - $singular_slug = '', |
|
| 94 | - $plural_slug = '', |
|
| 95 | - array $override_arguments = array() |
|
| 96 | - ) { |
|
| 97 | - $wp_post_type = register_post_type( |
|
| 98 | - $post_type, |
|
| 99 | - $this->prepareArguments( |
|
| 100 | - $post_type, |
|
| 101 | - $singular_name, |
|
| 102 | - $plural_name, |
|
| 103 | - $singular_slug, |
|
| 104 | - $plural_slug, |
|
| 105 | - $override_arguments |
|
| 106 | - ) |
|
| 107 | - ); |
|
| 108 | - if ($wp_post_type instanceof WP_Error) { |
|
| 109 | - throw new DomainException($wp_post_type->get_error_message()); |
|
| 110 | - } |
|
| 111 | - return $wp_post_type; |
|
| 112 | - } |
|
| 72 | + /** |
|
| 73 | + * Registers a new custom post type. Sets default settings given only the following params. |
|
| 74 | + * Returns the registered post type object, or an error object. |
|
| 75 | + * |
|
| 76 | + * @param string $post_type the actual post type name |
|
| 77 | + * IMPORTANT: |
|
| 78 | + * this must match what the slug is for admin pages related to this CPT |
|
| 79 | + * Also any models must use this slug as well |
|
| 80 | + * @param string $singular_name a pre-internationalized string for the singular name of the objects |
|
| 81 | + * @param string $plural_name a pre-internationalized string for the plural name of the objects |
|
| 82 | + * @param string $singular_slug |
|
| 83 | + * @param string $plural_slug |
|
| 84 | + * @param array $override_arguments exactly like $args as described in |
|
| 85 | + * http://codex.wordpress.org/Function_Reference/register_post_type |
|
| 86 | + * @return WP_Post_Type|WP_Error |
|
| 87 | + * @throws DomainException |
|
| 88 | + */ |
|
| 89 | + public function registerCustomPostType( |
|
| 90 | + $post_type, |
|
| 91 | + $singular_name, |
|
| 92 | + $plural_name, |
|
| 93 | + $singular_slug = '', |
|
| 94 | + $plural_slug = '', |
|
| 95 | + array $override_arguments = array() |
|
| 96 | + ) { |
|
| 97 | + $wp_post_type = register_post_type( |
|
| 98 | + $post_type, |
|
| 99 | + $this->prepareArguments( |
|
| 100 | + $post_type, |
|
| 101 | + $singular_name, |
|
| 102 | + $plural_name, |
|
| 103 | + $singular_slug, |
|
| 104 | + $plural_slug, |
|
| 105 | + $override_arguments |
|
| 106 | + ) |
|
| 107 | + ); |
|
| 108 | + if ($wp_post_type instanceof WP_Error) { |
|
| 109 | + throw new DomainException($wp_post_type->get_error_message()); |
|
| 110 | + } |
|
| 111 | + return $wp_post_type; |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * @param string $post_type the actual post type name |
|
| 117 | - * @param string $singular_name a pre-internationalized string for the singular name of the objects |
|
| 118 | - * @param string $plural_name a pre-internationalized string for the plural name of the objects |
|
| 119 | - * @param string $singular_slug |
|
| 120 | - * @param string $plural_slug |
|
| 121 | - * @param array $override_arguments The default values set in this function will be overridden |
|
| 122 | - * by whatever you set in $override_arguments |
|
| 123 | - * @return array |
|
| 124 | - */ |
|
| 125 | - protected function prepareArguments( |
|
| 126 | - $post_type, |
|
| 127 | - $singular_name, |
|
| 128 | - $plural_name, |
|
| 129 | - $singular_slug, |
|
| 130 | - $plural_slug, |
|
| 131 | - array $override_arguments = array() |
|
| 132 | - ) { |
|
| 133 | - // verify plural slug and singular slug, if they aren't we'll use $singular_name and $plural_name |
|
| 134 | - $singular_slug = ! empty($singular_slug) ? $singular_slug : $singular_name; |
|
| 135 | - $plural_slug = ! empty($plural_slug) ? $plural_slug : $plural_name; |
|
| 136 | - $labels = $this->getLabels( |
|
| 137 | - $singular_name, |
|
| 138 | - $plural_name, |
|
| 139 | - $singular_slug, |
|
| 140 | - $plural_slug |
|
| 141 | - ); |
|
| 142 | - // note the page_templates arg in the supports index is something specific to EE. |
|
| 143 | - // WordPress doesn't actually have that in their register_post_type api. |
|
| 144 | - $arguments = $this->getDefaultArguments($labels, $post_type, $plural_slug); |
|
| 145 | - if ($override_arguments) { |
|
| 146 | - if (isset($override_arguments['labels'])) { |
|
| 147 | - $labels = array_merge($arguments['labels'], $override_arguments['labels']); |
|
| 148 | - } |
|
| 149 | - $arguments = array_merge($arguments, $override_arguments); |
|
| 150 | - $arguments['labels'] = $labels; |
|
| 151 | - } |
|
| 152 | - return $arguments; |
|
| 153 | - } |
|
| 115 | + /** |
|
| 116 | + * @param string $post_type the actual post type name |
|
| 117 | + * @param string $singular_name a pre-internationalized string for the singular name of the objects |
|
| 118 | + * @param string $plural_name a pre-internationalized string for the plural name of the objects |
|
| 119 | + * @param string $singular_slug |
|
| 120 | + * @param string $plural_slug |
|
| 121 | + * @param array $override_arguments The default values set in this function will be overridden |
|
| 122 | + * by whatever you set in $override_arguments |
|
| 123 | + * @return array |
|
| 124 | + */ |
|
| 125 | + protected function prepareArguments( |
|
| 126 | + $post_type, |
|
| 127 | + $singular_name, |
|
| 128 | + $plural_name, |
|
| 129 | + $singular_slug, |
|
| 130 | + $plural_slug, |
|
| 131 | + array $override_arguments = array() |
|
| 132 | + ) { |
|
| 133 | + // verify plural slug and singular slug, if they aren't we'll use $singular_name and $plural_name |
|
| 134 | + $singular_slug = ! empty($singular_slug) ? $singular_slug : $singular_name; |
|
| 135 | + $plural_slug = ! empty($plural_slug) ? $plural_slug : $plural_name; |
|
| 136 | + $labels = $this->getLabels( |
|
| 137 | + $singular_name, |
|
| 138 | + $plural_name, |
|
| 139 | + $singular_slug, |
|
| 140 | + $plural_slug |
|
| 141 | + ); |
|
| 142 | + // note the page_templates arg in the supports index is something specific to EE. |
|
| 143 | + // WordPress doesn't actually have that in their register_post_type api. |
|
| 144 | + $arguments = $this->getDefaultArguments($labels, $post_type, $plural_slug); |
|
| 145 | + if ($override_arguments) { |
|
| 146 | + if (isset($override_arguments['labels'])) { |
|
| 147 | + $labels = array_merge($arguments['labels'], $override_arguments['labels']); |
|
| 148 | + } |
|
| 149 | + $arguments = array_merge($arguments, $override_arguments); |
|
| 150 | + $arguments['labels'] = $labels; |
|
| 151 | + } |
|
| 152 | + return $arguments; |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * @param string $singular_name |
|
| 158 | - * @param string $plural_name |
|
| 159 | - * @param string $singular_slug |
|
| 160 | - * @param string $plural_slug |
|
| 161 | - * @return array |
|
| 162 | - */ |
|
| 163 | - private function getLabels($singular_name, $plural_name, $singular_slug, $plural_slug) |
|
| 164 | - { |
|
| 165 | - return array( |
|
| 166 | - 'name' => $plural_name, |
|
| 167 | - 'singular_name' => $singular_name, |
|
| 168 | - 'singular_slug' => $singular_slug, |
|
| 169 | - 'plural_slug' => $plural_slug, |
|
| 170 | - 'add_new' => sprintf( |
|
| 171 | - esc_html_x('Add %s', 'Add Event', 'event_espresso'), |
|
| 172 | - $singular_name |
|
| 173 | - ), |
|
| 174 | - 'add_new_item' => sprintf( |
|
| 175 | - esc_html_x('Add New %s', 'Add New Event', 'event_espresso'), |
|
| 176 | - $singular_name |
|
| 177 | - ), |
|
| 178 | - 'edit_item' => sprintf( |
|
| 179 | - esc_html_x('Edit %s', 'Edit Event', 'event_espresso'), |
|
| 180 | - $singular_name |
|
| 181 | - ), |
|
| 182 | - 'new_item' => sprintf( |
|
| 183 | - esc_html_x('New %s', 'New Event', 'event_espresso'), |
|
| 184 | - $singular_name |
|
| 185 | - ), |
|
| 186 | - 'all_items' => sprintf( |
|
| 187 | - esc_html_x('All %s', 'All Events', 'event_espresso'), |
|
| 188 | - $plural_name |
|
| 189 | - ), |
|
| 190 | - 'view_item' => sprintf( |
|
| 191 | - esc_html_x('View %s', 'View Event', 'event_espresso'), |
|
| 192 | - $singular_name |
|
| 193 | - ), |
|
| 194 | - 'search_items' => sprintf( |
|
| 195 | - esc_html_x('Search %s', 'Search Events', 'event_espresso'), |
|
| 196 | - $plural_name |
|
| 197 | - ), |
|
| 198 | - 'not_found' => sprintf( |
|
| 199 | - esc_html_x('No %s found', 'No Events found', 'event_espresso'), |
|
| 200 | - $plural_name |
|
| 201 | - ), |
|
| 202 | - 'not_found_in_trash' => sprintf( |
|
| 203 | - esc_html_x('No %s found in Trash', 'No Events found in Trash', 'event_espresso'), |
|
| 204 | - $plural_name |
|
| 205 | - ), |
|
| 206 | - 'parent_item_colon' => '', |
|
| 207 | - 'menu_name' => $plural_name, |
|
| 208 | - ); |
|
| 209 | - } |
|
| 156 | + /** |
|
| 157 | + * @param string $singular_name |
|
| 158 | + * @param string $plural_name |
|
| 159 | + * @param string $singular_slug |
|
| 160 | + * @param string $plural_slug |
|
| 161 | + * @return array |
|
| 162 | + */ |
|
| 163 | + private function getLabels($singular_name, $plural_name, $singular_slug, $plural_slug) |
|
| 164 | + { |
|
| 165 | + return array( |
|
| 166 | + 'name' => $plural_name, |
|
| 167 | + 'singular_name' => $singular_name, |
|
| 168 | + 'singular_slug' => $singular_slug, |
|
| 169 | + 'plural_slug' => $plural_slug, |
|
| 170 | + 'add_new' => sprintf( |
|
| 171 | + esc_html_x('Add %s', 'Add Event', 'event_espresso'), |
|
| 172 | + $singular_name |
|
| 173 | + ), |
|
| 174 | + 'add_new_item' => sprintf( |
|
| 175 | + esc_html_x('Add New %s', 'Add New Event', 'event_espresso'), |
|
| 176 | + $singular_name |
|
| 177 | + ), |
|
| 178 | + 'edit_item' => sprintf( |
|
| 179 | + esc_html_x('Edit %s', 'Edit Event', 'event_espresso'), |
|
| 180 | + $singular_name |
|
| 181 | + ), |
|
| 182 | + 'new_item' => sprintf( |
|
| 183 | + esc_html_x('New %s', 'New Event', 'event_espresso'), |
|
| 184 | + $singular_name |
|
| 185 | + ), |
|
| 186 | + 'all_items' => sprintf( |
|
| 187 | + esc_html_x('All %s', 'All Events', 'event_espresso'), |
|
| 188 | + $plural_name |
|
| 189 | + ), |
|
| 190 | + 'view_item' => sprintf( |
|
| 191 | + esc_html_x('View %s', 'View Event', 'event_espresso'), |
|
| 192 | + $singular_name |
|
| 193 | + ), |
|
| 194 | + 'search_items' => sprintf( |
|
| 195 | + esc_html_x('Search %s', 'Search Events', 'event_espresso'), |
|
| 196 | + $plural_name |
|
| 197 | + ), |
|
| 198 | + 'not_found' => sprintf( |
|
| 199 | + esc_html_x('No %s found', 'No Events found', 'event_espresso'), |
|
| 200 | + $plural_name |
|
| 201 | + ), |
|
| 202 | + 'not_found_in_trash' => sprintf( |
|
| 203 | + esc_html_x('No %s found in Trash', 'No Events found in Trash', 'event_espresso'), |
|
| 204 | + $plural_name |
|
| 205 | + ), |
|
| 206 | + 'parent_item_colon' => '', |
|
| 207 | + 'menu_name' => $plural_name, |
|
| 208 | + ); |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | 211 | |
| 212 | - /** |
|
| 213 | - * @param array $labels |
|
| 214 | - * @param string $post_type |
|
| 215 | - * @param string $plural_slug |
|
| 216 | - * @return array |
|
| 217 | - */ |
|
| 218 | - private function getDefaultArguments(array $labels, $post_type, $plural_slug) |
|
| 219 | - { |
|
| 220 | - return array( |
|
| 221 | - 'labels' => $labels, |
|
| 222 | - 'public' => true, |
|
| 223 | - 'publicly_queryable' => true, |
|
| 224 | - 'show_ui' => false, |
|
| 225 | - 'show_ee_ui' => true, |
|
| 226 | - 'show_in_menu' => false, |
|
| 227 | - 'show_in_nav_menus' => false, |
|
| 228 | - 'query_var' => true, |
|
| 229 | - 'rewrite' => apply_filters( |
|
| 230 | - 'FHEE__EventEspresso_core_domain_entities_custom_post_types_RegisterCustomPostTypes__getDefaultArguments__rewrite', |
|
| 231 | - // legacy filter applied for now, |
|
| 232 | - // later on we'll run a has_filter($tag) check and throw a doing_it_wrong() notice |
|
| 233 | - apply_filters( |
|
| 234 | - 'FHEE__EE_Register_CPTs__register_CPT__rewrite', |
|
| 235 | - array('slug' => $plural_slug), |
|
| 236 | - $post_type |
|
| 237 | - ), |
|
| 238 | - $post_type, |
|
| 239 | - $plural_slug |
|
| 240 | - ), |
|
| 241 | - 'capability_type' => 'post', |
|
| 242 | - 'map_meta_cap' => true, |
|
| 243 | - 'has_archive' => true, |
|
| 244 | - 'hierarchical' => false, |
|
| 245 | - 'menu_position' => null, |
|
| 246 | - 'supports' => array( |
|
| 247 | - 'title', |
|
| 248 | - 'editor', |
|
| 249 | - 'author', |
|
| 250 | - 'thumbnail', |
|
| 251 | - 'excerpt', |
|
| 252 | - 'custom-fields', |
|
| 253 | - 'comments', |
|
| 254 | - ), |
|
| 255 | - ); |
|
| 256 | - } |
|
| 212 | + /** |
|
| 213 | + * @param array $labels |
|
| 214 | + * @param string $post_type |
|
| 215 | + * @param string $plural_slug |
|
| 216 | + * @return array |
|
| 217 | + */ |
|
| 218 | + private function getDefaultArguments(array $labels, $post_type, $plural_slug) |
|
| 219 | + { |
|
| 220 | + return array( |
|
| 221 | + 'labels' => $labels, |
|
| 222 | + 'public' => true, |
|
| 223 | + 'publicly_queryable' => true, |
|
| 224 | + 'show_ui' => false, |
|
| 225 | + 'show_ee_ui' => true, |
|
| 226 | + 'show_in_menu' => false, |
|
| 227 | + 'show_in_nav_menus' => false, |
|
| 228 | + 'query_var' => true, |
|
| 229 | + 'rewrite' => apply_filters( |
|
| 230 | + 'FHEE__EventEspresso_core_domain_entities_custom_post_types_RegisterCustomPostTypes__getDefaultArguments__rewrite', |
|
| 231 | + // legacy filter applied for now, |
|
| 232 | + // later on we'll run a has_filter($tag) check and throw a doing_it_wrong() notice |
|
| 233 | + apply_filters( |
|
| 234 | + 'FHEE__EE_Register_CPTs__register_CPT__rewrite', |
|
| 235 | + array('slug' => $plural_slug), |
|
| 236 | + $post_type |
|
| 237 | + ), |
|
| 238 | + $post_type, |
|
| 239 | + $plural_slug |
|
| 240 | + ), |
|
| 241 | + 'capability_type' => 'post', |
|
| 242 | + 'map_meta_cap' => true, |
|
| 243 | + 'has_archive' => true, |
|
| 244 | + 'hierarchical' => false, |
|
| 245 | + 'menu_position' => null, |
|
| 246 | + 'supports' => array( |
|
| 247 | + 'title', |
|
| 248 | + 'editor', |
|
| 249 | + 'author', |
|
| 250 | + 'thumbnail', |
|
| 251 | + 'excerpt', |
|
| 252 | + 'custom-fields', |
|
| 253 | + 'comments', |
|
| 254 | + ), |
|
| 255 | + ); |
|
| 256 | + } |
|
| 257 | 257 | } |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | * |
| 157 | 157 | * @access public |
| 158 | 158 | * @param mixed $identifier |
| 159 | - * @return mixed |
|
| 159 | + * @return boolean |
|
| 160 | 160 | */ |
| 161 | 161 | public function get($identifier) |
| 162 | 162 | { |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | * advances pointer to the provided object |
| 279 | 279 | * |
| 280 | 280 | * @access public |
| 281 | - * @param $object |
|
| 281 | + * @param \EventEspresso\core\libraries\form_sections\form_handlers\SequentialStepForm $object |
|
| 282 | 282 | * @return boolean |
| 283 | 283 | */ |
| 284 | 284 | public function setCurrentUsingObject($object) |
@@ -316,7 +316,7 @@ discard block |
||
| 316 | 316 | * |
| 317 | 317 | * @see http://stackoverflow.com/a/8736013 |
| 318 | 318 | * @param $object |
| 319 | - * @return boolean|int|string |
|
| 319 | + * @return integer |
|
| 320 | 320 | */ |
| 321 | 321 | public function indexOf($object) |
| 322 | 322 | { |
@@ -21,471 +21,471 @@ |
||
| 21 | 21 | class Collection extends SplObjectStorage implements CollectionInterface |
| 22 | 22 | { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * a unique string for identifying this collection |
|
| 26 | - * |
|
| 27 | - * @type string $collection_identifier |
|
| 28 | - */ |
|
| 29 | - protected $collection_identifier; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * an interface (or class) name to be used for restricting the type of objects added to the storage |
|
| 33 | - * this should be set from within the child class constructor |
|
| 34 | - * |
|
| 35 | - * @type string $interface |
|
| 36 | - */ |
|
| 37 | - protected $collection_interface; |
|
| 38 | - |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Collection constructor |
|
| 42 | - * |
|
| 43 | - * @param string $collection_interface |
|
| 44 | - * @throws InvalidInterfaceException |
|
| 45 | - */ |
|
| 46 | - public function __construct($collection_interface) |
|
| 47 | - { |
|
| 48 | - $this->setCollectionInterface($collection_interface); |
|
| 49 | - $this->setCollectionIdentifier(); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @return string |
|
| 55 | - */ |
|
| 56 | - public function collectionIdentifier() |
|
| 57 | - { |
|
| 58 | - return $this->collection_identifier; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * creates a very readable unique 9 character identifier like: CF2-532-DAC |
|
| 64 | - * and appends it to the non-qualified class name, ex: ThingCollection-CF2-532-DAC |
|
| 65 | - * |
|
| 66 | - * @return void |
|
| 67 | - */ |
|
| 68 | - protected function setCollectionIdentifier() |
|
| 69 | - { |
|
| 70 | - // hash a few collection details |
|
| 71 | - $identifier = md5(spl_object_hash($this) . $this->collection_interface . time()); |
|
| 72 | - // grab a few characters from the start, middle, and end of the hash |
|
| 73 | - $id = array(); |
|
| 74 | - for ($x = 0; $x < 19; $x += 9) { |
|
| 75 | - $id[] = substr($identifier, $x, 3); |
|
| 76 | - } |
|
| 77 | - $identifier = basename(str_replace('\\', '/', get_class($this))); |
|
| 78 | - $identifier .= '-' . strtoupper(implode('-', $id)); |
|
| 79 | - $this->collection_identifier = $identifier; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * setCollectionInterface |
|
| 85 | - * |
|
| 86 | - * @access protected |
|
| 87 | - * @param string $collection_interface |
|
| 88 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 89 | - */ |
|
| 90 | - protected function setCollectionInterface($collection_interface) |
|
| 91 | - { |
|
| 92 | - if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
| 93 | - throw new InvalidInterfaceException($collection_interface); |
|
| 94 | - } |
|
| 95 | - $this->collection_interface = $collection_interface; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * add |
|
| 101 | - * attaches an object to the Collection |
|
| 102 | - * and sets any supplied data associated with the current iterator entry |
|
| 103 | - * by calling EE_Object_Collection::set_identifier() |
|
| 104 | - * |
|
| 105 | - * @access public |
|
| 106 | - * @param $object |
|
| 107 | - * @param mixed $identifier |
|
| 108 | - * @return bool |
|
| 109 | - * @throws InvalidEntityException |
|
| 110 | - * @throws DuplicateCollectionIdentifierException |
|
| 111 | - */ |
|
| 112 | - public function add($object, $identifier = null) |
|
| 113 | - { |
|
| 114 | - if (! $object instanceof $this->collection_interface) { |
|
| 115 | - throw new InvalidEntityException($object, $this->collection_interface); |
|
| 116 | - } |
|
| 117 | - if ($this->contains($object)) { |
|
| 118 | - throw new DuplicateCollectionIdentifierException($identifier); |
|
| 119 | - } |
|
| 120 | - $this->attach($object); |
|
| 121 | - $this->setIdentifier($object, $identifier); |
|
| 122 | - return $this->contains($object); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * setIdentifier |
|
| 128 | - * Sets the data associated with an object in the Collection |
|
| 129 | - * if no $identifier is supplied, then the spl_object_hash() is used |
|
| 130 | - * |
|
| 131 | - * @access public |
|
| 132 | - * @param $object |
|
| 133 | - * @param mixed $identifier |
|
| 134 | - * @return bool |
|
| 135 | - */ |
|
| 136 | - public function setIdentifier($object, $identifier = null) |
|
| 137 | - { |
|
| 138 | - $identifier = ! empty($identifier) |
|
| 139 | - ? $identifier |
|
| 140 | - : spl_object_hash($object); |
|
| 141 | - $this->rewind(); |
|
| 142 | - while ($this->valid()) { |
|
| 143 | - if ($object === $this->current()) { |
|
| 144 | - $this->setInfo($identifier); |
|
| 145 | - $this->rewind(); |
|
| 146 | - return true; |
|
| 147 | - } |
|
| 148 | - $this->next(); |
|
| 149 | - } |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * get |
|
| 156 | - * finds and returns an object in the Collection based on the identifier that was set using addObject() |
|
| 157 | - * PLZ NOTE: the pointer is reset to the beginning of the collection before returning |
|
| 158 | - * |
|
| 159 | - * @access public |
|
| 160 | - * @param mixed $identifier |
|
| 161 | - * @return mixed |
|
| 162 | - */ |
|
| 163 | - public function get($identifier) |
|
| 164 | - { |
|
| 165 | - $this->rewind(); |
|
| 166 | - while ($this->valid()) { |
|
| 167 | - if ($identifier === $this->getInfo()) { |
|
| 168 | - $object = $this->current(); |
|
| 169 | - $this->rewind(); |
|
| 170 | - return $object; |
|
| 171 | - } |
|
| 172 | - $this->next(); |
|
| 173 | - } |
|
| 174 | - return null; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * has |
|
| 180 | - * returns TRUE or FALSE |
|
| 181 | - * depending on whether the object is within the Collection |
|
| 182 | - * based on the supplied $identifier |
|
| 183 | - * |
|
| 184 | - * @access public |
|
| 185 | - * @param mixed $identifier |
|
| 186 | - * @return bool |
|
| 187 | - */ |
|
| 188 | - public function has($identifier) |
|
| 189 | - { |
|
| 190 | - $this->rewind(); |
|
| 191 | - while ($this->valid()) { |
|
| 192 | - if ($identifier === $this->getInfo()) { |
|
| 193 | - $this->rewind(); |
|
| 194 | - return true; |
|
| 195 | - } |
|
| 196 | - $this->next(); |
|
| 197 | - } |
|
| 198 | - return false; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * hasObject |
|
| 204 | - * returns TRUE or FALSE depending on whether the supplied object is within the Collection |
|
| 205 | - * |
|
| 206 | - * @access public |
|
| 207 | - * @param $object |
|
| 208 | - * @return bool |
|
| 209 | - */ |
|
| 210 | - public function hasObject($object) |
|
| 211 | - { |
|
| 212 | - return $this->contains($object); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * hasObjects |
|
| 218 | - * returns true if there are objects within the Collection, and false if it is empty |
|
| 219 | - * |
|
| 220 | - * @access public |
|
| 221 | - * @return bool |
|
| 222 | - */ |
|
| 223 | - public function hasObjects() |
|
| 224 | - { |
|
| 225 | - return $this->count() !== 0; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * isEmpty |
|
| 231 | - * returns true if there are no objects within the Collection, and false if there are |
|
| 232 | - * |
|
| 233 | - * @access public |
|
| 234 | - * @return bool |
|
| 235 | - */ |
|
| 236 | - public function isEmpty() |
|
| 237 | - { |
|
| 238 | - return $this->count() === 0; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * remove |
|
| 244 | - * detaches an object from the Collection |
|
| 245 | - * |
|
| 246 | - * @access public |
|
| 247 | - * @param $object |
|
| 248 | - * @return bool |
|
| 249 | - */ |
|
| 250 | - public function remove($object) |
|
| 251 | - { |
|
| 252 | - $this->detach($object); |
|
| 253 | - return true; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * setCurrent |
|
| 259 | - * advances pointer to the object whose identifier matches that which was provided |
|
| 260 | - * |
|
| 261 | - * @access public |
|
| 262 | - * @param mixed $identifier |
|
| 263 | - * @return boolean |
|
| 264 | - */ |
|
| 265 | - public function setCurrent($identifier) |
|
| 266 | - { |
|
| 267 | - $this->rewind(); |
|
| 268 | - while ($this->valid()) { |
|
| 269 | - if ($identifier === $this->getInfo()) { |
|
| 270 | - return true; |
|
| 271 | - } |
|
| 272 | - $this->next(); |
|
| 273 | - } |
|
| 274 | - return false; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * setCurrentUsingObject |
|
| 280 | - * advances pointer to the provided object |
|
| 281 | - * |
|
| 282 | - * @access public |
|
| 283 | - * @param $object |
|
| 284 | - * @return boolean |
|
| 285 | - */ |
|
| 286 | - public function setCurrentUsingObject($object) |
|
| 287 | - { |
|
| 288 | - $this->rewind(); |
|
| 289 | - while ($this->valid()) { |
|
| 290 | - if ($this->current() === $object) { |
|
| 291 | - return true; |
|
| 292 | - } |
|
| 293 | - $this->next(); |
|
| 294 | - } |
|
| 295 | - return false; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Returns the object occupying the index before the current object, |
|
| 301 | - * unless this is already the first object, in which case it just returns the first object |
|
| 302 | - * |
|
| 303 | - * @return mixed |
|
| 304 | - */ |
|
| 305 | - public function previous() |
|
| 306 | - { |
|
| 307 | - $index = $this->indexOf($this->current()); |
|
| 308 | - if ($index === 0) { |
|
| 309 | - return $this->current(); |
|
| 310 | - } |
|
| 311 | - $index--; |
|
| 312 | - return $this->objectAtIndex($index); |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Returns the index of a given object, or false if not found |
|
| 318 | - * |
|
| 319 | - * @see http://stackoverflow.com/a/8736013 |
|
| 320 | - * @param $object |
|
| 321 | - * @return boolean|int|string |
|
| 322 | - */ |
|
| 323 | - public function indexOf($object) |
|
| 324 | - { |
|
| 325 | - if (! $this->contains($object)) { |
|
| 326 | - return false; |
|
| 327 | - } |
|
| 328 | - foreach ($this as $index => $obj) { |
|
| 329 | - if ($obj === $object) { |
|
| 330 | - return $index; |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | - return false; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Returns the object at the given index |
|
| 339 | - * |
|
| 340 | - * @see http://stackoverflow.com/a/8736013 |
|
| 341 | - * @param int $index |
|
| 342 | - * @return mixed |
|
| 343 | - */ |
|
| 344 | - public function objectAtIndex($index) |
|
| 345 | - { |
|
| 346 | - $iterator = new LimitIterator($this, $index, 1); |
|
| 347 | - $iterator->rewind(); |
|
| 348 | - return $iterator->current(); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Returns the sequence of objects as specified by the offset and length |
|
| 354 | - * |
|
| 355 | - * @see http://stackoverflow.com/a/8736013 |
|
| 356 | - * @param int $offset |
|
| 357 | - * @param int $length |
|
| 358 | - * @return array |
|
| 359 | - */ |
|
| 360 | - public function slice($offset, $length) |
|
| 361 | - { |
|
| 362 | - $slice = array(); |
|
| 363 | - $iterator = new LimitIterator($this, $offset, $length); |
|
| 364 | - foreach ($iterator as $object) { |
|
| 365 | - $slice[] = $object; |
|
| 366 | - } |
|
| 367 | - return $slice; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Inserts an object at a certain point |
|
| 373 | - * |
|
| 374 | - * @see http://stackoverflow.com/a/8736013 |
|
| 375 | - * @param mixed $object A single object |
|
| 376 | - * @param int $index |
|
| 377 | - * @param mixed $identifier |
|
| 378 | - * @return bool |
|
| 379 | - * @throws DuplicateCollectionIdentifierException |
|
| 380 | - * @throws InvalidEntityException |
|
| 381 | - */ |
|
| 382 | - public function insertObjectAt($object, $index, $identifier = null) |
|
| 383 | - { |
|
| 384 | - // check to ensure that objects don't already exist in the collection |
|
| 385 | - if ($this->has($identifier)) { |
|
| 386 | - throw new DuplicateCollectionIdentifierException($identifier); |
|
| 387 | - } |
|
| 388 | - // detach any objects at or past this index |
|
| 389 | - $remaining_objects = array(); |
|
| 390 | - if ($index < $this->count()) { |
|
| 391 | - $remaining_objects = $this->slice($index, $this->count() - $index); |
|
| 392 | - foreach ($remaining_objects as $key => $remaining_object) { |
|
| 393 | - // we need to grab the identifiers for each object and use them as keys |
|
| 394 | - $remaining_objects[ $remaining_object->getInfo() ] = $remaining_object; |
|
| 395 | - // and then remove the object from the current tracking array |
|
| 396 | - unset($remaining_objects[ $key ]); |
|
| 397 | - // and then remove it from the Collection |
|
| 398 | - $this->detach($remaining_object); |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - // add the new object we're splicing in |
|
| 402 | - $this->add($object, $identifier); |
|
| 403 | - // attach the objects we previously detached |
|
| 404 | - foreach ($remaining_objects as $key => $remaining_object) { |
|
| 405 | - $this->add($remaining_object, $key); |
|
| 406 | - } |
|
| 407 | - return $this->contains($object); |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * Inserts an object (or an array of objects) at a certain point |
|
| 413 | - * |
|
| 414 | - * @see http://stackoverflow.com/a/8736013 |
|
| 415 | - * @param mixed $objects A single object or an array of objects |
|
| 416 | - * @param int $index |
|
| 417 | - */ |
|
| 418 | - public function insertAt($objects, $index) |
|
| 419 | - { |
|
| 420 | - if (! is_array($objects)) { |
|
| 421 | - $objects = array($objects); |
|
| 422 | - } |
|
| 423 | - // check to ensure that objects don't already exist in the collection |
|
| 424 | - foreach ($objects as $key => $object) { |
|
| 425 | - if ($this->contains($object)) { |
|
| 426 | - unset($objects[ $key ]); |
|
| 427 | - } |
|
| 428 | - } |
|
| 429 | - // do we have any objects left? |
|
| 430 | - if (! $objects) { |
|
| 431 | - return; |
|
| 432 | - } |
|
| 433 | - // detach any objects at or past this index |
|
| 434 | - $remaining = array(); |
|
| 435 | - if ($index < $this->count()) { |
|
| 436 | - $remaining = $this->slice($index, $this->count() - $index); |
|
| 437 | - foreach ($remaining as $object) { |
|
| 438 | - $this->detach($object); |
|
| 439 | - } |
|
| 440 | - } |
|
| 441 | - // add the new objects we're splicing in |
|
| 442 | - foreach ($objects as $object) { |
|
| 443 | - $this->attach($object); |
|
| 444 | - } |
|
| 445 | - // attach the objects we previously detached |
|
| 446 | - foreach ($remaining as $object) { |
|
| 447 | - $this->attach($object); |
|
| 448 | - } |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Removes the object at the given index |
|
| 454 | - * |
|
| 455 | - * @see http://stackoverflow.com/a/8736013 |
|
| 456 | - * @param int $index |
|
| 457 | - */ |
|
| 458 | - public function removeAt($index) |
|
| 459 | - { |
|
| 460 | - $this->detach($this->objectAtIndex($index)); |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * detaches ALL objects from the Collection |
|
| 466 | - */ |
|
| 467 | - public function detachAll() |
|
| 468 | - { |
|
| 469 | - $this->rewind(); |
|
| 470 | - while ($this->valid()) { |
|
| 471 | - $object = $this->current(); |
|
| 472 | - $this->next(); |
|
| 473 | - $this->detach($object); |
|
| 474 | - } |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * unsets and detaches ALL objects from the Collection |
|
| 480 | - */ |
|
| 481 | - public function trashAndDetachAll() |
|
| 482 | - { |
|
| 483 | - $this->rewind(); |
|
| 484 | - while ($this->valid()) { |
|
| 485 | - $object = $this->current(); |
|
| 486 | - $this->next(); |
|
| 487 | - $this->detach($object); |
|
| 488 | - unset($object); |
|
| 489 | - } |
|
| 490 | - } |
|
| 24 | + /** |
|
| 25 | + * a unique string for identifying this collection |
|
| 26 | + * |
|
| 27 | + * @type string $collection_identifier |
|
| 28 | + */ |
|
| 29 | + protected $collection_identifier; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * an interface (or class) name to be used for restricting the type of objects added to the storage |
|
| 33 | + * this should be set from within the child class constructor |
|
| 34 | + * |
|
| 35 | + * @type string $interface |
|
| 36 | + */ |
|
| 37 | + protected $collection_interface; |
|
| 38 | + |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Collection constructor |
|
| 42 | + * |
|
| 43 | + * @param string $collection_interface |
|
| 44 | + * @throws InvalidInterfaceException |
|
| 45 | + */ |
|
| 46 | + public function __construct($collection_interface) |
|
| 47 | + { |
|
| 48 | + $this->setCollectionInterface($collection_interface); |
|
| 49 | + $this->setCollectionIdentifier(); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @return string |
|
| 55 | + */ |
|
| 56 | + public function collectionIdentifier() |
|
| 57 | + { |
|
| 58 | + return $this->collection_identifier; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * creates a very readable unique 9 character identifier like: CF2-532-DAC |
|
| 64 | + * and appends it to the non-qualified class name, ex: ThingCollection-CF2-532-DAC |
|
| 65 | + * |
|
| 66 | + * @return void |
|
| 67 | + */ |
|
| 68 | + protected function setCollectionIdentifier() |
|
| 69 | + { |
|
| 70 | + // hash a few collection details |
|
| 71 | + $identifier = md5(spl_object_hash($this) . $this->collection_interface . time()); |
|
| 72 | + // grab a few characters from the start, middle, and end of the hash |
|
| 73 | + $id = array(); |
|
| 74 | + for ($x = 0; $x < 19; $x += 9) { |
|
| 75 | + $id[] = substr($identifier, $x, 3); |
|
| 76 | + } |
|
| 77 | + $identifier = basename(str_replace('\\', '/', get_class($this))); |
|
| 78 | + $identifier .= '-' . strtoupper(implode('-', $id)); |
|
| 79 | + $this->collection_identifier = $identifier; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * setCollectionInterface |
|
| 85 | + * |
|
| 86 | + * @access protected |
|
| 87 | + * @param string $collection_interface |
|
| 88 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 89 | + */ |
|
| 90 | + protected function setCollectionInterface($collection_interface) |
|
| 91 | + { |
|
| 92 | + if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
| 93 | + throw new InvalidInterfaceException($collection_interface); |
|
| 94 | + } |
|
| 95 | + $this->collection_interface = $collection_interface; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * add |
|
| 101 | + * attaches an object to the Collection |
|
| 102 | + * and sets any supplied data associated with the current iterator entry |
|
| 103 | + * by calling EE_Object_Collection::set_identifier() |
|
| 104 | + * |
|
| 105 | + * @access public |
|
| 106 | + * @param $object |
|
| 107 | + * @param mixed $identifier |
|
| 108 | + * @return bool |
|
| 109 | + * @throws InvalidEntityException |
|
| 110 | + * @throws DuplicateCollectionIdentifierException |
|
| 111 | + */ |
|
| 112 | + public function add($object, $identifier = null) |
|
| 113 | + { |
|
| 114 | + if (! $object instanceof $this->collection_interface) { |
|
| 115 | + throw new InvalidEntityException($object, $this->collection_interface); |
|
| 116 | + } |
|
| 117 | + if ($this->contains($object)) { |
|
| 118 | + throw new DuplicateCollectionIdentifierException($identifier); |
|
| 119 | + } |
|
| 120 | + $this->attach($object); |
|
| 121 | + $this->setIdentifier($object, $identifier); |
|
| 122 | + return $this->contains($object); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * setIdentifier |
|
| 128 | + * Sets the data associated with an object in the Collection |
|
| 129 | + * if no $identifier is supplied, then the spl_object_hash() is used |
|
| 130 | + * |
|
| 131 | + * @access public |
|
| 132 | + * @param $object |
|
| 133 | + * @param mixed $identifier |
|
| 134 | + * @return bool |
|
| 135 | + */ |
|
| 136 | + public function setIdentifier($object, $identifier = null) |
|
| 137 | + { |
|
| 138 | + $identifier = ! empty($identifier) |
|
| 139 | + ? $identifier |
|
| 140 | + : spl_object_hash($object); |
|
| 141 | + $this->rewind(); |
|
| 142 | + while ($this->valid()) { |
|
| 143 | + if ($object === $this->current()) { |
|
| 144 | + $this->setInfo($identifier); |
|
| 145 | + $this->rewind(); |
|
| 146 | + return true; |
|
| 147 | + } |
|
| 148 | + $this->next(); |
|
| 149 | + } |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * get |
|
| 156 | + * finds and returns an object in the Collection based on the identifier that was set using addObject() |
|
| 157 | + * PLZ NOTE: the pointer is reset to the beginning of the collection before returning |
|
| 158 | + * |
|
| 159 | + * @access public |
|
| 160 | + * @param mixed $identifier |
|
| 161 | + * @return mixed |
|
| 162 | + */ |
|
| 163 | + public function get($identifier) |
|
| 164 | + { |
|
| 165 | + $this->rewind(); |
|
| 166 | + while ($this->valid()) { |
|
| 167 | + if ($identifier === $this->getInfo()) { |
|
| 168 | + $object = $this->current(); |
|
| 169 | + $this->rewind(); |
|
| 170 | + return $object; |
|
| 171 | + } |
|
| 172 | + $this->next(); |
|
| 173 | + } |
|
| 174 | + return null; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * has |
|
| 180 | + * returns TRUE or FALSE |
|
| 181 | + * depending on whether the object is within the Collection |
|
| 182 | + * based on the supplied $identifier |
|
| 183 | + * |
|
| 184 | + * @access public |
|
| 185 | + * @param mixed $identifier |
|
| 186 | + * @return bool |
|
| 187 | + */ |
|
| 188 | + public function has($identifier) |
|
| 189 | + { |
|
| 190 | + $this->rewind(); |
|
| 191 | + while ($this->valid()) { |
|
| 192 | + if ($identifier === $this->getInfo()) { |
|
| 193 | + $this->rewind(); |
|
| 194 | + return true; |
|
| 195 | + } |
|
| 196 | + $this->next(); |
|
| 197 | + } |
|
| 198 | + return false; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * hasObject |
|
| 204 | + * returns TRUE or FALSE depending on whether the supplied object is within the Collection |
|
| 205 | + * |
|
| 206 | + * @access public |
|
| 207 | + * @param $object |
|
| 208 | + * @return bool |
|
| 209 | + */ |
|
| 210 | + public function hasObject($object) |
|
| 211 | + { |
|
| 212 | + return $this->contains($object); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * hasObjects |
|
| 218 | + * returns true if there are objects within the Collection, and false if it is empty |
|
| 219 | + * |
|
| 220 | + * @access public |
|
| 221 | + * @return bool |
|
| 222 | + */ |
|
| 223 | + public function hasObjects() |
|
| 224 | + { |
|
| 225 | + return $this->count() !== 0; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * isEmpty |
|
| 231 | + * returns true if there are no objects within the Collection, and false if there are |
|
| 232 | + * |
|
| 233 | + * @access public |
|
| 234 | + * @return bool |
|
| 235 | + */ |
|
| 236 | + public function isEmpty() |
|
| 237 | + { |
|
| 238 | + return $this->count() === 0; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * remove |
|
| 244 | + * detaches an object from the Collection |
|
| 245 | + * |
|
| 246 | + * @access public |
|
| 247 | + * @param $object |
|
| 248 | + * @return bool |
|
| 249 | + */ |
|
| 250 | + public function remove($object) |
|
| 251 | + { |
|
| 252 | + $this->detach($object); |
|
| 253 | + return true; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * setCurrent |
|
| 259 | + * advances pointer to the object whose identifier matches that which was provided |
|
| 260 | + * |
|
| 261 | + * @access public |
|
| 262 | + * @param mixed $identifier |
|
| 263 | + * @return boolean |
|
| 264 | + */ |
|
| 265 | + public function setCurrent($identifier) |
|
| 266 | + { |
|
| 267 | + $this->rewind(); |
|
| 268 | + while ($this->valid()) { |
|
| 269 | + if ($identifier === $this->getInfo()) { |
|
| 270 | + return true; |
|
| 271 | + } |
|
| 272 | + $this->next(); |
|
| 273 | + } |
|
| 274 | + return false; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * setCurrentUsingObject |
|
| 280 | + * advances pointer to the provided object |
|
| 281 | + * |
|
| 282 | + * @access public |
|
| 283 | + * @param $object |
|
| 284 | + * @return boolean |
|
| 285 | + */ |
|
| 286 | + public function setCurrentUsingObject($object) |
|
| 287 | + { |
|
| 288 | + $this->rewind(); |
|
| 289 | + while ($this->valid()) { |
|
| 290 | + if ($this->current() === $object) { |
|
| 291 | + return true; |
|
| 292 | + } |
|
| 293 | + $this->next(); |
|
| 294 | + } |
|
| 295 | + return false; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Returns the object occupying the index before the current object, |
|
| 301 | + * unless this is already the first object, in which case it just returns the first object |
|
| 302 | + * |
|
| 303 | + * @return mixed |
|
| 304 | + */ |
|
| 305 | + public function previous() |
|
| 306 | + { |
|
| 307 | + $index = $this->indexOf($this->current()); |
|
| 308 | + if ($index === 0) { |
|
| 309 | + return $this->current(); |
|
| 310 | + } |
|
| 311 | + $index--; |
|
| 312 | + return $this->objectAtIndex($index); |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Returns the index of a given object, or false if not found |
|
| 318 | + * |
|
| 319 | + * @see http://stackoverflow.com/a/8736013 |
|
| 320 | + * @param $object |
|
| 321 | + * @return boolean|int|string |
|
| 322 | + */ |
|
| 323 | + public function indexOf($object) |
|
| 324 | + { |
|
| 325 | + if (! $this->contains($object)) { |
|
| 326 | + return false; |
|
| 327 | + } |
|
| 328 | + foreach ($this as $index => $obj) { |
|
| 329 | + if ($obj === $object) { |
|
| 330 | + return $index; |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + return false; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Returns the object at the given index |
|
| 339 | + * |
|
| 340 | + * @see http://stackoverflow.com/a/8736013 |
|
| 341 | + * @param int $index |
|
| 342 | + * @return mixed |
|
| 343 | + */ |
|
| 344 | + public function objectAtIndex($index) |
|
| 345 | + { |
|
| 346 | + $iterator = new LimitIterator($this, $index, 1); |
|
| 347 | + $iterator->rewind(); |
|
| 348 | + return $iterator->current(); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Returns the sequence of objects as specified by the offset and length |
|
| 354 | + * |
|
| 355 | + * @see http://stackoverflow.com/a/8736013 |
|
| 356 | + * @param int $offset |
|
| 357 | + * @param int $length |
|
| 358 | + * @return array |
|
| 359 | + */ |
|
| 360 | + public function slice($offset, $length) |
|
| 361 | + { |
|
| 362 | + $slice = array(); |
|
| 363 | + $iterator = new LimitIterator($this, $offset, $length); |
|
| 364 | + foreach ($iterator as $object) { |
|
| 365 | + $slice[] = $object; |
|
| 366 | + } |
|
| 367 | + return $slice; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Inserts an object at a certain point |
|
| 373 | + * |
|
| 374 | + * @see http://stackoverflow.com/a/8736013 |
|
| 375 | + * @param mixed $object A single object |
|
| 376 | + * @param int $index |
|
| 377 | + * @param mixed $identifier |
|
| 378 | + * @return bool |
|
| 379 | + * @throws DuplicateCollectionIdentifierException |
|
| 380 | + * @throws InvalidEntityException |
|
| 381 | + */ |
|
| 382 | + public function insertObjectAt($object, $index, $identifier = null) |
|
| 383 | + { |
|
| 384 | + // check to ensure that objects don't already exist in the collection |
|
| 385 | + if ($this->has($identifier)) { |
|
| 386 | + throw new DuplicateCollectionIdentifierException($identifier); |
|
| 387 | + } |
|
| 388 | + // detach any objects at or past this index |
|
| 389 | + $remaining_objects = array(); |
|
| 390 | + if ($index < $this->count()) { |
|
| 391 | + $remaining_objects = $this->slice($index, $this->count() - $index); |
|
| 392 | + foreach ($remaining_objects as $key => $remaining_object) { |
|
| 393 | + // we need to grab the identifiers for each object and use them as keys |
|
| 394 | + $remaining_objects[ $remaining_object->getInfo() ] = $remaining_object; |
|
| 395 | + // and then remove the object from the current tracking array |
|
| 396 | + unset($remaining_objects[ $key ]); |
|
| 397 | + // and then remove it from the Collection |
|
| 398 | + $this->detach($remaining_object); |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + // add the new object we're splicing in |
|
| 402 | + $this->add($object, $identifier); |
|
| 403 | + // attach the objects we previously detached |
|
| 404 | + foreach ($remaining_objects as $key => $remaining_object) { |
|
| 405 | + $this->add($remaining_object, $key); |
|
| 406 | + } |
|
| 407 | + return $this->contains($object); |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * Inserts an object (or an array of objects) at a certain point |
|
| 413 | + * |
|
| 414 | + * @see http://stackoverflow.com/a/8736013 |
|
| 415 | + * @param mixed $objects A single object or an array of objects |
|
| 416 | + * @param int $index |
|
| 417 | + */ |
|
| 418 | + public function insertAt($objects, $index) |
|
| 419 | + { |
|
| 420 | + if (! is_array($objects)) { |
|
| 421 | + $objects = array($objects); |
|
| 422 | + } |
|
| 423 | + // check to ensure that objects don't already exist in the collection |
|
| 424 | + foreach ($objects as $key => $object) { |
|
| 425 | + if ($this->contains($object)) { |
|
| 426 | + unset($objects[ $key ]); |
|
| 427 | + } |
|
| 428 | + } |
|
| 429 | + // do we have any objects left? |
|
| 430 | + if (! $objects) { |
|
| 431 | + return; |
|
| 432 | + } |
|
| 433 | + // detach any objects at or past this index |
|
| 434 | + $remaining = array(); |
|
| 435 | + if ($index < $this->count()) { |
|
| 436 | + $remaining = $this->slice($index, $this->count() - $index); |
|
| 437 | + foreach ($remaining as $object) { |
|
| 438 | + $this->detach($object); |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | + // add the new objects we're splicing in |
|
| 442 | + foreach ($objects as $object) { |
|
| 443 | + $this->attach($object); |
|
| 444 | + } |
|
| 445 | + // attach the objects we previously detached |
|
| 446 | + foreach ($remaining as $object) { |
|
| 447 | + $this->attach($object); |
|
| 448 | + } |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Removes the object at the given index |
|
| 454 | + * |
|
| 455 | + * @see http://stackoverflow.com/a/8736013 |
|
| 456 | + * @param int $index |
|
| 457 | + */ |
|
| 458 | + public function removeAt($index) |
|
| 459 | + { |
|
| 460 | + $this->detach($this->objectAtIndex($index)); |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * detaches ALL objects from the Collection |
|
| 466 | + */ |
|
| 467 | + public function detachAll() |
|
| 468 | + { |
|
| 469 | + $this->rewind(); |
|
| 470 | + while ($this->valid()) { |
|
| 471 | + $object = $this->current(); |
|
| 472 | + $this->next(); |
|
| 473 | + $this->detach($object); |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * unsets and detaches ALL objects from the Collection |
|
| 480 | + */ |
|
| 481 | + public function trashAndDetachAll() |
|
| 482 | + { |
|
| 483 | + $this->rewind(); |
|
| 484 | + while ($this->valid()) { |
|
| 485 | + $object = $this->current(); |
|
| 486 | + $this->next(); |
|
| 487 | + $this->detach($object); |
|
| 488 | + unset($object); |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | 491 | } |
@@ -68,14 +68,14 @@ discard block |
||
| 68 | 68 | protected function setCollectionIdentifier() |
| 69 | 69 | { |
| 70 | 70 | // hash a few collection details |
| 71 | - $identifier = md5(spl_object_hash($this) . $this->collection_interface . time()); |
|
| 71 | + $identifier = md5(spl_object_hash($this).$this->collection_interface.time()); |
|
| 72 | 72 | // grab a few characters from the start, middle, and end of the hash |
| 73 | 73 | $id = array(); |
| 74 | 74 | for ($x = 0; $x < 19; $x += 9) { |
| 75 | 75 | $id[] = substr($identifier, $x, 3); |
| 76 | 76 | } |
| 77 | 77 | $identifier = basename(str_replace('\\', '/', get_class($this))); |
| 78 | - $identifier .= '-' . strtoupper(implode('-', $id)); |
|
| 78 | + $identifier .= '-'.strtoupper(implode('-', $id)); |
|
| 79 | 79 | $this->collection_identifier = $identifier; |
| 80 | 80 | } |
| 81 | 81 | |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | */ |
| 90 | 90 | protected function setCollectionInterface($collection_interface) |
| 91 | 91 | { |
| 92 | - if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
| 92 | + if ( ! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
| 93 | 93 | throw new InvalidInterfaceException($collection_interface); |
| 94 | 94 | } |
| 95 | 95 | $this->collection_interface = $collection_interface; |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | */ |
| 112 | 112 | public function add($object, $identifier = null) |
| 113 | 113 | { |
| 114 | - if (! $object instanceof $this->collection_interface) { |
|
| 114 | + if ( ! $object instanceof $this->collection_interface) { |
|
| 115 | 115 | throw new InvalidEntityException($object, $this->collection_interface); |
| 116 | 116 | } |
| 117 | 117 | if ($this->contains($object)) { |
@@ -322,7 +322,7 @@ discard block |
||
| 322 | 322 | */ |
| 323 | 323 | public function indexOf($object) |
| 324 | 324 | { |
| 325 | - if (! $this->contains($object)) { |
|
| 325 | + if ( ! $this->contains($object)) { |
|
| 326 | 326 | return false; |
| 327 | 327 | } |
| 328 | 328 | foreach ($this as $index => $obj) { |
@@ -391,9 +391,9 @@ discard block |
||
| 391 | 391 | $remaining_objects = $this->slice($index, $this->count() - $index); |
| 392 | 392 | foreach ($remaining_objects as $key => $remaining_object) { |
| 393 | 393 | // we need to grab the identifiers for each object and use them as keys |
| 394 | - $remaining_objects[ $remaining_object->getInfo() ] = $remaining_object; |
|
| 394 | + $remaining_objects[$remaining_object->getInfo()] = $remaining_object; |
|
| 395 | 395 | // and then remove the object from the current tracking array |
| 396 | - unset($remaining_objects[ $key ]); |
|
| 396 | + unset($remaining_objects[$key]); |
|
| 397 | 397 | // and then remove it from the Collection |
| 398 | 398 | $this->detach($remaining_object); |
| 399 | 399 | } |
@@ -417,17 +417,17 @@ discard block |
||
| 417 | 417 | */ |
| 418 | 418 | public function insertAt($objects, $index) |
| 419 | 419 | { |
| 420 | - if (! is_array($objects)) { |
|
| 420 | + if ( ! is_array($objects)) { |
|
| 421 | 421 | $objects = array($objects); |
| 422 | 422 | } |
| 423 | 423 | // check to ensure that objects don't already exist in the collection |
| 424 | 424 | foreach ($objects as $key => $object) { |
| 425 | 425 | if ($this->contains($object)) { |
| 426 | - unset($objects[ $key ]); |
|
| 426 | + unset($objects[$key]); |
|
| 427 | 427 | } |
| 428 | 428 | } |
| 429 | 429 | // do we have any objects left? |
| 430 | - if (! $objects) { |
|
| 430 | + if ( ! $objects) { |
|
| 431 | 431 | return; |
| 432 | 432 | } |
| 433 | 433 | // detach any objects at or past this index |
@@ -16,26 +16,26 @@ |
||
| 16 | 16 | class InvalidCollectionIdentifierException extends OutOfBoundsException |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * InvalidCollectionIdentifierException constructor. |
|
| 21 | - * |
|
| 22 | - * @param $identifier |
|
| 23 | - * @param string $message |
|
| 24 | - * @param int $code |
|
| 25 | - * @param Exception|null $previous |
|
| 26 | - */ |
|
| 27 | - public function __construct($identifier, $message = '', $code = 0, Exception $previous = null) |
|
| 28 | - { |
|
| 29 | - if (empty($message)) { |
|
| 30 | - $message = sprintf( |
|
| 31 | - __( |
|
| 32 | - 'The supplied identifier "%1$s" does not exist within this collection. |
|
| 19 | + /** |
|
| 20 | + * InvalidCollectionIdentifierException constructor. |
|
| 21 | + * |
|
| 22 | + * @param $identifier |
|
| 23 | + * @param string $message |
|
| 24 | + * @param int $code |
|
| 25 | + * @param Exception|null $previous |
|
| 26 | + */ |
|
| 27 | + public function __construct($identifier, $message = '', $code = 0, Exception $previous = null) |
|
| 28 | + { |
|
| 29 | + if (empty($message)) { |
|
| 30 | + $message = sprintf( |
|
| 31 | + __( |
|
| 32 | + 'The supplied identifier "%1$s" does not exist within this collection. |
|
| 33 | 33 | You may need to delay adding this asset until the required dependency has been added.', |
| 34 | - 'event_espresso' |
|
| 35 | - ), |
|
| 36 | - $identifier |
|
| 37 | - ); |
|
| 38 | - } |
|
| 39 | - parent::__construct($message, $code, $previous); |
|
| 40 | - } |
|
| 34 | + 'event_espresso' |
|
| 35 | + ), |
|
| 36 | + $identifier |
|
| 37 | + ); |
|
| 38 | + } |
|
| 39 | + parent::__construct($message, $code, $previous); |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -16,25 +16,25 @@ |
||
| 16 | 16 | class DuplicateCollectionIdentifierException extends OutOfRangeException |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * DuplicateCollectionIdentifierException constructor. |
|
| 21 | - * |
|
| 22 | - * @param $identifier |
|
| 23 | - * @param string $message |
|
| 24 | - * @param int $code |
|
| 25 | - * @param Exception|null $previous |
|
| 26 | - */ |
|
| 27 | - public function __construct($identifier, $message = '', $code = 0, Exception $previous = null) |
|
| 28 | - { |
|
| 29 | - if (empty($message)) { |
|
| 30 | - $message = sprintf( |
|
| 31 | - __( |
|
| 32 | - 'The supplied identifier "%1$s" already exists within this collection.', |
|
| 33 | - 'event_espresso' |
|
| 34 | - ), |
|
| 35 | - $identifier |
|
| 36 | - ); |
|
| 37 | - } |
|
| 38 | - parent::__construct($message, $code, $previous); |
|
| 39 | - } |
|
| 19 | + /** |
|
| 20 | + * DuplicateCollectionIdentifierException constructor. |
|
| 21 | + * |
|
| 22 | + * @param $identifier |
|
| 23 | + * @param string $message |
|
| 24 | + * @param int $code |
|
| 25 | + * @param Exception|null $previous |
|
| 26 | + */ |
|
| 27 | + public function __construct($identifier, $message = '', $code = 0, Exception $previous = null) |
|
| 28 | + { |
|
| 29 | + if (empty($message)) { |
|
| 30 | + $message = sprintf( |
|
| 31 | + __( |
|
| 32 | + 'The supplied identifier "%1$s" already exists within this collection.', |
|
| 33 | + 'event_espresso' |
|
| 34 | + ), |
|
| 35 | + $identifier |
|
| 36 | + ); |
|
| 37 | + } |
|
| 38 | + parent::__construct($message, $code, $previous); |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -16,28 +16,28 @@ |
||
| 16 | 16 | class InvalidIdentifierException extends InvalidArgumentException |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * InvalidIdentifierException constructor. |
|
| 21 | - * |
|
| 22 | - * @param string $actual the identifier that was supplied |
|
| 23 | - * @param string $expected example of an acceptable identifier |
|
| 24 | - * @param string $message |
|
| 25 | - * @param int $code |
|
| 26 | - * @param Exception $previous |
|
| 27 | - */ |
|
| 28 | - public function __construct($actual, $expected, $message = '', $code = 0, Exception $previous = null) |
|
| 29 | - { |
|
| 30 | - if (empty($message)) { |
|
| 31 | - $message = sprintf( |
|
| 32 | - __( |
|
| 33 | - 'The supplied identifier "%1$s" is invalid. A value like "%2$s" was expected.', |
|
| 34 | - 'event_espresso' |
|
| 35 | - ), |
|
| 36 | - $actual, |
|
| 37 | - $expected |
|
| 38 | - ); |
|
| 39 | - } |
|
| 40 | - parent::__construct($message, $code, $previous); |
|
| 41 | - } |
|
| 19 | + /** |
|
| 20 | + * InvalidIdentifierException constructor. |
|
| 21 | + * |
|
| 22 | + * @param string $actual the identifier that was supplied |
|
| 23 | + * @param string $expected example of an acceptable identifier |
|
| 24 | + * @param string $message |
|
| 25 | + * @param int $code |
|
| 26 | + * @param Exception $previous |
|
| 27 | + */ |
|
| 28 | + public function __construct($actual, $expected, $message = '', $code = 0, Exception $previous = null) |
|
| 29 | + { |
|
| 30 | + if (empty($message)) { |
|
| 31 | + $message = sprintf( |
|
| 32 | + __( |
|
| 33 | + 'The supplied identifier "%1$s" is invalid. A value like "%2$s" was expected.', |
|
| 34 | + 'event_espresso' |
|
| 35 | + ), |
|
| 36 | + $actual, |
|
| 37 | + $expected |
|
| 38 | + ); |
|
| 39 | + } |
|
| 40 | + parent::__construct($message, $code, $previous); |
|
| 41 | + } |
|
| 42 | 42 | } |
| 43 | 43 | // Location: core/exceptions/InvalidIdentifierException.php |
@@ -16,26 +16,26 @@ |
||
| 16 | 16 | class ManifestFile extends Asset |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Asset constructor. |
|
| 21 | - * |
|
| 22 | - * @param DomainInterface $domain |
|
| 23 | - * @throws InvalidDataTypeException |
|
| 24 | - */ |
|
| 25 | - public function __construct(DomainInterface $domain) |
|
| 26 | - { |
|
| 27 | - parent::__construct(Asset::TYPE_MANIFEST, $domain->assetNamespace(), $domain); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - public function urlBase() |
|
| 32 | - { |
|
| 33 | - return $this->domain->distributionAssetsUrl(); |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - public function filepath() |
|
| 38 | - { |
|
| 39 | - return $this->domain->distributionAssetsPath(); |
|
| 40 | - } |
|
| 19 | + /** |
|
| 20 | + * Asset constructor. |
|
| 21 | + * |
|
| 22 | + * @param DomainInterface $domain |
|
| 23 | + * @throws InvalidDataTypeException |
|
| 24 | + */ |
|
| 25 | + public function __construct(DomainInterface $domain) |
|
| 26 | + { |
|
| 27 | + parent::__construct(Asset::TYPE_MANIFEST, $domain->assetNamespace(), $domain); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + public function urlBase() |
|
| 32 | + { |
|
| 33 | + return $this->domain->distributionAssetsUrl(); |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + public function filepath() |
|
| 38 | + { |
|
| 39 | + return $this->domain->distributionAssetsPath(); |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | */ |
| 83 | 83 | private function setType($type) |
| 84 | 84 | { |
| 85 | - if (! in_array($type, $this->validAssetTypes(), true)) { |
|
| 85 | + if ( ! in_array($type, $this->validAssetTypes(), true)) { |
|
| 86 | 86 | throw new InvalidDataTypeException( |
| 87 | 87 | 'Asset::$type', |
| 88 | 88 | $type, |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | */ |
| 100 | 100 | private function setHandle($handle) |
| 101 | 101 | { |
| 102 | - if (! is_string($handle)) { |
|
| 102 | + if ( ! is_string($handle)) { |
|
| 103 | 103 | throw new InvalidDataTypeException( |
| 104 | 104 | '$handle', |
| 105 | 105 | $handle, |
@@ -16,154 +16,154 @@ |
||
| 16 | 16 | abstract class Asset |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * indicates the file extension for a build distribution CSS file |
|
| 21 | - */ |
|
| 22 | - const FILE_EXTENSION_DISTRIBUTION_CSS = '.dist.css'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * indicates the file extension for a build distribution JS file |
|
| 26 | - */ |
|
| 27 | - const FILE_EXTENSION_DISTRIBUTION_JS = '.dist.js'; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * indicates a Cascading Style Sheet asset |
|
| 31 | - */ |
|
| 32 | - const TYPE_CSS = 'css'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * indicates a Javascript asset |
|
| 36 | - */ |
|
| 37 | - const TYPE_JS = 'js'; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * indicates a Javascript manifest file |
|
| 41 | - */ |
|
| 42 | - const TYPE_MANIFEST = 'manifest'; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @var DomainInterface $domain |
|
| 46 | - */ |
|
| 47 | - protected $domain; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @var string $type |
|
| 51 | - */ |
|
| 52 | - private $type; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @var string $handle |
|
| 56 | - */ |
|
| 57 | - private $handle; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @var bool $registered |
|
| 61 | - */ |
|
| 62 | - private $registered = false; |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Asset constructor. |
|
| 67 | - * |
|
| 68 | - * @param $type |
|
| 69 | - * @param string $handle |
|
| 70 | - * @param DomainInterface $domain |
|
| 71 | - * @throws InvalidDataTypeException |
|
| 72 | - */ |
|
| 73 | - public function __construct($type, $handle, DomainInterface $domain) |
|
| 74 | - { |
|
| 75 | - $this->domain = $domain; |
|
| 76 | - $this->setType($type); |
|
| 77 | - $this->setHandle($handle); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @return array |
|
| 83 | - */ |
|
| 84 | - public function validAssetTypes() |
|
| 85 | - { |
|
| 86 | - return array( |
|
| 87 | - Asset::TYPE_CSS, |
|
| 88 | - Asset::TYPE_JS, |
|
| 89 | - Asset::TYPE_MANIFEST, |
|
| 90 | - ); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @param string $type |
|
| 96 | - * @throws InvalidDataTypeException |
|
| 97 | - */ |
|
| 98 | - private function setType($type) |
|
| 99 | - { |
|
| 100 | - if (! in_array($type, $this->validAssetTypes(), true)) { |
|
| 101 | - throw new InvalidDataTypeException( |
|
| 102 | - 'Asset::$type', |
|
| 103 | - $type, |
|
| 104 | - 'one of the TYPE_* class constants on \EventEspresso\core\domain\values\Asset is required' |
|
| 105 | - ); |
|
| 106 | - } |
|
| 107 | - $this->type = $type; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param string $handle |
|
| 113 | - * @throws InvalidDataTypeException |
|
| 114 | - */ |
|
| 115 | - private function setHandle($handle) |
|
| 116 | - { |
|
| 117 | - if (! is_string($handle)) { |
|
| 118 | - throw new InvalidDataTypeException( |
|
| 119 | - '$handle', |
|
| 120 | - $handle, |
|
| 121 | - 'string' |
|
| 122 | - ); |
|
| 123 | - } |
|
| 124 | - $this->handle = $handle; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @return string |
|
| 130 | - */ |
|
| 131 | - public function assetNamespace() |
|
| 132 | - { |
|
| 133 | - return $this->domain->assetNamespace(); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @return string |
|
| 139 | - */ |
|
| 140 | - public function type() |
|
| 141 | - { |
|
| 142 | - return $this->type; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @return string |
|
| 148 | - */ |
|
| 149 | - public function handle() |
|
| 150 | - { |
|
| 151 | - return $this->handle; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @return bool |
|
| 156 | - */ |
|
| 157 | - public function isRegistered() |
|
| 158 | - { |
|
| 159 | - return $this->registered; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @param bool $registered |
|
| 164 | - */ |
|
| 165 | - public function setRegistered($registered = true) |
|
| 166 | - { |
|
| 167 | - $this->registered = filter_var($registered, FILTER_VALIDATE_BOOLEAN); |
|
| 168 | - } |
|
| 19 | + /** |
|
| 20 | + * indicates the file extension for a build distribution CSS file |
|
| 21 | + */ |
|
| 22 | + const FILE_EXTENSION_DISTRIBUTION_CSS = '.dist.css'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * indicates the file extension for a build distribution JS file |
|
| 26 | + */ |
|
| 27 | + const FILE_EXTENSION_DISTRIBUTION_JS = '.dist.js'; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * indicates a Cascading Style Sheet asset |
|
| 31 | + */ |
|
| 32 | + const TYPE_CSS = 'css'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * indicates a Javascript asset |
|
| 36 | + */ |
|
| 37 | + const TYPE_JS = 'js'; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * indicates a Javascript manifest file |
|
| 41 | + */ |
|
| 42 | + const TYPE_MANIFEST = 'manifest'; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @var DomainInterface $domain |
|
| 46 | + */ |
|
| 47 | + protected $domain; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @var string $type |
|
| 51 | + */ |
|
| 52 | + private $type; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @var string $handle |
|
| 56 | + */ |
|
| 57 | + private $handle; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @var bool $registered |
|
| 61 | + */ |
|
| 62 | + private $registered = false; |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Asset constructor. |
|
| 67 | + * |
|
| 68 | + * @param $type |
|
| 69 | + * @param string $handle |
|
| 70 | + * @param DomainInterface $domain |
|
| 71 | + * @throws InvalidDataTypeException |
|
| 72 | + */ |
|
| 73 | + public function __construct($type, $handle, DomainInterface $domain) |
|
| 74 | + { |
|
| 75 | + $this->domain = $domain; |
|
| 76 | + $this->setType($type); |
|
| 77 | + $this->setHandle($handle); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @return array |
|
| 83 | + */ |
|
| 84 | + public function validAssetTypes() |
|
| 85 | + { |
|
| 86 | + return array( |
|
| 87 | + Asset::TYPE_CSS, |
|
| 88 | + Asset::TYPE_JS, |
|
| 89 | + Asset::TYPE_MANIFEST, |
|
| 90 | + ); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @param string $type |
|
| 96 | + * @throws InvalidDataTypeException |
|
| 97 | + */ |
|
| 98 | + private function setType($type) |
|
| 99 | + { |
|
| 100 | + if (! in_array($type, $this->validAssetTypes(), true)) { |
|
| 101 | + throw new InvalidDataTypeException( |
|
| 102 | + 'Asset::$type', |
|
| 103 | + $type, |
|
| 104 | + 'one of the TYPE_* class constants on \EventEspresso\core\domain\values\Asset is required' |
|
| 105 | + ); |
|
| 106 | + } |
|
| 107 | + $this->type = $type; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param string $handle |
|
| 113 | + * @throws InvalidDataTypeException |
|
| 114 | + */ |
|
| 115 | + private function setHandle($handle) |
|
| 116 | + { |
|
| 117 | + if (! is_string($handle)) { |
|
| 118 | + throw new InvalidDataTypeException( |
|
| 119 | + '$handle', |
|
| 120 | + $handle, |
|
| 121 | + 'string' |
|
| 122 | + ); |
|
| 123 | + } |
|
| 124 | + $this->handle = $handle; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @return string |
|
| 130 | + */ |
|
| 131 | + public function assetNamespace() |
|
| 132 | + { |
|
| 133 | + return $this->domain->assetNamespace(); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @return string |
|
| 139 | + */ |
|
| 140 | + public function type() |
|
| 141 | + { |
|
| 142 | + return $this->type; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @return string |
|
| 148 | + */ |
|
| 149 | + public function handle() |
|
| 150 | + { |
|
| 151 | + return $this->handle; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @return bool |
|
| 156 | + */ |
|
| 157 | + public function isRegistered() |
|
| 158 | + { |
|
| 159 | + return $this->registered; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @param bool $registered |
|
| 164 | + */ |
|
| 165 | + public function setRegistered($registered = true) |
|
| 166 | + { |
|
| 167 | + $this->registered = filter_var($registered, FILTER_VALIDATE_BOOLEAN); |
|
| 168 | + } |
|
| 169 | 169 | } |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | */ |
| 94 | 94 | private function setSource($source) |
| 95 | 95 | { |
| 96 | - if (! is_string($source)) { |
|
| 96 | + if ( ! is_string($source)) { |
|
| 97 | 97 | throw new InvalidDataTypeException( |
| 98 | 98 | '$source', |
| 99 | 99 | $source, |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | */ |
| 127 | 127 | public function setVersion($version = EVENT_ESPRESSO_VERSION) |
| 128 | 128 | { |
| 129 | - if (! is_string($version)) { |
|
| 129 | + if ( ! is_string($version)) { |
|
| 130 | 130 | throw new InvalidDataTypeException( |
| 131 | 131 | '$version', |
| 132 | 132 | $version, |
@@ -16,136 +16,136 @@ |
||
| 16 | 16 | abstract class BrowserAsset extends Asset |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var string $source |
|
| 21 | - */ |
|
| 22 | - private $source; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var array $dependencies |
|
| 26 | - */ |
|
| 27 | - private $dependencies; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var string $version |
|
| 31 | - */ |
|
| 32 | - private $version; |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Asset constructor. |
|
| 37 | - * |
|
| 38 | - * @param string $type |
|
| 39 | - * @param string $handle |
|
| 40 | - * @param string $source |
|
| 41 | - * @param array $dependencies |
|
| 42 | - * @param DomainInterface $domain |
|
| 43 | - * @throws InvalidDataTypeException |
|
| 44 | - */ |
|
| 45 | - public function __construct($type, $handle, $source, array $dependencies, DomainInterface $domain) |
|
| 46 | - { |
|
| 47 | - parent::__construct($type, $handle, $domain); |
|
| 48 | - $this->setSource($source); |
|
| 49 | - $this->setDependencies($dependencies); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @since 4.9.62.p |
|
| 55 | - */ |
|
| 56 | - abstract public function enqueueAsset(); |
|
| 57 | - |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @return array |
|
| 61 | - */ |
|
| 62 | - public function dependencies() |
|
| 63 | - { |
|
| 64 | - return $this->dependencies; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @param array $dependencies |
|
| 70 | - */ |
|
| 71 | - private function setDependencies(array $dependencies) |
|
| 72 | - { |
|
| 73 | - $this->dependencies = $dependencies; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @since 4.9.62.p |
|
| 79 | - * @return bool |
|
| 80 | - */ |
|
| 81 | - public function hasDependencies() |
|
| 82 | - { |
|
| 83 | - return count($this->dependencies) > 0; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @return string |
|
| 89 | - */ |
|
| 90 | - public function source() |
|
| 91 | - { |
|
| 92 | - return $this->source; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @param string $source |
|
| 98 | - * @throws InvalidDataTypeException |
|
| 99 | - */ |
|
| 100 | - private function setSource($source) |
|
| 101 | - { |
|
| 102 | - if (! is_string($source)) { |
|
| 103 | - throw new InvalidDataTypeException( |
|
| 104 | - '$source', |
|
| 105 | - $source, |
|
| 106 | - 'string' |
|
| 107 | - ); |
|
| 108 | - } |
|
| 109 | - $this->source = $source; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @return string |
|
| 115 | - * @throws InvalidDataTypeException |
|
| 116 | - */ |
|
| 117 | - public function version() |
|
| 118 | - { |
|
| 119 | - // if version is NOT set and this asset was NOT built for distribution, |
|
| 120 | - // then set the version equal to the EE core plugin version |
|
| 121 | - if ( |
|
| 122 | - $this->version === null |
|
| 123 | - && ( |
|
| 124 | - substr($this->source, -8) !== Asset::FILE_EXTENSION_DISTRIBUTION_JS |
|
| 125 | - || substr($this->source, -9) !== Asset::FILE_EXTENSION_DISTRIBUTION_CSS |
|
| 126 | - ) |
|
| 127 | - ) { |
|
| 128 | - $this->setVersion(); |
|
| 129 | - } |
|
| 130 | - return $this->version; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @param string $version |
|
| 136 | - * @return BrowserAsset |
|
| 137 | - * @throws InvalidDataTypeException |
|
| 138 | - */ |
|
| 139 | - public function setVersion($version = EVENT_ESPRESSO_VERSION) |
|
| 140 | - { |
|
| 141 | - if (! is_string($version)) { |
|
| 142 | - throw new InvalidDataTypeException( |
|
| 143 | - '$version', |
|
| 144 | - $version, |
|
| 145 | - 'string' |
|
| 146 | - ); |
|
| 147 | - } |
|
| 148 | - $this->version = $version; |
|
| 149 | - return $this; |
|
| 150 | - } |
|
| 19 | + /** |
|
| 20 | + * @var string $source |
|
| 21 | + */ |
|
| 22 | + private $source; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var array $dependencies |
|
| 26 | + */ |
|
| 27 | + private $dependencies; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var string $version |
|
| 31 | + */ |
|
| 32 | + private $version; |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Asset constructor. |
|
| 37 | + * |
|
| 38 | + * @param string $type |
|
| 39 | + * @param string $handle |
|
| 40 | + * @param string $source |
|
| 41 | + * @param array $dependencies |
|
| 42 | + * @param DomainInterface $domain |
|
| 43 | + * @throws InvalidDataTypeException |
|
| 44 | + */ |
|
| 45 | + public function __construct($type, $handle, $source, array $dependencies, DomainInterface $domain) |
|
| 46 | + { |
|
| 47 | + parent::__construct($type, $handle, $domain); |
|
| 48 | + $this->setSource($source); |
|
| 49 | + $this->setDependencies($dependencies); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @since 4.9.62.p |
|
| 55 | + */ |
|
| 56 | + abstract public function enqueueAsset(); |
|
| 57 | + |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @return array |
|
| 61 | + */ |
|
| 62 | + public function dependencies() |
|
| 63 | + { |
|
| 64 | + return $this->dependencies; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @param array $dependencies |
|
| 70 | + */ |
|
| 71 | + private function setDependencies(array $dependencies) |
|
| 72 | + { |
|
| 73 | + $this->dependencies = $dependencies; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @since 4.9.62.p |
|
| 79 | + * @return bool |
|
| 80 | + */ |
|
| 81 | + public function hasDependencies() |
|
| 82 | + { |
|
| 83 | + return count($this->dependencies) > 0; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @return string |
|
| 89 | + */ |
|
| 90 | + public function source() |
|
| 91 | + { |
|
| 92 | + return $this->source; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @param string $source |
|
| 98 | + * @throws InvalidDataTypeException |
|
| 99 | + */ |
|
| 100 | + private function setSource($source) |
|
| 101 | + { |
|
| 102 | + if (! is_string($source)) { |
|
| 103 | + throw new InvalidDataTypeException( |
|
| 104 | + '$source', |
|
| 105 | + $source, |
|
| 106 | + 'string' |
|
| 107 | + ); |
|
| 108 | + } |
|
| 109 | + $this->source = $source; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @return string |
|
| 115 | + * @throws InvalidDataTypeException |
|
| 116 | + */ |
|
| 117 | + public function version() |
|
| 118 | + { |
|
| 119 | + // if version is NOT set and this asset was NOT built for distribution, |
|
| 120 | + // then set the version equal to the EE core plugin version |
|
| 121 | + if ( |
|
| 122 | + $this->version === null |
|
| 123 | + && ( |
|
| 124 | + substr($this->source, -8) !== Asset::FILE_EXTENSION_DISTRIBUTION_JS |
|
| 125 | + || substr($this->source, -9) !== Asset::FILE_EXTENSION_DISTRIBUTION_CSS |
|
| 126 | + ) |
|
| 127 | + ) { |
|
| 128 | + $this->setVersion(); |
|
| 129 | + } |
|
| 130 | + return $this->version; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @param string $version |
|
| 136 | + * @return BrowserAsset |
|
| 137 | + * @throws InvalidDataTypeException |
|
| 138 | + */ |
|
| 139 | + public function setVersion($version = EVENT_ESPRESSO_VERSION) |
|
| 140 | + { |
|
| 141 | + if (! is_string($version)) { |
|
| 142 | + throw new InvalidDataTypeException( |
|
| 143 | + '$version', |
|
| 144 | + $version, |
|
| 145 | + 'string' |
|
| 146 | + ); |
|
| 147 | + } |
|
| 148 | + $this->version = $version; |
|
| 149 | + return $this; |
|
| 150 | + } |
|
| 151 | 151 | } |