Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Feb 3, 2016
2 parents 4a15231 + 2300c71 commit 8e5d3c0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
42 changes: 37 additions & 5 deletions src/Models/Operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
*/
Expand All @@ -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
];

/**
Expand All @@ -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')
Expand All @@ -94,13 +105,15 @@ 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;

/**
* Get operators
* @param integer $classFlags
* @return integer Operator class
* @throws \Exception When invalid operator class flags are given
*/
public static function getByClassFlags($classFlags)
{
Expand Down Expand Up @@ -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');
}
Expand All @@ -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)
Expand All @@ -158,7 +178,8 @@ public static function parse($operatorValueString)
'|',
array_merge(
Operator::getOrderableOperators(),
Operator::getLikeOperators()
Operator::getLikeOperators(),
Operator::getInArrayOperators()
)
);

Expand Down Expand Up @@ -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[]
*/
Expand Down
9 changes: 5 additions & 4 deletions src/Models/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use \Phramework\Exceptions\MissingParametersException;
use \Phramework\Exceptions\IncorrectParametersException;
use \Phramework\Validate\UnsignedIntegerValidator;
use \Phramework\Validate\Validate;

/**
* Request related functions
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 16 additions & 14 deletions src/Phramework.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down

0 comments on commit 8e5d3c0

Please sign in to comment.