How to match part of string inside array - PHP - php

Is there a way to match a part of a string within an array in PHP?
I would like to validate a user ip against allowed IPs. Therefore I have created an array with IPs and coresponding partner_id. This works, however I also want to allow an entire subnet and would therefore need to mach against part of the array. Is this possible?
This is my code:
# define partner IPs
$partner_ips = array(
'192.168.56.1' => 0, // dev
'192.168.57.*' => 1 // office ips
);
# test for partner IP and associate partner_id if found
if (array_key_exists($_SERVER['REMOTE_ADDR'], $partner_ips))
$partner_id = $partner_ips[$_SERVER['REMOTE_ADDR']];
else
$partner_id = false;
Thank you for any help on this.

Check the ip format first. Build two different arrays, one for full ip adresses and one for subnets. An example class (feel free to make it PSR-2 compliant, since you use PHP 5.6 you can also declare the two arrays as class constants instead of static variables):
class RemoteAddress {
private $ip;
private $id;
private static $partners_ips = [
'192.168.56.1' => 0,
'192.168.58.4' => 2,
'192.168.59.2' => 3 ];
private static $partners_subnets = [ // note that subnets must end with a dot
'192.168.57.' => 1,
'192.168.60.' => 4,
'192.168.61.' => 5 ];
public function __construct($ip) {
if (filter_var($ip, FILTER_VALIDATE_IP) === false)
throw new Exception("invalid IP address");
$this->ip = $ip;
$this->id = $this->searchID();
}
public function getIDPartner() {
return $this->id;
}
private function searchID() {
if (array_key_exists($this->ip, self::$partners_ips))
return self::$partners_ips[$this->ip];
foreach (self::$partners_subnets as $subnet => $id) {
if (strpos($this->ip, $subnet) === 0)
return $id;
}
return false;
}
}
You can use it like this:
try {
if (isset($_SERVER['REMOTE_ADDR'])) {
$remAddr = new RemoteAddress($_SERVER['REMOTE_ADDR']);
var_dump($remAddr->getIDPartner());
} else throw new Exception('$_SERVER[\'REMOTE_ADDR\'] is not defined');
} catch(Exception $e) {
echo $e->getMessage();
}

You can use in_array for check if your string exist in array or not
http://php.net/manual/en/function.in-array.php

Related

Use Trait Function with Same Name but Optionally

