| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Handles the "extra" functionality for the main class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace projectcleverweb\color; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Handles the "extra" functionality for the main class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | abstract class main_peripheral implements \Serializable, \JsonSerializable { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	 * Makes sure cloning works as expected | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	public function __clone() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		$this->color = clone $this->color; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		$this->cache = clone $this->cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 * Custom serialize function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 * @return string This instance serialized as an RGB string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 	public function serialize() :string { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 		return $this->color->serialize(); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * Custom unserialize function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * @param  string $serialized This instance serialized as an RGB string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	public function unserialize($serialized) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		$unserialized = (array) json_decode((string) $serialized); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		regulate::rgb_array($unserialized); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$this->set($unserialized, 'rgb'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * Custom JSON serialize function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * @return string This instance serialized as an JSON RGB string | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	public function jsonSerialize() :array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		return $this->color->jsonSerialize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	protected function get_scheme(string $scheme_name, string $return_type = 'hex', $scheme_class) :array { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		if (!is_null($cached = $this->cache->get(get_class($scheme_class).'_'.$scheme_name.'_'.$return_type, $this->hex()))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 			return $cached; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$result = static::_scheme($scheme_name, [$scheme_class, strtolower($return_type)], $this->hsl(3)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$this->cache->set(get_class($scheme_class).'_'.$scheme_name.'_'.$return_type, $this->hex(), $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * Handles scheme generator callbacks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * @param  string $scheme_name The name of the scheme algorithm to use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * @param  string $callback    The return type callback | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * @param  array  $hsl         The base color as an HSL array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 * @return array               The resulting scheme in the proper format, OR an empty array on failure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	protected static function _scheme(string $scheme_name, array $callback, array $hsl) :array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		if (is_callable($callback)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			return call_user_func($callback, $hsl['h'], $hsl['s'], $hsl['l'], $scheme_name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		error::trigger(error::INVALID_ARGUMENT, sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 			'The $callback "%s" is not a valid callback', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			print_r($callback, 1), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			__CLASS__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			__FUNCTION__ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 * Set whether or not caching should be active. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * @param  bool $active If TRUE caching is turned on, otherwise cashing is turned off. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	public function cache(bool $active = TRUE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		$this->cache->active = $active; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * Reset the cache for this instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	public function reset_cache() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$this->cache->reset(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  | 
            
                        
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]orarray<String>.