Example #1
0
 /**
  * Create a hydrator instance from a configuration array
  *
  * @param array $config Hydrator configuration
  * @return HydratorInterface Hydrator
  * @throws InvalidArgumentException If the hydrator configuration is invalid
  * @throws InvalidArgumentException If the hydrator model is invalid
  * @throws InvalidArgumentException If the hydrator model class is invalid
  */
 public static function build(array $config)
 {
     // If the configuration is empty
     if (!count($config)) {
         throw new InvalidArgumentException('Invalid hydrator configuration', InvalidArgumentException::INVALID_HYDRATOR_CONFIGURATION);
         // Else if it's the short instantiation notation
     } elseif (is_string($config[0]) && (new \ReflectionClass($config[0]))->isSubclassOf(AbstractSinglepartHydrator::class)) {
         $config[0] = array(HydratorInterface::STANDARD => $config[0]);
         // Else: Make sure the content model is an array
     } elseif (!is_array($config[0]) || !count($config[0])) {
         throw new InvalidArgumentException('Invalid hydrator content model', InvalidArgumentException::INVALID_HYDRATOR_CONTENT_MODEL);
     }
     // Run through all subhydrators
     foreach (array_keys($config[0]) as $subhydratorName) {
         AbstractPart::validatePartIdentifier($subhydratorName);
     }
     // If the content model has more than one part
     if (count($config[0]) > 1) {
         return self::buildMultipart($config);
     }
     // Build a single part hydrator
     return self::buildSingle($config);
 }
Example #2
0
 /**
  * Split a part path string into path identifiers
  *
  * @param string $path Part path string
  * @return array Part path identifiers
  */
 protected function partPath($path)
 {
     return trim($path) == '/' ? [] : array_map(function ($pathIdentifier) {
         $pathIdentifier = trim($pathIdentifier);
         AbstractPart::validatePartIdentifier($pathIdentifier);
         return $pathIdentifier;
     }, explode('/', ltrim($path, '/')));
 }
 /**
  * Multipart hydrator constructor
  *
  * @param array $subhydrators Subpart hydrators
  * @param int $minOccurrences Minimum occurrences
  * @param int $maxOccurrences Maximum occurrences
  */
 public function __construct(array $subhydrators, $minOccurrences = 1, $maxOccurrences = 1)
 {
     parent::__construct(HydratorInterface::STANDARD);
     // Run through all subhydrators
     foreach ($subhydrators as $part => $subhydrator) {
         // Validate the hydrator name
         AbstractPart::validatePartIdentifier($part);
         // If the subhydrator needs to be instantiated from a string or array
         if (!$subhydrator instanceof HydratorInterface) {
             $subhydrator = HydratorFactory::build(is_array($subhydrator) ? $subhydrator : [[$part => $subhydrator]]);
         }
         $this->subhydrators[$part] = $subhydrator;
     }
     // Validate the occurrence numbers
     self::validateParameters($minOccurrences, $maxOccurrences);
     $this->minimumOccurrences = intval($minOccurrences);
     $this->maximumOccurrences = intval($maxOccurrences);
 }
 /**
  * Delegate a method call to a subpart
  *
  * @param string $method Method nae
  * @param array $subparts Subpart identifiers
  * @param array $arguments Method arguments
  * @return mixed Method result
  */
 public function delegate($method, array $subparts, array $arguments)
 {
     // If there are subpart identifiers: Delegate method call
     if (count($subparts)) {
         $occurrence = 0;
         $part = '';
         $subpart = $this->getImmediateSubpart($subparts, $occurrence, $part);
         $result = $subpart->delegate($method, $subparts, $arguments);
         // If it's a setter method
         if (!strncmp('set', $method, 3)) {
             // Exchange the modified part
             $this->occurrences[$occurrence][$part] = $result;
             // Return a self reference
             return $this;
         }
         // Return the method result
         return $result;
     }
     return parent::delegate($method, $subparts, $arguments);
 }
Example #5
0
 /**
  * Delegate a method call to a subpart
  *
  * @param string $method Method nae
  * @param array $subparts Subpart identifiers
  * @param array $arguments Method arguments
  * @return mixed Method result
  * @throws InvalidArgumentException If there are subpart identifiers
  */
 public function delegate($method, array $subparts, array $arguments)
 {
     // If there are subpart identifiers given
     if (count($subparts)) {
         throw new InvalidArgumentException(sprintf('Subparts are not allowed (%s)', implode('/', $subparts)), InvalidArgumentException::SUBPARTS_NOT_ALLOWED);
     }
     return parent::delegate($method, $subparts, $arguments);
 }