diff --git a/src/Models/Operator.php b/src/Models/Operator.php index 5f3d94a..34907de 100644 --- a/src/Models/Operator.php +++ b/src/Models/Operator.php @@ -41,6 +41,15 @@ class Operator const OPERATOR_IN = 'IN'; const OPERATOR_NOT_IN = 'NOT IN'; + /** + * ∈, is an element of array *(URL encoded : `%E2%88%88"`)* + */ + const OPERATOR_IN_ARRAY = '∈'; + /** + * ∉, is not an element of array *(URL encoded : `%E2%88%89`)* + */ + const OPERATOR_NOT_IN_ARRAY = '∉'; + /** * @var string[] */ @@ -60,7 +69,9 @@ class Operator Operator::OPERATOR_IN, Operator::OPERATOR_NOT_IN, Operator::OPERATOR_LIKE, - Operator::OPERATOR_NOT_LIKE + Operator::OPERATOR_NOT_LIKE, + Operator::OPERATOR_IN_ARRAY, + Operator::OPERATOR_NOT_IN_ARRAY ]; /** @@ -77,7 +88,7 @@ public static function getOperators() * @param string $operator * @param string $attributeName * *[Optional]* Attribute's name, used for thrown exception - * @throws Phramework\Exceptions\IncorrectParametersException + * @throws \Phramework\Exceptions\IncorrectParametersException * @return string Returns the operator */ public static function validate($operator, $attributeName = 'operator') @@ -94,6 +105,7 @@ public static function validate($operator, $attributeName = 'operator') const CLASS_COMPARABLE = 1; const CLASS_ORDERABLE = 2; const CLASS_LIKE = 4; + const CLASS_IN_ARRAY = 32; const CLASS_NULLABLE = 64; const CLASS_JSONOBJECT = 128; @@ -101,6 +113,7 @@ public static function validate($operator, $attributeName = 'operator') * Get operators * @param integer $classFlags * @return integer Operator class + * @throws \Exception When invalid operator class flags are given */ public static function getByClassFlags($classFlags) { @@ -134,6 +147,13 @@ public static function getByClassFlags($classFlags) ); } + if (($classFlags & Operator::CLASS_IN_ARRAY) !== 0) { + $operators = array_merge( + $operators, + Operator::getInArrayOperators() + ); + } + if (empty($operators)) { throw new \Exception('Invalid operator class flags'); } @@ -143,10 +163,10 @@ public static function getByClassFlags($classFlags) /** * @param string $operatorValueString - * @return string[2] [operator, operant] + * @return string[2] [operator, operand] * @example * ```php - * list($operator, $operant) = Operator::parse('>=5'); + * list($operator, $operand) = Operator::parse('>=5'); * ``` */ public static function parse($operatorValueString) @@ -158,7 +178,8 @@ public static function parse($operatorValueString) '|', array_merge( Operator::getOrderableOperators(), - Operator::getLikeOperators() + Operator::getLikeOperators(), + Operator::getInArrayOperators() ) ); @@ -212,6 +233,17 @@ public static function getEqualityOperators() ]; } + /** + * @return string[] + */ + public static function getInArrayOperators() + { + return [ + Operator::OPERATOR_IN_ARRAY, + Operator::OPERATOR_NOT_IN_ARRAY + ]; + } + /** * @return string[] */ diff --git a/src/Models/Request.php b/src/Models/Request.php index dc7f670..25250ac 100644 --- a/src/Models/Request.php +++ b/src/Models/Request.php @@ -21,6 +21,7 @@ use \Phramework\Exceptions\MissingParametersException; use \Phramework\Exceptions\IncorrectParametersException; use \Phramework\Validate\UnsignedIntegerValidator; +use \Phramework\Validate\Validate; /** * Request related functions @@ -65,7 +66,7 @@ public static function checkPermission($userId = false) * Check if required parameters are set * @param array|object $parameters Request's parameters * @param string|array $required The required parameters - * @throws Phramework\Exceptions\MissingParametersException + * @throws \Phramework\Exceptions\MissingParametersException */ public static function requireParameters($parameters, $required) { @@ -95,7 +96,7 @@ public static function requireParameters($parameters, $required) * Require id parameter if it's set else return NULL, it uses `resource_id` or `id` parameter if available * @param array|object $parameters The request parameters * @param boolean $UINTEGER *[Optional]*, Check id's type to be unsigned integer - * @throws Phramework\Exceptions\IncorrectParameters When value is not correct + * @throws \Phramework\Exceptions\IncorrectParameters When value is not correct * @return string|integer Returns the id or NULL if not set, * if $UINTEGER the returned value will be converted to unsigned integer */ @@ -131,8 +132,8 @@ public static function resourceId($parameters, $UINTEGER = true) * Require id parameter, it uses `resource_id` or `id` parameter if available * @param array|object $parameters The request paramters * @param boolean $UINTEGER *[Optional]*, Check id's type to be unsigned integer, default is true - * @throws Phramework\Exceptions\IncorrectParameters When value is not correct - * @throws Phramework\Exceptions\MissingParametersException When id is missing + * @throws \Phramework\Exceptions\IncorrectParameters When value is not correct + * @throws \Phramework\Exceptions\MissingParametersException When id is missing * if $UINTEGER the returned value will be converted to unsigned integer */ public static function requireId($parameters, $UINTEGER = true) diff --git a/src/Phramework.php b/src/Phramework.php index e9cce48..955e821 100644 --- a/src/Phramework.php +++ b/src/Phramework.php @@ -231,10 +231,10 @@ public static function getTranslated( /** * Execute the API - * @throws Phramework\Exceptions\PermissionException - * @throws Phramework\Exceptions\NotFoundException - * @throws Phramework\Exceptions\IncorrectParametersException - * @throws Phramework\Exceptions\RequestException + * @throws \Phramework\Exceptions\PermissionException + * @throws \Phramework\Exceptions\NotFoundException + * @throws \Phramework\Exceptions\IncorrectParametersException + * @throws \Phramework\Exceptions\RequestException * @todo change default timezone * @todo change default language * @todo initialize database if set @@ -366,19 +366,21 @@ public function invoke() )) { $input = trim(file_get_contents('php://input')); - //note if input length is >0 and decode returns null then its bad data - //json_last_error() + if (!empty($input)) { + //note if input length is >0 and decode returns null then its bad data + //json_last_error() - $input = json_decode($input); + $inputObject = json_decode($input); - if (json_last_error() !== JSON_ERROR_NONE) { - throw new \Phramework\Exceptions\RequestException( - 'JSON parse error - ' . json_last_error_msg() - ); - } + if (json_last_error() !== JSON_ERROR_NONE) { + throw new \Phramework\Exceptions\RequestException( + 'JSON parse error: ' . json_last_error_msg() + ); + } - if ($input && !empty($input)) { - $params = (object)array_merge((array)$params, (array)$input); + if ($inputObject && !empty($inputObject)) { + $params = (object)array_merge((array)$params, (array)$inputObject); + } } } }