public function package_exists($name) { return array_contains($this->get_packages(), $name); }
public function __construct($request_path, $request_params, $request_method, $return_type) { $function = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__); //validating the return type and set default if not found if (array_contains($return_type, Constants::get('allowed_return_types') == false)) { $return_type = Constants::get('default_return_type'); } else { $this->return_type = set_default($return_type, Constants::get('allowed_return_types')); } //validate if request path is valid else throw error if (is_ready($request_path) == false) { $error = Tool::prepare('Request path is invalid, unable to process routing request.', 'Request path is null, verify that index router has parsed the information correctly.', __LINE__, $this->return_type, Constants::get('default_error_code')); Tool::error($function, $error, false); } else { enforce_inputs(array($request_path, 'array', null, null, false), $this->return_type); $this->original_path = $request_path; $this->request_path = $request_path; } //validate if request method is valid, else set as default (post takes precendence if both are used) $this->request_method = set_default($request_method, Constants::get('default_http_method')); $allowed_http_methods = Constants::get('allowed_http_methods'); if (array_contains($request_method, $allowed_http_methods, false) == false) { $request_method = Constants::get('default_http_method'); } $this->request_method = strtolower($request_method); //add post params to class if exist $this->request_params = $request_params; //normalise request_path if (is_ready(end($this->request_path)) == false) { array_pop($this->request_path); } }
/** * @param string $contentType * @param string $dataClass * * @throws InvalidDataClassException */ public function addMapping($contentType, $dataClass) { if (!class_exists($dataClass) || !array_contains(class_implements($dataClass), IHttpData::class)) { throw new InvalidDataClassException(s('Class "%s" must implement the "%s" interface.', $dataClass, IHttpData::class)); } $this->mappings[$contentType] = $dataClass; }
function array_include(&$array, $item) { if (!array_contains($array, $item)) { $array[] = $item; } return $array; }
/** * @param $string * @param bool $default * * @return bool */ public function ask($string, $default = null) { if ($default === true) { $suffix = 'Y/n'; } else { if ($default === false) { $suffix = 'y/N'; } else { $suffix = 'y/n'; } } $this->output->write("<question>{$string}</question> [<yellow>{$suffix}</yellow>]: "); $input = $this->input->readLine(); if (array_contains(['yes', 'y'], $input)) { return true; } if (array_contains(['no', 'n'], $input)) { return false; } if (empty($input)) { if ($default === true) { return true; } if ($default === false) { return false; } } return $this->ask($string, $default); }
/** * @param IHttpHeaders $headers * @param array $server * @param array $specialHeaders */ public function extractSpecialHeaders(IHttpHeaders $headers, array $server, array $specialHeaders) { foreach ($server as $header => $value) { if (array_contains($specialHeaders, $header)) { $headers->add($this->formatHeader($header), $value); } } }
function array_erase(&$array, $item) { if (!array_contains($array, $item)) { return $array; } array_splice($array, $index, 1); return $array; }
public function testArrayContains() { $arrayA = array('foo', 'bar', 'baz'); $arrayB = array('foo', 'bar'); $arrayC = array('foo', 'apple'); $this->assertTrue(array_contains($arrayA, $arrayB)); $this->assertFalse(array_contains($arrayA, $arrayC)); $this->assertTrue(array_contains($arrayA, 'bar')); }
/** * check times of occurrence of $target in $universal_set * $target: array of string * $universal_set: array of array of string */ function occurrence($target, $universal_set) { $count = 0; foreach ($universal_set as $keywords_arr) { if (array_contains($keywords_arr, $target)) { $count++; } } return $count; }
/** * @param array $server * * @return mixed */ public function parseMethod(array $server) { $method = array_get($server, 'REQUEST_METHOD'); if (array_has($server, '_method')) { $_method = strtoupper(array_get($server, '_method')); if (array_contains(HttpRequestMethod::getMethods(), $_method)) { return $_method; } } return $method; }
public static function getLayout($layout) { if (array_contains($this->layouts, $layout)) { return "Layout not found!"; } foreach (self::$layoutFolders as $folder) { if (Files::exists($folder . "/" . $layout . ".php")) { return Files::readString($folder . "/" . $layout . ".php"); } } }
/** * @param $value * @param IValidationData $data * * @return bool */ public function check($value, IValidationData $data = null) { if (is_array($value)) { foreach ($value as $item) { if (array_contains($this->forbidden, $item)) { return false; } } return true; } return false; }
/** * @param $name * @param $repositoryClass * * @return $this * @throws InvalidRepositoryClassException * @throws RepositoryClassNotFoundException */ public function resolve($name, $repositoryClass) { if (!class_exists($repositoryClass)) { throw new RepositoryClassNotFoundException(s('Class "%s" not found.', $repositoryClass)); } if (!array_contains(class_implements($repositoryClass), ObjectRepository::class)) { throw new InvalidRepositoryClassException(s('Class "%s" must implement interface "%s".', $repositoryClass, ObjectRepository::class)); } $this->router->addResolver($name, function ($parameter, IContainer $container) use($repositoryClass) { $repository = $container->get($repositoryClass); return $repository->find($parameter); }); return $this; }
/** * @param mixed $data * @param array $keys * * @return array */ protected function getValues($data, array $keys) { $key = array_shift($keys); if (array_contains(['', null], $key)) { return []; } if ($key === ValidationToken::WILDCARD_VALUES) { $values = $this->getWildcardValues($data, $keys); } else { if ($key === ValidationToken::WILDCARD_KEYS) { $values = $this->getWildcardKeys($data, $keys); } else { $values = $this->getRegularValues($data, $key, $keys); } } return $values; }
function product_is_wishlisted($retailer, $pid) { $product = product_get_raw($retailer, $pid); $lists = lists_get_for_user(); $list_ids = array(); foreach ($lists as $list) { $list_ids[] = $list['id']; } $list_ids[] = 'def_' . current_user(); if ($product) { $lists = @$product['lists'] ?: array(); foreach ($list_ids as $list_id) { if (array_contains($lists, $list_id)) { return true; } } } return false; }
/** * @param Zend_Form_Element|Zend_Form|array $target * @param string $class */ function ifr_add_class(&$target, $class) { if (!is_array($target)) { $attrib = $target->getAttrib('class'); $classes = explode(' ', $attrib); } else { $attrib = array_key_exists('class', $target) ? $target['class'] : ''; $classes = is_array($attrib) ? $attrib : explode(' ', $attrib); } $classes = array_filter($classes); if (!array_contains($classes, $class)) { if (!is_array($target)) { if (!empty($attrib)) { $attrib .= ' '; } $target->setAttrib('class', $attrib . $class); } else { $classes[] = $class; $target['class'] = implode(' ', $classes); } } }
/** * @param IConsole $console */ public function render(IConsole $console) { $facts = $this->gatherFacts($console->getCommands()); if (count($facts) > 0) { $table = new TableWidget($this->input, $this->output); $table->setTitle("<header>Available commands:</header>"); $headers = []; foreach ($facts as $name => $description) { if (stripos($name, ':') !== false) { $header = array_first(explode(':', $name)); if (!array_contains($headers, $header)) { $headers[] = $header; $table->addSection("<header>{$header}</header>"); } } $table->addRow("<keyword>{$name}</keyword>", $description); } $this->output->writeLine(); $table->render(); } else { $this->output->writeLineIndented('There are no commands yet'); } }
/** * @param string $style * * @return IConsoleStyle */ public function parseStyle($style) { $groups = $this->formatParser->parseStyle($style); foreach ($groups as $group) { $type = $group[1]; $value = $group[2]; if (array_contains(['color', 'clr', 'fg'], $type)) { foreach (explode(',', $value) as $color) { $this->setColor($color); } } if (array_contains(['background', 'bg'], $type)) { foreach (explode(',', $value) as $background) { $this->setBackground($background); } } if (array_contains(['format', 'fmt'], $type)) { foreach (explode(',', $value) as $format) { $this->addFormat($format); } } } return $this; }
/** * @return callable */ private function addAliasToConditionDg() { $thisColumns = array_keys($this->schemaColumnsGet()); $thisAlias = $this->getAlias(); return function ($condition) use($thisColumns, $thisAlias) { if (preg_match('/^[`]?([a-zA-Z0-9_]+)[`]\\.[`]?([a-zA-Z0-9_]+)[`]/', $condition, $matches)) { // table.alias $ret = $condition; } elseif (preg_match('/^[`]?([a-zA-Z0-9_]+)[`]?([^.].*)$/', $condition, $matches)) { // table $columnName = $matches[1]; $else = $matches[2]; if (array_contains($thisColumns, $columnName)) { // assume column that needs to be prefixed $ret = "`{$thisAlias}`.`{$columnName}`{$else}"; } else { // assume not a column name $ret = $condition; } } else { // assume has alias $ret = $condition; } return $ret; }; }
function post_quote() { $file = get_random_file(); if (!array_contains($file)) { post_image(); persist(); // as uploaded } }
/** * @param $value * @param IValidationData $data * * @return bool */ public function check($value, IValidationData $data = null) { return array_contains([null, ''], $value) ? false : true; }
$normal_item = 'x'; $null_item = null; $empty_item = ''; $single_space = ' '; echo 'set_default($normal_item, "default"): ' . set_default($normal_item, 'default') . '<br/>'; echo 'set_default($null_item, "default"): ' . set_default($null_item, 'default') . '<br/>'; echo 'set_default($empty_item, "default"): ' . set_default($empty_item, 'default') . '<br/>'; echo 'set_default($single_space, "default"): ' . set_default($single_space, 'default') . '<br/>'; echo '<br/>Passed<br/><hr/>'; /* Rewrite method of in_array to check if the array contains the item. However, it sanitizes the input before checking */ echo 'Signature: array_contains($item, $allowed_array, $case_sensitive = false)<br/>'; echo '//check if items is in array, but sanitize all input before checking, default not case sensitive<br/><br/>'; echo 'array_contains("x ", array("x", "y", "z")): ' . (array_contains("x ", array("x", "y", "z")) ? 'true' : 'false') . '<br/>'; echo 'array_contains("x ", array("x ", "y", "z")): ' . (array_contains("x ", array("x", "y", "z")) ? 'true' : 'false') . ' - both inputs and list are sanitized<br/>'; echo 'array_contains("X ", array("x ", "y", "z")): ' . (array_contains("X ", array("x", "y", "z")) ? 'true' : 'false') . '<br/>'; echo 'array_contains("X ", array("x ", "y", "z"), true): ' . (array_contains("X ", array("x", "y", "z"), true) ? 'true' : 'false') . '<br/>'; echo '<h3>String Manipulation</h3>'; /* rewrite method of strcmp which sanitizes and trim inputs, including case sensitive comparision */ echo '<hr/>Signature: compare_string($input_one = "", $input_two = "", $case_sensitive = false)<br/>'; echo '//default comparision is not case sensitive, passing variables in works accordingly.<br/><br/>'; echo 'compare_string("", ""): ' . (compare_string('', '') ? 'true' : 'false') . '<br/>'; echo 'compare_string("xyz", "xyz"): ' . (compare_string('xyz', 'xyz') ? 'true' : 'false') . '<br/>'; echo 'compare_string("xyz", "XYZ"): ' . (compare_string('xyz', 'XYZ') ? 'true' : 'false') . '<br/>'; echo 'compare_string("xyz", "XYZ", true): ' . (compare_string('xyz', 'XYZ', true) ? 'true' : 'false') . '<br/>'; echo '<br/>Passed<br/><hr/>'; /* takes in a string list of items separated by the ':', it matches it against an array to check if the item exist in the array function is similar to array_contains/in_array however it allows multiple items to be validated at the same time @$greedy is a boolean value, when set to true, all items set to validate against the list must match if @$greedy is set to false, as long as any item matches, true is returned */ echo 'Signature: list_contains($items, $list, $greedy = true)<br/>';
$count = strpos($request, '&') == false ? strlen($request) : strpos($request, '&'); $request = substr($request, 0, $count); $request_path = explode("/", $request); //extract return type, check if return type is in the allowed list $return_type = end($request_path); $return_type = set_default($return_type, Constants::get('default_return_type')); $return_type = strrchr($return_type, "."); $return_type = substr($return_type, 1); $allowed_return_types = Constants::get('allowed_return_types'); if (array_contains($return_type, $allowed_return_types, false) == false) { $return_type = Constants::get('default_return_type'); } else { //remove the return type from the request_path $item = end($request_path); $item = substr($item, 0, strlen($item) - (strlen($return_type) + 1)); $request_path[count($request_path) - 1] = $item; } //extract request_method, can be defined in both get and post reauest, post takes precedence $request_method = set_default($_REQUEST['request_method'], $_SERVER['REQUEST_METHOD']); $allowed_http_methods = Constants::get('allowed_http_methods'); if (array_contains($request_method, $allowed_http_methods, false) == false) { $request_method = Constants::get('default_http_method'); } $request_params = $_REQUEST; //form route and send through router $route = new Route($request_path, $request_params, $request_method, $return_type); $router = new Router($route); $router->route(); //TODO: Can handle redirection to api introduction if no specific request is made } else { }
/** * @param array $array * @param bool $strict * @return callable */ function key_in_dg($array, $strict = false) { return function ($val, $key) use($array, $strict) { return array_contains($array, $key, $strict); }; }
/** * @param array $array * @param mixed $value * @return bool */ function debug_assert_array_contains($array, $value) { return debug_assert(array_contains($array, $value), "Parameter {$value} is not one of " . implode(',', $array)); }
function enforce_inputs() { $function = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__); $message = ''; $variable = ''; $line = ''; //retrieve last item as return type if available $array = func_get_args(); $return_type = end($array); if (is_string($return_type) == false) { $return_type = Constants::get('default_return_type'); } else { $allowed_return_types = Constants::get('allowed_return_types'); if (array_contains($return_type, $allowed_return_types) == false) { $return_type = Constants::get('default_return_type'); } } //loop through every item to validate, sets a message to throw the error at the end of the method foreach ($array as $list) { if (is_ready($list)) { //every list must be ready if (is_array($list)) { //ensure that item is an array if (count($list) == 5) { //every list contains the required items $variable = set_default($list[0], null); $type = set_default($list[1], ':'); $min = set_default($list[2], null); $max = set_default($list[3], null); $nullable = set_default($list[4], false); $validation_list = null; if (isset($variable)) { //ensure that variable is set //ensures that valid variable list type is request if (list_contains($type, Constants::get('variable_list')) == false) { $message = 'Invalid variable validation requirement - type (' . $type . ') unrecognized.'; $line = __LINE__; } //if either one is not a numeric, check for array if (validate_type($min, 'numeric') == false || validate_type($max, 'numeric') == false) { if (validate_type($min, 'array') == true || validate_type($max, 'array') == true) { $validation_list = (is_null($min) == true || isset($min) == false) && validate_type($max, 'array') ? $max : $min; if (validate_type($validation_list, 'array') == false) { $message = 'Invalid variable validation requirement - an array to validate is required.'; $line = __LINE__; } } else { if (is_null($min) == false || is_null($max) == false) { $message = 'Invalid variable validation requirement - min/max must be numeric or an array of list in either one or both nulls.'; $line = __LINE__; } } //ensure than max is less than min } else { if ($max < $min) { $message = 'Invalid variable validation requirement - min is more than max.'; $line = __LINE__; } } //ensure that nullable is boolean if (validate_type($nullable, 'bool') == false) { $message = 'Invalid variable validation requirement - nullable must be boolean.'; $line = __LINE__; } //verify variable if not null if (!is_null($variable)) { if (validate_type($variable, $type) == false) { $message = 'Variable is not a ' . $type . '.'; $line = __LINE__; } if (is_null($validation_list) == false && isset($validation_list) == true) { if (array_contains($variable, $validation_list) == false) { $message = 'Variable is not found in the list provided.'; $line = __LINE__; } } else { if (validate_type($variable, 'string:numeric') == true) { if (is_null($min) == false || is_null($max) == false) { if (validate_range($variable, $min, $max) == false) { $message = 'Variable does not meet the min/max requirement.'; $line = __LINE__; } } } } } } else { if ($nullable == false) { $message = 'Variable is not set, unable to validate variable'; $line = __LINE__; } } } else { $message = 'Incomplete variable validation list. [variable, type, min, max, nullable*]'; $line = __LINE__; } } } else { $message = 'Invalid variable validation list, an array is required. [variable, type, min, max, nullable*]'; $line = __LINE__; } } if (compare_string($message, '') == false) { $variable_name = variable_name($variable); if (isset($variable_name) == true && $variable_name != '') { $variable_name = '$' . $variable_name; } else { $variable_name = $type != 'password' ? $variable : '*password*'; } if ($variable_name != '') { $variable_name = ' [' . $variable_name . ']'; } $error = Tool::prepare($message . $variable_name, '', $line, $return_type, Constants::get('default_error_code')); Tool::error($function, $error, false); } }
function validate_hash($hash, $hash_type = 'md5') { $results = false; if (array_contains($hash_type, Constants::get('allowed_hash_types'), false)) { $results = preg_match(Regex::get('hash_' . strtolower($hash_type) . '_regex'), $hash); } else { $results = false; } return $results; }
/** * @param array $definition * * @return array */ protected function gatherRouteFacts(array $definition) { $route = array_get($definition, 'route'); $method = null; $path = null; $action = null; // routes is expected to be in format like: // GET POST /some/path someAction if (is_string($route)) { $parts = preg_split('/\\s+/', $route); $parts = array_map('trim', $parts); foreach ($parts as $part) { if (HttpRequestMethod::isValid($part) || $part === 'ANY') { if ($method === null) { $method = $part; } else { if (!is_array($method)) { $method = [$method, $part]; } else { $method[] = $part; } } } else { if ($path === null) { $path = $part; } else { if ($action === null) { $action = $part; } } } } } if ($method === null) { $method = array_get($definition, 'method'); } if ($method === 'ANY' || is_array($method) && array_contains($method, 'ANY')) { $method = HttpRequestMethod::getMethods(); } if ($path === null) { $path = array_get($definition, 'path'); } if ($action === null) { $action = array_get($definition, 'action'); } return [$method, $path, $action]; }
/** * @param $value * @param $type * * @return bool|float|int|mixed|null */ protected function _getValueByType($value, $type) { if (!is_null($value)) { switch ($type) { case "id": if (array_contains(array('', null), $value, true)) { $value = null; } else { $value = intval($value); } break; case "bool": case "boolean": $value = (bool) $value; break; case "int": $value = (int) $value; break; case "float": $value = (double) $value; break; case "object": case "array": if (is_object($value) || !is_array($value)) { $value = unserialize($value); } break; case "date": if ($value == '0000-00-00' || $value == '') { $value = null; } break; case "time": if ($value == '00:00:00' || $value == '') { $value = null; } break; case "datetime": if ($value == '0000-00-00 00:00:00' || $value == '') { $value = null; } break; case "text": default: $value = (string) $value; break; } } return $value; }
$product = product_create($retailer, $pid, array('title' => $product_title, 'url' => $product_url, 'price' => $product_price, 'currency' => 'GBP', 'image_url' => $product_image_url)); } if (!$product) { $product = array(); $product['title'] = 'Asus C300 Chromebook, Intel Celeron, 2GB RAM, 32GB SSD, 13.3", Red'; $product['image_url'] = 'http://johnlewis.scene7.com/is/image/JohnLewis/233845648alt3?$prod_main$'; $product['price'] = 239.9; $product['url'] = 'http://www.johnlewis.com/asus-c300-chromebook-intel-celeron-2gb-ram-32gb-ssd-13-3-red/p1625271'; } $logo = 'http://www.johnlewis.com/assets/header/john-lewis-logo.gif'; $product['logo'] = image_thumbnail_url($logo, '200x20', 'resize'); $product_image = image_thumbnail_url(@$product['image_url'], '320x250', 'resizenp'); $price_formatted = '£' . sprintf('%01.2f', $product['price']); $lists = lists_get_for_user(false); if ($product) { $list_ids = product_get_list_ids($retailer, $pid); if ($list_id) { if (array_contains($list_ids, $list_id)) { if (isset($_GET['list'])) { product_remove_from_list($retailer, $pid, $list_id); } } else { product_add_to_list($retailer, $pid, $list_id); } $list_ids = product_get_list_ids($retailer, $pid); } $data = array('product' => $product, 'product_image' => $product_image, 'price_formatted' => $price_formatted, 'lists' => $lists, 'added_list_id' => $list_id, 'pid' => $pid, 'retailer' => $retailer, 'list_ids' => $list_ids, 'is_popup' => $is_popup); show_template('add_product_popup', $data); } else { die('Bums!'); }