/** * The applies method is the only point where the rule can be validated. * * Internally each rule implements the _applies() method to do the actual * validation. * * There are only two types of outcome for each rule. * Either the rule applies (true) or doesn't (false). * Any other outcome should always be thrown as an exception so no false * assumptions can be made by the caller. For example when a rule returns a * NULL value the caller may asume that false is meant. The rule cannot * trust that the caller checks the boolean type so we will. * * @return boolean * @throws Exception https://en.wikipedia.org/wiki/Template_method_pattern */ public final function applies() { try { if ($this->shouldReturnCache()) { return $this->getCache(); } $this->clearResult(); $this->clearCache(); $result = $this->_applies(); if (is_bool($result)) { $this->setCache($result); return $result; } else { throw new Exception('A rule must return a boolean.', Exception::CODE_NONBOOLEAN); } } catch (Exception $e) { $this->handleException($e); throw $e; } catch (\Exception $e) { $e = new Exception($e->getMessage(), $e->getCode(), $e); $this->handleException($e); throw $e; } }