» /rd/lib/removal/Removal.php

<?php
/**
 * Usage:
 *   $items = [123 => ["p" => 13344567465], 131 => ["p" => 13344567499]]
 *   /rd/bin/pe "removal\Removal::filterRemoved($items, $items, 'mark')"
 *   /rd/bin/pe "removal\Removal::filterRemoved($items, $items, 'unset')"
 *
 *   $items = [13344567465, 13344567499];
 *   $entities = [0=>["phone" => 13344567465], 1=>["phone" => 13344567499]];
 *   /rd/bin/pe "removal\Removal::filterRemoved($entities, $items, 'unset')"
 * 
 */
namespace removal;

class 
Removal {
    static 
$VERSION 3;
    static 
$MYNAME "Removal v3";
    
    const 
REQUEST_STATUS_NOT_CONFIRMED 1;
    const 
REQUEST_STATUS_CONFIRMED 2;
    const 
REQUEST_STATUS_APPROVED 3;
    const 
REQUEST_STATUS_APPLIED 4;
    const 
REQUEST_STATUS_FRAUD 99;
    const 
REQUEST_STATUS_DELETED 100;
    
    const 
REQUEST_TXT = [
        
self::REQUEST_STATUS_NOT_CONFIRMED => 'Not confirmed',
        
self::REQUEST_STATUS_CONFIRMED => 'Confirmed',
        
self::REQUEST_STATUS_APPROVED => 'Approve',
        
self::REQUEST_STATUS_APPLIED => 'Applied',
        
self::REQUEST_STATUS_FRAUD => 'Fraud',
        
self::REQUEST_STATUS_DELETED => 'Deleted',
    ];

    
/**
     * Doing very similar to removal\Removal::filterRemoved() but removes from &$data only those records that has NAME ( as fl, fname_id+lname_id ) that is removed or marked as "idle"
     * @param array $entities - array of [ uk => entity ]. see Hasher::allHashes() docs for entity structure
     * @param array $data - array of [ uk => any_data_record ]. Uks of $entities and $data should be the same;
     * @param string $method - "unset" | "mark"
     *                         "unset" removes records from $data
     *                         "mark" addes fields "_deleted" and "_forbidden" to removed records in $data
     * @return array
     * @throws \Exception
     */
    
static function filterNoLinks($entities, &$data$method "unset") {
        
\Profiler::in(self::$MYNAME"Filter links:" count($entities));

        if (isset(
$_GET["UPDATE"])) {
            
Hasher::syncRemovalHashes();
            
Hasher::reloadRemovals();
        }

        
$to_check = [];
        
$no_links = [];
        
// Filter deleted/blocked entities
        
if ($removed Removal::filterRemoved($entities$data$method)) {
            
\Profiler::warn("Removed " count($removed) . " names"$removed);
        }

        
// Filter unwanted entities that should not have links
        
foreach ($entities as $e_id => $entity) {
            if (!
$fl = ($entity["fl"] ?? null)) {
                if ((
$fname_id $entity["fname_id"] ?? 0) && ($lname_id $entity["lname_id"] ?? 0)) {
                    
$fl \HB::fl($fname_id$lname_id);
                    
$entities[$e_id]["fl"] = $fl;
                }
            }
            if (
$fl) {
                
$to_check[] = $fl;
            }
        }
        if (
$to_check) {
            
$to_check array_unique($to_check);
            
$no_links i('go-api.name-server')->fl2mantra($to_check);
            if (
$no_links) {
                
\Profiler::warn(Removal::$MYNAME"No links found ".count($no_links));
                
$time time();
                foreach (
$entities as $e_id => $entity) {
                    if (
$reason = ($no_links[$entity["fl"]??0]??0)) {
                        switch (
$method) {
                            case 
"unset":
                                unset(
$data[$e_id]);
                                break;
                            case 
"mark":
                                
$data[$e_id]["_deleted"] = $time;
/*
                                switch ($reason) {
                                    case Entity_Base::ENTITY_TYPE_IDLE:
                                        $data[$e_id]["_idle"] = 1;
                                        break;
                                    case Entity_Base::ENTITY_TYPE_NAME:
                                        $data[$e_id]["_deleted"] = 1;
                                        break;
                                }
*/
                                
break;
                        }
                    }
                }
            }

        }
        
\Profiler::out();
        return 
$no_links;
    }


    
/**
     * Find removed records in $data using $entities as index. Process removed records according to $method
     * @param array $entities - array of [ uk => entity ]. see Hasher::allHashes() docs for entity structure
     * @param array $data - array of [ uk => any_data_record ]. Uks of $entities and $data should be the same;
     * @param string $method - "unset" | "mark"
     *                         "unset" removes records from $data
     *                         "mark" addes fields "_deleted" and "_forbidden" to removed records in $data
     * @return array
     * @throws \Exception
     */
    
static function filterRemoved($entities, &$data$method "unset"){
        
$uk_fl_empty array_filter($entities, function($item){  
            if(!
is_array($item))
                return 
false;

            return (empty(
$item['uk']) && !empty($item['fname_id']) && !empty($item['lname_id']));
        });

        if(!empty(
$uk_fl_empty)) {
            
$entities array_map(function($item){
                
$item['fl'] = \HB::fl($item['fname_id']??0,$item['lname_id']??0);
                return 
$item;
            }, 
$entities);
        }

        if (!empty(
$entities)) {
            
\Profiler::in(self::$MYNAME"Filter data cnt:" count($entities));

            if ($removed Hasher::findRemoved($entities)) {

                switch (
$method) {
                    case 
"unset":
                        foreach (
$removed as $k => $v) {
                            unset(
$data[$k]);
                        }
                        break;
                    case 
"mark":
                        
$time time();
                        foreach (
$removed as $k => $v) {
                            if(!empty(
$data[$k]) && is_array($data[$k])) {
                                if (!(
$data[$k]["_deleted"]??0)) {
                                    
$data[$k]["_deleted"] = $time;
                                }

                                if (!(
$data[$k]["_forbidden"]??0)) {
                                    
$data[$k]["_forbidden"] = self::forbiddenFingerprint($v);
                                }
                            } else if (!empty(
$data[$k]) && $data[$k] instanceof Hasher_ItemInterface) {
                                if(!
property_exists($data[$k], '_deleted')) {
                                    
$data[$k]->_deleted $time;
                                }

                                if(!
property_exists($data[$k], '_forbidden')) {
                                    
$data[$k]->_forbidden self::forbiddenFingerprint($v);
                                }

                            }
                        }
                        break;
                }
            }

            
\Profiler::out();
        }
        return 
$removed ?? [];
    }
    
    
///////////////////////////////////////////////////
    //
    //  REMOVALS
    //
    ///////////////////////////////////////////////////
    
    
    
static function forbiddenFingerprint($removed_reason){
        return [
"comment" => "removal_v" self::$VERSION] + $removed_reason;
    }

    static function 
isAdultName($fl){
        
$NAMES = [
            
11111368157569221 => "Alma Moore",
            
8505551369586752 => "Reagan Foxx",
            
9997665957893486 => "Tina Shellenberger"
        
];
        return 
$NAMES[$fl]??0;
    }

    static function 
getStatus(int $status) {
        return 
self::REQUEST_TXT[$status] ?? 'Undefined status';
    }
    
}