コード例 #1
0
 protected function addHeaderWithoutOverriding($headers, $headerName, $value)
 {
     $headerLine;
     $headerName = CString::trim($headerName);
     $value = CString::trim($value);
     $foundHeaderPos;
     $alreadyExists = CArray::find($headers, $headerName, function ($element0, $element1) {
         return CRegex::find($element0, "/^\\h*" . CRegex::enterTd($element1) . "\\h*:/i");
     }, $foundHeaderPos);
     if (!$alreadyExists) {
         $headerLine = "{$headerName}: {$value}";
     } else {
         // The header already exists. Combine the header values, removing duplicates based on case-insensitive
         // equality.
         $currValue = CRegex::remove($headers[$foundHeaderPos], "/^.*?:\\h*/");
         CArray::remove($headers, $foundHeaderPos);
         $values = CString::split("{$currValue}, {$value}", ",");
         $len = CArray::length($values);
         for ($i = 0; $i < $len; $i++) {
             $values[$i] = CString::trim($values[$i]);
         }
         $values = CArray::filter($values, function ($element) {
             return !CString::isEmpty($element);
         });
         $values = CArray::unique($values, function ($element0, $element1) {
             return CString::equalsCi($element0, $element1);
         });
         $combinedValue = CArray::join($values, ", ");
         $headerLine = "{$headerName}: {$combinedValue}";
     }
     CArray::push($headers, $headerLine);
 }
コード例 #2
0
ファイル: CArrayTest.php プロジェクト: nunodotferreira/Phred
 public function testUnique()
 {
     // Using the default comparator.
     $array = CArray::fromElements("a", "b", "c", "d", "e", "a", "c", "e");
     $array = CArray::unique($array);
     $this->assertTrue(CArray::equals($array, CArray::fromElements("a", "b", "c", "d", "e")));
     // Using a custom comparator.
     $array = CArray::fromElements("A", "b", "C", "d", "E", "a", "c", "e");
     $comparator = function ($string0, $string1) {
         return CString::toLowerCase($string0) === CString::toLowerCase($string1);
     };
     $array = CArray::unique($array, $comparator);
     $this->assertTrue(CArray::equals($array, CArray::fromElements("A", "b", "C", "d", "E")));
 }
コード例 #3
0
 /**
  * Removes repeating elements from an array so that only unique elements are kept and returns the new array.
  *
  * The array is not modified by this method.
  *
  * You can use your own comparator for element comparison, but the default comparator has got you covered when
  * comparing scalar values, such as `string`, `int`, `float`, and `bool`. And the default comparator is smart
  * enough to know how to compare objects of those classes that conform to the IEquality or IEqualityAndOrder
  * interface (static or not), including CUStringObject, CArrayObject, CMapObject, CTime etc. See the
  * [CComparator](CComparator.html) class for more on this.
  *
  * @param  callable $comparator **OPTIONAL. Default is** `CComparator::EQUALITY`. The function or method to be
  * used for element comparison. If this parameter is provided, the comparator should take two parameters, which are
  * the two elements being compared, and return `true` if the two elements are equal and `false` otherwise.
  *
  * @return CArrayObject The cleaned up array.
  *
  * @link   CComparator.html CComparator
  */
 public function unique($comparator = CComparator::EQUALITY)
 {
     return self::fromSplArray(CArray::unique($this->m_splArray, $comparator));
 }