function get_instant_article()

in webserver/article.php [86:128]


function get_instant_article($html_markup, $url, $rules, &$response) {
  $cache_key = $rules . $url;

  $possible_instant_article = try_get_cached_value($cache_key, CACHE_KEY_TYPE_IA);

  // Cache hit
  if (!is_null($possible_instant_article))
    return $possible_instant_article;

  // Cache miss
  // Load rules
  //-----------
  $transformer = new Transformer();
  $transformer->loadRules($rules);

  // Transform
  //----------
  $instant_article = InstantArticle::create();
  $transformer->transformString($instant_article, $html_markup);

  $warnings = $transformer->getWarnings();

  $warnings_func = function($warning) {
    $warning_obj = new stdClass();
    $warning_obj->message = $warning->__toString();

    if (method_exists($warning,'getSelector')) {
      $warning_obj->selector = $warning->getSelector() ?: null;
    }
    if (method_exists($warning,'getFields')) {
      $warning_obj->field = $warning->getFields() ?: null;
    }
    return $warning_obj;
  };
  $warnings = array_unique(array_map($warnings_func, $warnings), SORT_REGULAR);
  $response->warnings = $warnings;

  try_set_cached_value($cache_key, $warnings, CACHE_KEY_TYPE_WARNINGS);

  try_set_cached_value($cache_key, $instant_article, CACHE_KEY_TYPE_IA);

  return $instant_article;
}