|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | +* This file is part of the Altumo library. |
| 5 | +* |
| 6 | +* (c) Steve Sperandeo <[email protected]> |
| 7 | +* (c) Juan Jaramillo <[email protected]> |
| 8 | +* |
| 9 | +* For the full copyright and license information, please view the LICENSE |
| 10 | +* file that was distributed with this source code. |
| 11 | +*/ |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +namespace Altumo\Functions; |
| 17 | + |
| 18 | + |
| 19 | +/** |
| 20 | +* This class contains functions for getting more information and validating |
| 21 | +* function calls. |
| 22 | +* |
| 23 | +* @author Juan Jaramillo <[email protected]> |
| 24 | +*/ |
| 25 | +class Call{ |
| 26 | + |
| 27 | + /** |
| 28 | + * Artificially makes a function either Protected or Private. |
| 29 | + * |
| 30 | + * Ensures a function call came from its parent class or a child class (depending |
| 31 | + * on $assert_as_private). |
| 32 | + * |
| 33 | + * |
| 34 | + * @param array $backtrace // the debug_bracktrace() array |
| 35 | + * // expects a slice of the array containing |
| 36 | + * // the callee first, then the caller. |
| 37 | + * |
| 38 | + * @param bool $assert_as_private // weather to assert as private or protected. |
| 39 | + * |
| 40 | + * |
| 41 | + * @throws Exception // if method is being called from a class that is not |
| 42 | + * // function's parent or one of its children. |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + static protected function assertProtectedOrPrivate( $backtrace, $assert_as_private = false ){ |
| 46 | + |
| 47 | + // ignore if not called from a function method. |
| 48 | + if( !is_array($backtrace) || count($backtrace) != 2 ){ |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // get the classes & functions involved |
| 53 | + $callee_class = &$backtrace[0]['class']; |
| 54 | + $caller_class = &$backtrace[1]['class']; |
| 55 | + |
| 56 | + $callee_function = &$backtrace[0]['function']; |
| 57 | + $caller_function = &$backtrace[1]['function']; |
| 58 | + |
| 59 | + // if both are the same class, no problem |
| 60 | + if( $callee_class === $caller_class ){ |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + // if protected and called from a child class, no problem |
| 66 | + if( !$assert_as_private && in_array( $callee_class, class_parents($caller_class) ) ){ |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + $term = $assert_as_private ? 'private' : 'protected'; |
| 71 | + |
| 72 | + throw new \Exception( "{$callee_class}::{$callee_function} cannot be called from {$caller_class}::{$caller_function}. It is \"{$term}\"." ); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * Artificially makes a function Protected. |
| 79 | + * |
| 80 | + * Ensures a function call came from its parent class or a child class. |
| 81 | + * |
| 82 | + * This is particularly useful when extending from a class that you don't have |
| 83 | + * control over to make certain methods private. |
| 84 | + * |
| 85 | + * @param bool $assert_as_private // weather to assert as private or protected. |
| 86 | + * |
| 87 | + * @throws Exception // if method is being called from a class that is not |
| 88 | + * // function's parent or one of its children. |
| 89 | + * @return void |
| 90 | + */ |
| 91 | + static public function assertProtected(){ |
| 92 | + |
| 93 | + return self::assertProtectedOrPrivate( array_slice( debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 1, 2 ), false ); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + /** |
| 99 | + * Artificially makes a function Private. |
| 100 | + * |
| 101 | + * Ensures a function call came from its parent class |
| 102 | + * |
| 103 | + * This is particularly useful when extending from a class that you don't have |
| 104 | + * control over to make certain methods private. |
| 105 | + * |
| 106 | + * @param bool $assert_as_private // weather to assert as private or protected. |
| 107 | + * |
| 108 | + * @throws Exception // if method is being called from a class that is not |
| 109 | + * // function's parent or one of its children. |
| 110 | + * @return void |
| 111 | + */ |
| 112 | + static public function assertPrivate(){ |
| 113 | + |
| 114 | + return self::assertProtectedOrPrivate( array_slice( debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 1, 2 ), true ); |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments