Ejemplo n.º 1
0
    /**
     * Constructor
     *
     * @param FacilityService $facilityService
     * @throws InvalidArgumentException
     *
     * */
    public function __construct(
        FacilityService $facilityService
    ) {
        $this->facilityService = $facilityService;

        $this->add(
            [
                'name' => 'name',
                'filters' => [
                    [
                        'name' => 'StripTags'
                    ],
                    [
                        'name' => 'StringTrim'
                    ],
                ],
            ]
        );
        $this->facilityValidator = new Callback(function ($value) {
            if ($value instanceof Facility) {
                return true;
            } else if (!(is_array($value) && isset($value['id']))) {
                $this->facilityValidator->setMessage('No valid identifier specified');
                return false;
            }

            try {
                $this->facilityService->find($value);
                return true;
            } catch (ModelNotFoundException $e) {
                $this->facilityValidator->setMessage($e->getMessage());
            } catch (UnauthorizedException $e) {
                $this->facilityValidator->setMessage($e->getMessage());
            } catch (Exception $e) {
                $this->facilityValidator->setMessage('Unknown error during validation');
            }
            return false;
        });
        $this->add(
            [
                'name' => 'facility',
                'validators' => [
                    $this->facilityValidator
                ]
            ]
        );

    }
Ejemplo n.º 2
0
 /**
  * Fetch a facility
  *
  * @param  integer $id
  * @return Facility
  * @throws ServiceException
  */
 public function fetch($id)
 {
     $facility = $this->facilityService->find($id);
     return $facility;
 }