Check that a phone number is unique.

@return array An associative array with a boolean 'result' entry and a
'message' entry.
161011
public function checkPhone() {
    if (empty($this->phone)) {
      return ['result' => false, 'message' => 'Please specify a phone number.'];
    }

    $stripToDigits = preg_replace('/\D/', '', $this->phone);
    if (!preg_match('/\d{10}/', $stripToDigits)) {
      return [
        'result' => false,
        'message' =>
          'Phone must contain 10 digits, but formatting does not matter.'
      ];
    }
    $selector = ['&',
        'strict' => ['phone', $stripToDigits]
      ];
    if (isset($this->guid)) {
      $selector['!guid'] = $this->guid;
    }
    $test = Nymph::getEntity(
        ['class' => '\Tilmeld\Entities\User', 'skip_ac' => true],
        $selector
    );
    if (isset($test->guid)) {
      return ['result' => false, 'message' => 'Phone number is in use.'];
    }

    return [
      'result' => true,
      'message' => (
        isset($this->guid) ? 'Phone number is valid.' : 'Phone number is valid!'
      )
    ];
  }
374384
public static function array_unique($array)
    {
        $keys = array_keys($array);
        $toDrop = array();
        for ($i = count($keys) - 1; $i >=0 ; $i-- ) {
            for ($j = $i - 1; $j >=0 ; $j--) {
                if ($array[$keys[$i]]->__equals($array[$keys[$j]])) {
                    $toDrop[] = $keys[$i];
                    break;
                }
            }
        }
        foreach ($toDrop as $key) {
            unset($array[$key]);
        }
        return $array;
    }
1090403
public function UniqueElementEntries()
	{
		$result = array(); // Build a new array

		foreach ( $this->elements as $name => $entries )
		{
			if ( ! isset( $result[ $name ] ) ) $result[ $name ] = array();

			foreach ( $entries as $key => $entry )
			{
				if ( isset( $entry['tuple_elements'] ) )
				{
					// Just take it
					$result[ $name ][] = $entry;
					continue;
				}

				if ( $this->entry_is_unique( $result[ $name ], $entry ) )
					$result[ $name ][] = $entry;
			}
		}

		return new InstanceElementsFilter( $this, $result );
	}
1191920
public function isUnique(array $keys)
    {
        if (1 === count($keys)) {
            $column = $keys[0] instanceof Column ? $keys[0] : $this->getColumn($keys[0]);
            if ($column) {
                if ($column->isUnique()) {
                    return true;
                }

                if ($column->isPrimaryKey() && 1 === count($column->getTable()->getPrimaryKey())) {
                    return true;
                }
            }
        }

        // check if pk == $keys
        if (count($this->getPrimaryKey()) === count($keys)) {
            $allPk = true;
            $stringArray = is_string($keys[0]);
            foreach ($this->getPrimaryKey() as $pk) {
                if ($stringArray) {
                    if (!in_array($pk->getName(), $keys)) {
                        $allPk = false;
                        break;
                    }
                } else {
                    if (!in_array($pk, $keys)) {
                        $allPk = false;
                        break;
                    }
                }
            }

            if ($allPk) {
                return true;
            }
        }

        // check if there is a unique constrains that contains exactly the $keys
        if ($this->unices) {
            foreach ($this->unices as $unique) {
                if (count($unique->getColumns()) === count($keys)) {
                    $allAvailable = true;
                    foreach ($keys as $key) {
                        if (!$unique->hasColumn($key instanceof Column ? $key->getName() : $key)) {
                            $allAvailable = false;
                            break;
                        }
                    }
                    if ($allAvailable) {
                        return true;
                    }
                } else {
                    continue;
                }
            }
        }

        return false;
    }
716958
public static function unique($array)
    {
        $results = [];
        if (self::isMulti($array)) {
            $array = self::multiToAssoc($array);
            foreach ($array as $key => $value) {
                $results[] = $value;
            }

            return array_unique($array);
        }

        return array_unique($array);
    }
691729
public function getUniqueIdentifier()
    {
        $formDefinition = $this->getRootForm();
        $uniqueIdentifier = sprintf('%s-%s', $formDefinition->getIdentifier(), $this->identifier);
        $uniqueIdentifier = preg_replace('/[^a-zA-Z0-9-_]/', '_', $uniqueIdentifier);
        return lcfirst($uniqueIdentifier);
    }
