Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119603 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 6488 invoked from network); 24 Feb 2023 20:41:44 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Feb 2023 20:41:44 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DC14818005D for ; Fri, 24 Feb 2023 12:41:43 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 24 Feb 2023 12:41:40 -0800 (PST) Received: by mail-wr1-f45.google.com with SMTP id h14so394541wru.4 for ; Fri, 24 Feb 2023 12:41:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=VYnvHBloTQ1+fNY3+9rEb1G2D3XfjUsklz0bLlH4sSc=; b=ODiVAW/Ds8Z/8EVmWRQUWgezwxoUBQKK9NX6W1bu9PqYdgy3EaUwLCZY8Ilbs+RygG goApC+b8ZaCCRWJRsrOjraTcA3KSvs7a9sy2MXkFZffkK/sm6ADfvc6e2/nGOqSgAPsY 8CMkpwkxa9PDH84XQGU8jlgNM88RVGEe98Dxgv5R5FmPjSRJGBQv9yy0WolVAvQ0R7hX hzMe+h7JMY0dZXejpigWlADz4oR7yTuQymR8pRTgeWHIdy9WKo6BMp67mb09c5OdIUUC 3s5ZwcFQ1YIP03Scsx92Sa5d72YrG3qQTNon4zYTs1u2E1sIHoDXzQBlLngEvV9BLiuo hrcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VYnvHBloTQ1+fNY3+9rEb1G2D3XfjUsklz0bLlH4sSc=; b=vk3rutdOO37DqcwJO3yEmtJ03T4GCGuK01R29C+c09x6HlwwkaS168b8EUx58BkOoY mfrsT87gFC2ffAHj+6MzPn0/xlsz9701f/KtCg+eGduckasOtBO+M0vHMXw0Oqqqbcqq QkaZ9gPAoBNIYnQZ4KzNqgG1HX5S2i94Zk9AeY/8r055uH9SHKKrbojqZVueiFB5hgiE +md9znB0hZ7SCierBdFn9Noq6IZoqirCvBuXFYA4v3STemL8JVQsW/wLlvTEIz7+aOhu 1GTaIEfXAoJTMWvCz8keTbJhfJ3jB5A2rmGmNL+FW8q9eyqa23lbNh6cBEBc85dPDifI IDzg== X-Gm-Message-State: AO0yUKXMxMapSsngSGZcT/Vseaa0jrYbQS4ltAlexEa8QV/jYBIq3h5q vCVeCyIVd07339Ss9jNed4SwEvPY08rJJ0JOvkr8C3cJ X-Google-Smtp-Source: AK7set9zsqgQ+aXpeZylPV1mDaeJsswsrUKoqYgRK5m8SDa214EGMTDZ++hWgbmqphgMZ0288TAePibcsq/zXhfGjcQ= X-Received: by 2002:a05:6000:1e12:b0:2c5:5941:a04b with SMTP id bj18-20020a0560001e1200b002c55941a04bmr244684wrb.7.1677271299048; Fri, 24 Feb 2023 12:41:39 -0800 (PST) MIME-Version: 1.0 Received: by 2002:adf:f7c8:0:0:0:0:0 with HTTP; Fri, 24 Feb 2023 12:41:38 -0800 (PST) In-Reply-To: References: Date: Sat, 25 Feb 2023 06:41:38 +1000 Message-ID: To: Robert Landers Cc: internals Content-Type: multipart/alternative; boundary="000000000000f8ca4a05f578276a" Subject: Re: [PHP-DEV] RFC idea: array_*(Enum) From: mickmackusa@gmail.com (mickmackusa) --000000000000f8ca4a05f578276a Content-Type: text/plain; charset="UTF-8" On Thursday, February 23, 2023, Robert Landers wrote: > Hello, internals, > > Currently, this is an Error: > > $allowedRoles = [Role::Admin]; > $hasRoles = [Role::User, Role::Admin]; > > if(!empty(array_intersect($allowedRoles, $hasRoles))) doSomething(); > > This is an error because enums cannot be cast to a string (and trying > to make them stringable is not allowed) and many array functions > expect something stringable. To work around this limitation, the above > example must be rewritten like this: > > array_uintersect($allowedRles, $hasRoles, fn($l, $r) => $l === $r ? 0 : > 1)); > > Robert Landers > Software Engineer > Utrecht NL > ---- ...I'd like to add as an aside that: if(!empty(array_intersect($allowedRoles, $hasRoles))) is more simply written as if (array_intersect($allowedRoles, $hasRoles)) -- And for anyone unfamiliar with array_udiff_ and array_uintersect_ functions: 1. The two variables in their callback function signature do not relate by position to the input arrays. This is why it is common to use $a and $b (as is common when calling usort()). In other words, the first input array's data may occur as the first parameter in the callback or the second parameter (like wise with subsequent input arrays). 2. The return value from these functions is expected to be an integer from the result of a three-way comparison -- not the int value of a binary evaluation. Most simply, make a habit of implementing the spaceship operator (<=>). See "array_uintersect() gives unexpected results when callback only returns 0 or 1" @ https://stackoverflow.com/q/8176881/2943403 Another example: https://stackoverflow.com/a/71049380/2943403 The deep dive: https://stackoverflow.com/a/70817582/2943403 mickmackusa --000000000000f8ca4a05f578276a--