: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @author Michael Pratt <yo@michael-pratt.com>
* @link http://www.michael-pratt.com/
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
use Embera\Http\HttpClient;
use Embera\Http\OembedClient;
use Embera\Http\HttpClientInterface;
use Embera\Html\IgnoreTags;
use Embera\Html\ResponsiveEmbeds;
use Embera\Provider\ProviderInterface;
use Embera\ProviderCollection\ProviderCollectionInterface;
use Embera\ProviderCollection\DefaultProviderCollection;
* The Main Class of this library.
/** @var string Current Library Version */
const VERSION = '2.0.16';
* Constants describing how the library is
* going to handle fake responses.
* ALLOW_FAKE_RESPONSES is the default value.
const ALLOW_FAKE_RESPONSES = 1;
const DISABLE_FAKE_RESPONSES = 2;
const ONLY_FAKE_RESPONSES = 3;
/** @var array Configuration settings */
/** @var array Logged errors */
/** @var array Closures to be used on oembed responses */
/** @var ProviderCollectionInterface */
protected $providerCollection;
/** @var HttpClientInterface */
* @param ProviderCollectionInterface $collection
* @param HttpClientInterface $httpClient
public function __construct(array $config = [], ProviderCollectionInterface $collection = null, HttpClientInterface $httpClient = null)
$this->providerCollection = $collection;
$this->providerCollection = new DefaultProviderCollection();
$this->httpClient = $httpClient;
$this->httpClient = new HttpClient();
$this->setConfig($config);
public function setConfig(array $config = [])
$this->config = array_merge([
'fake_responses' => self::ALLOW_FAKE_RESPONSES,
'ignore_tags' => [ 'pre', 'code', 'a', 'img', 'iframe', 'oembed' ],
$this->config['maxwidth'] = max($this->config['width'], $this->config['maxwidth']);
$this->config['maxheight'] = max($this->config['height'], $this->config['maxheight']);
unset($this->config['height'], $this->config['width']);
// Set the config just in case.
$this->providerCollection->setConfig($this->config);
$this->httpClient->setConfig($this->config);
* Embeds known/available services into the given text.
public function autoEmbed($text)
$providers = $this->getUrlData($text);
foreach ($providers as $url => $response) {
if (!empty($response['html'])) {
$table[$url] = $response['html'];
if (!empty($this->config['ignore_tags']) && strpos($text, '>') !== false) {
$ignoreTags = new IgnoreTags($this->config['ignore_tags']);
return $ignoreTags->replace($text, $table);
return strtr($text, $table);
$this->errors[] = 'For auto-embedding purposes, the input must be a string';
* Returns the oembed response from the given data.
* @param array|string $urls An array with urls or a string with urls.
public function getUrlData($urls)
* @var ProviderInterface $provider
foreach ($this->providerCollection->findProviders($urls) as $url => $provider) {
if ( $provider->shouldSendRequest() ) {
$oembedClient = new OembedClient($this->config, $this->httpClient);
$response = $oembedClient->getResponseFrom($provider);
$response = $provider->getStaticResponse();
if ($this->config['responsive'] && !$provider->hasResponsiveSupport()) {
$responsive = new ResponsiveEmbeds();
$response = $responsive->transform($response);
$return[$url] = $this->applyFilters($response);
} catch (\Exception $e) {
$this->errors[] = $e->getMessage();
return array_filter($return);
* Adds a filter to the oembed response
* @param callable $closure
public function addFilter(callable $closure)
$this->filters[] = $closure;
* Applies registered filters/closures
* to the oembed response.
protected function applyFilters(array $response)
foreach ($this->filters as $closure) {
$response = $closure($response);
* Gets the last error found
public function getLastError()
return end($this->errors);
* Returns an array with all the errors
public function getErrors()
* Checks if there were errors
public function hasErrors()
return (!empty($this->errors));