902347
public function unique(callable $callback) {
		$that = $this
				->group($callback)
				->map(function($group) {
					return $group[0];
				});
		return $that->resetKeys();
	}
528407
protected function keys(array $results)
	{
		$keys = [];

		foreach($results as $result)
		{
			$keys[] = $result->getPrimaryKeyValue();
		}

		return array_unique($keys);
	}
1116090
public function hasMessage()
    {
        $has_message = false;

        foreach ($this->replicators as $replicator_id) {
            foreach ($this->input_cells as $cell) {
                if ($cell->getWidget($replicator_id)->hasMessage()) {
                    $has_message = true;
                    break 2;
                }
            }
        }

        return $has_message;
    }
586033
protected function genericMessageToArray()
    {
        $message = [];
        $message['recipient'] = $this->recipient;
        $message['notification_type'] = $this->notificationType;
        $message['message']['attachment']['type'] = 'template';
        $message['message']['attachment']['payload']['template_type'] = 'generic';
        $message['message']['attachment']['payload']['elements'] = $this->cards;

        return $message;
    }
1366742
public function getPhonenumber()
    {
        for ($i = 1; $i <= $this->maxPhoneNumber; $i++) {
            if (isset($this->_gemsData['grs_phone_' . $i]) && ! empty($this->_gemsData['grs_phone_' . $i])) {
                return $this->_gemsData['grs_phone_' . $i];
            }
        }

        return null;
    }
692361
public function isArray()
    {
        return
        (
            (
                (
                    ($this->isStruct() && $this->countOwnAttributes() === 1) ||
                    (!$this->isStruct() && $this->countOwnAttributes() <= 1)
                ) &&
                mb_stripos($this->getName(), 'array') !== false
            ) ||
            (!$this->isStruct() && $this->getMetaValueFirstSet(['arraytype', 'arrayType'], false) !== false)
        );
    }
1348647
public function getUniqueColumns()
    {
        $result = array();
        $columns = $this->getColumns(true);

        foreach ($columns as $name => $description)
        {
            if ($description->unique) {
                $result[$name] = $description;
            }
        }

        return $result;
    }
430636
public function getKeys() {
        $keys = [];
        foreach($this->arrays as $array) {
            $keys = array_merge($keys, $array instanceof \ArrayAccess ? $array->getKeys():array_keys($array));
        }
        return array_unique($keys);
    }
1251885
public function getUniqueFieldNames()
    {
        $results = [ $this->getPrimaryFieldName() ];
        if (!isset($this->data['fields']) || empty($this->data['fields'])) {
            return $results;
        }

        $table = $this->data['name'];
        foreach ($this->data['fields'] as $field) {
            $name = $table . '_' . $field['name'];

            if ($field['field']['type'] === 'uuid') {
                $results[] = $name;
                continue;
            }

            if (!isset($field['validation'])) {
                continue;
            }

            foreach ($field['validation'] as $validation) {
                if ($validation['method'] === 'unique') {
                    $results[] = $name;
                }
            }
        }

        return $results;
    }
321921
public function getCellPhoneNumber()
    {
        $mobil     = null;
        $mobilsRaw = $this->getFlexiData($this->getApiURL(),
            ['detail' => 'custom:id,mobil,kontakty(primarni,mobil)', 'relations' => 'kontakty']);
        if (is_array($mobilsRaw)) {
            $mobils = $mobilsRaw[0];
            if (array_key_exists('mobil', $mobils) && strlen(trim($mobils['mobil']))) {
                $mobil = $mobils['mobil'];
            }
            if (array_key_exists('kontakty', $mobils) && !empty($mobils['kontakty'])) {
                foreach ($mobils['kontakty'] as $kontakt) {
                    if (($kontakt['primarni'] == 'true') && strlen(trim($kontakt['mobil']))) {
                        $mobil = $kontakt['mobil'];
                        break;
                    }
                }
            }
        }
        return $mobil;
    }
1220798
public function getPhoneNumbersFormatted()
    {
        $formattedArray = [];
        foreach ($this->phoneNumbers as $key => $phoneNumber)
        {
            $formattedArray[$key] = [
                'number' => $phoneNumber->getNumber(),
                'extension' => null
            ];
        }

        return $formattedArray;
    }