PHP Class Using Same Name as Trait Function
Refer to the question I just asked above here. Here was my original code.
trait sampletrait{
function hello(){
echo "hello from trait";
}
}
class client{
use sampletrait;
function hello(){
echo "hello from class";
//From within here, how do I call traits hello() function also?
}
}
I can call the trait function like this thanks to the answer to the question.
class client{
use sampletrait {
hello as protected sampletrait_hello;
}
function hello(){
$this->sampletrait_hello();
echo "hello from class";
}
}
My question is if my class client did not have a function hello() but wanted to call it is this possible?
So for example...
trait sampletrait{
function hello(){
echo "hello from trait";
}
}
class client{
use sampletrait {
hello as protected sampletrait_hello;
}
}
I'm aware that I could just simply say use sampletrait; and it would have the function but in my use case I can't do that either. Is it possible to have the aliased name but still use the trait name default if it does not exist in the class?
Extra Information
My exact use case involves PHP-ActiveRecord
I have a trait called uniquecheck
trait uniquecheck {
//#JA - Used temporarely to determine if editing for the unique checker
static $isEditing = false;
//#JA - This is used by PHPActiveRecord to trigger events before validation on update calls only.
static $before_validation_on_update = array('before_validation_on_update_callback');
//#JA - This is function used as callback from PHPActiveRecord
public function before_validation_on_update_callback(){
self::$isEditing = true; //#JA - Requires Uniquecheck trait to work
}
//#JA - This function can do single and multi-unique checks.
//#JA - This is programmed to be replaced at a later date when validates_uniqueness_of is fixed (http://www.phpactiverecord.org/projects/main/wiki/Validations#validates_uniqueness_of)
//#JA - EXAMPLES
//SINGLE -- array('name','message' => 'Can't do this')
//MULTIPLE -- array( array('name1','name2'), 'message' => 'can't do this and that together')
//#JA - To be clear multiple does not mean 2 different uniques but a unique on 2 columns. Just use this function twice for 2 separate unique checks.
public function uniquecheck($rules = array()) {
$classname = get_class($this);
//#JA - Basic validation to confirm assumptions for function properties
if(count($rules)<=0){
die('uniquecheck.php -> Property array can not be empty');
}
//#JA - If its an array use the MULTIPLE method
if(is_array($rules[0])){
//#JA - First create the condition string
$conditionstring = '';
$conditionarray = array();
$uniques = $rules[0];
foreach($uniques as $unique){
$conditionstring .= "$unique = ? AND ";
}
$conditionstring = substr($conditionstring, 0, -5);
//#JA - Then generate the array we will use for the conditions
$conditionarray['conditions'][] = $conditionstring;
foreach($uniques as $unique){
$conditionarray['conditions'][] = $this->read_attribute($unique);
}
$results = $classname::find('all',$conditionarray);
if($classname::$isEditing == true){
die('was editing');
}else{
die('was creating');
}
//#JA - If in edit mode, if the values are exactly the same as it was before then ignore this check.
if (count($results)>=1) {
foreach($uniques as $unique){
$this->errors->add($unique, $rules['message']);
}
}
}else{ //#JA - Otherwise use the SINGLE method
$unique = $rules[0];
$results = $classname::find('all',array('conditions' => array("$unique = ?", $this->read_attribute($unique))));
//#JA - If there is more then 1 result then its not unique!
if (count($results)>=1) {
$this->errors->add($unique, $rules['message']);
}
}
}
}
?>
I use this in my model Client like so...
class Client extends ActiveRecord\Model {
use foreignkeycheck;
use uniquecheck {
before_validation_on_update_callback as protected uniquecheck_before_validation_on_update_callback;
}
static $before_destroy = array('before_destroy_callback');
//#gv hide columns that are not in use right now
static $columnsToHide = array(
'affiliate_code',
'autopay',
'stripe_customer_id',
'quickbooks_client_id',
'stripe_customer_info',
'stripe_customer_info_last_update',
'textingnumber'
);
static $easy_name = "Client";
static $validates_presence_of = array(
array('clienttype_id'),
array('company_id'),
array('contactfirstname'),
array('contactlastname'),
array('contactphonenumber')
);
static $validates_size_of = array(
array('contactfirstname', 'within' => array(1, 50)),
array('contactlastname', 'within' => array(1, 50)),
array('contactaddress', 'within' => array(1, 120), 'allow_null' => false),
array('companyaddress', 'within' => array(1, 120), 'allow_null' => true),
array('companyname', 'within' => array(1, 75), 'allow_null' => true),
);
// static $validates_uniqueness_of = array(
// array('affiliate_code', 'allow_null' => true),
// array(array('contactfirstname', 'contactlastname', 'contactemail', 'contactphonenumber', 'contactaddress'),
// 'message' => 'Can\'t have duplicate client.')
// );
static $validates_format_of = array(
array('contactemail', 'with' => '/\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,10}\b/sim',
'message' => 'Must be a correctly formatted email.', 'allow_blank' => true, 'allow_null' => true),
array('companyemail', 'with' => '/\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,10}\b/sim',
'message' => 'Must be a correctly formatted email.', 'allow_blank' => true, 'allow_null' => true),
array('companyphonenumber', 'with' => '/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i',
'message' => 'Phone number is invalid', 'allow_blank' => true, 'allow_null' => true),
array('contactphonenumber', 'with' => '/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i',
'message' => 'Phone number is invalid', 'allow_blank' => true, 'allow_null' => false)
);
//This allows you to use your own as well as still call the uniquechecks before_validation callback in case this method is not needed.
public function before_validation_on_update_callback(){
$this->uniquecheck_before_validation_on_update_callback();
}
public function before_destroy_callback(){
$conn = SELF::connection();
$conn->transaction();
try {
//USER *********
//Delete the associated user as well.
$related_users = User::find('all',array(
'conditions' => array(
'client_id' => $this->id)
));
foreach($related_users as $user){
$user->delete();
}
//PROPERTIES ********
//Delete all properties of the client, which in turn delets all routes & visits
$related_properties = Property::find('all',array(
'conditions' => array(
'client_id' => $this->id)
));
foreach($related_properties as $property){
$property->delete();
}
//Only have to delete the user, because deletes will cascade down
$conn->commit();
} catch (Exception $e) {
$conn->rollback();
}
return true; //will actually delete the client now.
}
public function validate() {
//Thought about putting user validation in here, but decided against it.
//Multi-unique check FAILS to work if the parameter is not passsed for one of the multi-uniques. This is BUG in PHP Active Record.
//Does not show message correctly for multi-uniques either. This is ALSO a bug in PHP Active Record.
//#JA - Uses multi-unique check. Its only not allowed if all 4 of these values are the same since its obviously duplicate at that point
$this->uniquecheck(array(array('company_id','contactfirstname', 'contactlastname', 'contactphonenumber', 'contactaddress'),'message' => 'Can\'t have duplicate client.'));
$this->foreignkeycheck('Clienttype');
$this->foreignkeycheck('Company');
$this->foreignkeycheck('Affiliate', 'affiliate_code', true); //Special case where foreign key is not _id, true sent to indicate validate is optional only if a value is not null.
}
public function getReadableColumnNames($flip = false) {
$readableColumns = array();
$readableColumns["contactfirstname"] = "First Name";
$readableColumns["contactlastname"] = "Last Name";
$readableColumns["contactphonenumber"] = "Phone Number";
$readableColumns["contactemail"] = "Email";
$readableColumns["contactaddress"] = "Address";
$readableColumns["companyaddress"] = "Company Address";
$readableColumns["companyemail"] = "Company Email";
$readableColumns["companyname"] = "Company Name";
$readableColumns["companyphonenumber"] = "Company Phone #";
$readableColumns["affiliate_code"] = "Affiliate #";
$readableColumns["clienttype_id"] = "Client Type";
$readableColumns["company_id"] = "Company";
$readableColumns["stripe_customer_id"] = "Stripe Customer ID";
$readableColumns["stripe_customer_info"] = "Stripe Customer Info";
$readableColumns["stripe_customer_info_last_update"] = "Stripe Info Last Update";
$readableColumns["welcome_email_sent"] = "Welcome Email Sent?";
$readableColumns["autopay"] = "Auto Pay?";
$readableColumns["active"] = "Active?";
if ($flip == true) {
$readableColumns = array_flip($readableColumns); //swap keys and values~
}
return $readableColumns;
}
public function getDefaultColumns() {
$defaultColumns = array();
$defaultColumns[] = "contactfirstname"; //first sort order
$defaultColumns[] = "contactlastname"; //second sort order
$defaultColumns[] = "contactphonenumber";
$defaultColumns[] = "contactemail"; //etc...
return $defaultColumns;
}
public function getColumnExceptions() {
$tableNames = array();
return $tableNames;
}
public function getBatchActions() {
$batchActions = array();
//$batchActions['Text to Appear'] = 'ClassName'
//For JS File To Call Correct Function ^^^^
//Order of array determines order in respective dropdown menu.
$batchActions["Make Inactive"] = "batch_make_inactive";
$batchActions["Send Email"] = "batch_send_email";
$batchActions["Send Welcome Email"] = "batch_send_client_welcomeEmail";
return $batchActions;
}
public function getRowActions() {
$rowActions = array();
$rowActions["Edit"] = array("edit_typename", true); //Call generic typename edit function, true means this is the item that shows first.
$rowActions["View Pictures"] = array("view_pictures_for_client", false); //shortcut to prefill information for property~
$rowActions["Add Property"] = array("add_property_for_client", false); //shortcut to prefill information for property~
//$rowActions["Update Quickbooks"] = array("qb_update_customer", false); //shortcut to add customer to quickbooks if connected.
$rowActions["Create User ID"] = array("create_userid_for_client", false); //shortcut method to create user_id straight from the client~
$rowActions["Send Welcome Email"] = array("send_client_welcome_email", false);
$rowActions["Make Inactive"] = array("allinactive_client", false); //will make the user inactive, property and user_id, along with recurring invoices, estimates, invoices that were referenced by client.
$rowActions["Make Active"] = array("allactive_client", false);
$rowActions["Delete"] = array("delete_typename", false); //call to generic typename delete function
//#gv Functions that do not work and not part of Release 1.0
//$rowActions["Add Estimate"] = array("add_estimate_for_client",false); //shortcut to prefill information for property~
//$rowActions["Add Invoice"] = array("add_invoice_for_client",false); //shortcut to prefill information for property~
//$rowActions["Add To Quickbooks"] = array("qb_add_customer",false); //shortcut to add customer to quickbooks if connected.
//$rowActions["Make Inactive"] = array("inactive_typename",false); //Way to filter results if you desired by clients that are not relevant anymore.
//$rowActions["Send Email"] = array("send_client_email",false);
//$rowActions["Send Text"] = array("text_client",false);
return $rowActions;
}
public function getColumnInterestedColumns() {
$columnInterestedColumns = array();
$columnInterestedColumns["clienttype_id"] = array("name");
$columnInterestedColumns["company_id"] = array("companyname");
$columnInterestedColumns["client_id"] = array("contactfirstname", "contactlastname"); //external reference.
return $columnInterestedColumns;
}
//This function indicates to the UI what fields are dependent upon others for purpose of 'flow' for new and edit areas.
//Happens in 2 areas, on initial PHP creation uses this to hide the field, and upon the restricted fields parent values taking on a value or losing a value.
public function getColumnRestrictions() {
global $user;
$restrictedColumns = array();
//$restrictedColumns["property_id"] = array("client_id");//this means that property_id can not show in UI until client_id is set.
return $restrictedColumns;
}
}
?>
I am trying to use this to get around phpactiverecords unique check bug since it does not work in there system for a project I'm working on.
It uses a callback like this (before_validation_on_update_callback), where it has to have that name.
I wanted to use a trait to include it in all my models for unique checking easily.
Refer to this (http://www.phpactiverecord.org/projects/main/wiki/Callbacks)
Try setting public on the trait function and then protected when you rename
trait sampletrait{
public function hello(){
echo "hello from trait";
}
}
class client{
use sampletrait {
hello as protected sampletrait_hello;
}
}
$c = new client();
$c->hello();
As said here PHP Class Using Same Name as Trait Function both hello and sampletrait_hello will exist, but as hello is public and sampletrait_hello protected only hello will be callable from an outer scope.
And if you overwrite hello, you will be able to call sampletrait_hello inside it.

Maintain Element in PHP Array And Update in PHP Class

I have one PHP class as below (part of the code):
class myclass{
private static $arrX = array();
private function is_val_exists($needle, $haystack) {
if(in_array($needle, $haystack)) {
return true;
}
foreach($haystack as $element) {
if(is_array($element) && $this->is_val_exists($needle, $element))
return true;
}
return false;
}
//the $anInput is a string e.g. Michael,18
public function doProcess($anInput){
$det = explode(",", $anInput);
if( $this->is_val_exists( $det[0], $this->returnProcess() ) ){
//update age of Michael
}
else{
array_push(self::$arrX, array(
'name' => $det[0],
'age' => $det[1]
));
}
}
public function returnProcess(){
return self::$arrX;
}
}
The calling code in index.php
$msg = 'Michael,18';
myclass::getHandle()->doProcess($msg);
In my webpage says index.php, it calls function doProcess() over and over again. When the function is called, string is passed and stored in an array. In the next call, if let's say same name is passed again, I want to update his age. My problem is I don't know how to check if the array $arrX contains the name. From my own finding, the array seems to be re-initiated (back to zero element) when the code is called. My code never does the update and always go to the array_push part. Hope somebody can give some thoughts on this. Thank you.
There is a ) missing in your else condition of your doProcess() function, it should read:
else{
array_push(self::$arrX, array(
'name' => $det[0],
'age' => $det[1]
)); // <-- there was the missing )
}
Here is a complete running solution based on your code:
<?php
class myclass{
private static $arrX = array();
private function is_val_exists($needle, $haystack) {
if(in_array($needle, $haystack)) {
return true;
}
foreach($haystack as $element) {
if(is_array($element) && $this->is_val_exists($needle, $element))
return true;
}
return false;
}
//the $anInput is a string e.g. Michael,18
public function doProcess($anInput){
$det = explode(",", $anInput);
if( $this->is_val_exists( $det[0], $this->returnProcess() ) ){
//update age of Michael
for ($i=0; $i<count(self::$arrX); $i++) {
if (is_array(self::$arrX[$i]) && self::$arrX[$i]['name'] == $det[0]) {
self::$arrX[$i]['age'] = $det[1];
break;
}
}
} else{
array_push(self::$arrX, array(
'name' => $det[0],
'age' => $det[1]
));
}
}
public function returnProcess(){
return self::$arrX;
}
}
$mc = new myclass();
$mc->doProcess('Michael,18');
$mc->doProcess('John,23');
$mc->doProcess('Michael,19');
$mc->doProcess('John,25');
print_r($mc->returnProcess());
?>
You can test it here: PHP Runnable
As I said in comments, it looks like you want to maintain state between requests. You can't use pure PHP to do that, you should use an external storage solution instead. If it's available, try Redis, it has what you need and is quite simple to use. Or, if you're familiar with SQL, you could go with MySQL for example.
On a side note, you should read more about how PHP arrays work.
Instead of array_push, you could have just used self::$arrX[] = ...
Instead of that, you could have used an associative array, e.g. self::$arrX[$det[0]] = $det[1];, that would make lookup much easier (array_key_exists etc.)
Can you try updating the is_val_exists as follows:
private function is_val_exists($needle, $haystack) {
foreach($haystack as $element) {
if ($element['name'] == $needle) {
return true;
}
return false;
}

RecursiveIteratorIterator loops too many times with LEAVES_ONLY, works with SELF_FIRST

This is a strange problem and I've been pulling my hair out over it for a while now. Basically, I have an array of arrays (2 levels). I am using a RecursiveIterator to "pretend" this array has more levels than it does. My goal is to "group" the array by 2 elements.
I created my own class that implements RecursiveIterator, then I threw it through a RecursiveIteratorIterator to loop over it. Each "child" level in the iterator is one of the keys in the array.
Let me give an example. Here's the array of emails I have:
$data = [
[
'address' => 'example#example.com',
'emailType' => 'reminder',
'data' => 'Test Reminder'
],
[
'address' => 'example2#example.com',
'emailType' => 'reminder',
'data' => 'Another Test Reminder'
],
[
'address' => 'example#example.com',
'emailType' => 'reminder',
'data' => 'Yet Another Test Reminder'
],
[
'address' => 'example#example.com',
'emailType' => 'order',
'data' => 'Testing Order'
]
];
First I want to group this by the address, then by the emailType. If they match, then group by the data values.
So, I want to see the following:
Sending reminder to example#example.com
-> Test Reminder, Yet Another Test Reminder
Sending order to example#example.com
-> Testing Order
Sending reminder to example2#example.com
-> Another Test Reminder
I have got this working with my custom RecursiveIterator class (and RecursiveIteratorIterator).
$iter = new RecursiveIteratorIterator(
new MyIterator($data), RecursiveIteratorIterator::SELF_FIRST
);
foreach($iter as $key=>$val){
if(!is_array($val)) continue;
echo "Sending {$iter->getEmailType()} to {$key}\n";
if(count($iter->getInnerIterator()) > 1){
echo '-> '.implode(', ', $val['data'])."\n";
}
else{
echo '-> '.$val['data']."\n";
}
}
DEMO: https://eval.in/179753
The problem here is that this is using RecursiveIteratorIterator::SELF_FIRST, which means I need if(!is_array($val)) continue;. I wanted to get rid of that, so I tried to use RecursiveIteratorIterator::LEAVES_ONLY instead, to just print the children. That's where the issue is. When I change that, I get too may iterations over my loop and repeated data.
$iter = new RecursiveIteratorIterator(
new MyIterator($data), RecursiveIteratorIterator::LEAVES_ONLY
);
foreach($iter as $key=>$val){
if(!is_array($val)) continue;
echo "Sending {$iter->getEmailType()} to {$key}\n";
if(count($iter->getInnerIterator()) > 1){
echo '-> '.implode(', ', $val['data'])."\n";
}
else{
echo '-> '.$val['data']."\n";
}
}
DEMO: https://eval.in/179754
This outputs:
Sending reminder to example#example.com
-> Test Reminder, Yet Another Test Reminder
Sending reminder to example#example.com
-> Test Reminder, Yet Another Test Reminder
Sending order to example#example.com
-> Testing Order
Sending reminder to example2#example.com
-> Another Test Reminder
Sending reminder to example#example.com
-> Test Reminder, Yet Another Test Reminder
Sending reminder to example#example.com
-> Test Reminder, Yet Another Test Reminder
Sending order to example#example.com
-> Testing Order
Sending reminder to example#example.com
-> Test Reminder, Yet Another Test Reminder
Sending reminder to example#example.com
-> Test Reminder, Yet Another Test Reminder
Sending order to example#example.com
-> Testing Order
The heck is going on? Why doesn't LEAVES_ONLY only print the "leaves", like it's supposed to?
Here is the code for my iterator. It's rather long, but it seems to get the job done.
<?php
/**
* Iterate recursively over an array
*/
class MyIterator implements RecursiveIterator, Countable{
private $data, $groups, $currentGroup, $position, $subPosition, $savedSubPosition, $seenRows;
// Prepare our iterator
public function __construct($array, $currentGroup=0, $savedSubPosition=[]){
$this->data = $array;
$this->groups = ['address', 'emailType', null];
$this->currentGroup = $currentGroup;
$this->savedSubPosition = $savedSubPosition;
// The class is loaded, let's go to the very beginning
$this->rewind();
}
/**
* From Iterator
* http://php.net/manual/en/class.iterator.php
*/
// Reset to 0 position
// Return void
function rewind(){
// -1, so we can call next()
$this->position = -1;
$this->subPosition = -1;
$this->seenRows = [];
// Let's get the "first" element, if there is one
$this->next();
}
// Return current position
function current($doNotMarkSeen=FALSE){
$group = $this->getGroupName();
if(is_null($group)){
// Reduce the entire array into one array
// Thus the last group will *always* have 1 (or 0) elements
return array_reduce($this->data, function($ret, $val){
if(empty($ret)){ // First iteration
return $val;
}
elseif(is_string($ret['data'])){ // Second Iteration
$ret['data'] = [$ret['data'], $val['data']];
return $ret;
}
else{ // Third+ Iteration
$ret['data'][] = $val['data'];
return $ret;
}
}, []);
}
else{
$rowVal = $this->data[$this->position][$group];
is_array($rowVal) && ($rowVal = $rowVal[$this->subPosition]);
return $doNotMarkSeen ? $rowVal : ($this->seenRows[] = $rowVal);
}
}
// Return current key
function key(){
// In this case, the key is the email address
// yes, the key might wind up being the same across iterations
// Well, it's only the email on the last group, otherwise it's a number
$group = $this->getGroupName();
return is_null($group) ? $this->getEmailAddress() : $this->position;
}
// Shift position by one
// Return void
function next(){
// We need to shift to the next group member,
// that means either the next email address, or the next emailType
do{
$group = $this->getGroupName();
$incrementMain = true;
// Which one do we need to increment?
if($this->subPosition > -1 && isset($this->data[$this->position]) && count($this->data[$this->position][$group]) > $this->subPosition){
$this->subPosition++;
$incrementMain = !(count($this->data[$this->position][$group]) > $this->subPosition);
}
if($incrementMain){
$this->position++;
$this->subPosition = -1;
if(isset($this->data[$this->position]) && !is_null($group) && is_array($this->data[$this->position][$group])){
$this->subPosition++;
}
}
} while($this->_hasSeenRow());
}
// Is there data at our current position?
// Return boolean
function valid(){
// This is called *after* next() (or rewind()) is
// The last group is weird and does not follow this formula
$group = $this->getGroupName();
if(is_null($group)){
// We are doing array_reduce, so there's only one "position"
return count($this->data) > 0 && $this->position === 0;
}
else{
$isValid = isset($this->data[$this->position]);
if($isValid){
$validPosition = count($this->data) > $this->position;
if($validPosition && !is_null($group) && is_array($this->data[$this->position][$group])){
$validPosition = count($this->data[$this->position][$group]) > $this->subPosition;
}
return $validPosition;
}
else{
return false;
}
}
}
/**
* Here's where the fun is. Do we have any child arrays?
*
* From RecursiveIterator
* http://php.net/manual/en/class.recursiveiterator.php
*/
// Are there any child arrays?
// Return boolean
function hasChildren(){
return !is_null($this->getGroupName());
}
// Return an iterator for the child array
function getChildren(){
// Send it the same array. It's not actually 2-d, we're just pretending it is
$groupName = $this->getGroupName();
$thisValue = $this->current(true);
$subPosition = $this->subPosition;
!is_null($groupName) && ($this->savedSubPosition[$groupName] = $subPosition);
// Note to self: array_filter preserves keys.
return new self(array_values(array_filter($this->data, function($a) use($groupName, $thisValue, $subPosition){
$val = $a[$groupName];
return (is_array($val) ? $val[$subPosition] : $val) === $thisValue;
})), $this->currentGroup+1, $this->savedSubPosition);
}
/**
* From GroupIterator
*/
// Return the current group name
function getGroupName(){
return $this->groups[$this->currentGroup];
}
/**
* From Countable
* http://php.net/manual/en/countable.count.php
*/
function count($mode=COUNT_NORMAL){ // <- $mode is only passed in PHP 5.6
switch($mode){
case COUNT_NORMAL:
$groupName = $this->getGroupName();
if(!is_null($groupName)){
$allVals = array_map(function($a) use($groupName){
return $a[$groupName];
}, $this->data);
if(isset($allVals[0]) && is_array($allVals[0])){
$allVals = call_user_func_array('array_merge', $allVals);
}
return count(array_unique($allVals));
}
else{
return count($this->data);
}
break;
case COUNT_RECURSIVE: // <- Only exists in PHP 5.6
// TODO: Count *all* groups from this one forward, and add 'em up
break;
}
}
/**
* Custom functions
*/
function getEmailType(){
return $this->_getRowVal('emailType');
}
function getEmailAddress(){
return $this->_getRowVal('address');
}
/**
* Helper functions
*/
private function _hasSeenRow(){
$groupName = $this->getGroupName();
return ($this->valid() && !is_null($groupName)) ? in_array($this->current(true), $this->seenRows, true) : false;
}
private function _getRowVal($name){
$val = $this->data[$this->position][$name];
return is_array($val) ? $val[!empty($this->savedSubPosition) ? $this->savedSubPosition[$name] : $this->subPosition] : $val;
}
}
?>

How to get values that doesn't pass from FilterIterator

I'm using FilterIterator to filter out the values and implemented the accept() method successfully. However I was wondering how would it be possible to get the values that returned false from my accept method in single iteration. Let's take the code below as an example (taken from php.net);
class UserFilter extends FilterIterator
{
private $userFilter;
public function __construct(Iterator $iterator , $filter )
{
parent::__construct($iterator);
$this->userFilter = $filter;
}
public function accept()
{
$user = $this->getInnerIterator()->current();
if( strcasecmp($user['name'],$this->userFilter) == 0) {
return false;
}
return true;
}
}
On the code above, it directly filters out the values and returns the values that pass from the filteriterator. Implemented as;
$array = array(
array('name' => 'Jonathan','id' => '5'),
array('name' => 'Abdul' ,'id' => '22')
);
$object = new ArrayObject($array);
$iterator = new UserFilter($object->getIterator(),'abdul');
It will contain only the array with name Jonathan. However I was wondering would it be possible to store the object with name Abdul in another variable using the same filter with a slight addition instead of reimplementing the entire filter to do the opposite?. One way I was thinking would exactly copy paste the FilterIterator and basically change values of true and false. However are there any neat ways of doing it, since it will require another traversal on the list.
I think you must rewrite the accept() mechanic. Instead of returning true or false, you may want to break down the array to
$result = array(
'passed' => array(...),
'not_passed' => array(...)
);
Your code may look like this
if (strcasecmp($user['name'], $this->userFilter) == 0) {
$result['not_passed'][] = $user;
} else {
$result['passed'][] = $user;
}
return $result;

PHP Object Validation

I'm currently working on an OO PHP application. I have a class called validation which I would like to use to check all of the data submitted is valid, however I obviously need somewhere to define the rules for each property to be checked. At the moment, I'm using arrays during the construction of a new object. eg:
$this->name = array(
'maxlength' => 10,
'minlength' => 2,
'required' => true,
'value' => $namefromparameter
)
One array for each property.
I would then call a static method from the validation class which would carry out various checks depending on the values defined in each array.
Is there a more efficient way of doing this?
Any advice appreciated.
Thanks.
I know the associative array is used commonly to configure things in PHP (it's called magic container pattern and is considered bad practice, btw), but why don't you create multiple validator classes instead, each of which able to handle one rule? Something like this:
interface IValidator {
public function validate($value);
}
$validators[] = new StringLengthValidator(2, 10);
$validators[] = new NotNollValidator();
$validators[] = new UsernameDoesNotExistValidator();
This has multiple advantages over the implementation using arrays:
You can document them (very important), phpdoc cannot parse comments for array keys.
Your code becomes typo-safe (array('reqiured' => true))
It is fully OO and does not introduce new concepts
It is more readable (although much more verbose)
The implementation of each constraint can be found intuitively (it's not in a 400-line function, but in the proper class)
EDIT: Here is a link to an answer I gave to a different question, but that is mostly applicable to this one as well.
Since using OO it would be cleaner if you used classes for validating properties. E.g.
class StringProperty
{
public $maxLength;
public $minlength;
public $required;
public $value;
function __construct($value,$maxLength,$minLength,$required)
{
$this->value = $value;
$this-> maxLength = $maxLength;
$this-> minLength = $minLength;
$this-> required = $required;
}
function isValidat()
{
// Check if it is valid
}
function getValidationErrorMessage()
{
}
}
$this->name = new StringProperty($namefromparameter,10,2,true);
if(!$this->name->isValid())
{
$validationMessage = $this->name-getValidationErrorMessage();
}
Using a class has the advantage of encapsulating logic inside of it that the array (basically a structure) does not have.
Maybe get inspired by Zend-Framework Validation.
So define a master:
class BaseValidator {
protected $msgs = array();
protected $params = array();
abstract function isValid($value);
public function __CONSTRUCT($_params) {
$this->params = $_params;
}
public function getMessages() {
// returns errors-messages
return $this->msgs;
}
}
And then build your custom validators:
class EmailValidator extends BaseValidator {
public function isValid($val=null) {
// if no value set use the params['value']
if ($val==null) {
$val = $this->params['value'];
}
// validate the value
if (strlen($val) < $this->params['maxlength']) {
$this->msgs[] = 'Length too short';
}
return count($this->msgs) > 0 ? false : true;
}
}
Finally your inital array could become something like:
$this->name = new EmailValidator(
array(
'maxlength' => 10,
'minlength' => 2,
'required' => true,
'value' => $namefromparameter,
),
),
);
validation could then be done like this:
if ($this->name->isValid()) {
echo 'everything fine';
} else {
echo 'Error: '.implode('<br/>', $this->name->getMessages());
}

Categories