custom post types – Combine multiple CPT names to create valid permalinks

I have three different Custom Post Types:

  1. Company
  2. Employee
  3. Project

They are connected with ACF relationship fields so as the Company can have multiple Employees and each Employee can have a project attached. The get_field() function retrieves attached post objects to create some sort of relationship.

What I need to do is create three permalink structures for these CPT:

  1. For Companies:
  2. For Employees:
  3. For Projects:

What I have already managed to do is to set permalink structure to them with this piece of code:

function dd_custom_permalinks($permalink, $post, $leavename) {
    $post_id = $post->ID;
    if(($post->post_type != 'project' && $post->post_type != 'employee' && $post->post_type != 'company') || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
        return $permalink;

    if (get_field("employees", $post->ID)) {
        $permalink = str_replace('%company%', $post->post_name, $permalink);
    } elseif (get_field("company", $post->ID)) {
        $permalink = str_replace('%company%', get_field("company", $post->ID)(0)->post_name, $permalink);
        $permalink = str_replace('%employee%', $post->post_name, $permalink);
    } elseif (get_field("employee", $post->ID)) {
        $employee= get_field("employee", $post->ID)(0);
        $company= get_field("company", $employee->ID)(0);
        $permalink = str_replace('%company%', $company->post_name, $permalink);
        $permalink = str_replace('%employee%', $employee->post_name, $permalink);
        $permalink = str_replace('%project%', $post->post_name, $permalink);

    return $permalink;
add_filter('post_type_link', 'dd_custom_permalinks', 10, 3);

The problem is I cannot find a way to make them valid, as the structure always leads to Company (first part of the custom url structure). Probably I have to bind the further with the CPT query somehow?

I have also tried using custom rewrite rules, but I can’t understand how should this be done to be fair. Here’s what I got so far (not working):

function dd_custom_rewrite_rules() {
    add_rewrite_tag('%company%', '((^/)+)', 'company=');
    add_permastruct('company', '/%company%', false);

    add_rewrite_tag('%employee%', '((^/)+)', 'employee=');
    add_permastruct('employee', '/%company%/%employee%', false);
    add_rewrite_tag('%project%', '((^/)+)', 'project=');
    add_permastruct('project', '/%company%/%employee%/%project%', false);

add_action( 'init', 'dd_custom_rewrite_rules' );

Any ideas how (or maybe IF) can I get this to work properly?