3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ValidationException extends \Exception { } interface Validator { /** * @param mixed $data * * @throws ValidationException */ public function validate($data); } class ValidatorComposite { /** * @var Validator[] */ protected $validators; public function addValidator(Validator $validator) { $this->validators[] = $validator; } public function validate($data) { $exceptions = []; foreach ($this->validators as $validator) { try { $validator->validate($data); } catch (ValidationException $e) { $exceptions[] = $e->getMessage(); } /** Now you have a list of exceptions that were thrown in $exceptions **/ return $exceptions; } } } class ArrayKeyExistsValidator implements Validator { protected $requiredKeys = []; public function __construct(array $requiredKeys) { $this->requiredKeys = $requiredKeys; } public function validate($data) { if (!is_array($data)) { // either try to convert to array or throw exception } foreach ($this->requiredKeys as $requiredKey) { /** recursively search your array for each key**/ // I'm doing non recursively here if (!array_key_exists($requiredKey, $data)) { throw new ValidationException( sprintf('Key: "%s" not found in array: "%s"', $requiredKey, print_r($data, true)) ); } } } } // Object Usage $composite = new ValidatorComposite; $validator = new ArrayKeyExistsValidator(array('key1', 'key2')); $composite->addValidator($validator); $dataToValidate = array('LOL', 'key1', 'thereIsntAKey2'); $composite->validate($dataToValidate);
Output for git.master, git.master_jit, rfc.property-hooks

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
63.37 ms | 401 KiB | 8 Q